Completed
Push — require/versionedDO2 ( a1f545 )
by Jason
05:34
created

LocatorForm::getValidator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 12
1
<?php
2
3
/**
4
 * Class LocatorForm
5
 */
6
class LocatorForm extends Form
7
{
8
    /**
9
     * @var bool
10
     */
11
    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...
12
13
    /**
14
     * @var array
15
     */
16
    private static $radius_array = [
0 ignored issues
show
Unused Code introduced by
The property $radius_array 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...
17
        '25' => '25',
18
        '50' => '50',
19
        '75' => '75',
20
        '100' => '100',
21
    ];
22
23
    /**
24
     * LocatorForm constructor.
25
     * @param Controller $controller
26
     * @param string $name
27
     */
28
    public function __construct(Controller $controller, $name)
29
    {
30
31
        $fields = FieldList::create(
32
            TextField::create('Address')
33
                ->setTitle('')
34
                ->setAttribute('placeholder', 'address or zip code')
35
        );
36
37
        $pageCategories = Locator::locator_categories_by_locator($controller->data()->ID);
38
        if ($pageCategories && $pageCategories->count() > 0) {
0 ignored issues
show
Bug introduced by
The method count cannot be called on $pageCategories (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
39
            $categories = false;
40
        } else {
41
            $categories = Locator::get_all_categories();
42
        }
43
44
        if ($categories) {
45
            $categoriesField = DropdownField::create('CategoryID')
46
                ->setTitle('')
47
                ->setEmptyString('all categories')
48
                ->setSource($categories->map());
49
            $fields->push($categoriesField);
50
        }
51
52
        if (Config::inst()->get('LocatorForm', 'show_radius')) {
53
            $radiusArray = Config::inst()->get('LocatorForm', 'radius_array');
54
            $this->extend('overrideRadiusArray', $radiusArray);
55
            $fields->push(DropdownField::create('Radius', '', $radiusArray)
56
                ->setEmptyString('radius')
57
            );
58
        }
59
60
        $actions = FieldList::create(
61
            FormAction::create('doFilterLocations')
62
                ->setTitle('Search')
63
        );
64
65
        $validator = $this->getValidator();
66
67
        parent::__construct($controller, $name, $fields, $actions, $validator);
68
    }
69
70
    /**
71
     * @return Validator
72
     */
73
    public function getValidator()
74
    {
75
        $validator = parent::getValidator();
76
        if (empty($validator)) {
77
            if (!$this->validator instanceof RequiredFields) $this->setValidator(RequiredFields::create('Address'));
78
            $validator = $this->validator;
79
        }
80
        $this->extend('updateRequiredFields', $validator);
81
        return $validator;
82
    }
83
84
}