2008-08-11 00:49:59 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM\FieldType;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\CheckboxSetField;
|
2008-08-11 00:49:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents an multi-select enumeration field.
|
|
|
|
*/
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBMultiEnum extends DBEnum {
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name, $enum = NULL, $default = NULL) {
|
2010-04-13 03:46:27 +02:00
|
|
|
// MultiEnum needs to take care of its own defaults
|
|
|
|
parent::__construct($name, $enum, null);
|
|
|
|
|
|
|
|
// Validate and assign the default
|
|
|
|
$this->default = null;
|
|
|
|
if($default) {
|
2014-03-10 17:44:36 +01:00
|
|
|
$defaults = preg_split('/ *, */',trim($default));
|
2010-04-13 03:46:27 +02:00
|
|
|
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);
|
|
|
|
}
|
2009-11-05 02:14:17 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requireField(){
|
2016-07-01 04:26:18 +02:00
|
|
|
// @todo: Remove mysql-centric logic from this
|
2016-06-15 06:03:16 +02:00
|
|
|
$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
|
|
|
|
$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
|
2012-09-26 23:34:00 +02:00
|
|
|
$values=array(
|
|
|
|
'type'=>'set',
|
|
|
|
'parts'=>array(
|
|
|
|
'enums'=>$this->enum,
|
2015-09-05 02:50:26 +02:00
|
|
|
'character set'=> $charset,
|
|
|
|
'collate'=> $collation,
|
2013-06-21 00:32:08 +02:00
|
|
|
'default'=> $this->default,
|
2012-09-26 23:34:00 +02:00
|
|
|
'table'=>$this->tableName,
|
|
|
|
'arrayValue'=>$this->arrayValue
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::require_field($this->tableName, $this->name, $values);
|
2010-04-13 03:50:15 +02:00
|
|
|
|
2008-08-11 00:49:59 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2008-08-11 00:49:59 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Return a {@link CheckboxSetField} suitable for editing this field
|
2016-06-17 08:49:23 +02:00
|
|
|
*
|
|
|
|
* @param string $title
|
|
|
|
* @param string $name
|
|
|
|
* @param bool $hasEmpty
|
|
|
|
* @param string $value
|
|
|
|
* @param string $emptyString
|
|
|
|
* @return CheckboxSetField
|
2008-08-11 00:49:59 +02:00
|
|
|
*/
|
2016-06-17 08:49:23 +02:00
|
|
|
public function formField($title = null, $name = null, $hasEmpty = false, $value = "", $emptyString = null) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 00:49:59 +02:00
|
|
|
if(!$title) $title = $this->name;
|
|
|
|
if(!$name) $name = $this->name;
|
|
|
|
|
2016-06-17 08:49:23 +02:00
|
|
|
$field = new CheckboxSetField($name, $title, $this->enumValues($hasEmpty), $value);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 03:46:27 +02:00
|
|
|
return $field;
|
2008-08-11 00:49:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|