Completed
Push — master ( 29ed75...a64902 )
by Jason
29s
created

code/Locator.php (12 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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