Completed
Push — master ( 5c8995...29ed75 )
by Jason
28s
created

Locator::getPageCategories()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 12
1
<?php
2
3
class Locator extends Page
4
{
5
    private static $db = array(
1 ignored issue
show
Unused Code introduced by
The property $db 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...
6
        'AutoGeocode' => 'Boolean',
7
        'ModalWindow' => 'Boolean',
8
        'Unit' => 'Enum("m,km","m")',
9
    );
10
11
    private static $many_many = array(
1 ignored issue
show
Unused Code introduced by
The property $many_many 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...
12
        'Categories' => 'LocationCategory',
13
    );
14
15
    private static $defaults = array(
1 ignored issue
show
Unused Code introduced by
The property $defaults 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...
16
        'AutoGeocode' => true,
17
    );
18
19 1
    private static $singular_name = 'Locator';
1 ignored issue
show
Unused Code introduced by
The property $singular_name 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...
20
    private static $plural_name = 'Locators';
1 ignored issue
show
Unused Code introduced by
The property $plural_name 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...
21 1
    private static $description = 'Find locations on a map';
1 ignored issue
show
Unused Code introduced by
The property $description 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
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
24 1
    {
25 1
        $fields = parent::getCMSFields();
26 1
27
        // Settings
28
        $fields->addFieldsToTab('Root.Settings', array(
29 1
            HeaderField::create('DisplayOptions', 'Display Options', 3),
30 1
            OptionsetField::create('Unit', 'Unit of measure', array('m' => 'Miles', 'km' => 'Kilometers')),
31
            CheckboxField::create('AutoGeocode', 'Auto Geocode - Automatically filter map results based on user location')
32
                ->setDescription('Note: if any locations are set as featured, the auto geocode is automatically disabled.'),
33 1
            CheckboxField::create('ModalWindow', 'Modal Window - Show Map results in a modal window'),
34 1
        ));
35 1
36 1
        // Filter categories
37 1
        $config = GridFieldConfig_RelationEditor::create();
38 1
        if (class_exists('GridFieldAddExistingSearchButton')) {
39 1
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
40
            $config->addComponent(new GridFieldAddExistingSearchButton());
41 1
        }
42
        $categories = $this->Categories();
43 1
        $categoriesField = GridField::create('Categories', 'Categories', $categories, $config)
44
            ->setDescription('only show locations from the selected category');
45
46 3
            // Filter
47
            $fields->addFieldsToTab('Root.Filter', array(
48 3
                HeaderField::create('CategoryOptionsHeader', 'Location Filtering', 3),
49
                $categoriesField,
50 3
            ));
51 3
52 3
        $this->extend('updateCMSFields', $fields);
53 3
54
        return $fields;
55
    }
56 1
57
    public static function getLocations($filter = array(), $exclude = array(), $filterAny = array())
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
58 1
    {
59
        $filter['ShowInLocator'] = true;
60
        $exclude['Lat'] = 0;
61 1
62
        $Locations = Location::get()->exclude($exclude)->filter($filter)->filterAny($filterAny);
63 1
64
        return $Locations;
65
    }
66
67
    public function getAreLocations()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
68
    {
69
        return self::getLocations();
70
    }
71
72
    public function getAllCategories()
73
    {
74
        return LocationCategory::get();
75
    }
76
77
    public static function getPageCategories($id = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
78
    {
79
        if ($id) {
80
            if ($locator = Locator::get()->byID($id)) {
81
                return $locator->Categories();
82
            }
83
            return false;
84
        }
85
        return false;
86
    }
87
}
88
89
class Locator_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...
90
{
91
    // allowed actions
92
    private static $allowed_actions = array('xml');
1 ignored issue
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...
93
94
    // Set Requirements based on input from CMS
95
    public function init()
96
    {
97
        parent::init();
98
99
        $themeDir = SSViewer::get_theme_folder();
100
101
        Requirements::javascript('framework/thirdparty/jquery/jquery.js');
102
        if (Locator::getLocations()) {
103
            Requirements::javascript('http://maps.google.com/maps/api/js?sensor=false');
104
            Requirements::javascript('locator/thirdparty/handlebars/handlebars-v1.3.0.js');
105
            Requirements::javascript('locator/thirdparty/jquery-store-locator/js/jquery.storelocator.js');
106
        }
107
108
        Requirements::css('locator/css/map.css');
109
110
        $featured = (Locator::getLocations(array('Featured' => 1))->count() > 0) ?
111
            'featuredLocations: true' :
112
            'featuredLocations: false';
113
114
        // map config based on user input in Settings tab
115
        // AutoGeocode or Full Map
116
        $load = ($this->data()->AutoGeocode) ?
117
            'autoGeocode: true, fullMapStart: false,' :
118
            'autoGeocode: false, fullMapStart: true, storeLimit: 1000, maxDistance: true,';
119
120
        $base = Director::baseFolder();
121
        $themePath = $base.'/'.$themeDir;
122
123
        $listTemplatePath = (file_exists($themePath.'/templates/location-list-description.html')) ?
124
            $themeDir.'/templates/location-list-description.html' :
125
            'locator/templates/location-list-description.html';
126
        $infowindowTemplatePath = (file_exists($themePath.'/templates/infowindow-description.html')) ?
127
            $themeDir.'/templates/infowindow-description.html' :
128
            'locator/templates/infowindow-description.html';
129
130
        // in page or modal
131
        $modal = ($this->data()->ModalWindow) ? 'modalWindow: true' : 'modalWindow: false';
132
133
        $kilometer = ($this->data()->Unit == 'km') ? 'lengthUnit: "km"' : 'lengthUnit: "m"';
134
135
        $link = $this->Link().'xml.xml';
136
137
        // init map
138
        if (Locator::getLocations()) {
139
            Requirements::customScript("
140
                $(function($) {
141
                    $('#map-container').storeLocator({
142
                        ".$load."
143
                        dataLocation: '".$link."',
144
                        listTemplatePath: '".$listTemplatePath."',
145
                        infowindowTemplatePath: '".$infowindowTemplatePath."',
146
                        originMarker: true,
147
                        ".$modal.',
148
                        '.$featured.",
149
                        slideMap: false,
150
                        zoomLevel: 0,
151
                        distanceAlert: 120,
152
                        formID: 'Form_LocationSearch',
153
                        inputID: 'Form_LocationSearch_address',
154
                        categoryID: 'Form_LocationSearch_category',
155
                        distanceAlert: -1,
156
                        ".$kilometer.'
157
                    });
158
                });
159
            ');
160
        }
161
    }
162
163
    /**
164
     * Find all locations for map.
165
     *
166
     * Will return a XML feed of all locations marked "show in locator".
167
     *
168 1
     * @return XML file
169
     *
170 1
     * @todo rename/refactor to allow for json/xml
171 1
     * @todo allow $filter to run off of getVars key/val pair
172 1
     */
173 1
    public function xml(SS_HTTPRequest $request)
174
    {
175 1
        $filter = array();
176 1
        $exclude = array();
177 1
        $filterAny = array();
178 1
179
        //if a category filter selected
180 1
        if ($this->Categories()->exists()) {
181
            $categories = $this->Categories();
182 1
            foreach ($categories as $category) {
183
                $filterAny['CategoryID'] = $category->ID;
184 1
            }
185
        }
186
187
        $Locations = Locator::getLocations($filter, $exclude, $filterAny);
188
189
        return $this->customise(array(
190
            'Locations' => $Locations,
191
        ))->renderWith('LocationXML');
192 1
    }
193
194 1
    /**
195 1
     * LocationSearch form.
196 1
     *
197
     * Search form for locations, updates map and results list via AJAX
198 1
     *
199
     * @return Form
200
     */
201 1
    public function LocationSearch()
202
    {
203
        $fields = FieldList::create(
204
            $address = TextField::create('address', '')
205
        );
206
        $address->setAttribute('placeholder', 'address or zip code');
207
208
        $locatorCategories = Locator::getPageCategories($this->ID);
209
210
        if (LocationCategory::get()->Count() > 0 && $locatorCategories && $locatorCategories->Count() != 1) {
211
            $categories = LocationCategory::get();
212
213
            if ($categories->count() > 0) {
214
                $fields->push(
215
                    DropdownField::create(
216
                        'category',
217
                        '',
218
                        $categories->map('Title', 'Title')
219
                    )->setEmptyString('Select Category'));
220
            }
221
        }
222
223
        $actions = FieldList::create(
224
            FormAction::create('', 'Search')
225
        );
226
227
        return Form::create($this, 'LocationSearch', $fields, $actions);
228
    }
229
}
230