Version: current

      1 <?php
      2 /**
      3  * ComplexTableField designed to edit a has_many join.
      4  * @package forms
      5  * @subpackage fields-relational
      6  */
      7 class HasManyComplexTableField extends ComplexTableField {
      8
      9     public $joinField;
     10
     11     protected $addTitle;
     12
     13     protected $htmlListEndName = 'CheckedList'; // If you change the value, do not forget to change it also in the JS file
     14
     15     protected $htmlListField = 'selected'; // If you change the value, do not forget to change it also in the JS file
     16
     17     public $template = 'RelationComplexTableField';
     18
     19     public $itemClass = 'HasManyComplexTableField_Item';
     20
     21     protected $relationAutoSetting = false;
     22
     23     function __construct($controller, $name, $sourceClass, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
     24         parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
     25
     26         $this->Markable = true;
     27
     28         if($controllerClass = $this->controllerClass()) {
     29             $this->joinField = $this->getParentIdName($controllerClass, $this->sourceClass);
     30             if(!$this->joinField) user_error("Can't find a has_one relationship from '$this->sourceClass' to '$controllerClass'", E_USER_WARNING);
     31         } else {
     32             user_error("Can't figure out the data class of $controller", E_USER_WARNING);
     33         }
     34
     35     }
     36
     37     function FieldHolder() {
     38         $ret = parent::FieldHolder();
     39
     40         Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
     41         Requirements::javascript(SAPPHIRE_DIR . "/javascript/HasManyFileField.js");
     42         Requirements::javascript(SAPPHIRE_DIR . '/javascript/RelationComplexTableField.js');
     43         Requirements::css(SAPPHIRE_DIR . '/css/HasManyFileField.css');
     44
     45         return $ret;
     46     }
     47
     48     /**
     49      * Try to determine the DataObject that this field is built on top of
     50      */
     51     function controllerClass() {
     52         if($this->controller instanceof DataObject) return $this->controller->class;
     53         elseif($this->controller instanceof ContentController) return $this->controller->data()->class;
     54     }
     55
     56     function getControllerID() {
     57         return $this->controller->ID;
     58     }
     59
     60     function saveInto(DataObject $record) {
     61         $fieldName = $this->name;
     62         $saveDest = $record->$fieldName();
     63
     64         if(! $saveDest)
     65             user_error("HasManyComplexTableField::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
     66
     67         $items = array();
     68
     69         if($list = $this->value[ $this->htmlListField ]) {
     70             if($list != 'undefined')
     71                 $items = explode(',', $list);
     72         }
     73
     74         $saveDest->setByIDList($items);
     75     }
     76
     77     function setAddTitle($addTitle) {
     78         if(is_string($addTitle))
     79             $this->addTitle = $addTitle;
     80     }
     81
     82     function Title() {
     83         return $this->addTitle ? $this->addTitle : parent::Title();
     84     }
     85
     86     /**
     87      * Get the IDs of the selected items, in a has_many or many_many relation
     88      */
     89     function selectedItemIDs() {
     90         $fieldName = $this->name;
     91         $selectedItems = $this->form->getRecord()->$fieldName();
     92         $itemIDs = array();
     93         foreach($selectedItems as $item) $itemIDs[] = $item->ID;
     94         return $itemIDs;
     95     }
     96
     97     function ExtraData() {
     98         $items = array();
     99
    100         $list = implode(',', $this->selectedItemIDs());
    101         $inputId = $this->id() . '_' . $this->htmlListEndName;
    102         return <<<HTML
    103         <input id="$inputId" name="{$this->name}[{$this->htmlListField}]" type="hidden" value="$list"/>
    104 HTML;
    105     }
    106 }
    107
    108 /**
    109  * Single record of a {@link HasManyComplexTableField} field.
    110  * @package forms
    111  * @subpackage fields-relational
    112  */
    113 class HasManyComplexTableField_Item extends ComplexTableField_Item {
    114
    115     function MarkingCheckbox() {
    116         $name = $this->parent->Name() . '[]';
    117
    118         if(!$this->parent->joinField) {
    119             user_error("joinField not set in HasManyComplexTableField '{$this->parent->name}'", E_USER_WARNING);
    120             return null;
    121         }
    122
    123         $joinVal = $this->item->{$this->parent->joinField};
    124         $parentID = $this->parent->getControllerID();
    125
    126         if($this->parent->IsReadOnly || ($joinVal > 0 && $joinVal != $parentID))
    127             return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" disabled=\"disabled\"/>";
    128         else if($joinVal == $parentID)
    129             return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" checked=\"checked\"/>";
    130         else
    131             return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\"/>";
    132     }
    133 }
    134
    135 ?>
    136

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