2009-03-25 04:37:49 +01:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2021-02-26 04:13:23 +01:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorSanitiser;
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\CompositeField;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2023-01-19 09:16:49 +01:00
|
|
|
use SilverStripe\View\Parsers\HTMLValue;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2009-03-25 04:37:49 +01:00
|
|
|
/**
|
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
|
2015-09-11 00:20:06 +02:00
|
|
|
*
|
2009-04-17 04:26:40 +02:00
|
|
|
* @package userforms
|
2021-02-26 04:13:23 +01:00
|
|
|
* @property string $Content
|
|
|
|
* @property int $HideFromReports
|
|
|
|
* @property int $HideLabel
|
2009-03-25 04:37:49 +01:00
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableLiteralField extends EditableFormField
|
|
|
|
{
|
|
|
|
private static $singular_name = 'HTML Block';
|
|
|
|
|
|
|
|
private static $plural_name = 'HTML Blocks';
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $table_name = 'EditableLiteralField';
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Mark as literal only
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $literal = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $db = [
|
2016-07-21 07:53:59 +02:00
|
|
|
'Content' => 'HTMLText', // From CustomSettings
|
|
|
|
'HideFromReports' => 'Boolean(0)', // from CustomSettings
|
|
|
|
'HideLabel' => 'Boolean(0)'
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $defaults = [
|
2016-07-21 07:53:59 +02:00
|
|
|
'HideFromReports' => false
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
/**
|
2017-08-09 01:55:09 +02:00
|
|
|
* Returns the {@see HTMLEditorConfig} instance to use for sanitisation
|
2016-07-21 07:53:59 +02:00
|
|
|
*
|
2017-08-09 01:55:09 +02:00
|
|
|
* @return HTMLEditorConfig
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
|
|
|
protected function getEditorConfig()
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
$editorConfig = $this->config()->get('editor_config');
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($editorConfig) {
|
2017-08-09 01:55:09 +02:00
|
|
|
return HTMLEditorConfig::get($editorConfig);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2017-08-09 01:55:09 +02:00
|
|
|
return HTMLEditorConfig::get_active();
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2017-08-11 01:39:58 +02:00
|
|
|
if (!HTMLEditorField::config()->get('sanitise_server_side')) {
|
2016-07-21 07:53:59 +02:00
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform sanitisation
|
2023-01-19 09:16:49 +01:00
|
|
|
$htmlValue = Injector::inst()->create(HTMLValue::class, $content);
|
2017-08-09 01:55:09 +02:00
|
|
|
$santiser = Injector::inst()->create(HTMLEditorSanitiser::class, $this->getEditorConfig());
|
2016-07-21 07:53:59 +02:00
|
|
|
$santiser->sanitise($htmlValue);
|
|
|
|
return $htmlValue->getContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get HTML Content of this literal field
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getContent()
|
|
|
|
{
|
|
|
|
// Apply html editor sanitisation rules
|
|
|
|
$content = $this->getField('Content');
|
|
|
|
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);
|
|
|
|
$this->setField('Content', $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2022-03-03 04:47:45 +01:00
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) {
|
|
|
|
$fields->removeByName(['Default', 'Validation', 'RightTitle']);
|
|
|
|
|
|
|
|
$fields->addFieldsToTab('Root.Main', [
|
2023-01-19 09:16:49 +01:00
|
|
|
HTMLEditorField::create('Content', _t(__CLASS__ . '.CONTENT', 'HTML'))
|
2022-03-03 04:47:45 +01:00
|
|
|
->setRows(4)
|
|
|
|
->setColumns(20),
|
|
|
|
CheckboxField::create(
|
|
|
|
'HideFromReports',
|
2023-01-19 09:16:49 +01:00
|
|
|
_t(__CLASS__ . '.HIDEFROMREPORT', 'Hide from reports?')
|
2022-03-03 04:47:45 +01:00
|
|
|
),
|
|
|
|
CheckboxField::create(
|
|
|
|
'HideLabel',
|
2023-01-19 09:16:49 +01:00
|
|
|
_t(__CLASS__ . '.HIDELABEL', "Hide 'Title' label on frontend?")
|
2022-03-03 04:47:45 +01:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFormField()
|
|
|
|
{
|
2017-05-18 00:31:18 +02:00
|
|
|
$content = LiteralField::create(
|
|
|
|
"LiteralFieldContent-{$this->ID}]",
|
|
|
|
$this->dbObject('Content')->forTemplate()
|
2016-07-21 07:53:59 +02:00
|
|
|
);
|
|
|
|
|
2017-05-18 00:31:18 +02:00
|
|
|
$field = CompositeField::create($content)
|
|
|
|
->setName($this->Name)
|
2017-09-04 23:45:16 +02:00
|
|
|
// ->setID($this->Name) // @todo: https://github.com/silverstripe/silverstripe-framework/issues/7264
|
2017-08-14 02:29:57 +02:00
|
|
|
->setFieldHolderTemplate(__CLASS__ . '_holder');
|
2017-05-18 00:31:18 +02:00
|
|
|
|
|
|
|
$this->doUpdateFormField($field);
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
2017-05-18 00:31:18 +02:00
|
|
|
protected function updateFormField($field)
|
|
|
|
{
|
|
|
|
parent::updateFormField($field);
|
|
|
|
|
|
|
|
if ($this->HideLabel) {
|
|
|
|
$this->ExtraClass .= ' nolabel';
|
|
|
|
} else {
|
|
|
|
$field->setTitle($this->Title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function showInReports()
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
return !$this->HideFromReports;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2012-07-17 05:58:24 +02:00
|
|
|
}
|