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.
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
function __construct($name, $enum = NULL, $default = NULL) {
|
|
|
|
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(){
|
2009-09-30 00:01:06 +02:00
|
|
|
$parts=Array('datatype'=>'enum', 'enums'=>$this->enum, 'character set'=>'utf8', 'collate'=> 'utf8_general_ci', 'default'=>$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;
|
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
$field = new DropdownField($name, $title, $this->enumValues($hasEmpty), $value, $form, $emptyString);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
2009-04-29 03:20:24 +02:00
|
|
|
|
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
|
|
|
return $this->formField($title);
|
|
|
|
}
|
2007-07-19 12:40:28 +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() {
|
|
|
|
return StringField::Lower();
|
|
|
|
}
|
|
|
|
function Upper() {
|
|
|
|
return StringField::Upper();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-11-23 02:01:03 +01:00
|
|
|
?>
|