EditableTextField::getCMSFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 26
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Forms\DropdownField;
6
use SilverStripe\Forms\FieldGroup;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\FormField;
9
use SilverStripe\Forms\LiteralField;
10
use SilverStripe\Forms\NumericField;
11
use SilverStripe\Forms\TextareaField;
12
use SilverStripe\Forms\TextField;
13
use SilverStripe\ORM\ValidationResult;
14
use SilverStripe\UserForms\Model\EditableFormField;
15
16
/**
17
 * EditableTextField
18
 *
19
 * This control represents a user-defined text field in a user defined form
20
 *
21
 * @package userforms
22
 */
23
24
class EditableTextField extends EditableFormField
25
{
26
    private static $singular_name = 'Text Field';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
27
28
    private static $plural_name = 'Text Fields';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
29
30
    private static $has_placeholder = true;
0 ignored issues
show
introduced by
The private property $has_placeholder is not used, and could be removed.
Loading history...
31
32
    /** @skipUpgrade */
33
    private static $autocomplete_options = [
0 ignored issues
show
introduced by
The private property $autocomplete_options is not used, and could be removed.
Loading history...
34
        'off' => 'Off',
35
        'on' => 'On',
36
        'name' => 'Full name',
37
        'honorific-prefix' => 'Prefix or title',
38
        'given-name' => 'First name',
39
        'additional-name' => 'Additional name',
40
        'family-name' => 'Family name',
41
        'honorific-suffix' => 'Suffix (e.g Jr.)',
42
        'nickname' => 'Nickname',
43
        'email' => 'Email',
44
        'organization-title' => 'Job title',
45
        'organization' => 'Organization',
46
        'street-address' => 'Street address',
47
        'address-line1' => 'Address line 1',
48
        'address-line2' => 'Address line 2',
49
        'address-line3' => 'Address line 3',
50
        'address-level1' => 'Address level 1',
51
        'address-level2' => 'Address level 2',
52
        'address-level3' => 'Address level 3',
53
        'address-level4' => 'Address level 4',
54
        'country' => 'Country',
55
        'country-name' => 'Country name',
56
        'postal-code' => 'Postal code',
57
        'bday' => 'Birthday',
58
        'sex' => 'Gender identity',
59
        'tel' => 'Telephone number',
60
        'url' => 'Home page'
61
    ];
62
63
    protected $jsEventHandler = 'keyup';
64
65
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
66
        'MinLength' => 'Int',
67
        'MaxLength' => 'Int',
68
        'Rows' => 'Int(1)',
69
        'Autocomplete' => 'Varchar(255)'
70
    ];
71
72
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
73
        'Rows' => 1
74
    ];
75
76
    private static $table_name = 'EditableTextField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
77
78
    public function getCMSFields()
79
    {
80
        $this->beforeUpdateCMSFields(function ($fields) {
81
            $fields->addFieldsToTab(
82
                'Root.Main',
83
                [
84
                    NumericField::create(
85
                        'Rows',
86
                        _t(__CLASS__.'.NUMBERROWS', 'Number of rows')
87
                    )->setDescription(_t(
88
                        __CLASS__.'.NUMBERROWS_DESCRIPTION',
89
                        'Fields with more than one row will be generated as a textarea'
90
                    )),
91
                    DropdownField::create(
92
                        'Autocomplete',
93
                        _t(__CLASS__.'.AUTOCOMPLETE', 'Autocomplete'),
94
                        $this->config()->get('autocomplete_options')
95
                    )->setDescription(_t(
96
                        __CLASS__.'.AUTOCOMPLETE_DESCRIPTION',
97
                        'Supported browsers will attempt to populate this field automatically with the users information, use to set the value populated'
98
                    ))
99
                ]
100
            );
101
        });
102
103
        return parent::getCMSFields();
104
    }
105
106
    /**
107
     * @return ValidationResult
108
     */
109
    public function validate()
110
    {
111
        $result = parent::validate();
112
113
        if ($this->MinLength > $this->MaxLength) {
0 ignored issues
show
Bug Best Practice introduced by
The property MaxLength does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property MinLength does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
114
            $result->addError(_t(
115
                __CLASS__ . 'MINMAXLENGTHCHECK',
116
                'Minimum length should be less than the Maximum length.'
117
            ));
118
        }
119
120
        return $result;
121
    }
122
123
    /**
124
     * @return FieldList
125
     */
126
    public function getFieldValidationOptions()
127
    {
128
        $fields = parent::getFieldValidationOptions();
129
130
        $fields->merge([
131
            FieldGroup::create(
132
                _t(__CLASS__.'.TEXTLENGTH', 'Allowed text length'),
133
                [
134
                    NumericField::create('MinLength', false)
135
                        ->setAttribute('aria-label', _t(__CLASS__ . '.MIN_LENGTH', 'Minimum text length')),
136
                    LiteralField::create(
137
                        'RangeLength',
138
                        '<span class="userform-field__allowed-length-separator">'
139
                        . _t(__CLASS__ . '.RANGE_TO', 'to')
140
                        . '</span>'
141
                    ),
142
                    NumericField::create('MaxLength', false)
143
                        ->setAttribute('aria-label', _t(__CLASS__ . '.MAX_LENGTH', 'Maximum text length')),
144
                ]
145
            )->addExtraClass('userform-field__allowed-length')
146
        ]);
147
148
        return $fields;
149
    }
150
151
    /**
152
     * @return TextareaField|TextField
153
     */
154
    public function getFormField()
155
    {
156
        if ($this->Rows > 1) {
0 ignored issues
show
Bug Best Practice introduced by
The property Rows does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
157
            $field = TextareaField::create($this->Name, $this->Title ?: false, $this->Default)
158
                ->setFieldHolderTemplate(EditableFormField::class . '_holder')
159
                ->setTemplate(str_replace('EditableTextField', 'EditableTextareaField', __CLASS__))
160
                ->setRows($this->Rows);
161
        } else {
162
            $field = TextField::create($this->Name, $this->Title ?: false, $this->Default)
163
                ->setFieldHolderTemplate(EditableFormField::class . '_holder')
164
                ->setTemplate(EditableFormField::class);
165
        }
166
167
        $this->doUpdateFormField($field);
168
169
        return $field;
170
    }
171
172
    /**
173
     * Updates a formfield with the additional metadata specified by this field
174
     *
175
     * @param FormField $field
176
     */
177
    protected function updateFormField($field)
178
    {
179
        parent::updateFormField($field);
180
181
        if (is_numeric($this->MinLength) && $this->MinLength > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The property MinLength does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
182
            $field->setAttribute('data-rule-minlength', (int) $this->MinLength);
183
        }
184
185
        if (is_numeric($this->MaxLength) && $this->MaxLength > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The property MaxLength does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
186
            if ($field instanceof TextField) {
187
                $field->setMaxLength((int) $this->MaxLength);
188
            }
189
            $field->setAttribute('data-rule-maxlength', (int) $this->MaxLength);
190
        }
191
192
        if ($this->Autocomplete) {
0 ignored issues
show
Bug Best Practice introduced by
The property Autocomplete does not exist on SilverStripe\UserForms\M...Field\EditableTextField. Since you implemented __get, consider adding a @property annotation.
Loading history...
193
            $field->setAttribute('autocomplete', $this->Autocomplete);
194
        }
195
    }
196
}
197