Passed
Pull Request — master (#173)
by Matthew
13:34
created

Locator::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 33
rs 8.8571
1
<?php
2
3
namespace Dynamic\Locator;
4
5
use \Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\Manifest\ModuleLoader;
8
use SilverStripe\Forms\DropdownField;
9
use SilverStripe\Forms\FieldList;
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';
34
35
    /**
36
     * @var string
37
     */
38
    private static $plural_name = 'Locators';
39
40
    /**
41
     * @var string
42
     */
43
    private static $description = 'Display locations on a map';
44
45
    /**
46
     * @var bool
47
     */
48
    private static $show_radius = true;
0 ignored issues
show
Unused Code introduced by
The property $show_radius 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...
49
50
    private static $limit = -1;
0 ignored issues
show
Unused Code introduced by
The property $limit 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...
51
52
    private static $locationClass = Location::class;
0 ignored issues
show
Unused Code introduced by
The property $locationClass 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...
53
54
    /**
55
     * @var array
56
     */
57
    private static $db = array(
58
        'Unit' => 'Enum("m,km","m")',
59
        'Clusters' => 'Enum("false,true","false")',
60
    );
61
62
    /**
63
     * @var array
64
     */
65
    private static $many_many = array(
66
        'Categories' => LocationCategory::class,
67
    );
68
69
    /**
70
     * @var string
71
     */
72
    private static $table_name = 'Locator';
0 ignored issues
show
Unused Code introduced by
The property $table_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...
73
74
    /**
75
     * @var string
76
     */
77
    private static $location_class = Location::class;
0 ignored issues
show
Unused Code introduced by
The property $location_class 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...
78
79
    /**
80
     * @return FieldList
81
     */
82
    public function getCMSFields()
83
    {
84
        // so it can easily be extended - concept taken from the blog module
85
        $this->beforeUpdateCMSFields(function ($fields) {
86
            // Settings
87
            $fields->addFieldsToTab('Root.Settings', array(
88
                HeaderField::create('DisplayOptions', 'Display Options', 3),
0 ignored issues
show
Bug introduced by
'DisplayOptions' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

88
                HeaderField::create(/** @scrutinizer ignore-type */ 'DisplayOptions', 'Display Options', 3),
Loading history...
Bug introduced by
3 of type integer is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

88
                HeaderField::create('DisplayOptions', 'Display Options', /** @scrutinizer ignore-type */ 3),
Loading history...
89
                OptionsetField::create('Unit', 'Unit of measure', array(
90
                    'm' => 'Miles',
91
                    'km' => 'Kilometers'
92
                ), 'm'),
93
                OptionsetField::create('Clusters', 'Use clusters?', array(
94
                    'false' => 'No',
95
                    'true' => 'Yes'
96
                ), 'false'),
97
            ));
98
99
            // Filter categories
100
            $config = GridFieldConfig_RelationEditor::create();
101
            $config->removeComponentsByType('GridFieldAddExistingAutocompleter');
102
            $config->addComponent(new GridFieldAddExistingSearchButton());
103
            $categories = $this->Categories();
104
            $categoriesField = GridField::create('Categories', 'Categories', $categories, $config)
105
                                        ->setDescription('only show locations from the selected category');
106
107
            // Filter
108
            $fields->addFieldsToTab('Root.Filter', array(
109
                HeaderField::create('CategoryOptionsHeader', 'Location Filtering', 3),
110
                $categoriesField,
111
            ));
112
        });
113
114
        return parent::getCMSFields();
115
    }
116
117
    /**
118
     * Gets the list of radius
119
     *
120
     * @return ArrayList
121
     */
122
    public function getRadii()
123
    {
124
        $radii = [
125
            '0' => '25',
126
            '1' => '50',
127
            '2' => '75',
128
            '3' => '100',
129
        ];
130
131
        $config_radii = Config::inst()->get(Locator::class, 'radius_array');
132
        if ($config_radii) {
133
            $radii = $config_radii;
134
        }
135
136
        $list = [];
137
        foreach ($radii as $radius) {
138
            $list[] = new ArrayData(array(
139
                'Radius' => $radius,
140
            ));
141
        }
142
143
        return new ArrayList($list);
144
    }
145
146
    /**
147
     * Gets the limit of locations
148
     * @return mixed
149
     */
150
    public function getLimit()
151
    {
152
        return Config::inst()->get(Locator::class, 'limit');
153
    }
154
155
    /**
156
     * Gets if the radius drop down should be shown
157
     * @return mixed
158
     */
159
    public function getShowRadius()
160
    {
161
        return Config::inst()->get(Locator::class, 'show_radius');
162
    }
163
164
    /**
165
     * @return mixed
166
     */
167
    public function getCategories()
168
    {
169
        return $this->Categories()->filter(array(
170
            'Locations.ID:GreaterThan' => 0
171
        ));
172
    }
173
174
    /**
175
     * Gets the info window template
176
     * @return mixed
177
     */
178 View Code Duplication
    public function getInfoWindowTemplate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    {
180
        $contents = json_encode(
181
            file_get_contents(
182
                $this->parseModuleResourceReference(
183
                    Config::inst()->get(
184
                        Locator::class,
185
                        'infoWindowTemplate'
186
                    )
187
                )
188
            )
189
        );
190
191
        return str_replace('\n', '', $contents);
192
    }
193
194
    /**
195
     * Gets the template for locations in the list
196
     * @return string
197
     */
198 View Code Duplication
    public function getListTemplate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
    {
200
        $contents = json_encode(
201
            file_get_contents(
202
                $this->parseModuleResourceReference(
203
                    Config::inst()->get(
204
                        Locator::class,
205
                        'listTemplate'
206
                    )
207
                )
208
            )
209
        );
210
211
        return str_replace('\n', '', $contents);
212
    }
213
214
    /**
215
     * From SilverStripe/View/Requirements_Backend
216
     *
217
     * Modified to use the base folder
218
     */
219
    protected function parseModuleResourceReference($file)
220
    {
221
        // String of the form vendor/package:resource. Excludes "http://bla" as that's an absolute URL
222
        if (preg_match('#([^\/\/][^ /]*\/[^ /]*) *: *([^ ]*)#', $file, $matches)) {
223
            list(, $module, $resource) = $matches;
224
            $moduleObj = ModuleLoader::getModule($module);
225
            if (!$moduleObj) {
226
                throw new \InvalidArgumentException("Can't find module '$module'");
227
            }
228
229
            return Director::baseFolder() . '/' . $moduleObj->getRelativeResourcePath($resource);
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Core\Manife...tRelativeResourcePath() has been deprecated: 4.0...5.0 Use getResource($path)->getRelativePath() instead ( Ignorable by Annotation )

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

229
            return Director::baseFolder() . '/' . /** @scrutinizer ignore-deprecated */ $moduleObj->getRelativeResourcePath($resource);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
230
        }
231
232
        return $file;
233
    }
234
235
    /**
236
     * @param array $filter
237
     * @param array $filterAny
238
     * @param array $exclude
239
     * @param null|callable $callback
240
     *
241
     * @return DataList|ArrayList
242
     */
243
    public static function get_locations(
244
        $filter = [],
245
        $filterAny = [],
246
        $exclude = [],
247
        $callback = null
248
    ) {
249
        $locationClass = Config::inst()->get(Locator::class, 'location_class');
250
        $locations = $locationClass::get()->filter($filter)->exclude($exclude);
251
        if (!empty($filterAny)) {
252
            $locations = $locations->filterAny($filterAny);
253
        }
254
255
        if (!empty($exclude)) {
256
            $locations = $locations->exclude($exclude);
257
        }
258
259
        if ($callback !== null && is_callable($callback)) {
260
            $locations->filterByCallback($callback);
261
        }
262
263
        return $locations;
264
    }
265
}
266