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'; |
|
|
|
|
14
|
|
|
|
15
|
|
|
private static $plural_name = 'HTML Blocks'; |
|
|
|
|
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( |
|
|
|
|
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) { |
|
|
|
|
149
|
1 |
|
$this->ExtraClass .= ' nolabel'; |
|
|
|
|
150
|
1 |
|
} else { |
151
|
|
|
$field->setTitle($this->Title); |
152
|
|
|
} |
153
|
1 |
|
} |
154
|
|
|
|
155
|
|
|
public function showInReports() |
156
|
|
|
{ |
157
|
|
|
return ! $this->HideFromReports; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.