2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
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.
|
|
|
|
* See {@link DropdownField} for a {@link FormField} to select enum values.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Enum extends DBField {
|
|
|
|
|
|
|
|
protected $enum, $default;
|
|
|
|
|
2010-10-13 05:40:36 +02:00
|
|
|
public static $default_search_filter_class = 'ExactMatchFilter';
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new Enum field.
|
2010-10-15 01:58:09 +02:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param enum: A string containing a comma separated list of options or an array of Vals.
|
|
|
|
* @param default The default option, which is either NULL or one of the items in the enumeration.
|
|
|
|
*/
|
BUGFIX Versioned's constructor doesn't provide suitable defaults. Previously a bug/feature in singleton, where it would pass null,true as params to strong_create, which would then get passed through as params to Versioned's constructor, meant that the code still executed fine (as was set to something that wasn't an array, so the null and true were instead taken as args). The fact that the usage of singleton(Versioned) never really used the classes code, purely for value lookup, meant that this never propagated errors. I've now switched singleton() to use the injector for retrieving values, which means these dud values are no longer passed through
CHANGE Given that Config::inst is an implementation of the singleton pattern itself, I've removed the extra call to singleton(). A side effect of this is that it gets around a possibly nasty circular reference with the dependency injector (which relies on the config object); in future, this dependency structure should really be structured from the DI directly.
MINOR Change singleton and strong_create to use dependency injector
BUGFIX: Provide default constructor values for classes (fixes issues when used in 'singleton' scenario during dev/build in particular)
MINOR Clear out injector state when resetting db schema during tests (a follow on from changing singleton() calls to use the injector underneath)
2012-05-22 02:34:08 +02:00
|
|
|
function __construct($name = null, $enum = NULL, $default = NULL) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($enum) {
|
|
|
|
if(!is_array($enum)){
|
2009-06-18 11:34:17 +02:00
|
|
|
$enum = preg_split("/ *, */", trim($enum));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->enum = $enum;
|
|
|
|
|
|
|
|
// If there's a default, then
|
|
|
|
if($default) {
|
|
|
|
if(in_array($default, $enum)) {
|
|
|
|
$this->default = $default;
|
|
|
|
} else {
|
2010-04-13 03:45:53 +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
|
|
|
}
|
|
|
|
|
|
|
|
// By default, set the default value to the first item
|
|
|
|
} else {
|
|
|
|
$this->default = reset($enum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent::__construct($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
function requireField(){
|
2011-03-21 23:29:33 +01:00
|
|
|
$parts=Array('datatype'=>'enum', 'enums'=>Convert::raw2sql($this->enum), 'character set'=>'utf8', 'collate'=> 'utf8_general_ci', 'default'=>Convert::raw2sql($this->default), 'table'=>$this->tableName, 'arrayValue'=>$this->arrayValue);
|
2008-11-23 02:01:03 +01:00
|
|
|
$values=Array('type'=>'enum', 'parts'=>$parts);
|
|
|
|
DB::requireField($this->tableName, $this->name, $values);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a dropdown field suitable for editing this field
|
|
|
|
*/
|
2009-04-29 03:20:24 +02:00
|
|
|
function formField($title = null, $name = null, $hasEmpty = false, $value = "", $form = null, $emptyString = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$title) $title = $this->name;
|
|
|
|
if(!$name) $name = $this->name;
|
|
|
|
|
2012-05-23 12:45:04 +02:00
|
|
|
$field = new DropdownField($name, $title, $this->enumValues($hasEmpty), $value, $form);
|
|
|
|
$field->setEmptyString($emptyString);
|
|
|
|
|
|
|
|
return $field;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
function scaffoldSearchField($title = null) {
|
2009-04-29 03:20:24 +02:00
|
|
|
$anyText = _t('Enum.ANY', 'Any');
|
|
|
|
return $this->formField($title, null, false, '', null, "($anyText)");
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the values of this enum, suitable for insertion into a dropdown field.
|
|
|
|
*/
|
|
|
|
function enumValues($hasEmpty = false) {
|
|
|
|
return ($hasEmpty) ? array_merge(array('' => ''), ArrayLib::valuekey($this->enum)) : ArrayLib::valuekey($this->enum);
|
|
|
|
}
|
2009-11-21 02:41:24 +01:00
|
|
|
|
|
|
|
function Lower() {
|
2012-04-03 20:58:17 +02:00
|
|
|
return StringField::LowerCase();
|
2009-11-21 02:41:24 +01:00
|
|
|
}
|
|
|
|
function Upper() {
|
2012-04-03 20:58:17 +02:00
|
|
|
return StringField::UpperCase();
|
2009-11-21 02:41:24 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|