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'; |
|
|
|
|
23
|
|
|
|
24
|
|
|
private static $plural_name = 'HTML Blocks'; |
|
|
|
|
25
|
|
|
|
26
|
|
|
private static $table_name = 'EditableLiteralField'; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Mark as literal only |
30
|
|
|
* |
31
|
|
|
* @config |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private static $literal = true; |
|
|
|
|
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; |
|
|
|
|
43
|
|
|
|
44
|
|
|
private static $db = [ |
|
|
|
|
45
|
|
|
'Content' => 'HTMLText', // From CustomSettings |
46
|
|
|
'HideFromReports' => 'Boolean(0)', // from CustomSettings |
47
|
|
|
'HideLabel' => 'Boolean(0)' |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
private static $defaults = [ |
|
|
|
|
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 |
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; |
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 |
|
|
|
|
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) { |
|
|
|
|
160
|
|
|
$this->ExtraClass .= ' nolabel'; |
|
|
|
|
161
|
|
|
} else { |
162
|
|
|
$field->setTitle($this->Title); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function showInReports() |
167
|
|
|
{ |
168
|
|
|
return !$this->HideFromReports; |
|
|
|
|
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|