2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Model\FieldType;
|
|
|
|
|
|
|
|
use DB;
|
|
|
|
use ArrayLib;
|
|
|
|
use DropdownField;
|
|
|
|
use Config;
|
|
|
|
|
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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
|
|
|
*/
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBEnum extends DBString {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
/**
|
|
|
|
* List of enum values
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $enum = array();
|
2016-02-25 02:51:59 +01:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
/**
|
|
|
|
* Default value
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $default = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_search_filter_class = 'ExactMatchFilter';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new Enum field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Example usage in {@link DataObject::$db} with comma-separated string
|
2012-07-18 01:54:42 +02:00
|
|
|
* notation ('Val1' is default)
|
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
* <code>
|
|
|
|
* "MyField" => "Enum('Val1, Val2, Val3', 'Val1')"
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Example usage in in {@link DataObject::$db} with array notation
|
2012-07-18 01:54:42 +02:00
|
|
|
* ('Val1' is default)
|
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
* <code>
|
|
|
|
* "MyField" => "Enum(array('Val1', 'Val2', 'Val3'), 'Val1')"
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @param enum: A string containing a comma separated list of options or an
|
2012-07-18 01:54:42 +02:00
|
|
|
* array of Vals.
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string The default option, which is either NULL or one of the
|
2012-07-18 01:54:42 +02:00
|
|
|
* items in the enumeration.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-07-18 01:54:42 +02:00
|
|
|
public function __construct($name = null, $enum = NULL, $default = NULL) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($enum) {
|
2015-09-04 05:49:22 +02:00
|
|
|
$this->setEnum($enum);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// If there's a default, then
|
2007-07-19 12:40:28 +02:00
|
|
|
if($default) {
|
2015-09-04 05:49:22 +02:00
|
|
|
if(in_array($default, $this->getEnum())) {
|
|
|
|
$this->setDefault($default);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Enum::__construct() The default value '$default' does not match any item in the"
|
|
|
|
. " enumeration", E_USER_ERROR);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// By default, set the default value to the first item
|
|
|
|
} else {
|
2015-09-04 05:49:22 +02:00
|
|
|
$enum = $this->getEnum();
|
|
|
|
$this->setDefault(reset($enum));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-18 01:54:42 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct($name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-07-18 01:54:42 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function requireField() {
|
2015-09-05 02:50:26 +02:00
|
|
|
$charset = Config::inst()->get('MySQLDatabase', 'charset');
|
|
|
|
$collation = Config::inst()->get('MySQLDatabase', 'collation');
|
|
|
|
|
2012-07-18 01:54:42 +02:00
|
|
|
$parts = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'datatype' => 'enum',
|
2015-09-04 05:49:22 +02:00
|
|
|
'enums' => $this->getEnum(),
|
2015-09-05 02:50:26 +02:00
|
|
|
'character set' => $charset,
|
|
|
|
'collate' => $collation,
|
2015-09-04 05:49:22 +02:00
|
|
|
'default' => $this->getDefault(),
|
|
|
|
'table' => $this->getTable(),
|
2012-07-18 01:54:42 +02:00
|
|
|
'arrayValue' => $this->arrayValue
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-07-18 01:54:42 +02:00
|
|
|
$values = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'type' => 'enum',
|
2012-07-18 01:54:42 +02:00
|
|
|
'parts' => $parts
|
|
|
|
);
|
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
DB::require_field($this->getTable(), $this->getName(), $values);
|
2012-07-18 01:54:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2012-07-18 01:54:42 +02:00
|
|
|
* Return a dropdown field suitable for editing this field.
|
|
|
|
*
|
|
|
|
* @return DropdownField
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public function formField($title = null, $name = null, $hasEmpty = false, $value = "", $form = null,
|
|
|
|
$emptyString = null) {
|
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
if(!$title) {
|
|
|
|
$title = $this->getName();
|
|
|
|
}
|
|
|
|
if(!$name) {
|
|
|
|
$name = $this->getName();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2013-10-19 14:30:52 +02:00
|
|
|
$field = new DropdownField($name, $title, $this->enumValues(false), $value, $form);
|
2012-10-17 01:30:05 +02:00
|
|
|
if($hasEmpty) {
|
|
|
|
$field->setEmptyString($emptyString);
|
|
|
|
}
|
2012-05-23 12:45:04 +02:00
|
|
|
|
|
|
|
return $field;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-04-29 03:20:24 +02:00
|
|
|
|
2012-07-18 01:54:42 +02:00
|
|
|
/**
|
|
|
|
* @return DropdownField
|
|
|
|
*/
|
2009-04-29 03:20:24 +02:00
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
|
|
|
return $this->formField($title);
|
|
|
|
}
|
2012-05-23 12:45:04 +02:00
|
|
|
|
2012-07-18 01:54:42 +02:00
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*
|
|
|
|
* @return DropdownField
|
|
|
|
*/
|
|
|
|
public function scaffoldSearchField($title = null) {
|
2009-04-29 03:20:24 +02:00
|
|
|
$anyText = _t('Enum.ANY', 'Any');
|
2013-10-19 14:30:52 +02:00
|
|
|
return $this->formField($title, null, true, $anyText, null, "($anyText)");
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Returns the values of this enum as an array, suitable for insertion into
|
2012-07-18 01:54:42 +02:00
|
|
|
* a {@link DropdownField}
|
|
|
|
*
|
|
|
|
* @param boolean
|
|
|
|
*
|
|
|
|
* @return array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-07-18 01:54:42 +02:00
|
|
|
public function enumValues($hasEmpty = false) {
|
2014-08-15 08:53:05 +02:00
|
|
|
return ($hasEmpty)
|
2015-09-04 05:49:22 +02:00
|
|
|
? 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)) {
|
2016-04-29 07:50:55 +02:00
|
|
|
$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))
|
|
|
|
);
|
2015-09-04 05:49:22 +02:00
|
|
|
}
|
|
|
|
$this->enum = $enum;
|
|
|
|
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;
|
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-10-17 01:30:05 +02:00
|
|
|
}
|