silverstripe-framework/model/FieldType/DBMultiEnum.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

73 lines
1.7 KiB
PHP

<?php
namespace SilverStripe\Model\FieldType;
use DB;
use CheckboxSetField;
/**
* @package framework
* @subpackage model
*/
/**
* Represents an multi-select enumeration field.
* @package framework
* @subpackage model
*/
class DBMultiEnum extends DBEnum {
public function __construct($name, $enum = NULL, $default = NULL) {
// MultiEnum needs to take care of its own defaults
parent::__construct($name, $enum, null);
// Validate and assign the default
$this->default = null;
if($default) {
$defaults = preg_split('/ *, */',trim($default));
foreach($defaults as $thisDefault) {
if(!in_array($thisDefault, $this->enum)) {
user_error("Enum::__construct() The default value '$thisDefault' does not match "
. "any item in the enumeration", E_USER_ERROR);
return;
}
}
$this->default = implode(',',$defaults);
}
}
public function requireField(){
$charset = Config::inst()->get('MySQLDatabase', 'charset');
$collation = Config::inst()->get('MySQLDatabase', 'collation');
$values=array(
'type'=>'set',
'parts'=>array(
'enums'=>$this->enum,
'character set'=> $charset,
'collate'=> $collation,
'default'=> $this->default,
'table'=>$this->tableName,
'arrayValue'=>$this->arrayValue
)
);
DB::require_field($this->tableName, $this->name, $values);
}
/**
* Return a {@link CheckboxSetField} suitable for editing this field
*/
public function formField($title = null, $name = null, $hasEmpty = false, $value = "", $form = null,
$emptyString = null) {
if(!$title) $title = $this->name;
if(!$name) $name = $this->name;
$field = new CheckboxSetField($name, $title, $this->enumValues($hasEmpty), $value, $form);
return $field;
}
}