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');
if (root.length && getNode(root[0], 'error') == 0) {
trackId = getNode(root[0], 'trackid');
trackCnt = getNode(root[0], 'trackcnt');
getTracks(userid);
loadTrack(userid, trackId, 1);
if (trackCnt > 1) {
alert(sprintf(lang['imultiple'], trackCnt));
}
return;
}
errorMsg = getNode(root[0], 'message');

View File

@ -118,4 +118,5 @@ $lang["import"] = "Import track";
$lang["iuploadfailure"] = "Uploading failed";
$lang["iparsefailure"] = "Parsing failed";
$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["iparsefailure"] = "Błąd parsowania 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
* @param boolean $isError Error if true
* @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");
$xml = new XMLWriter();
$xml->openURI("php://output");
@ -46,9 +47,14 @@ function exitWithStatus($isError, $errorMessage = NULL, $trackId = NULL) {
$xml->writeElement("error", (int) $isError);
if ($isError) {
$xml->writeElement("message", $errorMessage);
} else {
$xml->writeElement("trackid", $trackId);
}
if (!empty($extra)) {
foreach ($extra as $key => $value) {
$xml->writeElement($key, $value);
}
}
$xml->endElement();
$xml->endDocument();
$xml->flush();
@ -108,17 +114,19 @@ else if (empty($gpx->trk)) {
exitWithStatus(true, $lang["idatafailure"]);
}
$trackName = empty($gpx->trk->name) ? $gpxName : $gpx->trk->name->__toString();
$metaName = empty($gpx->metadata->name) ? NULL : $gpx->metadata->name->__toString();
$track = new uTrack();
$trackId = $track->add($user->id, $trackName, $metaName);
if ($trackId === false) {
$trackCnt = 0;
foreach ($gpx->trk as $trk) {
$trackName = empty($trk->name) ? $gpxName : $trk->name->__toString();
$metaName = empty($gpx->metadata->name) ? NULL : $gpx->metadata->name->__toString();
$track = new uTrack();
$trackId = $track->add($user->id, $trackName, $metaName);
if ($trackId === false) {
exitWithStatus(true, $lang["servererror"]);
break;
}
}
$position = new uPosition();
foreach($gpx->trk->trkseg as $segment) {
$position = new uPosition();
foreach($trk->trkseg as $segment) {
foreach($segment->trkpt as $point) {
$ret = $position->add($user->id, $trackId,
strtotime($point->time), $point["lat"], $point["lon"], $point->ele,
@ -127,9 +135,11 @@ foreach($gpx->trk->trkseg as $segment) {
exitWithStatus(true, $lang["servererror"]);
}
}
}
$trackCnt++;
}
// return track id
exitWithStatus(false, NULL, $trackId);
// return track id and tracks count
exitWithStatus(false, NULL, [ "trackid" => $trackId, "trackcnt" => $trackCnt ]);
?>