silverstripe-framework/model/FieldType/DBEnum.php

207 lines
4.2 KiB
PHP
Raw Normal View History

<?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.
2014-08-15 08:53:05 +02:00
*
* @package framework
* @subpackage model
*/
class DBEnum extends DBString {
2014-08-15 08:53:05 +02:00
/**
* List of enum values
*
* @var array
*/
protected $enum = array();
/**
* Default value
*
* @var string|null
*/
protected $default = null;
2014-08-15 08:53:05 +02:00
private static $default_search_filter_class = 'ExactMatchFilter';
2014-08-15 08:53:05 +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
* notation ('Val1' is default)
*
* <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
* ('Val1' is default)
*
* <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
* array of Vals.
2014-08-15 08:53:05 +02:00
* @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);
2014-08-15 08:53:05 +02:00
// 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);
}
2014-08-15 08:53:05 +02:00
// By default, set the default value to the first item
} else {
$enum = $this->getEnum();
$this->setDefault(reset($enum));
}
}
parent::__construct($name);
}
2014-08-15 08:53:05 +02:00
/**
* @return void
*/
public function requireField() {
$charset = Config::inst()->get('MySQLDatabase', 'charset');
$collation = Config::inst()->get('MySQLDatabase', 'collation');
$parts = array(
2014-08-15 08:53:05 +02:00
'datatype' => 'enum',
'enums' => $this->getEnum(),
'character set' => $charset,
'collate' => $collation,
'default' => $this->getDefault(),
'table' => $this->getTable(),
'arrayValue' => $this->arrayValue
);
2014-08-15 08:53:05 +02:00
$values = array(
2014-08-15 08:53:05 +02:00
'type' => 'enum',
'parts' => $parts
);
DB::require_field($this->getTable(), $this->getName(), $values);
}
2014-08-15 08:53:05 +02:00
/**
* 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)");
}
2014-08-15 08:53:05 +02:00
/**
2014-08-15 08:53:05 +02: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) {
2014-08-15 08:53:05 +02:00
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)) {
Merge 3 into master # Conflicts: # .travis.yml # admin/css/ie7.css # admin/css/ie7.css.map # admin/css/ie8.css.map # admin/css/screen.css # admin/css/screen.css.map # admin/javascript/LeftAndMain.js # admin/scss/_style.scss # admin/scss/_uitheme.scss # control/HTTPRequest.php # core/Object.php # css/AssetUploadField.css # css/AssetUploadField.css.map # css/ConfirmedPasswordField.css.map # css/Form.css.map # css/GridField.css.map # css/TreeDropdownField.css.map # css/UploadField.css # css/UploadField.css.map # css/debug.css.map # dev/Debug.php # docs/en/00_Getting_Started/00_Server_Requirements.md # docs/en/02_Developer_Guides/06_Testing/00_Unit_Testing.md # docs/en/02_Developer_Guides/06_Testing/index.md # docs/en/02_Developer_Guides/14_Files/02_Images.md # docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/How_Tos/Extend_CMS_Interface.md # filesystem/File.php # filesystem/Folder.php # filesystem/GD.php # filesystem/Upload.php # forms/ToggleField.php # forms/Validator.php # javascript/lang/en_GB.js # javascript/lang/fr.js # javascript/lang/src/en.js # javascript/lang/src/fr.js # model/Image.php # model/UnsavedRelationList.php # model/Versioned.php # model/connect/MySQLDatabase.php # model/fieldtypes/DBField.php # model/fieldtypes/Enum.php # scss/AssetUploadField.scss # scss/UploadField.scss # templates/email/ChangePasswordEmail.ss # templates/forms/DropdownField.ss # tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php # tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php # tests/forms/EnumFieldTest.php # tests/security/MemberTest.php # tests/security/MemberTest.yml # tests/security/SecurityTest.php
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))
);
}
$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;
}
}