Completed
Push — master ( 12981b...2f1b03 )
by Robbie
14s
created

model/editableformfields/EditableTextField.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * EditableTextField
4
 *
5
 * This control represents a user-defined text field in a user defined form
6
 *
7
 * @package userforms
8
 */
9
10
class EditableTextField extends EditableFormField
11
{
12
13
    private static $singular_name = 'Text Field';
14
15
    private static $plural_name = 'Text Fields';
16
17
    private static $has_placeholder = true;
18
19
    private static $autocomplete_options = array(
20
        'off' => 'Off',
21
        'on' => 'On',
22
        'name' => 'Full name',
23
        'honorific-prefix' => 'Prefix or title',
24
        'given-name' => 'First name',
25
        'additional-name' => 'Additional name',
26
        'family-name' => 'Family name',
27
        'honorific-suffix' => 'Suffix (e.g Jr.)',
28
        'nickname' => 'Nickname',
29
        'email' => 'Email',
30
        'organization-title' => 'Job title',
31
        'organization' => 'Organization',
32
        'street-address' => 'Street address',
33
        'address-line1' => 'Address line 1',
34
        'address-line2' => 'Address line 2',
35
        'address-line3' => 'Address line 3',
36
        'address-level1' => 'Address level 1',
37
        'address-level2' => 'Address level 2',
38
        'address-level3' => 'Address level 3',
39
        'address-level4' => 'Address level 4',
40
        'country' => 'Country',
41
        'country-name' => 'Country name',
42
        'postal-code' => 'Postal code',
43
        'bday' => 'Birthday',
44
        'sex' => 'Gender identity',
45
        'tel' => 'Telephone number',
46
        'url' => 'Home page'
47
      );
48
49
    protected $jsEventHandler = 'keyup';
50
51
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $db 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...
52
        'MinLength' => 'Int',
53
        'MaxLength' => 'Int',
54
        'Rows' => 'Int(1)',
55
        'Autocomplete' => 'Varchar(255)'
56
    );
57
58
    private static $defaults = array(
59
        'Rows' => 1
60
    );
61
62
    public function getCMSFields()
63
    {
64
        $this->beforeUpdateCMSFields(function ($fields) {
65
            $fields->addFieldToTab(
66
                'Root.Main',
67
                NumericField::create(
68
                    'Rows',
69
                    _t('EditableTextField.NUMBERROWS', 'Number of rows')
70
                )->setDescription(_t(
71
                    'EditableTextField.NUMBERROWS_DESCRIPTION',
72
                    'Fields with more than one row will be generated as a textarea'
73
                ))
74
            );
75
76
            $fields->addFieldToTab(
77
                'Root.Main',
78
                DropdownField::create(
79
                    'Autocomplete',
80
                    _t('EditableTextField.AUTOCOMPLETE', 'Autocomplete'),
81
                    $this->config()->get('autocomplete_options')
82
                  )->setDescription(_t(
83
                      'EditableTextField.AUTOCOMPLETE_DESCRIPTION',
84
                      'Supported browsers will attempt to populate this field automatically with the users information, use to set the value populated'
85
                  ))
86
            );
87
88
        });
89
90
        return parent::getCMSFields();
91
    }
92
93
    /**
94
     * @return FieldList
95
     */
96
    public function getFieldValidationOptions()
97
    {
98
        $fields = parent::getFieldValidationOptions();
99
100
        $fields->merge(array(
101
            FieldGroup::create(
102
                _t('EditableTextField.TEXTLENGTH', 'Allowed text length'),
103
                array(
104
                    NumericField::create('MinLength', false),
105
                    LiteralField::create('RangeLength', _t("EditableTextField.RANGE_TO", "to")),
106
                    NumericField::create('MaxLength', false)
107
                )
108
            )
109
        ));
110
111
        return $fields;
112
    }
113
114
    /**
115
     * @return TextareaField|TextField
116
     */
117 10
    public function getFormField()
118
    {
119 10
        if ($this->Rows > 1) {
120 1
            $field = TextareaField::create($this->Name, $this->EscapedTitle, $this->Default)
121 1
                ->setFieldHolderTemplate('UserFormsField_holder')
122 1
                ->setTemplate('UserFormsTextareaField')
123 1
                ->setRows($this->Rows);
124
        } else {
125 10
            $field = TextField::create($this->Name, $this->EscapedTitle, $this->Default)
126 10
                ->setFieldHolderTemplate('UserFormsField_holder')
127 10
                ->setTemplate('UserFormsField');
128
        }
129
130 10
        $this->doUpdateFormField($field);
131
132 10
        return $field;
133
    }
134
135
    /**
136
     * Updates a formfield with the additional metadata specified by this field
137
     *
138
     * @param FormField $field
139
     */
140 10
    protected function updateFormField($field)
141
    {
142 10
        parent::updateFormField($field);
143
144 10
        if (is_numeric($this->MinLength) && $this->MinLength > 0) {
145 1
            $field->setAttribute('data-rule-minlength', intval($this->MinLength));
146
        }
147
148 10
        if (is_numeric($this->MaxLength) && $this->MaxLength > 0) {
149 1
            if ($field instanceof TextField) {
150 1
                $field->setMaxLength(intval($this->MaxLength));
151
            }
152 1
            $field->setAttribute('data-rule-maxlength', intval($this->MaxLength));
153
        }
154
155 10
        if ($this->Autocomplete) {
156
            $field->setAttribute('autocomplete', $this->Autocomplete);
157
        }
158
159 10
    }
160
}
161