Import: handle multiple tracks

This commit is contained in:
Bartek Fabiszewski 2017-05-09 16:00:02 +02:00
parent 97cfd8a723
commit a7f52aa215
4 changed files with 37 additions and 22 deletions

View File

@ -232,8 +232,12 @@ function importFile(input){
var root = xml.getElementsByTagName('root'); var root = xml.getElementsByTagName('root');
if (root.length && getNode(root[0], 'error') == 0) { if (root.length && getNode(root[0], 'error') == 0) {
trackId = getNode(root[0], 'trackid'); trackId = getNode(root[0], 'trackid');
trackCnt = getNode(root[0], 'trackcnt');
getTracks(userid); getTracks(userid);
loadTrack(userid, trackId, 1); loadTrack(userid, trackId, 1);
if (trackCnt > 1) {
alert(sprintf(lang['imultiple'], trackCnt));
}
return; return;
} }
errorMsg = getNode(root[0], 'message'); errorMsg = getNode(root[0], 'message');

View File

@ -118,4 +118,5 @@ $lang["import"] = "Import track";
$lang["iuploadfailure"] = "Uploading failed"; $lang["iuploadfailure"] = "Uploading failed";
$lang["iparsefailure"] = "Parsing failed"; $lang["iparsefailure"] = "Parsing failed";
$lang["idatafailure"] = "No track data in imported file"; $lang["idatafailure"] = "No track data in imported file";
$lang["imultiple"] = "Notice, multiple tracks imported (%d)"; // substitutes number of imported tracks
?> ?>

View File

@ -113,5 +113,5 @@ $lang["import"] = "Importuj trasę";
$lang["iuploadfailure"] = "Błąd przesyłania pliku"; $lang["iuploadfailure"] = "Błąd przesyłania pliku";
$lang["iparsefailure"] = "Błąd parsowania pliku"; $lang["iparsefailure"] = "Błąd parsowania pliku";
$lang["idatafailure"] = "Brak trasy w importowanym pliku"; $lang["idatafailure"] = "Brak trasy w importowanym pliku";
$lang["imultiple"] = "Uwaga, zaimportowano kilka tras (%d)";
?> ?>

View File

@ -35,8 +35,9 @@ $uploadErrors[UPLOAD_ERR_EXTENSION] = "A PHP extension stopped the file upload";
* Exit with xml response * Exit with xml response
* @param boolean $isError Error if true * @param boolean $isError Error if true
* @param string $errorMessage Optional error message * @param string $errorMessage Optional error message
* @param array|null $extra Optional array of extra parameters
*/ */
function exitWithStatus($isError, $errorMessage = NULL, $trackId = NULL) { function exitWithStatus($isError, $errorMessage = NULL, $extra = NULL) {
header("Content-type: text/xml"); header("Content-type: text/xml");
$xml = new XMLWriter(); $xml = new XMLWriter();
$xml->openURI("php://output"); $xml->openURI("php://output");
@ -46,9 +47,14 @@ function exitWithStatus($isError, $errorMessage = NULL, $trackId = NULL) {
$xml->writeElement("error", (int) $isError); $xml->writeElement("error", (int) $isError);
if ($isError) { if ($isError) {
$xml->writeElement("message", $errorMessage); $xml->writeElement("message", $errorMessage);
} else {
$xml->writeElement("trackid", $trackId);
} }
if (!empty($extra)) {
foreach ($extra as $key => $value) {
$xml->writeElement($key, $value);
}
}
$xml->endElement(); $xml->endElement();
$xml->endDocument(); $xml->endDocument();
$xml->flush(); $xml->flush();
@ -108,17 +114,19 @@ else if (empty($gpx->trk)) {
exitWithStatus(true, $lang["idatafailure"]); exitWithStatus(true, $lang["idatafailure"]);
} }
$trackName = empty($gpx->trk->name) ? $gpxName : $gpx->trk->name->__toString(); $trackCnt = 0;
$metaName = empty($gpx->metadata->name) ? NULL : $gpx->metadata->name->__toString(); foreach ($gpx->trk as $trk) {
$track = new uTrack(); $trackName = empty($trk->name) ? $gpxName : $trk->name->__toString();
$trackId = $track->add($user->id, $trackName, $metaName); $metaName = empty($gpx->metadata->name) ? NULL : $gpx->metadata->name->__toString();
if ($trackId === false) { $track = new uTrack();
$trackId = $track->add($user->id, $trackName, $metaName);
if ($trackId === false) {
exitWithStatus(true, $lang["servererror"]); exitWithStatus(true, $lang["servererror"]);
break; break;
} }
$position = new uPosition(); $position = new uPosition();
foreach($gpx->trk->trkseg as $segment) { foreach($trk->trkseg as $segment) {
foreach($segment->trkpt as $point) { foreach($segment->trkpt as $point) {
$ret = $position->add($user->id, $trackId, $ret = $position->add($user->id, $trackId,
strtotime($point->time), $point["lat"], $point["lon"], $point->ele, strtotime($point->time), $point["lat"], $point["lon"], $point->ele,
@ -127,9 +135,11 @@ foreach($gpx->trk->trkseg as $segment) {
exitWithStatus(true, $lang["servererror"]); exitWithStatus(true, $lang["servererror"]);
} }
} }
}
$trackCnt++;
} }
// return track id // return track id and tracks count
exitWithStatus(false, NULL, $trackId); exitWithStatus(false, NULL, [ "trackid" => $trackId, "trackcnt" => $trackCnt ]);
?> ?>