Version: current

      1 <?php
      2
      3 class HasManyFileDataObjectManager extends FileDataObjectManager {
      4     public $joinField;
      5     public $addTitle;
      6     public $RelationType = "HasMany";
      7     protected $htmlListEndName = 'CheckedList';
      8     protected $htmlListField = 'selected';
      9     public $template = 'RelationFileDataObjectManager';
     10     public $itemClass = 'HasManyFileDataObjectManager_Item';
     11     protected $relationAutoSetting = false;
     12     protected $markingPermission;
     13
     14
     15     /**
     16      * Most of the code below was copied from HasManyComplexTableField.
     17      * Painful, but necessary, until PHP supports multiple inheritance.
     18      */
     19
     20     function __construct($controller, $name, $sourceClass, $fileFieldName,  $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "Created DESC", $sourceJoin = "") {
     21         parent::__construct($controller, $name, $sourceClass, $fileFieldName, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
     22
     23         $this->Markable = true;
     24
     25         if($controllerClass = $this->controllerClass()) {
     26             $this->joinField = $this->getParentIdName($controllerClass, $this->sourceClass);
     27         } else {
     28             user_error("Can't figure out the data class of $controller", E_USER_WARNING);
     29         }
     30
     31     }
     32
     33
     34     /**
     35      * Try to determine the DataObject that this field is built on top of
     36      */
     37     function controllerClass() {
     38         if($this->controller instanceof DataObject) return $this->controller->class;
     39         elseif($this->controller instanceof ContentController) return $this->controller->data()->class;
     40     }
     41
     42     public function setMarkingPermission($perm) {
     43         $this->markingPermission = $perm;
     44     }
     45
     46     public function hasMarkingPermission() {
     47         if(is_bool($this->markingPermission))
     48             return $this->markingPermission;
     49         elseif($this->markingPermission)
     50             return Permission::check($this->markingPermission);
     51         return true;
     52     }
     53
     54
     55     function getQuery($limitClause = null) {
     56         if($this->customQuery) {
     57             $query = $this->customQuery;
     58             $query->select[] = "{$this->sourceClass}.ID AS ID";
     59             $query->select[] = "{$this->sourceClass}.ClassName AS ClassName";
     60             $query->select[] = "{$this->sourceClass}.ClassName AS RecordClassName";
     61         }
     62         else {
     63             $query = singleton($this->sourceClass)->extendedSQL($this->sourceFilter, $this->sourceSort, $limitClause, $this->sourceJoin);
     64
     65             // Add more selected fields if they are from joined table.
     66
     67             $SNG = singleton($this->sourceClass);
     68             foreach($this->FieldList() as $k => $title) {
     69                 if(! $SNG->hasField($k) && ! $SNG->hasMethod('get' . $k))
     70                     $query->select[] = $k;
     71             }
     72         }
     73         return clone $query;
     74     }
     75
     76     public function setParentClass($class) {
     77         parent::setParentClass($class);
     78         $this->joinField = $this->getParentIdName($class, $this->sourceClass);
     79     }
     80
     81
     82     function sourceItems() {
     83         if($this->sourceItems)
     84             return $this->sourceItems;
     85
     86         $limitClause = '';
     87         if(isset($_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ]) && is_numeric($_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ]))
     88             $limitClause = $_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ] . ", $this->pageSize";
     89         else
     90             $limitClause = "0, $this->pageSize";
     91
     92         $dataQuery = $this->getQuery($limitClause);
     93         $records = $dataQuery->execute();
     94         $items = new DataObjectSet();
     95         foreach($records as $record) {
     96             if(! is_object($record)) {
     97                 $class = $this->sourceClass;
     98                 $record = new $class($record);
     99             }
    100             $items->push($record);
    101         }
    102
    103         $dataQuery = $this->getQuery();
    104         $records = $dataQuery->execute();
    105         $unpagedItems = new DataObjectSet();
    106         foreach($records as $record) {
    107             if(! is_object($record))
    108                 $record = new DataObject($record);
    109             $unpagedItems->push($record);
    110         }
    111         $this->unpagedSourceItems = $unpagedItems;
    112
    113         $this->totalCount = ($this->unpagedSourceItems) ? $this->unpagedSourceItems->TotalItems() : null;
    114
    115         return $items;
    116     }
    117
    118     function getControllerID() {
    119         return $this->controller->ID;
    120     }
    121
    122     public function SortableClass() {
    123         return $this->sourceClass();
    124     }
    125
    126
    127     function saveInto(DataObject $record) {
    128         $fieldName = $this->name;
    129         $saveDest = $record->$fieldName();
    130
    131         if(! $saveDest)
    132             user_error("HasManyDataObjectManager::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
    133
    134         $items = array();
    135
    136         if($list = $this->value[ $this->htmlListField ]) {
    137             if($list != 'undefined')
    138                 $items = explode(',', trim($list,","));
    139         }
    140
    141         $saveDest->setByIDList($items);
    142     }
    143
    144     function ExtraData() {
    145         $items = array();
    146         foreach($this->unpagedSourceItems as $item) {
    147             if($item->{$this->joinField} == $this->controller->ID)
    148                 $items[] = $item->ID;
    149         }
    150         $list = implode(',', $items);
    151         $value = ",";
    152         $value .= !empty($list) ? $list."," : "";
    153         $inputId = $this->id() . '_' . $this->htmlListEndName;
    154         return <<<HTML
    155         <input id="$inputId" name="{$this->name}[{$this->htmlListField}]" type="hidden" value="{$value}"/>
    156 HTML;
    157     }
    158
    159 }
    160
    161
    162 class HasManyFileDataObjectManager_Item extends FileDataObjectManager_Item {
    163
    164     function MarkingCheckbox() {
    165         $name = $this->parent->Name() . '[]';
    166
    167         $joinVal = $this->item->{$this->parent->joinField};
    168         $parentID = $this->parent->getControllerID();
    169         $disabled = $this->parent->hasMarkingPermission() ? "" : "disabled='disabled'";
    170
    171         if($this->parent->IsReadOnly || ($joinVal > 0 && $joinVal != $parentID))
    172             return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" disabled=\"disabled\"/>";
    173         else if($joinVal == $parentID)
    174             return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" checked=\"checked\" $disabled />";
    175         else
    176             return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" $disabled />";
    177     }
    178 }
    179
    180
    181
    182 ?>

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