/** * Returns the walk specified by the walkid parameter in the get string. * Loads it from the database if necessary. */ public function getWalk() { if (!$this->fetchedWalk) { // TODO: Check user has permission to view this walk $this->walk = Walk::getSingle(JRequest::getInt("walkid", 0, "get")); $this->fetchedWalk = true; } return $this->walk; }
function canEdit($walkOrID) { if (JFactory::getUser()->authorise("walk.editall", "com_swg_walklibrary")) { return true; } else { if (!JFactory::getUser()->authorise("walk.editown", "com_swg_walklibrary")) { return false; } else { if (is_numeric($walkOrID)) { $walk = Walk::getSingle($walkOrID); } else { if ($walkOrID instanceof Walk) { $walk = $walkOrID; } } if (empty($walk)) { throw new InvalidArgumentException("Invalid walk or ID"); } return $walk->suggestedBy == Leader::fromJoomlaUser(JFactory::getUser()->id); } } }
/** * Loads the walk specified, or a blank one if none specified */ public function loadWalk($walkid) { if (empty($walkid)) { $this->walk = new Walk(); } else { $this->walk = Walk::getSingle($walkid); $this->walk->loadRoute(); } }
<?php // No direct access to this file defined('_JEXEC') or die('Restricted access'); // What type of event to we want? // TODO: Probably shouldn't return anything that isn't OK to publish - this is publicly accessible. $id = JRequest::getVar('id', null, "get", "INTEGER"); if (!isset($id)) { jexit("ID must be specified"); } include_once JPATH_BASE . "/swg/Models/Walk.php"; $result = Walk::getSingle($id); echo $result->jsonEncode(); die;
public function __get($name) { // Load connected objects from the database as needed switch ($name) { case "meetPoint": $this->meetPoint = new WalkMeetingPoint($this->meetPointId, $this->start, $this->meetPlaceTime); break; case "leader": $this->leader = Leader::getLeader($this->leaderId); if (!empty($this->leaderName)) { $this->leader->setDisplayName($this->leaderName); } break; case "backmarker": $this->backmarker = Leader::getLeader($this->backmarkerId); if (!empty($this->backmarkerName)) { $this->backmarker->setDisplayName($this->backmarkerName); } break; case "walk": return Walk::getSingle($this->walkid); case "track": // Load the track if we don't have it already // TODO: Only try once, and catch exceptions if (!isset($this->track)) { $this->loadTrack(); } return $this->track; case "distance": // If we don't have a (real) distance set, convert the (estimated) miles into metres and give that break; // If the walk is circular, return the equivalent start point // If the walk is circular, return the equivalent start point case "endPlaceName": case "endLatLng": case "endGridRef": if ($this->isLinear) { return $this->{$name}; } else { $var = "start" . substr($name, 3); return $this->{$var}; } } return $this->{$name}; // TODO: What params should be exposed? }
$route = Route::loadSingle($routeid); if (!empty($route)) { print $route->jsonEncode(); jexit(); } } // If we get here, we failed to load a route. // Get a route for a walkinstance (this means getting the walk) if (isset($walkinstanceid)) { $f = SWG::walkInstanceFactory(); $walk = $f->getSingle($walkinstanceid); } // Get the route for a particular walk if walkid is set if (isset($walkid)) { include_once JPATH_BASE . "/swg/Models/Walk.php"; $walk = Walk::getSingle($walkid); } if (isset($walk)) { $routes = Route::loadForWalkable($walk, true, $type, 1); // TODO: Support logged routes if (!empty($routes)) { $result = $routes[0]; } else { $result = false; } // Error condition - notify caller there are no routes available } if ($result instanceof SWGBaseModel) { echo $result->jsonEncode(); } else { echo json_encode($result);
/** * Loads a route from the database from its ID * @param int $id Route ID to load * @param Walkable $w Walk to attach this route to, if the object already exists. A new one will be created if not. * @throws InvalidArgumentException If walkable passed in does not match walkable set in database */ public static function loadSingle($id, Walkable $w = null) { $id = (int) $id; $db =& JFactory::getDBO(); // First, get the route's general data $query = $db->getQuery(true); $query->select("*"); $query->from("routes"); $query->where(array("routeid = " . $id)); $db->setQuery($query); $res = $db->query(); if ($db->getNumRows($res) == 0) { return null; } $dbArr = $db->loadAssoc(); $wiFactory = SWG::walkInstanceFactory(); // If we've been given a Walkable, make sure it matches the one on the route // It's OK to load a route for a WalkInstance on a Walk, and vice-versa if ($w != null) { if ($w instanceof Walk) { if ($dbArr['walkid'] != $w->id) { // Try to load a matching walkInstance if (!empty($dbArr['walkinstanceid'])) { $wi = $wiFactory->getSingle($dbArr['walkinstanceid']); if ($wi->walkid != $w->id) { throw new InvalidArgumentException("Loaded route is for WalkInstance " . $wi->id . ", Walk " . $wi->walkid . " (does not match Walk " . $w->id . ")"); } } else { throw new InvalidArgumentException("Loaded route is for Walk " . $dbArr['walkid'] . " (does not match Walk " . $w->id . ")"); } } } else { if ($dbArr['walkinstanceid'] != $w->id && $dbArr['walkid'] != $w->walkid) { throw new InvalidArgumentException("Loaded route is not for given WalkInstance"); } } } else { // Load the Walkable if (empty($dbArr['walkinstanceid'])) { // A Walk $w = Walk::getSingle($dbArr['walkid']); } else { // A WalkInstance $w = $wiFactory->getSingle($dbArr['walkinstanceid']); } } // Create the route object $rt = new Route($w); // TODO: uploadedby/time, length, ascent // Load the basic route properties $rt->id = $id; $rt->distance = $dbArr['length']; $rt->ascent = $dbArr['ascent']; $rt->uploadedBy = $dbArr['uploadedby']; // TODO: Load the actual user? Also, uploadedby should be a Joomla user, not a Leader $rt->uploadedDateTime = strtotime($dbArr['uploadeddatetime']); $rt->visibility = (int) $dbArr['visibility']; $rt->type = (int) $dbArr['type']; // Set all the waypoints $query = $db->getQuery(true); $query->select('*'); $query->from("routepoints"); $query->where(array("routeid = " . $id)); $query->order("sequenceid ASC"); $db->setQuery($query); $res = $db->query(); // Joomla can't be arsed to support/provide documentation for fetching individual rows like EVERYTHING ELSE DOES $megaArray = $db->loadAssocList("sequenceid"); foreach ($megaArray as $i => $dbArr) { $wp = new Waypoint(); $wp->osRef = new OSRef($dbArr['easting'], $dbArr['northing']); $wp->altitude = $dbArr['altitude']; $wp->time = $dbArr['datetime']; $rt->setWaypoint($i, $wp); } return $rt; }