Passed
Push — master ( 9ca53d...fa249d )
by Robbie
02:18
created

EditableFormHeading::getSelectorOnly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
19
20
    private static $plural_name = 'Headings';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
21
22
    private static $literal = true;
0 ignored issues
show
introduced by
The private property $literal is not used, and could be removed.
Loading history...
23
24
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
25
        'Level' => 'Int(3)', // From CustomSettings
26
        'HideFromReports' => 'Boolean(0)' // from CustomSettings
27
    ];
28
29
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
30
        'Level' => 3,
31
        'HideFromReports' => false
32
    ];
33
34
    private static $table_name = 'EditableFormHeading';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @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...
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',
0 ignored issues
show
Bug introduced by
'Level' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
                /** @scrutinizer ignore-type */ 'Level',
Loading history...
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;
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...
67
    }
68
69
    public function getFormField()
70
    {
71
        $labelField = HeaderField::create('userforms-header', $this->EscapedTitle)
0 ignored issues
show
Bug introduced by
'userforms-header' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $labelField = HeaderField::create(/** @scrutinizer ignore-type */ 'userforms-header', $this->EscapedTitle)
Loading history...
Bug Best Practice introduced by
The property EscapedTitle does not exist on SilverStripe\UserForms\M...eld\EditableFormHeading. Since you implemented __get, consider adding a @property annotation.
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The property RightTitle does not exist on SilverStripe\UserForms\M...eld\EditableFormHeading. Since you implemented __get, consider adding a @property annotation.
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The property ExtraClass does not exist on SilverStripe\UserForms\M...eld\EditableFormHeading. Since you implemented __get, consider adding a @property annotation.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property HideFromReports does not exist on SilverStripe\UserForms\M...eld\EditableFormHeading. Since you implemented __get, consider adding a @property annotation.
Loading history...
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