EditableCountryDropdownField   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSelectorField() 0 3 1
A getIcon() 0 9 2
A getValueFromData() 0 5 2
A getCMSFields() 0 24 1
A getFormField() 0 20 5
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Core\Manifest\ModuleLoader;
6
use SilverStripe\Forms\CheckboxField;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\TextField;
9
use SilverStripe\i18n\i18n;
10
use SilverStripe\UserForms\Model\EditableCustomRule;
11
use SilverStripe\UserForms\Model\EditableFormField;
12
13
/**
14
 * A dropdown field which allows the user to select a country
15
 *
16
 * @property bool $UseEmptyString
17
 * @property string $EmptyString
18
 *
19
 * @package userforms
20
 */
21
class EditableCountryDropdownField extends EditableFormField
22
{
23
    private static $singular_name = 'Country Dropdown';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
24
25
    private static $plural_name = 'Country Dropdowns';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
26
27
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
28
        'UseEmptyString' => 'Boolean',
29
        'EmptyString' => 'Varchar(255)',
30
    );
31
32
    private static $table_name = 'EditableCountryDropdownField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @return \SilverStripe\Forms\FieldList
36
     */
37
    public function getCMSFields()
38
    {
39
        $fields = parent::getCMSFields();
40
41
        $fields->removeByName('Default');
42
        $fields->addFieldToTab(
43
            'Root.Main',
44
            DropdownField::create('Default', _t(__CLASS__ . '.DEFAULT', 'Default value'))
45
                         ->setSource(i18n::getData()->getCountries())
46
                         ->setHasEmptyDefault(true)
47
                         ->setEmptyString('---')
48
        );
49
50
        $fields->addFieldToTab(
51
            'Root.Main',
52
            CheckboxField::create('UseEmptyString', _t(__CLASS__ . '.USE_EMPTY_STRING', 'Set default empty string'))
53
        );
54
55
        $fields->addFieldToTab(
56
            'Root.Main',
57
            TextField::create('EmptyString', _t(__CLASS__ . '.EMPTY_STRING', 'Empty String'))
58
        );
59
60
        return $fields;
61
    }
62
63
    public function getFormField()
64
    {
65
        $field = DropdownField::create($this->Name, $this->Title ?: false)
66
            ->setSource(i18n::getData()->getCountries())
67
            ->setFieldHolderTemplate(EditableFormField::class . '_holder')
68
            ->setTemplate(EditableDropdown::class);
69
70
        // Empty string
71
        if ($this->UseEmptyString) {
72
            $field->setEmptyString($this->EmptyString ?: '');
73
        }
74
75
        // Set default
76
        if ($this->Default) {
77
            $field->setValue($this->Default);
78
        }
79
80
        $this->doUpdateFormField($field);
81
82
        return $field;
83
    }
84
85
    public function getValueFromData($data)
86
    {
87
        if (!empty($data[$this->Name])) {
88
            $source = $this->getFormField()->getSource();
89
            return $source[$data[$this->Name]];
90
        }
91
    }
92
93
    public function getIcon()
94
    {
95
        $resource = ModuleLoader::getModule('silverstripe/userforms')->getResource('images/editabledropdown.png');
96
97
        if (!$resource->exists()) {
98
            return '';
99
        }
100
101
        return $resource->getURL();
102
    }
103
104
    public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
105
    {
106
        return "$(\"select[name='{$this->Name}']\")";
107
    }
108
}
109