Passed
Pull Request — master (#223)
by Nic
03:06
created

Locator   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 22
eloc 67
c 0
b 0
f 0
dl 0
loc 243
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getLimit() 0 3 1
A getCMSFields() 0 25 1
A getPageCategories() 0 3 1
A getShowRadius() 0 3 1
A getUsedCategories() 0 4 1
A getInfoWindowTemplate() 0 6 1
A getMapStyleJSONPath() 0 3 1
A get_locations() 0 21 5
A get_all_categories() 0 3 1
A getRadii() 0 14 2
A getMarkerIcon() 0 3 1
A getListTemplate() 0 6 1
A getMapStyle() 0 3 1
A getRadiiArrayList() 0 11 2
A locator_categories_by_locator() 0 7 2
1
<?php
2
3
namespace Dynamic\Locator\Page;
4
5
use Dynamic\Locator\Model\LocationCategory;
6
use Dynamic\SilverStripeGeocoder\AddressDataExtension;
7
use SilverStripe\Core\Manifest\ModuleResourceLoader;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
10
use SilverStripe\Forms\HeaderField;
11
use SilverStripe\Forms\OptionsetField;
12
use SilverStripe\Forms\GridField\GridField;
13
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
14
use SilverStripe\ORM\DataList;
15
use SilverStripe\ORM\ArrayList;
16
use SilverStripe\Core\Config\Config;
17
use SilverStripe\View\ArrayData;
18
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
19
20
/**
21
 * Class Locator
22
 *
23
 * @property bool $AutoGeocode
24
 * @property bool $ModalWindow
25
 * @property string $Unit
26
 * @method Categories|ManyManyList $Categories
27
 */
28
class Locator extends \Page
29
{
30
    /**
31
     * @var string
32
     */
33
    private static $singular_name = 'Locator';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
34
35
    /**
36
     * @var string
37
     */
38
    private static $plural_name = 'Locators';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
39
40
    /**
41
     * @var string
42
     */
43
    private static $description = 'Display locations on a map';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
44
45
    /**
46
     * @var array
47
     */
48
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
49
        'Unit' => 'Enum("m,km","m")',
50
    );
51
52
    /**
53
     * @var array
54
     */
55
    private static $many_many = array(
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
56
        'Categories' => LocationCategory::class,
57
    );
58
59
    /**
60
     * @var string
61
     */
62
    private static $table_name = 'Locator';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
63
64
    /**
65
     * @var string
66
     */
67
    private static $location_class = LocationPage::class;
0 ignored issues
show
introduced by
The private property $location_class is not used, and could be removed.
Loading history...
68
69
    /**
70
     * @return FieldList
71
     */
72
    public function getCMSFields()
73
    {
74
        $this->beforeUpdateCMSFields(function ($fields) {
75
            // Settings
76
            $fields->addFieldsToTab('Root.Settings', array(
77
                HeaderField::create('DisplayOptions', 'Display Options', 3),
78
                OptionsetField::create('Unit', 'Unit of measure', array('m' => 'Miles', 'km' => 'Kilometers')),
79
            ));
80
81
            // Filter categories
82
            $config = GridFieldConfig_RelationEditor::create();
83
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
84
            $config->addComponent(new GridFieldAddExistingSearchButton());
85
            $categories = $this->Categories();
0 ignored issues
show
Bug introduced by
The method Categories() does not exist on Dynamic\Locator\Page\Locator. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
            /** @scrutinizer ignore-call */ 
86
            $categories = $this->Categories();
Loading history...
86
            $categoriesField = GridField::create('Categories', 'Categories', $categories, $config)
87
                ->setDescription('only show locations from the selected category');
88
89
            // Filter
90
            $fields->addFieldsToTab('Root.Filter', array(
91
                HeaderField::create('CategoryOptionsHeader', 'Location Filtering', 3),
92
                $categoriesField,
93
            ));
94
        });
95
96
        return parent::getCMSFields();
97
    }
98
99
    /**
100
     * @param array $filter
101
     * @param array $filterAny
102
     * @param array $exclude
103
     * @param null|callable $callback
104
     *
105
     * @return DataList|ArrayList
106
     */
107
    public static function get_locations(
108
        $filter = [],
109
        $filterAny = [],
110
        $exclude = [],
111
        $callback = null
112
    ) {
113
        $locationClass = Config::inst()->get(static::class, 'location_class');
114
        $locations = $locationClass::get()->filter($filter)->exclude($exclude);
115
116
        if (!empty($filterAny)) {
117
            $locations = $locations->filterAny($filterAny);
118
        }
119
        if (!empty($exclude)) {
120
            $locations = $locations->exclude($exclude);
121
        }
122
123
        if ($callback !== null && is_callable($callback)) {
124
            $locations->filterByCallback($callback);
125
        }
126
127
        return $locations;
128
    }
129
130
    /**
131
     * @return DataList
132
     */
133
    public static function get_all_categories()
134
    {
135
        return LocationCategory::get();
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function getPageCategories()
142
    {
143
        return self::locator_categories_by_locator($this->ID);
144
    }
145
146
    /**
147
     * @param int $id
148
     *
149
     * @return bool|
150
     */
151
    public static function locator_categories_by_locator($id = 0)
152
    {
153
        if ($id == 0) {
154
            return false;
155
        }
156
157
        return static::get()->byID($id)->getUsedCategories();
158
    }
159
160
    /**
161
     * Gets the list of radii
162
     *
163
     * @return ArrayList
164
     */
165
    public function getRadii()
166
    {
167
        $radii = [
168
            '0' => '25',
169
            '1' => '50',
170
            '2' => '75',
171
            '3' => '100',
172
        ];
173
        $config_radii = $this->config()->get('radii');
174
        if ($config_radii) {
175
            $radii = $config_radii;
176
        }
177
178
        return $radii;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $radii also could return the type array<string,string> which is incompatible with the documented return type SilverStripe\ORM\ArrayList.
Loading history...
179
    }
180
181
    public function getRadiiArrayList()
182
    {
183
        $list = [];
184
185
        foreach ($this->getRadii() as $radius) {
186
            $list[] = new ArrayData(array(
187
                'Radius' => $radius,
188
            ));
189
        }
190
191
        return new ArrayList($list);
192
    }
193
194
    /**
195
     * Gets the limit of locations
196
     * @return mixed
197
     */
198
    public function getLimit()
199
    {
200
        return $this->config()->get('limit');
201
    }
202
203
    /**
204
     * Gets if the radius drop down should be shown
205
     * @return mixed
206
     */
207
    public function getShowRadius()
208
    {
209
        return $this->config()->get('show_radius');
210
    }
211
212
    /**
213
     * @return mixed
214
     */
215
    public function getUsedCategories()
216
    {
217
        return $this->Categories()->filter([
218
            'LocationSet.ID:GreaterThan' => 0,
219
        ]);
220
    }
221
222
    /**
223
     * Gets the path of the info window template
224
     *
225
     * @return string
226
     */
227
    public function getInfoWindowTemplate()
228
    {
229
        return ModuleResourceLoader::singleton()->resolveURL(
230
            Config::inst()->get(
231
                static::class,
232
                'infoWindowTemplate'
233
            )
234
        );
235
    }
236
237
    /**
238
     * Gets the path of the list template
239
     *
240
     * @return string
241
     */
242
    public function getListTemplate()
243
    {
244
        return ModuleResourceLoader::singleton()->resolveURL(
245
            Config::inst()->get(
246
                static::class,
247
                'listTemplate'
248
            )
249
        );
250
    }
251
252
    /**
253
     * @return null|string
254
     */
255
    public function getMapStyle()
256
    {
257
        return AddressDataExtension::getMapStyleJSON();
258
    }
259
260
    public function getMapStyleJSONPath()
261
    {
262
        return AddressDataExtension::getMapStyleJSONPath();
263
    }
264
265
    /**
266
     * @return null|string
267
     */
268
    public function getMarkerIcon()
269
    {
270
        return AddressDataExtension::getIconImage();
271
    }
272
}
273