Source of file GridFieldManyRelationHandler.php
Size: 3,039 Bytes - Last Modified: 2021-12-23T10:31:55+00:00
/var/www/docs.ssmods.com/process/src/gridfield/GridFieldManyRelationHandler.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 | <?php class GridFieldManyRelationHandler extends GridFieldRelationHandler implements GridField_DataManipulator { protected $cheatList; protected $cheatManyList; public function __construct($useToggle = true, $segement = 'before') { parent::__construct($useToggle, $segement); $this->cheatList = new GridFieldManyRelationHandler_HasManyList; $this->cheatManyList = new GridFieldManyRelationHandler_ManyManyList; } public function getColumnContent($gridField, $record, $columnName) { $list = $gridField->getList(); if(!$list instanceof RelationList) { user_error('GridFieldManyRelationHandler requires the GridField to have a RelationList. Got a ' . get_class($list) . ' instead.', E_USER_WARNING); } $state = $this->getState($gridField); $checked = in_array($record->ID, $state->RelationVal->toArray()); $field = array('Checked' => $checked, 'Value' => $record->ID, 'Name' => $this->relationName($gridField)); if($list instanceof HasManyList) { $key = $record->{$this->cheatList->getForeignKey($list)}; if($key && !$checked) { $field['Disabled'] = true; } } $field = new ArrayData($field); return $field->renderWith('GridFieldManyRelationHandlerItem'); } public function getManipulatedData(GridField $gridField, SS_List $list) { if(!$list instanceof RelationList) { user_error('GridFieldManyRelationHandler requires the GridField to have a RelationList. Got a ' . get_class($list) . ' instead.', E_USER_WARNING); } $state = $this->getState($gridField); // We don't use setupState() as we need the list if($state->FirstTime) { $state->RelationVal = array_values($list->getIdList()) ?: array(); } if(!$state->ShowingRelation && $this->useToggle) { return $list; } $query = clone $list->dataQuery(); try { $query->removeFilterOn($this->cheatList->getForeignIDFilter($list)); } catch(InvalidArgumentException $e) { /* NOP */ } $orgList = $list; $list = new DataList($list->dataClass()); $list = $list->setDataQuery($query); if($orgList instanceof ManyManyList) { $joinTable = $this->cheatManyList->getJoinTable($orgList); $baseClass = ClassInfo::baseDataClass($list->dataClass()); $localKey = $this->cheatManyList->getLocalKey($orgList); $query->leftJoin($joinTable, "\"$joinTable\".\"$localKey\" = \"$baseClass\".\"ID\""); $list = $list->setDataQuery($query); } return $list; } protected function relationName($gridField) { return $gridField->getName() . get_class($gridField->getList()); } protected function cancelGridRelation(GridField $gridField, $arguments, $data) { parent::cancelGridRelation($gridField, $arguments, $data); $state = $this->getState($gridField); $state->RelationVal = array_values($gridField->getList()->getIdList()) ?: array(); } protected function saveGridRelation(GridField $gridField, $arguments, $data) { $state = $this->getState($gridField); $gridField->getList()->setByIdList($state->RelationVal->toArray()); parent::saveGridRelation($gridField, $arguments, $data); } } |