EditableDropdown   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSelectorField() 0 3 1
A getCMSFields() 0 19 1
A getFormField() 0 17 5
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\UserForms\Model\EditableCustomRule;
9
use SilverStripe\UserForms\Model\EditableFormField;
10
11
/**
12
 * EditableDropdown
13
 *
14
 * Represents a modifiable dropdown (select) box on a form
15
 *
16
 * @property bool $UseEmptyString
17
 * @property string $EmptyString
18
 *
19
 * @package userforms
20
 */
21
class EditableDropdown extends EditableMultipleOptionField
22
{
23
    private static $singular_name = 'Dropdown Field';
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 = '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 = 'EditableDropdown';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @return FieldList
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...ableFormField\FieldList 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...
36
     */
37
    public function getCMSFields()
38
    {
39
        $fields = parent::getCMSFields();
40
41
        $fields->addFieldToTab(
42
            'Root.Main',
43
            CheckboxField::create('UseEmptyString')
44
                ->setTitle('Set default empty string')
45
        );
46
47
        $fields->addFieldToTab(
48
            'Root.Main',
49
            TextField::create('EmptyString')
50
                ->setTitle('Empty String')
51
        );
52
53
        $fields->removeByName('Default');
54
55
        return $fields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fields returns the type SilverStripe\Forms\FieldList which is incompatible with the documented return type SilverStripe\UserForms\M...ableFormField\FieldList.
Loading history...
56
    }
57
58
    /**
59
     * @return DropdownField
60
     */
61
    public function getFormField()
62
    {
63
        $field = DropdownField::create($this->Name, $this->Title ?: false, $this->getOptionsMap())
64
            ->setFieldHolderTemplate(EditableFormField::class . '_holder')
65
            ->setTemplate(__CLASS__);
66
67
        if ($this->UseEmptyString) {
68
            $field->setEmptyString(($this->EmptyString) ? $this->EmptyString : '');
69
        }
70
71
        // Set default
72
        $defaultOption = $this->getDefaultOptions()->first();
73
        if ($defaultOption) {
74
            $field->setValue($defaultOption->Value);
75
        }
76
        $this->doUpdateFormField($field);
77
        return $field;
78
    }
79
80
    public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
81
    {
82
        return "$(\"select[name='{$this->Name}']\")";
83
    }
84
}
85