Passed
Push — master ( affe64...6c9189 )
by Jason
02:51
created

Locator::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 25
rs 9.7998
c 1
b 0
f 0
1
<?php
2
3
namespace Dynamic\Locator;
4
5
use Dynamic\SilverStripeGeocoder\AddressDataExtension;
6
use SilverStripe\Core\Manifest\ModuleResourceLoader;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
9
use SilverStripe\Forms\HeaderField;
10
use SilverStripe\Forms\OptionsetField;
11
use SilverStripe\Forms\GridField\GridField;
12
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
13
use SilverStripe\ORM\DataList;
14
use SilverStripe\ORM\ArrayList;
15
use SilverStripe\Core\Config\Config;
16
use SilverStripe\View\ArrayData;
17
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
18
19
/**
20
 * Class Locator
21
 *
22
 * @property bool $AutoGeocode
23
 * @property bool $ModalWindow
24
 * @property string $Unit
25
 * @method Categories|ManyManyList $Categories
26
 */
27
class Locator extends \Page
28
{
29
    /**
30
     * @var string
31
     */
32
    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...
33
34
    /**
35
     * @var string
36
     */
37
    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...
38
39
    /**
40
     * @var string
41
     */
42
    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...
43
44
    /**
45
     * @var array
46
     */
47
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
48
        'Unit' => 'Enum("m,km","m")',
49
    );
50
51
    /**
52
     * @var array
53
     */
54
    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...
55
        'Categories' => LocationCategory::class,
56
    );
57
58
    /**
59
     * @var string
60
     */
61
    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...
62
63
    /**
64
     * @var string
65
     */
66
    private static $location_class = Location::class;
0 ignored issues
show
introduced by
The private property $location_class is not used, and could be removed.
Loading history...
67
68
    /**
69
     * @return FieldList
70
     */
71
    public function getCMSFields()
72
    {
73
        $this->beforeUpdateCMSFields(function ($fields) {
74
            // Settings
75
            $fields->addFieldsToTab('Root.Settings', array(
76
                HeaderField::create('DisplayOptions', 'Display Options', 3),
77
                OptionsetField::create('Unit', 'Unit of measure', array('m' => 'Miles', 'km' => 'Kilometers')),
78
            ));
79
80
            // Filter categories
81
            $config = GridFieldConfig_RelationEditor::create();
82
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
83
            $config->addComponent(new GridFieldAddExistingSearchButton());
84
            $categories = $this->Categories();
0 ignored issues
show
Bug introduced by
The method Categories() does not exist on Dynamic\Locator\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

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