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

EditableLiteralField   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 9

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 11
lcom 3
cbo 9
dl 0
loc 150
ccs 34
cts 51
cp 0.6667
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getEditorConfig() 0 8 2
A sanitiseContent() 0 13 2
A getContent() 0 6 1
A setContent() 0 6 1
A getFormField() 0 16 1
A showInReports() 0 4 1
A getCMSFields() 0 22 1
A updateFormField() 0 10 2
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
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...
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
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...
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;
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 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) {
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
        } else {
151 2
            $field->setTitle($this->Title);
152
        }
153 2
    }
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