silverstripe-framework/model/FieldType/DBEnum.php
Sam Minnee aeccb8b8e0 API: Move DBField subclasses into SilverStripe\Model\FieldType namespace
API: Deprecate SS_Datetime.

The DBField subclasses are have all been renamed to start with “DB” and
be in the SilverStripe\Model\FieldType namespace. To keep DataObject
definitions concise, the original short variations of their names are
preserved as service definitions. Most of the field generation code
doesn’t need to change, but where field classes are referenced directly,
changes will be needed.

SS_Datetime, which is commonly referenced outside the model system
itself, has been preserved as a subclass of DBDatetime. This has been
marked as deprecated and can be removed in SilverStripe 5.

A few places that referred to $db and $casting values weren’t using
the Injector to instantiate the relevant classes. This meant that the
remapping we have created as part of moving classes into a namespace
didn’t work.
2016-03-22 18:09:30 +13:00

203 lines
4.1 KiB
PHP

<?php
namespace SilverStripe\Model\FieldType;
use DB;
use ArrayLib;
use DropdownField;
use Config;
/**
* Class Enum represents an enumeration of a set of strings.
*
* See {@link DropdownField} for a {@link FormField} to select enum values.
*
* @package framework
* @subpackage model
*/
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';
/**
* Create a new Enum field.
*
* Example usage in {@link DataObject::$db} with comma-separated string
* notation ('Val1' is default)
*
* <code>
* "MyField" => "Enum('Val1, Val2, Val3', 'Val1')"
* </code>
*
* Example usage in in {@link DataObject::$db} with array notation
* ('Val1' is default)
*
* <code>
* "MyField" => "Enum(array('Val1', 'Val2', 'Val3'), 'Val1')"
* </code>
*
* @param enum: A string containing a comma separated list of options or an
* array of Vals.
* @param string The default option, which is either NULL or one of the
* items in the enumeration.
*/
public function __construct($name = null, $enum = NULL, $default = NULL) {
if($enum) {
$this->setEnum($enum);
// If there's a default, then
if($default) {
if(in_array($default, $this->getEnum())) {
$this->setDefault($default);
} else {
user_error("Enum::__construct() The default value '$default' does not match any item in the"
. " enumeration", E_USER_ERROR);
}
// By default, set the default value to the first item
} else {
$enum = $this->getEnum();
$this->setDefault(reset($enum));
}
}
parent::__construct($name);
}
/**
* @return void
*/
public function requireField() {
$charset = Config::inst()->get('MySQLDatabase', 'charset');
$collation = Config::inst()->get('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.
*
* @return DropdownField
*/
public function formField($title = null, $name = null, $hasEmpty = false, $value = "", $form = null,
$emptyString = null) {
if(!$title) {
$title = $this->getName();
}
if(!$name) {
$name = $this->getName();
}
$field = new DropdownField($name, $title, $this->enumValues(false), $value, $form);
if($hasEmpty) {
$field->setEmptyString($emptyString);
}
return $field;
}
/**
* @return DropdownField
*/
public function scaffoldFormField($title = null, $params = null) {
return $this->formField($title);
}
/**
* @param string
*
* @return DropdownField
*/
public function scaffoldSearchField($title = null) {
$anyText = _t('Enum.ANY', 'Any');
return $this->formField($title, null, true, $anyText, null, "($anyText)");
}
/**
* 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($enum, ", \t\n\r\0\x0B"));
}
$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;
}
}