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
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Core\Object;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Forms\FormField;
|
|
|
|
use SilverStripe\Forms\TextField;
|
2016-07-01 04:26:18 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\Filters\SearchFilter;
|
2016-06-17 08:49:23 +02:00
|
|
|
use SilverStripe\ORM\Queries\SQLSelect;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ViewableData;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Single field in the database.
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
2008-08-09 04:16:46 +02:00
|
|
|
* Every field from the database is represented as a sub-class of DBField.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
* <b>Multi-value DBField objects</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-06-28 00:29:07 +02:00
|
|
|
* Sometimes you will want to make DBField classes that don't have a 1-1 match
|
|
|
|
* to database fields. To do this, there are a number of fields for you to
|
|
|
|
* overload:
|
|
|
|
*
|
|
|
|
* - Overload {@link writeToManipulation} to add the appropriate references to
|
|
|
|
* the INSERT or UPDATE command
|
|
|
|
* - Overload {@link addToQuery} to add the appropriate items to a SELECT
|
|
|
|
* query's field list
|
2014-08-15 08:53:05 +02:00
|
|
|
* - Add appropriate accessor methods
|
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
* <b>Subclass Example</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* The class is easy to overload with custom types, e.g. the MySQL "BLOB" type
|
|
|
|
* (http://dev.mysql.com/doc/refman/5.0/en/blob.html).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
* <code>
|
2012-09-26 23:34:00 +02:00
|
|
|
* class Blob extends DBField {
|
2012-09-19 12:07:39 +02:00
|
|
|
* function requireField() {
|
2010-10-15 01:58:09 +02:00
|
|
|
* DB::requireField($this->tableName, $this->name, "blob");
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-02-23 16:40:52 +01:00
|
|
|
* @todo remove MySQL specific code from subclasses
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
abstract class DBField extends ViewableData {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Raw value of this field
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $value;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Table this field belongs to
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $tableName;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Name of this field
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $name;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Used for generating DB schema. {@see DBSchemaManager}
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-09-30 00:00:45 +02:00
|
|
|
protected $arrayValue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
/**
|
|
|
|
* The escape type for this field when inserted into a template - either "xml" or "raw".
|
|
|
|
*
|
|
|
|
* @var string
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2009-10-11 02:06:58 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $escape_type = 'raw';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
/**
|
|
|
|
* Subclass of {@link SearchFilter} for usage in {@link defaultSearchFilter()}.
|
|
|
|
*
|
|
|
|
* @var string
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-08-11 01:17:51 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_search_filter_class = 'PartialMatchFilter';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
private static $casting = array(
|
|
|
|
'ATT' => 'HTMLFragment',
|
|
|
|
'CDATA' => 'HTMLFragment',
|
|
|
|
'HTML' => 'HTMLFragment',
|
|
|
|
'HTMLATT' => 'HTMLFragment',
|
|
|
|
'JS' => 'HTMLFragment',
|
|
|
|
'RAW' => 'HTMLFragment',
|
|
|
|
'RAWURLATT' => 'HTMLFragment',
|
|
|
|
'URLATT' => 'HTMLFragment',
|
|
|
|
'XML' => 'HTMLFragment',
|
|
|
|
'ProcessedRAW' => 'HTMLFragment',
|
|
|
|
);
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* @var $default mixed Default-value in the database.
|
|
|
|
* Might be overridden on DataObject-level, but still useful for setting defaults on
|
|
|
|
* already existing records after a db-build.
|
|
|
|
*/
|
|
|
|
protected $defaultVal;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->name = $name;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a DBField object that's not bound to any particular field.
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* Useful for accessing the classes behaviour for other parts of your code.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $className class of field to construct
|
|
|
|
* @param mixed $value value of field
|
|
|
|
* @param string $name Name of field
|
|
|
|
* @param mixed $object Additional parameter to pass to field constructor
|
|
|
|
* @return DBField
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function create_field($className, $value, $name = null, $object = null) {
|
2016-06-03 10:51:02 +02:00
|
|
|
/** @var DBField $dbField */
|
2008-10-14 00:20:41 +02:00
|
|
|
$dbField = Object::create($className, $name, $object);
|
2009-05-27 02:09:23 +02:00
|
|
|
$dbField->setValue($value, null, false);
|
2014-06-28 00:29:07 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $dbField;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
/**
|
|
|
|
* Set the name of this field.
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
|
|
|
* The name should never be altered, but it if was never given a name in
|
|
|
|
* the first place you can set a name.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* If you try an alter the name a warning will be thrown.
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return DBField
|
2008-08-09 04:16:46 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setName($name) {
|
2014-06-28 00:29:07 +02:00
|
|
|
if($this->name && $this->name !== $name) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("DBField::setName() shouldn't be called once a DBField already has a name."
|
|
|
|
. "It's partially immutable - it shouldn't be altered after it's given a value.", E_USER_WARNING);
|
2008-08-09 04:16:46 +02:00
|
|
|
}
|
2014-06-28 00:29:07 +02:00
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
$this->name = $name;
|
2014-06-28 00:29:07 +02:00
|
|
|
|
|
|
|
return $this;
|
2008-08-09 04:16:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
/**
|
2009-02-02 00:49:53 +01:00
|
|
|
* Returns the name of this field.
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
2009-02-02 00:49:53 +01:00
|
|
|
* @return string
|
2008-08-09 04:16:46 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getName() {
|
2008-08-09 04:16:46 +02:00
|
|
|
return $this->name;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-13 10:30:32 +01:00
|
|
|
/**
|
|
|
|
* Returns the value of this field.
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
2009-02-13 10:30:32 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getValue() {
|
2009-02-13 10:30:32 +01:00
|
|
|
return $this->value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 04:00:40 +02:00
|
|
|
/**
|
2015-09-04 05:49:22 +02:00
|
|
|
* Set the value of this field in various formats.
|
|
|
|
* Used by {@link DataObject->getField()}, {@link DataObject->setCastedField()}
|
|
|
|
* {@link DataObject->dbObject()} and {@link DataObject->write()}.
|
2014-06-28 00:29:07 +02:00
|
|
|
*
|
2015-09-04 05:49:22 +02:00
|
|
|
* As this method is used both for initializing the field after construction,
|
|
|
|
* and actually changing its values, it needs a {@link $markChanged}
|
|
|
|
* parameter.
|
2008-08-09 04:00:40 +02:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
2015-09-04 05:49:22 +02:00
|
|
|
* @param DataObject|array $record An array or object that this field is part of
|
2016-07-01 04:26:18 +02:00
|
|
|
* @param bool $markChanged Indicate wether this field should be marked changed.
|
2015-09-04 05:49:22 +02:00
|
|
|
* Set to FALSE if you are initializing this field after construction, rather
|
|
|
|
* than setting a new value.
|
2008-08-09 04:00:40 +02:00
|
|
|
*/
|
2015-09-04 05:49:22 +02:00
|
|
|
public function setValue($value, $record = null, $markChanged = true) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->value = $value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-14 08:02:20 +02:00
|
|
|
/**
|
|
|
|
* Get default value assigned at the DB level
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getDefaultValue() {
|
|
|
|
return $this->defaultVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set default value to use at the DB level
|
|
|
|
*
|
|
|
|
* @param mixed $defaultValue
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setDefaultValue($defaultValue) {
|
|
|
|
$this->defaultVal = $defaultValue;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 04:00:40 +02:00
|
|
|
/**
|
2014-06-28 00:29:07 +02:00
|
|
|
* Determines if the field has a value which is not considered to be 'null'
|
|
|
|
* in a database context.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-09 04:00:40 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-10-11 02:06:58 +02:00
|
|
|
public function exists() {
|
2014-11-07 00:16:12 +01:00
|
|
|
return (bool)$this->value;
|
2009-10-11 02:06:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-11 08:22:33 +02:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Return the transformed value ready to be sent to the database. This value
|
|
|
|
* will be escaped automatically by the prepared query processor, so it
|
|
|
|
* should not be escaped or quoted at all.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* The field values could also be in paramaterised format, such as
|
2014-08-15 08:53:05 +02:00
|
|
|
* array('MAX(?,?)' => array(42, 69)), allowing the use of raw SQL values such as
|
2013-06-21 00:32:08 +02:00
|
|
|
* array('NOW()' => array()).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @see SQLWriteExpression::addAssignments for syntax examples
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-09-11 08:22:33 +02:00
|
|
|
* @param $value mixed The value to check
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return mixed The raw value, or escaped parameterised details
|
2008-09-11 08:22:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function prepValueForDB($value) {
|
2008-09-11 08:22:33 +02:00
|
|
|
if($value === null || $value === "" || $value === false) {
|
2013-06-21 00:32:08 +02:00
|
|
|
return null;
|
2008-09-11 08:22:33 +02:00
|
|
|
} else {
|
2013-06-21 00:32:08 +02:00
|
|
|
return $value;
|
2008-09-11 08:22:33 +02:00
|
|
|
}
|
2012-03-20 09:20:35 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 04:00:40 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Prepare the current field for usage in a
|
2008-08-09 04:00:40 +02:00
|
|
|
* database-manipulation (works on a manipulation reference).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-09 04:00:40 +02:00
|
|
|
* Make value safe for insertion into
|
2014-08-15 08:53:05 +02:00
|
|
|
* a SQL SET statement by applying addslashes() -
|
2008-09-11 08:22:33 +02:00
|
|
|
* can also be used to apply special SQL-commands
|
|
|
|
* to the raw value (e.g. for GIS functionality).
|
|
|
|
* {@see prepValueForDB}
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-09 04:00:40 +02:00
|
|
|
* @param array $manipulation
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function writeToManipulation(&$manipulation) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$manipulation['fields'][$this->name] = $this->exists()
|
2012-09-26 23:34:00 +02:00
|
|
|
? $this->prepValueForDB($this->value) : $this->nullValue();
|
2008-08-09 04:00:40 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 04:00:40 +02:00
|
|
|
/**
|
|
|
|
* Add custom query parameters for this field,
|
2014-08-15 08:53:05 +02:00
|
|
|
* mostly SELECT statements for multi-value fields.
|
|
|
|
*
|
2008-08-09 04:00:40 +02:00
|
|
|
* By default, the ORM layer does a
|
|
|
|
* SELECT <tablename>.* which
|
|
|
|
* gets you the default representations
|
|
|
|
* of all columns.
|
|
|
|
*
|
2016-06-17 08:49:23 +02:00
|
|
|
* @param SQLSelect $query
|
2008-08-09 04:00:40 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function addToQuery(&$query) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 04:16:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
/**
|
|
|
|
* Assign this DBField to a table
|
|
|
|
*
|
|
|
|
* @param string $tableName
|
|
|
|
* @return $this
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setTable($tableName) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->tableName = $tableName;
|
2015-09-04 05:49:22 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the table this field belongs to, if assigned
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getTable() {
|
|
|
|
return $this->tableName;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:06:58 +02:00
|
|
|
/**
|
2016-06-03 10:51:02 +02:00
|
|
|
* Determine 'default' casting for this field.
|
|
|
|
*
|
2009-10-11 02:06:58 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function forTemplate() {
|
2016-06-17 08:49:23 +02:00
|
|
|
// Default to XML encoding
|
|
|
|
return $this->XML();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Gets the value appropriate for a HTML attribute string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function HTMLATT() {
|
2016-06-30 16:54:44 +02:00
|
|
|
return Convert::raw2htmlatt($this->RAW());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* urlencode this string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function URLATT() {
|
2016-06-30 16:54:44 +02:00
|
|
|
return urlencode($this->RAW());
|
2008-04-06 05:52:57 +02:00
|
|
|
}
|
2012-01-31 11:44:16 +01:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* rawurlencode this string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function RAWURLATT() {
|
2016-06-30 16:54:44 +02:00
|
|
|
return rawurlencode($this->RAW());
|
2008-04-06 06:02:55 +02:00
|
|
|
}
|
2012-01-31 11:44:16 +01:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Gets the value appropriate for a HTML attribute string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function ATT() {
|
2016-06-30 16:54:44 +02:00
|
|
|
return Convert::raw2att($this->RAW());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Gets the raw value for this field.
|
|
|
|
* Note: Skips processors implemented via forTemplate()
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function RAW() {
|
2016-06-03 10:51:02 +02:00
|
|
|
return $this->getValue();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Gets javascript string literal value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function JS() {
|
2016-06-30 16:54:44 +02:00
|
|
|
return Convert::raw2js($this->RAW());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-04-21 06:59:11 +02:00
|
|
|
/**
|
|
|
|
* Return JSON encoded value
|
2016-06-03 10:51:02 +02:00
|
|
|
*
|
2016-04-21 06:59:11 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function JSON() {
|
2016-07-05 00:45:47 +02:00
|
|
|
return Convert::raw2json($this->RAW());
|
2016-04-21 06:59:11 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Alias for {@see XML()}
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function HTML(){
|
2016-06-03 10:51:02 +02:00
|
|
|
return $this->XML();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* XML encode this value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-06-17 08:49:23 +02:00
|
|
|
public function XML() {
|
2016-06-30 16:54:44 +02:00
|
|
|
return Convert::raw2xml($this->RAW());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Safely escape for XML string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function CDATA() {
|
2016-06-17 08:49:23 +02:00
|
|
|
return $this->XML();
|
2016-06-03 10:51:02 +02:00
|
|
|
}
|
|
|
|
|
2008-08-22 05:36:00 +02:00
|
|
|
/**
|
2007-07-19 12:40:28 +02:00
|
|
|
* Returns the value to be set in the database to blank this field.
|
|
|
|
* Usually it's a choice between null, 0, and ''
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @return mixed
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function nullValue() {
|
2013-06-21 00:32:08 +02:00
|
|
|
return null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves this field to the given data object.
|
2016-06-03 10:51:02 +02:00
|
|
|
*
|
|
|
|
* @param DataObject $dataObject
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function saveInto($dataObject) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$fieldName = $this->name;
|
2016-06-03 10:51:02 +02:00
|
|
|
if(empty($fieldName)) {
|
|
|
|
throw new \BadMethodCallException("DBField::saveInto() Called on a nameless '" . get_class($this) . "' object");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-06-17 08:49:23 +02:00
|
|
|
$dataObject->$fieldName = $this->value;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-06 04:43:46 +02:00
|
|
|
/**
|
|
|
|
* Returns a FormField instance used as a default
|
|
|
|
* for form scaffolding.
|
|
|
|
*
|
2009-03-22 23:59:14 +01:00
|
|
|
* Used by {@link SearchContext}, {@link ModelAdmin}, {@link DataObject::scaffoldFormFields()}
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-06 04:43:46 +02:00
|
|
|
* @param string $title Optional. Localized title of the generated instance
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param array $params
|
2008-08-06 04:43:46 +02:00
|
|
|
* @return FormField
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
2008-08-06 04:43:46 +02:00
|
|
|
$field = new TextField($this->name, $title);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-06 04:43:46 +02:00
|
|
|
return $field;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-06 04:43:46 +02:00
|
|
|
/**
|
|
|
|
* Returns a FormField instance used as a default
|
|
|
|
* for searchform scaffolding.
|
|
|
|
*
|
2009-03-22 23:59:14 +01:00
|
|
|
* Used by {@link SearchContext}, {@link ModelAdmin}, {@link DataObject::scaffoldFormFields()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-06 04:43:46 +02:00
|
|
|
* @param string $title Optional. Localized title of the generated instance
|
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
public function scaffoldSearchField($title = null) {
|
|
|
|
return $this->scaffoldFormField($title);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-06 05:28:25 +02:00
|
|
|
/**
|
|
|
|
* @todo documentation
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-06 05:28:25 +02:00
|
|
|
* @todo figure out how we pass configuration parameters to
|
2012-09-26 23:34:00 +02:00
|
|
|
* search filters (note: parameter hack now in place to pass in the required full path - using $this->name
|
|
|
|
* won't work)
|
2008-08-06 05:28:25 +02:00
|
|
|
*
|
2016-06-03 10:51:02 +02:00
|
|
|
* @param string $name Override name of this field
|
2008-08-06 05:28:25 +02:00
|
|
|
* @return SearchFilter
|
|
|
|
*/
|
2016-06-03 10:51:02 +02:00
|
|
|
public function defaultSearchFilter($name = null) {
|
2008-08-09 08:53:26 +02:00
|
|
|
$name = ($name) ? $name : $this->name;
|
2008-08-11 01:17:51 +02:00
|
|
|
$filterClass = $this->stat('default_search_filter_class');
|
2016-08-19 00:51:35 +02:00
|
|
|
return Injector::inst()->create($filterClass, $name);
|
2008-08-06 05:28:25 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Add the field to the underlying database.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
abstract public function requireField();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function debug() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return <<<DBG
|
|
|
|
<ul>
|
|
|
|
<li><b>Name:</b>{$this->name}</li>
|
|
|
|
<li><b>Table:</b>{$this->tableName}</li>
|
|
|
|
<li><b>Value:</b>{$this->value}</li>
|
2007-09-14 03:24:30 +02:00
|
|
|
</ul>
|
2007-07-19 12:40:28 +02:00
|
|
|
DBG;
|
|
|
|
}
|
2013-03-12 00:17:20 +01:00
|
|
|
|
|
|
|
public function __toString() {
|
2016-06-03 10:51:02 +02:00
|
|
|
return (string)$this->forTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getArrayValue() {
|
|
|
|
return $this->arrayValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $value
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setArrayValue($value) {
|
|
|
|
$this->arrayValue = $value;
|
|
|
|
return $this;
|
2013-03-12 00:17:20 +01:00
|
|
|
}
|
2016-09-20 02:56:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get formfield schema value
|
|
|
|
*
|
|
|
|
* @return string|array Encoded string for use in formschema response
|
|
|
|
*/
|
|
|
|
public function getSchemaValue() {
|
|
|
|
return $this->RAW();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|