Passed
Push — master ( 07507d...4e9ab4 )
by Matthew
02:33
created

LocatorForm::Actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Locator;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Dev\Debug;
7
use SilverStripe\Forms\Form;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\TextField;
10
use SilverStripe\Forms\DropdownField;
11
use SilverStripe\Forms\FormAction;
12
use SilverStripe\Forms\RequiredFields;
13
use SilverStripe\Control\Controller;
14
use SilverStripe\ORM\ArrayLib;
15
16
/**
17
 * Class LocatorForm
18
 */
19
class LocatorForm extends Form
20
{
21
22
    /**
23
     * LocatorForm constructor.
24
     * @param Controller $controller
25
     * @param string $name
26
     */
27
    public function __construct(Controller $controller, $name)
28
    {
29
30
        $fields = FieldList::create(
31
            TextField::create('Address')
32
                ->setTitle('')
33
                ->setAttribute('placeholder', 'address or zip code')
34
        );
35
36
        $pageCategories = Locator::locator_categories_by_locator($controller->data()->ID);
37
        if ($pageCategories && $pageCategories->count() > 0) {
38
            $categories = false;
39
        } else {
40
            $categories = Locator::get_all_categories();
41
            if ($categories->count() < 1) {
42
                $categories = false;
43
            }
44
        }
45
46
        if ($categories) {
47
            $categoriesField = DropdownField::create('CategoryID')
48
                ->setTitle('')
49
                ->setEmptyString('all categories')
50
                ->setSource($categories->map());
51
            $fields->push($categoriesField);
52
        }
53
54
        if ($controller->getShowRadius()) {
55
            $radiusArray = array_values($controller->getRadii());
56
            $this->extend('overrideRadiusArray', $radiusArray);
57
            $fields->push(DropdownField::create('Radius', '', ArrayLib::valuekey($radiusArray))
58
                ->setEmptyString('radius'));
59
        }
60
61
        $actions = FieldList::create(
62
            FormAction::create('doFilterLocations')
63
                ->setTitle('Search')
64
        );
65
66
        $validator = $this->getValidator();
67
68
        parent::__construct($controller, $name, $fields, $actions, $validator);
69
    }
70
71
    /**
72
     * @return null|RequiredFields|\SilverStripe\Forms\Validator
73
     */
74
    public function getValidator()
75
    {
76
        $validator = parent::getValidator();
77
        if (empty($validator)) {
78
            if (!$this->validator instanceof RequiredFields) {
79
                $this->setValidator(RequiredFields::create('Address'));
80
            }
81
            $validator = $this->validator;
82
        }
83
        $this->extend('updateRequiredFields', $validator);
84
        return $validator;
85
    }
86
87
    /**
88
     * @return FieldList
89
     */
90
    public function Fields()
91
    {
92
        $fields = parent::Fields();
93
        $this->extend('updateLocatorFormFields', $fields);
94
        return $fields;
95
    }
96
97
    /**
98
     * @return \SilverStripe\Forms\FieldList
99
     */
100
    public function Actions()
101
    {
102
        $actions = parent::Actions();
103
        $this->extend('updateLocatorActions', $actions);
104
        return $actions;
105
    }
106
}
107