public function handleAction(GridField $gridField, $actionName, $arguments, $data) { if (!$this->checkDataType($gridField->getList())) { return; } $state = $gridField->State->GridFieldFilterHeader; if ($actionName === 'filter') { if (isset($data['filter'][$gridField->getName()])) { foreach ($data['filter'][$gridField->getName()] as $key => $filter) { $state->Columns->{$key} = $filter; } } } elseif ($actionName === 'reset') { $state->Columns = null; } }
/** * Loads the given form data into the underlying dataobject and relation * * @param array $data * @param Form $form * @throws ValidationException On error * @return DataObject Saved record */ protected function saveFormIntoRecord($data, $form) { $list = $this->gridField->getList(); // Check object matches the correct classname if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) { $newClassName = $data['ClassName']; // The records originally saved attribute was overwritten by $form->saveInto($record) before. // This is necessary for newClassInstance() to work as expected, and trigger change detection // on the ClassName attribute $this->record->setClassName($this->record->ClassName); // Replace $record with a new instance $this->record = $this->record->newClassInstance($newClassName); } // Save form and any extra saved data into this dataobject $form->saveInto($this->record); $this->record->write(); $extraData = $this->getExtraSavedData($this->record, $list); $list->add($this->record, $extraData); return $this->record; }
/** * Handle the actions and apply any changes to the GridField * * @param GridField $gridField * @param string $actionName * @param mixed $arguments * @param array $data - form data * @throws ValidationException */ public function handleAction(GridField $gridField, $actionName, $arguments, $data) { if ($actionName == 'deleterecord' || $actionName == 'unlinkrelation') { /** @var DataObject $item */ $item = $gridField->getList()->byID($arguments['RecordID']); if (!$item) { return; } if ($actionName == 'deleterecord') { if (!$item->canDelete()) { throw new ValidationException(_t('GridFieldAction_Delete.DeletePermissionsFailure', "No delete permissions"), 0); } $item->delete(); } else { if (!$item->canEdit()) { throw new ValidationException(_t('GridFieldAction_Delete.EditPermissionsFailure', "No permission to unlink record"), 0); } $gridField->getList()->remove($item); } } }
/** * Returns a json array of a search results that can be used by for example Jquery.ui.autosuggestion * * @param GridField $gridField * @param HTTPRequest $request * @return string */ public function doSearch($gridField, $request) { $dataClass = $gridField->getModelClass(); $allList = $this->searchList ? $this->searchList : DataList::create($dataClass); $searchFields = $this->getSearchFields() ? $this->getSearchFields() : $this->scaffoldSearchFields($dataClass); if (!$searchFields) { throw new LogicException(sprintf('GridFieldAddExistingAutocompleter: No searchable fields could be found for class "%s"', $dataClass)); } $params = array(); foreach ($searchFields as $searchField) { $name = strpos($searchField, ':') !== FALSE ? $searchField : "{$searchField}:StartsWith"; $params[$name] = $request->getVar('gridfield_relationsearch'); } $results = $allList->subtract($gridField->getList())->filterAny($params)->sort(strtok($searchFields[0], ':'), 'ASC')->limit($this->getResultsLimit()); $json = array(); Config::nest(); SSViewer::config()->update('source_file_comments', false); $viewer = SSViewer::fromString($this->resultsFormat); foreach ($results as $result) { $title = html_entity_decode($viewer->process($result)); $json[] = array('label' => $title, 'value' => $title, 'id' => $result->ID); } Config::unnest(); return Convert::array2json($json); }
public function handleAction(GridField $gridField, $actionName, $arguments, $data) { if (!$this->checkDataType($gridField->getList())) { return; } $state = $gridField->State->GridFieldSortableHeader; switch ($actionName) { case 'sortasc': $state->SortColumn = $arguments['SortColumn']; $state->SortDirection = 'asc'; break; case 'sortdesc': $state->SortColumn = $arguments['SortColumn']; $state->SortDirection = 'desc'; break; } }
/** * @return DataList */ public function getList() { return $this->grid->getList(); }
/** * Determines arguments to be passed to the template for building this field * * @param GridField $gridField * @return ArrayData If paging is available this will be an ArrayData * object of paging details with these parameters: * <ul> * <li>OnlyOnePage: boolean - Is there only one page?</li> * <li>FirstShownRecord: integer - Number of the first record displayed</li> * <li>LastShownRecord: integer - Number of the last record displayed</li> * <li>NumRecords: integer - Total number of records</li> * <li>NumPages: integer - The number of pages</li> * <li>CurrentPageNum (optional): integer - If OnlyOnePage is false, the number of the current page</li> * <li>FirstPage (optional): GridField_FormAction - Button to go to the first page</li> * <li>PreviousPage (optional): GridField_FormAction - Button to go to the previous page</li> * <li>NextPage (optional): GridField_FormAction - Button to go to the next page</li> * <li>LastPage (optional): GridField_FormAction - Button to go to last page</li> * </ul> */ public function getTemplateParameters(GridField $gridField) { if (!$this->checkDataType($gridField->getList())) { return null; } $state = $this->getGridPagerState($gridField); // Figure out which page and record range we're on $totalRows = $this->totalItems; if (!$totalRows) { return null; } $totalPages = (int) ceil($totalRows / $this->itemsPerPage); if ($totalPages == 0) { $totalPages = 1; } $firstShownRecord = ($state->currentPage - 1) * $this->itemsPerPage + 1; if ($firstShownRecord > $totalRows) { $firstShownRecord = $totalRows; } $lastShownRecord = $state->currentPage * $this->itemsPerPage; if ($lastShownRecord > $totalRows) { $lastShownRecord = $totalRows; } // If there is only 1 page for all the records in list, we don't need to go further // to sort out those first page, last page, pre and next pages, etc // we are not render those in to the paginator. if ($totalPages === 1) { return new ArrayData(array('OnlyOnePage' => true, 'FirstShownRecord' => $firstShownRecord, 'LastShownRecord' => $lastShownRecord, 'NumRecords' => $totalRows, 'NumPages' => $totalPages)); } else { // First page button $firstPage = new GridField_FormAction($gridField, 'pagination_first', 'First', 'paginate', 1); $firstPage->addExtraClass('btn btn-secondary btn--no-text font-icon-angle-double-left ss-gridfield-firstpage'); if ($state->currentPage == 1) { $firstPage = $firstPage->performDisabledTransformation(); } // Previous page button $previousPageNum = $state->currentPage <= 1 ? 1 : $state->currentPage - 1; $previousPage = new GridField_FormAction($gridField, 'pagination_prev', 'Previous', 'paginate', $previousPageNum); $previousPage->addExtraClass('btn btn-secondary btn--no-text font-icon-angle-left ss-gridfield-previouspage'); if ($state->currentPage == 1) { $previousPage = $previousPage->performDisabledTransformation(); } // Next page button $nextPageNum = $state->currentPage >= $totalPages ? $totalPages : $state->currentPage + 1; $nextPage = new GridField_FormAction($gridField, 'pagination_next', 'Next', 'paginate', $nextPageNum); $nextPage->addExtraClass('btn btn-secondary btn--no-text font-icon-angle-right ss-gridfield-nextpage'); if ($state->currentPage == $totalPages) { $nextPage = $nextPage->performDisabledTransformation(); } // Last page button $lastPage = new GridField_FormAction($gridField, 'pagination_last', 'Last', 'paginate', $totalPages); $lastPage->addExtraClass('btn btn-secondary btn--no-text font-icon-angle-double-right ss-gridfield-lastpage'); if ($state->currentPage == $totalPages) { $lastPage = $lastPage->performDisabledTransformation(); } // Render in template return new ArrayData(array('OnlyOnePage' => false, 'FirstPage' => $firstPage, 'PreviousPage' => $previousPage, 'CurrentPageNum' => $state->currentPage, 'NumPages' => $totalPages, 'NextPage' => $nextPage, 'LastPage' => $lastPage, 'FirstShownRecord' => $firstShownRecord, 'LastShownRecord' => $lastShownRecord, 'NumRecords' => $totalRows)); } }
/** * @covers SilverStripe\Forms\GridField\GridField::setList * @covers SilverStripe\Forms\GridField\GridField::getList */ public function testSetAndGetList() { $list = Member::get(); $arrayList = ArrayList::create(array(1, 2, 3)); $obj = new GridField('testfield', 'testfield', $list); $this->assertEquals($list, $obj->getList()); $obj->setList($arrayList); $this->assertEquals($arrayList, $obj->getList()); }
/** * * @param GridField $gridField * @param HTTPRequest $request * @return GridFieldDetailForm_ItemRequest */ public function handleItem($gridField, $request) { // Our getController could either give us a true Controller, if this is the top-level GridField. // It could also give us a RequestHandler in the form of GridFieldDetailForm_ItemRequest if this is a // nested GridField. $requestHandler = $gridField->getForm()->getController(); /** @var DataObject $record */ if (is_numeric($request->param('ID'))) { $record = $gridField->getList()->byID($request->param("ID")); } else { $record = Object::create($gridField->getModelClass()); } $handler = $this->getItemRequestHandler($gridField, $record, $requestHandler); // if no validator has been set on the GridField and the record has a // CMS validator, use that. if (!$this->getValidator() && (method_exists($record, 'getCMSValidator') || $record instanceof Object && $record->hasMethod('getCMSValidator'))) { $this->setValidator($record->getCMSValidator()); } return $handler->handleRequest($request, DataModel::inst()); }