Locator::getCMSFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 25
rs 9.7998
cc 1
nc 1
nop 0
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\Lumberjack\Model\Lumberjack;
15
use SilverStripe\ORM\DataList;
16
use SilverStripe\ORM\ArrayList;
17
use SilverStripe\Core\Config\Config;
18
use SilverStripe\View\ArrayData;
19
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
20
21
/**
22
 * Class Locator
23
 *
24
 * @property bool $AutoGeocode
25
 * @property bool $ModalWindow
26
 * @property string $Unit
27
 * @method Categories|ManyManyList $Categories
28
 */
29
class Locator extends \Page
30
{
31
    /**
32
     * @var string
33
     */
34
    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...
35
36
    /**
37
     * @var string
38
     */
39
    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...
40
41
    /**
42
     * @var string
43
     */
44
    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...
45
46
    /**
47
     * @var array
48
     */
49
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
50
        'Unit' => 'Enum("m,km","m")',
51
    ];
52
53
    /**
54
     * @var array
55
     */
56
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
57
        'Categories' => LocationCategory::class,
58
    ];
59
60
    /**
61
     * @var string
62
     */
63
    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...
64
65
    /**
66
     * @var string
67
     */
68
    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...
69
70
    /**
71
     * @var string[]
72
     */
73
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
74
        Lumberjack::class,
75
    ];
76
77
    /**
78
     * @var string[]
79
     */
80
    private static $allowed_children = [
0 ignored issues
show
introduced by
The private property $allowed_children is not used, and could be removed.
Loading history...
81
        LocationPage::class,
82
    ];
83
84
    /**
85
     * @return FieldList
86
     */
87
    public function getCMSFields()
88
    {
89
        $this->beforeUpdateCMSFields(function ($fields) {
90
            // Settings
91
            $fields->addFieldsToTab('Root.Settings', [
92
                HeaderField::create('DisplayOptions', 'Display Options', 3),
93
                OptionsetField::create('Unit', 'Unit of measure', ['m' => 'Miles', 'km' => 'Kilometers']),
94
            ]);
95
96
            // Filter categories
97
            $config = GridFieldConfig_RelationEditor::create();
98
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
99
            $config->addComponent(new GridFieldAddExistingSearchButton());
100
            $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

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