2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM\FieldType;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
|
|
|
use ShortcodeParser;
|
2016-05-06 01:43:47 +02:00
|
|
|
use HTMLEditorField;
|
2015-08-30 07:02:55 +02:00
|
|
|
use TextField;
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Represents a short text field that is intended to contain HTML content.
|
|
|
|
*
|
|
|
|
* This behaves similarly to Varchar, but the template processor won't escape any HTML content within it.
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2016-06-15 06:03:16 +02:00
|
|
|
* @subpackage orm
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBHTMLVarchar extends DBVarchar {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $escape_type = 'xml';
|
2013-03-11 21:52:30 +01:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Enable shortcode parsing on this field
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $processShortcodes = false;
|
2013-03-11 21:52:30 +01:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Check if shortcodes are enabled
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getProcessShortcodes() {
|
|
|
|
return $this->processShortcodes;
|
|
|
|
}
|
2013-03-11 21:52:30 +01:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Set shortcodes on or off by default
|
|
|
|
*
|
|
|
|
* @param bool $process
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setProcessShortcodes($process) {
|
|
|
|
$this->processShortcodes = (bool)$process;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* Options accepted in addition to those provided by Text:
|
|
|
|
*
|
|
|
|
* - shortcodes: If true, shortcodes will be turned into the appropriate HTML.
|
|
|
|
* If false, shortcodes will not be processed.
|
|
|
|
*
|
|
|
|
* - whitelist: If provided, a comma-separated list of elements that will be allowed to be stored
|
|
|
|
* (be careful on relying on this for XSS protection - some seemingly-safe elements allow
|
|
|
|
* attributes that can be exploited, for instance <img onload="exploiting_code();" src="..." />)
|
|
|
|
* Text nodes outside of HTML tags are filtered out by default, but may be included by adding
|
|
|
|
* the text() directive. E.g. 'link,meta,text()' will allow only <link /> <meta /> and text at
|
|
|
|
* the root level.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setOptions(array $options = array()) {
|
2013-03-11 21:52:30 +01:00
|
|
|
if(array_key_exists("shortcodes", $options)) {
|
2016-06-03 10:51:02 +02:00
|
|
|
$this->setProcessShortcodes(!!$options["shortcodes"]);
|
2013-03-11 21:52:30 +01:00
|
|
|
}
|
2016-06-03 10:51:02 +02:00
|
|
|
|
|
|
|
return parent::setOptions($options);
|
2013-03-11 21:52:30 +01:00
|
|
|
}
|
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
public function forTemplate() {
|
2016-06-30 16:46:23 +02:00
|
|
|
return $this->RAW();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function RAW() {
|
2013-03-11 21:52:30 +01:00
|
|
|
if ($this->processShortcodes) {
|
|
|
|
return ShortcodeParser::get_active()->parse($this->value);
|
2016-06-03 10:51:02 +02:00
|
|
|
} else {
|
2013-03-11 21:52:30 +01:00
|
|
|
return $this->value;
|
|
|
|
}
|
2016-06-03 10:51:02 +02:00
|
|
|
}
|
2016-06-30 16:54:44 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Safely escape for XML string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function CDATA() {
|
|
|
|
return sprintf(
|
|
|
|
'<![CDATA[%s]]>',
|
|
|
|
str_replace(']]>', ']]]]><![CDATA[>', $this->forTemplate())
|
|
|
|
);
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
2013-03-11 21:52:30 +01:00
|
|
|
|
2012-04-11 04:48:06 +02:00
|
|
|
public function exists() {
|
2016-06-30 16:54:44 +02:00
|
|
|
return parent::exists() && $this->RAW() != '<p></p>';
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
2016-05-06 01:43:47 +02:00
|
|
|
return new HTMLEditorField($this->name, $title, 1);
|
2008-08-06 04:43:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-06 04:43:46 +02:00
|
|
|
public function scaffoldSearchField($title = null) {
|
|
|
|
return new TextField($this->name, $title);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function NoHTML()
|
|
|
|
{
|
|
|
|
// Preserve line breaks
|
|
|
|
$text = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $this->RAW());
|
|
|
|
// Convert back to plain text
|
|
|
|
return \Convert::xml2raw(strip_tags($text));
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|