Completed
Push — master ( 2357ce...f9bf40 )
by Robbie
43:41 queued 08:38
created

EditableLiteralField::getFormField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
crap 1
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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...
Unused Code introduced by
The property $singular_name 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...
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...
Unused Code introduced by
The property $plural_name 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...
16
17
    /**
18
     * Mark as literal only
19
     *
20
     * @config
21
     * @var bool
22
     */
23
    private static $literal = true;
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...
Unused Code introduced by
The property $literal 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...
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;
0 ignored issues
show
Unused Code introduced by
The property $editor_config 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...
32
33
    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...
Unused Code introduced by
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...
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...
Unused Code introduced by
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 4
    protected function sanitiseContent($content)
64
    {
65
        // Check if sanitisation is enabled
66 4
        if (!HtmlEditorField::config()->sanitise_server_side) {
67 4
            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 4
    public function getContent()
83
    {
84
        // Apply html editor sanitisation rules
85 4
        $content = $this->getField('Content');
86 4
        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 1
    public function getFormField()
128
    {
129
        $content = LiteralField::create(
130 1
            "LiteralFieldContent-{$this->ID}]",
131 1
            $this->dbObject('Content')->forTemplate()
132 1
        );
133 1
134 1
        $field = CompositeField::create($content)
135 1
            ->setName($this->Name)
136
            ->setID($this->Name)
137
            ->setFieldHolderTemplate('UserFormsLiteralField_holder');
138 1
139 1
        $this->doUpdateFormField($field);
140 1
141
        return $field;
142
    }
143
144 1
    protected function updateFormField($field)
145 1
    {
146 1
        parent::updateFormField($field);
147 1
148 1
        if ($this->HideLabel) {
0 ignored issues
show
Documentation introduced by
The property HideLabel does not exist on object<EditableLiteralField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
149 1
            $this->ExtraClass .= ' nolabel';
0 ignored issues
show
Documentation introduced by
The property ExtraClass does not exist on object<EditableLiteralField>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
150 1
        } else {
151
            $field->setTitle($this->Title);
152
        }
153 1
    }
154
155
    public function showInReports()
156
    {
157
        return ! $this->HideFromReports;
0 ignored issues
show
Documentation introduced by
The property HideFromReports does not exist on object<EditableLiteralField>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
158
    }
159
}
160