Passed
Push — master ( ebdfbb...20570e )
by
unknown
02:32
created

Model/EditableFormField/EditableFormHeading.php (1 issue)

1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Forms\CheckboxField;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\HeaderField;
9
use SilverStripe\UserForms\Model\EditableFormField;
10
11
/**
12
 * Allows an editor to insert a generic heading into a field
13
 *
14
 * @package userforms
15
 */
16
class EditableFormHeading extends EditableFormField
17
{
18
    private static $singular_name = 'Heading';
19
20
    private static $plural_name = 'Headings';
21
22
    private static $literal = true;
23
24
    private static $db = [
25
        'Level' => 'Int(3)', // From CustomSettings
26
        'HideFromReports' => 'Boolean(0)' // from CustomSettings
27
    ];
28
29
    private static $defaults = [
30
        'Level' => 3,
31
        'HideFromReports' => false
32
    ];
33
34
    private static $table_name = 'EditableFormHeading';
35
36
    /**
37
     * @return FieldList
38
     */
39
    public function getCMSFields()
40
    {
41
        $fields = parent::getCMSFields();
42
43
        $fields->removeByName(['Default', 'Validation', 'RightTitle']);
44
45
        $levels = [
46
            '1' => '1',
47
            '2' => '2',
48
            '3' => '3',
49
            '4' => '4',
50
            '5' => '5',
51
            '6' => '6'
52
        ];
53
54
        $fields->addFieldsToTab('Root.Main', [
55
            DropdownField::create(
56
                'Level',
57
                _t(__CLASS__.'.LEVEL', 'Select Heading Level'),
58
                $levels
59
            ),
60
            CheckboxField::create(
61
                'HideFromReports',
62
                _t('SilverStripe\\UserForms\\Model\\EditableFormField\\EditableLiteralField.HIDEFROMREPORT', 'Hide from reports?')
63
            )
64
        ]);
65
66
        return $fields;
67
    }
68
69
    public function getFormField()
70
    {
71
        $labelField = HeaderField::create('userforms-header', $this->EscapedTitle)
72
            ->setHeadingLevel($this->Level);
0 ignored issues
show
Bug Best Practice introduced by
The property Level does not exist on SilverStripe\UserForms\M...eld\EditableFormHeading. Since you implemented __get, consider adding a @property annotation.
Loading history...
73
        $labelField->addExtraClass('FormHeading');
74
        $labelField->setAttribute('data-id', $this->Name);
75
        $this->doUpdateFormField($labelField);
76
        return $labelField;
77
    }
78
79
    protected function updateFormField($field)
80
    {
81
        // set the right title on this field
82
        if ($this->RightTitle) {
83
            // Since this field expects raw html, safely escape the user data prior
84
            $field->setRightTitle(Convert::raw2xml($this->RightTitle));
85
        }
86
87
        // if this field has an extra class
88
        if ($this->ExtraClass) {
89
            $field->addExtraClass($this->ExtraClass);
90
        }
91
92
        if (!$this->ShowOnLoad) {
93
            $field->addExtraClass($this->ShowOnLoadNice());
94
        }
95
    }
96
97
    public function showInReports()
98
    {
99
        return !$this->HideFromReports;
100
    }
101
102
    public function getFieldValidationOptions()
103
    {
104
        return false;
105
    }
106
107
    public function getSelectorHolder()
108
    {
109
        return "$(\":header[data-id='{$this->Name}']\")";
110
    }
111
112
    public function getSelectorOnly()
113
    {
114
        return "[data-id={$this->Name}]";
115
    }
116
117
    public function getLevel()
118
    {
119
        return $this->getField('Level') ?: 3;
120
    }
121
}
122