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\Config\Config;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\ORM\ArrayLib;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2010-10-15 01:58:09 +02:00
|
|
|
* Class Enum represents an enumeration of a set of strings.
|
2012-07-18 01:54:42 +02:00
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
* See {@link DropdownField} for a {@link FormField} to select enum values.
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class DBEnum extends DBString
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of enum values
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $enum = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default value
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $default = null;
|
|
|
|
|
|
|
|
private static $default_search_filter_class = 'ExactMatchFilter';
|
|
|
|
|
|
|
|
/**
|
2017-11-16 00:54:51 +01:00
|
|
|
* Create a new Enum field, which is a value within a defined set, with an optional default.
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-11-16 00:54:51 +01:00
|
|
|
* Example field specification strings:
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* <code>
|
2017-11-16 00:54:51 +01:00
|
|
|
* "MyField" => "Enum('Val1, Val2, Val3')" // First item 'Val1' is default implicitly
|
|
|
|
* "MyField" => "Enum('Val1, Val2, Val3', 'Val2')" // 'Val2' is default explicitly
|
|
|
|
* "MyField" => "Enum('Val1, Val2, Val3', null)" // Force empty (no) default
|
|
|
|
* "MyField" => "Enum(array('Val1', 'Val2', 'Val3'), 'Val1')" // Supports array notation as well
|
2016-11-29 00:31:16 +01:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string|array $enum A string containing a comma separated list of options or an array of Vals.
|
2017-11-16 00:54:51 +01:00
|
|
|
* @param string|int|null $default The default option, which is either NULL or one of the items in the enumeration.
|
|
|
|
* If passing in an integer (non-string) it will default to the index of that item in the list.
|
|
|
|
* Set to null or empty string to allow empty values
|
2017-06-23 07:40:06 +02:00
|
|
|
* @param array $options Optional parameters for this DB field
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-11-16 00:54:51 +01:00
|
|
|
public function __construct($name = null, $enum = null, $default = 0, $options = [])
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
if ($enum) {
|
|
|
|
$this->setEnum($enum);
|
2017-11-16 00:54:51 +01:00
|
|
|
$enum = $this->getEnum();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-11-16 00:54:51 +01:00
|
|
|
// If there's a default, then use this
|
|
|
|
if ($default && !is_int($default)) {
|
|
|
|
if (in_array($default, $enum)) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$this->setDefault($default);
|
|
|
|
} else {
|
2017-11-16 00:54:51 +01:00
|
|
|
user_error(
|
|
|
|
"Enum::__construct() The default value '$default' does not match any item in the enumeration",
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-11-16 00:54:51 +01:00
|
|
|
} elseif (is_int($default) && $default < count($enum)) {
|
|
|
|
// Set to specified index if given
|
|
|
|
$this->setDefault($enum[$default]);
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
2017-11-16 00:54:51 +01:00
|
|
|
// Set to null if specified
|
|
|
|
$this->setDefault(null);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:40:06 +02:00
|
|
|
parent::__construct($name, $options);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function requireField()
|
|
|
|
{
|
|
|
|
$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
|
|
|
|
$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
|
|
|
|
|
|
|
|
$parts = array(
|
|
|
|
'datatype' => 'enum',
|
|
|
|
'enums' => $this->getEnum(),
|
|
|
|
'character set' => $charset,
|
|
|
|
'collate' => $collation,
|
|
|
|
'default' => $this->getDefault(),
|
|
|
|
'table' => $this->getTable(),
|
|
|
|
'arrayValue' => $this->arrayValue
|
|
|
|
);
|
|
|
|
|
|
|
|
$values = array(
|
|
|
|
'type' => 'enum',
|
|
|
|
'parts' => $parts
|
|
|
|
);
|
|
|
|
|
|
|
|
DB::require_field($this->getTable(), $this->getName(), $values);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a dropdown field suitable for editing this field.
|
|
|
|
*
|
|
|
|
* @param string $title
|
|
|
|
* @param string $name
|
|
|
|
* @param bool $hasEmpty
|
|
|
|
* @param string $value
|
|
|
|
* @param string $emptyString
|
|
|
|
* @return DropdownField
|
|
|
|
*/
|
|
|
|
public function formField($title = null, $name = null, $hasEmpty = false, $value = "", $emptyString = null)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!$title) {
|
|
|
|
$title = $this->getName();
|
|
|
|
}
|
|
|
|
if (!$name) {
|
|
|
|
$name = $this->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
$field = new DropdownField($name, $title, $this->enumValues(false), $value);
|
|
|
|
if ($hasEmpty) {
|
|
|
|
$field->setEmptyString($emptyString);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scaffoldFormField($title = null, $params = null)
|
|
|
|
{
|
|
|
|
return $this->formField($title);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*
|
|
|
|
* @return DropdownField
|
|
|
|
*/
|
|
|
|
public function scaffoldSearchField($title = null)
|
|
|
|
{
|
2017-04-20 03:59:51 +02:00
|
|
|
$anyText = _t('SilverStripe\\ORM\\FieldType\\DBEnum.ANY', 'Any');
|
2018-10-19 06:05:12 +02:00
|
|
|
return $this->formField($title, null, true, '', "($anyText)");
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the values of this enum as an array, suitable for insertion into
|
|
|
|
* a {@link DropdownField}
|
|
|
|
*
|
|
|
|
* @param boolean
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function enumValues($hasEmpty = false)
|
|
|
|
{
|
|
|
|
return ($hasEmpty)
|
|
|
|
? array_merge(array('' => ''), ArrayLib::valuekey($this->getEnum()))
|
|
|
|
: ArrayLib::valuekey($this->getEnum());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of enum values
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getEnum()
|
|
|
|
{
|
|
|
|
return $this->enum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set enum options
|
|
|
|
*
|
|
|
|
* @param string|array $enum
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setEnum($enum)
|
|
|
|
{
|
|
|
|
if (!is_array($enum)) {
|
|
|
|
$enum = preg_split(
|
|
|
|
'/\s*,\s*/',
|
|
|
|
// trim commas only if they are on the right with a newline following it
|
|
|
|
ltrim(preg_replace('/,\s*\n\s*$/', '', $enum))
|
|
|
|
);
|
|
|
|
}
|
2017-11-16 00:54:51 +01:00
|
|
|
$this->enum = array_values($enum);
|
2016-11-29 00:31:16 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default vwalue
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getDefault()
|
|
|
|
{
|
|
|
|
return $this->default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set default value
|
|
|
|
*
|
|
|
|
* @param string $default
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setDefault($default)
|
|
|
|
{
|
|
|
|
$this->default = $default;
|
2017-11-16 00:54:51 +01:00
|
|
|
$this->setDefaultValue($default);
|
2016-11-29 00:31:16 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2012-10-17 01:30:05 +02:00
|
|
|
}
|