EditableLiteralField::setContent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
7
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
8
use SilverStripe\Forms\HTMLEditor\HTMLEditorSanitiser;
9
use SilverStripe\Forms\CheckboxField;
10
use SilverStripe\Forms\CompositeField;
11
use SilverStripe\Forms\LiteralField;
12
use SilverStripe\UserForms\Model\EditableFormField;
13
14
/**
15
 * Editable Literal Field. A literal field is just a blank slate where
16
 * you can add your own HTML / Images / Flash
17
 *
18
 * @package userforms
19
 */
20
class EditableLiteralField extends EditableFormField
21
{
22
    private static $singular_name = 'HTML Block';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
23
24
    private static $plural_name = 'HTML Blocks';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
25
26
    private static $table_name = 'EditableLiteralField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
27
28
    /**
29
     * Mark as literal only
30
     *
31
     * @config
32
     * @var bool
33
     */
34
    private static $literal = true;
0 ignored issues
show
introduced by
The private property $literal is not used, and could be removed.
Loading history...
35
36
    /**
37
     * Get the name of the editor config to use for HTML sanitisation. Defaults to the active config.
38
     *
39
     * @var string
40
     * @config
41
     */
42
    private static $editor_config = null;
0 ignored issues
show
introduced by
The private property $editor_config is not used, and could be removed.
Loading history...
43
44
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
45
        'Content' => 'HTMLText', // From CustomSettings
46
        'HideFromReports' => 'Boolean(0)', // from CustomSettings
47
        'HideLabel' => 'Boolean(0)'
48
    ];
49
50
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
51
        'HideFromReports' => false
52
    ];
53
54
    /**
55
     * Returns the {@see HTMLEditorConfig} instance to use for sanitisation
56
     *
57
     * @return HTMLEditorConfig
58
     */
59
    protected function getEditorConfig()
60
    {
61
        $editorConfig = $this->config()->get('editor_config');
62
        if ($editorConfig) {
63
            return HTMLEditorConfig::get($editorConfig);
64
        }
65
        return HTMLEditorConfig::get_active();
66
    }
67
68
    /**
69
     * Safely sanitise html content, if enabled
70
     *
71
     * @param string $content Raw html
72
     * @return string Safely sanitised html
73
     */
74
    protected function sanitiseContent($content)
75
    {
76
        // Check if sanitisation is enabled
77
        if (!HTMLEditorField::config()->get('sanitise_server_side')) {
78
            return $content;
79
        }
80
81
        // Perform sanitisation
82
        $htmlValue = Injector::inst()->create('HTMLValue', $content);
83
        $santiser = Injector::inst()->create(HTMLEditorSanitiser::class, $this->getEditorConfig());
84
        $santiser->sanitise($htmlValue);
85
        return $htmlValue->getContent();
86
    }
87
88
    /**
89
     * Get HTML Content of this literal field
90
     *
91
     * @return string
92
     */
93
    public function getContent()
94
    {
95
        // Apply html editor sanitisation rules
96
        $content = $this->getField('Content');
97
        return $this->sanitiseContent($content);
98
    }
99
100
    /**
101
     * Set the content with the given value
102
     *
103
     * @param string $content
104
     */
105
    public function setContent($content)
106
    {
107
        // Apply html editor sanitisation rules
108
        $content = $this->sanitiseContent($content);
109
        $this->setField('Content', $content);
110
    }
111
112
    /**
113
     * @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...
114
     */
115
    public function getCMSFields()
116
    {
117
        $fields = parent::getCMSFields();
118
119
        $fields->removeByName(['Default', 'Validation', 'RightTitle']);
120
121
        $fields->addFieldsToTab('Root.Main', [
122
            HTMLEditorField::create('Content', _t(__CLASS__.'.CONTENT', 'HTML'))
123
                ->setRows(4)
124
                ->setColumns(20),
125
            CheckboxField::create(
126
                'HideFromReports',
127
                _t(__CLASS__.'.HIDEFROMREPORT', 'Hide from reports?')
128
            ),
129
            CheckboxField::create(
130
                'HideLabel',
131
                _t(__CLASS__.'.HIDELABEL', "Hide 'Title' label on frontend?")
132
            )
133
        ]);
134
135
        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...
136
    }
137
138
    public function getFormField()
139
    {
140
        $content = LiteralField::create(
141
            "LiteralFieldContent-{$this->ID}]",
142
            $this->dbObject('Content')->forTemplate()
143
        );
144
145
        $field = CompositeField::create($content)
146
            ->setName($this->Name)
147
            // ->setID($this->Name) // @todo: https://github.com/silverstripe/silverstripe-framework/issues/7264
148
            ->setFieldHolderTemplate(__CLASS__ . '_holder');
149
150
        $this->doUpdateFormField($field);
151
152
        return $field;
153
    }
154
155
    protected function updateFormField($field)
156
    {
157
        parent::updateFormField($field);
158
159
        if ($this->HideLabel) {
0 ignored issues
show
Bug Best Practice introduced by
The property HideLabel does not exist on SilverStripe\UserForms\M...ld\EditableLiteralField. Since you implemented __get, consider adding a @property annotation.
Loading history...
160
            $this->ExtraClass .= ' nolabel';
0 ignored issues
show
Bug Best Practice introduced by
The property ExtraClass does not exist on SilverStripe\UserForms\M...ld\EditableLiteralField. Since you implemented __get, consider adding a @property annotation.
Loading history...
161
        } else {
162
            $field->setTitle($this->Title);
163
        }
164
    }
165
166
    public function showInReports()
167
    {
168
        return !$this->HideFromReports;
0 ignored issues
show
Bug Best Practice introduced by
The property HideFromReports does not exist on SilverStripe\UserForms\M...ld\EditableLiteralField. Since you implemented __get, consider adding a @property annotation.
Loading history...
169
    }
170
}
171