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

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