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.
|
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
|
|
|
|
*/
|
2012-07-18 01:54:42 +02:00
|
|
|
class Enum extends StringField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $enum, $default;
|
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) {
|
2012-07-18 01:54:42 +02:00
|
|
|
if(!is_array($enum)) {
|
2014-03-10 17:44:36 +01:00
|
|
|
$enum = preg_split("/ *, */", trim($enum));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->enum = $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) {
|
|
|
|
if(in_array($default, $enum)) {
|
|
|
|
$this->default = $default;
|
|
|
|
} 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 {
|
|
|
|
$this->default = reset($enum);
|
|
|
|
}
|
|
|
|
}
|
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() {
|
|
|
|
$parts = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'datatype' => 'enum',
|
|
|
|
'enums' => $this->enum,
|
|
|
|
'character set' => 'utf8',
|
|
|
|
'collate' => 'utf8_general_ci',
|
|
|
|
'default' => $this->default,
|
|
|
|
'table' => $this->tableName,
|
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
|
|
|
|
);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::require_field($this->tableName, $this->name, $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) {
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$title) $title = $this->name;
|
|
|
|
if(!$name) $name = $this->name;
|
|
|
|
|
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)
|
|
|
|
? array_merge(array('' => ''), ArrayLib::valuekey($this->enum))
|
2012-09-26 23:34:00 +02:00
|
|
|
: ArrayLib::valuekey($this->enum);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-10-17 01:30:05 +02:00
|
|
|
}
|