Skip to content

Commit

Permalink
add search option
Browse files Browse the repository at this point in the history
  • Loading branch information
ramblerswebs committed Jun 5, 2020
1 parent 90cad19 commit 89374db
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
5 changes: 3 additions & 2 deletions classes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ public static function errorEmail($feed, $error) {
$mailer->addAddress(NOTIFY, 'Web Master');
$mailer->isHTML(true);
$mailer->Subject = "Ramblers Feed Error";
$mailer->Body = "<p>Feed error found while running: " . TASK . "</p><p>"
. $error . "</p>";
$mailer->Body = "<p>Feed error found while running: " . TASK . "</p>".
"<p>Feed: ".$feed. "</p>"
. "<p>Error: ". $error . "</p>";
$mailer->send();
echo "Error message sent" . BR;
echo "Task: " . TASK . BR;
Expand Down
28 changes: 24 additions & 4 deletions classes/groups/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public function process($latitude, $longitude, $distance, $maxpoints) {
$dist = GeometryGreatcircle::distance($latitude, $longitude, $lat, $lon, GeometryGreatcircle::KM);
$group->distance = $dist;
}


usort($this->groups, "GroupsFile::cmpDistance");
foreach ($this->groups as $key => $group) {
if ($group->distance > $distance) {
Expand All @@ -45,8 +43,30 @@ public function process($latitude, $longitude, $distance, $maxpoints) {
}
return $this->groups;
}
private static function cmpDistance($a, $b) {
return $a->distance> $b->distance;

public function search($search, $number) {
$find = strtolower($search);
foreach ($this->groups as $key => $group) {
$found = false;
if (strpos(strtolower($group->name), $find) !== false) {
$found = true;
}
if (!$found) {
unset($this->groups[$key]);
}
}
$no = 0;
foreach ($this->groups as $key => $group) {
$no += 1;
if ($no > $number) {
unset($this->groups[$key]);
}
}
return $this->groups;
}

private static function cmpDistance($a, $b) {
return $a->distance > $b->distance;
}

public function allGroups() {
Expand Down
19 changes: 10 additions & 9 deletions classes/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* @author Chris Vaughan
*/
class Options {

private $gets = array();
private $posts = array();
private static $thisclass;

function __construct() {
self::$thisclass=$this;
self::$thisclass = $this;

foreach ($_GET as $key => $value) {
$this->gets[$key] = htmlspecialchars($value);
Expand All @@ -21,27 +21,28 @@ function __construct() {
$this->posts[$key] = htmlspecialchars($value);
}
}
public function getOptions(){

public function getOptions() {
return self::$thisclass;
}

public function gets($name) {
if (isset($this->gets[$name])){
return $this->gets[$name];
if (isset($this->gets[$name])) {
return $this->gets[$name];
} else {
return null;
}

}

public function posts($name) {
if (isset($this->posts[$name])){
return $this->posts[$name];
if (isset($this->posts[$name])) {
return $this->posts[$name];
} else {
return null;
}
}
public function noGets(){

public function noGets() {
return count($this->gets);
}

Expand Down
18 changes: 18 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

// https://groups.theramblers.org.uk/?latitude=51.4589653&longitude=-2.52582669&maxpoints=100&dist=30

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('assert.warning', 1);
Expand All @@ -26,12 +28,28 @@
$opts = new Options();
$groups = new GroupsFile();
If ($opts->noGets() === 0) {
// return all groups
$allGroups = $groups->allGroups();
header("Access-Control-Allow-Origin: *");
header("Content-type: application/json");
echo json_encode($allGroups);
}
$search = $opts->gets("search");
if ($search != null) {
// do a name search
$number = $opts->gets("number");
if ($number==null){
$number=20;
}
$groups = $groups->search($search, $number);
header("Access-Control-Allow-Origin: *");
header("Content-type: application/json");
echo json_encode($groups);

return;
}

// do a location search
$latitude = $opts->gets("latitude");
$longitude = $opts->gets("longitude");
$distance = $opts->gets("dist");
Expand Down

0 comments on commit 89374db

Please sign in to comment.