Version: current

      1 <?php
      2
      3 /**
      4  * Base class for multiple option fields such as {@link EditableDropdownField}
      5  * and radio sets.
      6  *
      7  * Implemented as a class but should be viewed as abstract, you should
      8  * instantiate a subclass such as {@link EditableDropdownField}
      9  *
     10  * @see EditableCheckboxGroupField
     11  * @see EditableDropdownField
     12  *
     13  * @package userforms
     14  */
     15
     16 class EditableMultipleOptionField extends EditableFormField {
     17
     18     static $has_many = array(
     19         "Options" => "EditableOption"
     20     );
     21
     22     /**
     23      * Publishing Versioning support.
     24      *
     25      * When publishing it needs to handle copying across / publishing
     26      * each of the individual field options
     27      *
     28      * @return void
     29      */
     30     public function doPublish($fromStage, $toStage, $createNewVersion = false) {
     31         $live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID");
     32
     33         if($live) {
     34             foreach($live as $option) {
     35                 $option->delete();
     36             }
     37         }
     38
     39         if($this->Options()) {
     40             foreach($this->Options() as $option) {
     41                 $option->publish($fromStage, $toStage, $createNewVersion);
     42             }
     43         }
     44
     45         $this->publish($fromStage, $toStage, $createNewVersion);
     46     }
     47
     48     /**
     49      * Unpublishing Versioning support
     50      *
     51      * When unpublishing the field it has to remove all options attached
     52      *
     53      * @return void
     54      */
     55     public function doDeleteFromStage($stage) {
     56         if($this->Options()) {
     57             foreach($this->Options() as $option) {
     58                 $option->deleteFromStage($stage);
     59             }
     60         }
     61
     62         $this->deleteFromStage($stage);
     63     }
     64
     65     /**
     66      * Deletes all the options attached to this field before deleting the
     67      * field. Keeps stray options from floating around
     68      *
     69      * @return void
     70      */
     71     public function delete() {
     72         $options = $this->Options();
     73
     74         if($options) {
     75             foreach($options as $option) {
     76                 $option->delete();
     77             }
     78         }
     79
     80         parent::delete();
     81     }
     82
     83     /**
     84      * Duplicate a pages content. We need to make sure all the fields attached
     85      * to that page go with it
     86      *
     87      * @return DataObject
     88      */
     89     public function duplicate($doWrite = true) {
     90         $clonedNode = parent::duplicate($doWrite);
     91
     92         if($this->Options()) {
     93             foreach($this->Options() as $field) {
     94                 $newField = $field->duplicate();
     95                 $newField->ParentID = $clonedNode->ID;
     96                 $newField->write();
     97             }
     98         }
     99
    100         return $clonedNode;
    101     }
    102
    103     /**
    104      * On before saving this object we need to go through and keep an eye on
    105      * all our option fields that are related to this field in the form
    106      *
    107      * @param ArrayData
    108      */
    109     public function populateFromPostData($data) {
    110         parent::populateFromPostData($data);
    111
    112         // get the current options
    113         $fieldSet = $this->Options();
    114
    115         // go over all the current options and check if ID and Title still exists
    116         foreach($fieldSet as $option) {
    117             if(isset($data[$option->ID]) && isset($data[$option->ID]['Title']) && $data[$option->ID]['Title'] != "field-node-deleted") {
    118                 $option->populateFromPostData($data[$option->ID]);
    119             }
    120             else {
    121                 $option->delete();
    122             }
    123         }
    124     }
    125
    126     /**
    127      * Return whether or not this field has addable options such as a
    128      * {@link EditableDropdownField} or {@link EditableRadioField}
    129      *
    130      * @return bool
    131      */
    132     public function getHasAddableOptions() {
    133         return true;
    134     }
    135
    136     /**
    137      * Return the form field for this object in the front end form view
    138      *
    139      * @return FormField
    140      */
    141     public function getFormField() {
    142         return user_error('Please implement getFormField() on '. $this->class, E_USER_ERROR);
    143     }
    144 }

[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. -