2009-03-25 04:37:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2009-04-17 04:26:40 +02:00
|
|
|
* Editable Literal Field. A literal field is just a blank slate where
|
|
|
|
* you can add your own HTML / Images / Flash
|
2009-03-25 04:37:49 +01:00
|
|
|
*
|
2009-04-17 04:26:40 +02:00
|
|
|
* @package userforms
|
2009-03-25 04:37:49 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
class EditableLiteralField extends EditableFormField {
|
|
|
|
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $singular_name = 'HTML Block';
|
2009-03-25 04:37:49 +01:00
|
|
|
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $plural_name = 'HTML Blocks';
|
2015-02-10 03:32:44 +01:00
|
|
|
|
2015-08-13 01:31:37 +02:00
|
|
|
/**
|
|
|
|
* Mark as literal only
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $literal = true;
|
|
|
|
|
2015-02-10 03:32:44 +01:00
|
|
|
/**
|
|
|
|
* Get the name of the editor config to use for HTML sanitisation. Defaults to the active config.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $editor_config = null;
|
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
private static $db = array(
|
|
|
|
'Content' => 'HTMLText', // From CustomSettings
|
|
|
|
'HideFromReports' => 'Boolean(0)' // from CustomSettings
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $defaults = array(
|
|
|
|
'HideFromReports' => false
|
|
|
|
);
|
|
|
|
|
2015-02-10 03:32:44 +01:00
|
|
|
/**
|
|
|
|
* Returns the {@see HtmlEditorConfig} instance to use for sanitisation
|
|
|
|
*
|
|
|
|
* @return HtmlEditorConfig
|
|
|
|
*/
|
|
|
|
protected function getEditorConfig() {
|
|
|
|
$editorConfig = $this->config()->editor_config;
|
|
|
|
if($editorConfig) return HtmlEditorConfig::get($editorConfig);
|
|
|
|
return HtmlEditorConfig::get_active();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safely sanitise html content, if enabled
|
|
|
|
*
|
|
|
|
* @param string $content Raw html
|
|
|
|
* @return string Safely sanitised html
|
|
|
|
*/
|
|
|
|
protected function sanitiseContent($content) {
|
|
|
|
// Check if sanitisation is enabled
|
|
|
|
if(!HtmlEditorField::config()->sanitise_server_side) return $content;
|
|
|
|
|
|
|
|
// Perform sanitisation
|
|
|
|
$htmlValue = Injector::inst()->create('HTMLValue', $content);
|
|
|
|
$santiser = Injector::inst()->create('HtmlEditorSanitiser', $this->getEditorConfig());
|
|
|
|
$santiser->sanitise($htmlValue);
|
|
|
|
return $htmlValue->getContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get HTML Content of this literal field
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getContent() {
|
|
|
|
// Apply html editor sanitisation rules
|
2015-07-24 04:37:48 +02:00
|
|
|
$content = $this->getField('Content');
|
2015-02-10 03:32:44 +01:00
|
|
|
return $this->sanitiseContent($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the content with the given value
|
|
|
|
*
|
|
|
|
* @param string $content
|
|
|
|
*/
|
|
|
|
public function setContent($content) {
|
|
|
|
// Apply html editor sanitisation rules
|
|
|
|
$content = $this->sanitiseContent($content);
|
2015-07-24 04:37:48 +02:00
|
|
|
$this->setField('Content', $content);
|
2015-02-10 03:32:44 +01:00
|
|
|
}
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields() {
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
|
2015-08-21 05:21:33 +02:00
|
|
|
$fields->removeByName(array('Default', 'Validation', 'RightTitle'));
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
$fields->addFieldsToTab('Root.Main', array(
|
|
|
|
HTMLEditorField::create('Content', _t('EditableLiteralField.CONTENT', 'HTML'))
|
|
|
|
->setRows(4)
|
|
|
|
->setColumns(20),
|
|
|
|
CheckboxField::create(
|
|
|
|
'HideFromReports',
|
|
|
|
_t('EditableLiteralField.HIDEFROMREPORT', 'Hide from reports?')
|
2011-12-05 09:38:10 +01:00
|
|
|
)
|
2015-07-24 04:37:48 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
return $fields;
|
2009-03-25 04:37:49 +01:00
|
|
|
}
|
2009-04-27 06:01:06 +02:00
|
|
|
|
2012-05-04 03:39:08 +02:00
|
|
|
public function getFormField() {
|
2015-08-10 07:03:36 +02:00
|
|
|
// Build label and css classes
|
|
|
|
$label = '';
|
|
|
|
$classes = $this->ExtraClass;
|
|
|
|
if(empty($this->Title)) {
|
|
|
|
$classes .= " nolabel";
|
|
|
|
} else {
|
|
|
|
$label = "<label class='left'>{$this->EscapedTitle}</label>";
|
|
|
|
}
|
2013-04-03 04:35:02 +02:00
|
|
|
|
2015-08-10 07:03:36 +02:00
|
|
|
$field = new LiteralField(
|
2015-07-24 04:37:48 +02:00
|
|
|
"LiteralField[{$this->ID}]",
|
|
|
|
sprintf(
|
|
|
|
"<div id='%s' class='field text%s'>
|
|
|
|
%s
|
|
|
|
<div class='middleColumn literalFieldArea'>%s</div>".
|
|
|
|
"</div>",
|
|
|
|
Convert::raw2htmlname($this->Name),
|
|
|
|
Convert::raw2att($classes),
|
|
|
|
$label,
|
|
|
|
$this->Content
|
|
|
|
)
|
2011-12-05 09:38:10 +01:00
|
|
|
);
|
2015-08-10 07:03:36 +02:00
|
|
|
|
|
|
|
// When dealing with literal fields there is no further customisation that can be added at this point
|
|
|
|
return $field;
|
2011-12-05 09:38:10 +01:00
|
|
|
}
|
|
|
|
|
2012-05-04 03:39:08 +02:00
|
|
|
public function showInReports() {
|
2015-07-24 04:37:48 +02:00
|
|
|
return ! $this->HideFromReports;
|
2009-03-25 04:37:49 +01:00
|
|
|
}
|
2012-07-17 05:58:24 +02:00
|
|
|
}
|