Version: current

     1 <?php
     2
     3 /**
     4  * @package sapphire
     5  * @subpackage model
     6  */
     7
     8 /**
     9  * Represents an multi-select enumeration field.
    10  * @package sapphire
    11  * @subpackage model
    12  */
    13 class MultiEnum extends Enum {
    14     function __construct($name, $enum = NULL, $default = NULL) {
    15         // MultiEnum needs to take care of its own defaults
    16         parent::__construct($name, $enum, null);
    17
    18         // Validate and assign the default
    19         $this->default = null;
    20         if($default) {
    21             $defaults = preg_split('/ *, */',trim($default));
    22             foreach($defaults as $thisDefault) {
    23                 if(!in_array($thisDefault, $this->enum)) {
    24                     user_error("Enum::__construct() The default value '$thisDefault' does not match "
    25                         . "any item in the enumeration", E_USER_ERROR);
    26                     return;
    27                 }
    28             }
    29             $this->default = implode(',',$defaults);
    30         }
    31     }
    32
    33     function requireField(){
    34
    35         $values=array('type'=>'set', 'parts'=>array('enums'=>$this->enum, 'character set'=>'utf8', 'collate'=> 'utf8_unicode_ci', 'default'=>Convert::raw2sql($this->default), 'table'=>$this->tableName, 'arrayValue'=>$this->arrayValue));
    36         DB::requireField($this->tableName, $this->name, $values);
    37
    38     }
    39
    40
    41     /**
    42      * Return a {@link CheckboxSetField} suitable for editing this field
    43      */
    44     function formField($title = null, $name = null, $hasEmpty = false, $value = "", $form = null) {
    45         if(!$title) $title = $this->name;
    46         if(!$name) $name = $this->name;
    47
    48         $field = new CheckboxSetField($name, $title, $this->enumValues($hasEmpty), $value, $form);
    49
    50         return $field;
    51     }
    52 }
    53
    54  ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -