NearestPOIPage::getCMSFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4286
cc 1
eloc 7
nc 1
nop 0
crap 1
1
<?php
2
3
class NearestPOIPage extends Page
4
{
5
    public static $has_one = array('PointsOfInterestLayer' => 'PointsOfInterestLayer');
6
7 1
    public function getCMSFields()
8
    {
9 1
        $fields = parent::getCMSFields();
10 1
        $field = DropdownField::create('PointsOfInterestLayerID', 'PointsOfInterestLayer',
11 1
            PointsOfInterestLayer::get()->map('ID', 'Title'))
12 1
                ->setEmptyString('-- Select one --');
13 1
        $fields->addFieldToTab('Root.Layer', $field);
14
15 1
        return $fields;
16
    }
17
}
18
19
class NearestPOIPage_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
20
{
21
    private static $allowed_actions = array('find');
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
    /*
24
    For a given point of interest layer, find the 25 nearest places to provided location
25
     */
26 1
    public function find()
27
    {
28 1
        $params = $this->request->getVars();
29 1
        $latitude = $params['lat'];
30 1
        $longitude = $params['lng'];
31 1
        $sql = "SELECT DISTINCT poi.ID,Name,Lat,Lon, ( 6371 * acos( cos( radians({$latitude}) ) * cos( radians( Lat ) )
32 1
* cos( radians( Lon ) - radians({$longitude}) ) + sin( radians({$latitude}) ) * sin(radians(Lat)) ) ) AS distance
33
FROM PointOfInterest poi
34
INNER JOIN PointsOfInterestLayer_PointsOfInterest poilpoi
35
ON poilpoi.PointOfInterestID = poi.ID
36 1
WHERE PointsOfInterestLayerID={$this->PointsOfInterestLayerID}
37
HAVING distance < 25
38
ORDER BY distance
39 1
LIMIT 0 , 20;";
40
41 1
        $records = DB::query($sql);
42 1
        $result = new ArrayList();
43 1
        foreach ($records as $record) {
44 1
            $dob = new DataObject();
45
            //number_format((float)$number, 2, '.', '');
46 1
            $dob->Latitude = number_format((float) $record['Lat'], 3, '.', '');
47 1
            $dob->Longitude = number_format((float) $record['Lon'], 3, '.', '');
48 1
            $dob->Name = $record['Name'];
49 1
            $dob->Distance = number_format((float) $record['distance'], 2, '.', '');
50 1
            $mapurl = "http://maps.google.com?q={$dob->Latitude},{$dob->Longitude}";
51 1
            $dob->MapURL = $mapurl;
52 1
            $dirurl = "http://maps.google.com?saddr={$latitude},{$longitude}&daddr={$dob->Latitude},{$dob->Longitude}";
53 1
            $dob->DirURL = $dirurl;
54 1
            $result->push($dob);
55 1
        }
56
57 1
        $vars = new ArrayData(array('Nearest' => $result, 'Action' => $this->Action));
0 ignored issues
show
Unused Code introduced by
$vars is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58 1
        $this->Nearest = $result;
59
60 1
        return array();
61
    }
62
}
63