} elseif (empty($userId) === false) { $reply->data = Trail::getTrailByUserId($pdo, $userId)->toArray(); } elseif (empty($submitId) === false) { $reply->data = Trail::getTrailBySubmitTrailId($pdo, $submitId)->toArray(); } elseif (empty($amenities) === false) { $reply->data = Trail::getTrailByTrailAmenities($pdo, $amenities)->toArray(); } elseif (empty($condition) === false) { $reply->data = Trail::getTrailByTrailCondition($pdo, $condition)->toArray(); } elseif (empty($description) === false) { $reply->data = Trail::getTrailByTrailDescription($pdo, $description)->toArray(); } elseif (empty($difficulty) === false) { $reply->data = Trail::getTrailByTrailDifficulty($pdo, $difficulty)->toArray(); } elseif (empty($distance) === false) { $reply->data = Trail::getTrailByTrailDistance($pdo, $distance)->toArray(); } elseif (empty($name) === false) { $reply->data = Trail::getTrailByTrailName($pdo, $name)->toArray(); } elseif (empty($submission) === false) { $reply->data = Trail::getTrailByTrailSubmissionType($pdo, $submission)->toArray(); } elseif (empty($terrain) === false) { $reply->data = Trail::getTrailByTrailTerrain($pdo, $terrain)->toArray(); } elseif (empty($traffic) === false) { $reply->data = Trail::getTrailByTrailTraffic($pdo, $traffic)->toArray(); } elseif (empty($use) === false) { $reply->data = Trail::getTrailByTrailUse($pdo, $use)->toArray(); //} elseif (empty($uuid) === false) { $reply->data = Trail::getTrailByTrailUuid($pdo, $uuid); } else { $reply->data = Trail::getAllTrails($pdo)->toArray(); } } //verify user and verify object is not empty
/** * * Decodes geoJson file, converts to string, sifts through the string and inserts the data into the database * * @param string $url * @throws PDOException PDO related errors * @throws Exception catch-all exception **/ public static function readTrailSegmentsGeoJson($url) { $context = stream_context_create(array("http" => array("ignore_errors" => true, "method" => "GET"))); try { $pdo = connectToEncryptedMySQL("/var/www/trailquail/encrypted-mysql/trailquail.ini"); if (($jsonData = file_get_contents($url, null, $context)) !== false) { if (($jsonFd = @fopen("php://memory", "wb+")) === false) { throw new RuntimeException("Memory Error: I can't remember"); } //decode the geoJson file $jsonConverted = json_decode($jsonData); $jsonFeatures = $jsonConverted->features; // create array from converted geoJson file $properties = new SplFixedArray(count($jsonFeatures)); //loop through array to get to json properties foreach ($jsonFeatures as $jsonFeature) { $properties[$properties->key()] = $jsonFeature->properties; $properties->next(); } //create an array of trails and a SplObjectStorage for the trail guide $trails = []; $trailGuide = new SplObjectStorage(); //loop through array to get trail name and trail use foreach ($properties as $property) { $trailName = $property->name; $trailUse = ""; //set $trailUse string based on information in geoJson properties field. if ($property->bicycle === "yes") { $trailUse = $trailUse . "bicycle: yes, "; } else { $trailUse = $trailUse . "bicycle: no, "; } if ($property->foot === "yes") { $trailUse = $trailUse . "foot: yes, "; } else { $trailUse = $trailUse . "foot: no, "; } if ($property->wheelchair === "yes") { $trailUse = $trailUse . "wheelchair: yes "; } else { $trailUse = $trailUse . "wheelchair: no "; } //get the trail by name and set the trail use try { $candidateTrails = Trail::getTrailByTrailName($pdo, $trailName); foreach ($candidateTrails as $trail) { $trail->setTrailUse($trailUse); $trail->update($pdo); $trails[] = $trail; } } catch (PDOException $pdoException) { $sqlStateCode = "23000"; $errorInfo = $pdoException->errorInfo; if ($errorInfo[0] === $sqlStateCode) { } else { throw new PDOException($pdoException->getMessage(), 0, $pdoException); } } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } } try { $trailIndex = 0; foreach ($jsonFeatures as $jsonFeature) { $jsonCoordinates = $jsonFeature->geometry->coordinates; if ($jsonFeature->geometry->type === "LineString") { $coordinates = new SplFixedArray(count($jsonCoordinates)); foreach ($jsonCoordinates as $coordinate) { $coordinates[$coordinates->key()] = $coordinate; $coordinates->next(); } $trailGuide[$trails[$trailIndex]] = $coordinates; $trailIndex++; } else { if ($jsonFeature->geometry->type === "MultiLineString") { $trailClones = []; $trails[$trailIndex]->delete($pdo); for ($i = 1; $i <= count($jsonCoordinates); $i++) { $trail = clone $trails[$trailIndex]; $trail->setTrailId(null); $trail->setTrailName($trail->getTrailName() . " {$i}"); $trail->insert($pdo); $trailClones[] = $trail; } array_splice($trails, $trailIndex, 1, $trailClones); foreach ($jsonCoordinates as $lineCoordinates) { $trailGuide[$trails[$trailIndex]] = $lineCoordinates; $trailIndex++; } } } } $trailGuide->rewind(); foreach ($trailGuide as $map) { $trail = $trailGuide->current(); $geo = $trailGuide->getInfo(); for ($indexTwo = 0; $indexTwo < count($geo) - 1; $indexTwo++) { $segmentStartX = $geo[$indexTwo][0]; $segmentStartY = $geo[$indexTwo][1]; // $segmentStartElevation = $geo[$indexTwo][2]; $segmentStopX = $geo[$indexTwo + 1][0]; $segmentStopY = $geo[$indexTwo + 1][1]; // $segmentStopElevation = $geo[$indexTwo + 1][2]; $segmentStart = new Point($segmentStartX, $segmentStartY); $segmentStop = new Point($segmentStopX, $segmentStopY); try { $segment = new Segment(null, $segmentStart, $segmentStop, 0, 0); $segment->insert($pdo); $relationship = new TrailRelationship($segment->getSegmentId(), $trail->getTrailId(), "T"); $relationship->insert($pdo); } catch (PDOException $pdoException) { $sqlStateCode = "23000"; $errorInfo = $pdoException->errorInfo; if ($errorInfo[0] === $sqlStateCode) { } else { throw new PDOException($pdoException->getMessage(), 0, $pdoException); } } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } } } } catch (PDOException $pdoException) { $sqlStateCode = "23000"; $errorInfo = $pdoException->errorInfo; if ($errorInfo[0] === $sqlStateCode) { } else { throw new PDOException($pdoException->getMessage(), 0, $pdoException); } } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } } fclose($jsonFd); } catch (PDOException $pdoException) { throw new PDOException($pdoException->getMessage(), 0, $pdoException); } catch (Exception $exception) { throw new Exception($exception->getMessage(), 0, $exception); } }
/** * test grabbing a Trail by TrailName that does not exist * * @expectedException PDOException **/ public function testGetInvalidTrailByTrailName() { //grab a TrailName that does not exist $trail = Trail::getTrailByTrailName($this->getPDO(), "<script></script>"); $this->assertNull($trail); }