<?php

define("_JEXEC", true);
define("JPATH_BASE", "/home/peter/swg/website/public_html");
require_once "../swg.php";
include_once "../Models/Walk.php";
$walk = new Walk();
$route = new Route($walk);
$route->readGPX(DOMDocument::loadXML(file_get_contents($argv[1])));
$walk->setRoute($route);
echo "Distance: " . $walk->miles . " miles (" . $walk->distanceGrade . ")\n";
if ($walk->isLinear) {
    echo "Linear";
} else {
    echo "Circular";
}
echo "\n";
echo "Start: " . $walk->startGridRef . " (" . $walk->startPlaceName . ")\n";
echo "End: " . $walk->endGridRef . " (" . $walk->endPlaceName . ")\n";
$route->save();
 /**
  * Update the current walk with passed in form data
  * This also handles GPX data
  */
 public function updateWalk(array $formData)
 {
     $this->loadWalk($formData['id']);
     // Update all basic fields
     // Fields that can't be saved are just ignored
     // Invalid fields throw an exception - display this to the user and continue
     foreach ($formData as $name => $value) {
         try {
             if ($name == "suggestedBy") {
                 $this->walk->suggestedBy = Leader::getLeader($value);
             } else {
                 $this->walk->{$name} = $value;
             }
         } catch (UnexpectedValueException $e) {
             echo "<p>";
             var_dump($name);
             var_dump($value);
             var_dump($e->getMessage());
             echo "</p>";
         }
     }
     // Handle the route file upload
     $file = JRequest::getVar('jform', array(), 'files', 'array');
     if (!empty($file) && $file['error']['route'] == UPLOAD_ERR_OK) {
         // We've been given a GPX file. Try to parse it.
         $gpx = DOMDocument::load($file['tmp_name']['route']);
         if ($gpx) {
             // Check for a GPX element at the root
             if ($gpx->getElementsByTagName("gpx")->length == 1) {
                 // Get the route ID if we have an existing route
                 if (isset($this->walk->route)) {
                     $routeID = $this->walk->route->id;
                 } else {
                     $routeID = null;
                 }
                 // TODO: Turn on or off overwriting of existing properties
                 if (isset($this->walk->route)) {
                     $route = $this->walk->route;
                 } else {
                     $route = new Route($this->walk);
                 }
                 $route->readGPX($gpx);
                 $route->uploadedBy = JFactory::getUser()->id;
                 $route->uploadedDateTime = time();
                 $this->walk->setRoute($route, true);
                 // Store this route for later requests
                 JFactory::getApplication()->setUserState("uploadedroute", serialize($route));
                 JFactory::getApplication()->setUserState("deleteroute", false);
             } else {
                 echo "Must have only one GPX tag";
             }
         } else {
             echo "Not a GPX file";
         }
         // Return to the form
     } else {
         // Restore previously uploaded file from state
         $route = unserialize(JFactory::getApplication()->getUserState("uploadedroute"));
         if ($route) {
             $route->setWalk($this->walk);
             $this->walk->setRoute($route);
         }
     }
     // If we have a route, allow the user to set options on it
     if (isset($this->walk->route)) {
         $this->walk->route->visibility = $formData['routeVisibility'];
     }
 }
 public function upload()
 {
     // Check for request forgeries.
     JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
     $this->errors = array();
     // Initialise variables.
     $app = JFactory::getApplication();
     $model = $this->getModel('uploadtrack');
     $view = $this->getView('uploadtrack', 'html');
     $view->setModel($model, true);
     $wi = $model->getWalkInstance();
     // Are we uploading a track, or saving the track we've already uploaded?
     $file = JRequest::getVar('file', array(), 'FILES', 'array');
     if (!empty($file) && $file['error'] == UPLOAD_ERR_OK) {
         // We've been given a GPX file. Try to parse it.
         $gpx = DOMDocument::load($file['tmp_name']);
         if ($gpx && $gpx->getElementsByTagName("gpx")->length == 1) {
             $route = new Route();
             $wi = $model->getWalkInstance();
             if ($wi != null) {
                 $route->setWalk($wi);
             }
             $route->readGPX($gpx);
             $route->uploadedBy = JFactory::getUser()->id;
             $route->uploadedDateTime = time();
             $route->type = Route::Type_Logged;
             if ($wi != null) {
                 // Check that this route matches the walk
                 if ($route->checkAgainstWalk($model->getWalkInstance(), $message, $details)) {
                     // Store this route for later requests
                     JFactory::getApplication()->setUserState("uploadedroute", serialize($route));
                 } else {
                     throw new UserException("Can't upload this track", 0, "This track doesn't match that walk", $message . ": " . $details);
                 }
             } else {
                 // Try to find a matching walk
                 $wi = $route->findMatchingWalk();
                 if (isset($wi)) {
                     $route->setWalk($wi);
                     // Store this route for later requests
                     JFactory::getApplication()->setUserState("uploadedroute", serialize($route));
                     $model->setWalkInstance($wi);
                 } else {
                     throw new Exception("That track doesn't match any walks. Check that it doesn't contain anything other than the walk.");
                 }
             }
         } else {
             throw new Exception("The track you uploaded is not a valid GPX file. If your track is in another format, please convert it to GPX first, then upload it again.");
         }
         $view->display();
     } else {
         $route = unserialize(JFactory::getApplication()->getUserState("uploadedroute"));
         if ($route) {
             $route->save();
             // Set the distance on the WalkInstance, if it's not already set
             if (empty($wi->distance)) {
                 $wi->distance = $route->getDistance();
                 $wi->save();
             }
         } else {
             throw new Exception("There was an error while saving your track. You can try again in a while, or email us if that doesn't work.");
         }
         // Redirect to the specified page after saving
         $itemid = JRequest::getInt('returnPage');
         if (empty($itemid)) {
             return false;
         }
         $item = JFactory::getApplication()->getMenu()->getItem($itemid);
         $link = new JURI("/" . $item->route);
         // Jump to the event?
         if (JRequest::getBool('jumpToEvent')) {
             $link->setFragment("walk_" . $wi->id);
         }
         JFactory::getApplication()->redirect($link, "Track saved");
     }
 }