/**
  * Place the export button in a <p> tag below the field
  *
  * @param GridField $gridField
  * @return array
  */
 public function getHTMLFragments($gridField)
 {
     $button = new GridField_FormAction($gridField, 'export', _t('TableListField.CSVEXPORT', 'Export to CSV'), 'export', null);
     $button->setAttribute('data-icon', 'download-csv');
     $button->addExtraClass('no-ajax font-icon-down-circled action_export');
     $button->setForm($gridField->getForm());
     return array($this->targetFragment => '<p class="grid-csv-button">' . $button->Field() . '</p>');
 }
 /**
  * Place the print button in a <p> tag below the field
  *
  * @param GridField
  *
  * @return array
  */
 public function getHTMLFragments($gridField)
 {
     $button = new GridField_FormAction($gridField, 'print', _t('TableListField.Print', 'Print'), 'print', null);
     $button->setAttribute('data-icon', 'grid_print');
     $button->addExtraClass('gridfield-button-print');
     $button->addExtraClass('font-icon-print');
     return array($this->targetFragment => '<p class="grid-print-button">' . $button->Field() . '</p>');
 }
 /**
  *
  * @param GridField $gridField
  * @param DataObject $record
  * @param string $columnName
  * @return string the HTML for the column
  */
 public function getColumnContent($gridField, $record, $columnName)
 {
     if ($this->removeRelation) {
         if (!$record->canEdit()) {
             return null;
         }
         $field = GridField_FormAction::create($gridField, 'UnlinkRelation' . $record->ID, false, "unlinkrelation", array('RecordID' => $record->ID))->addExtraClass('btn btn--no-text btn--icon-md font-icon-link-broken grid-field__icon-action gridfield-button-unlink')->setAttribute('title', _t('GridAction.UnlinkRelation', "Unlink"));
     } else {
         if (!$record->canDelete()) {
             return null;
         }
         $field = GridField_FormAction::create($gridField, 'DeleteRecord' . $record->ID, false, "deleterecord", array('RecordID' => $record->ID))->addExtraClass('gridfield-button-delete btn--icon-md font-icon-trash-bin btn--no-text grid-field__icon-action')->setAttribute('title', _t('GridAction.Delete', "Delete"))->setDescription(_t('GridAction.DELETE_DESCRIPTION', 'Delete'));
     }
     return $field->Field();
 }
 /**
  *
  * @param GridField $gridField
  * @return string[] - HTML
  */
 public function getHTMLFragments($gridField)
 {
     $dataClass = $gridField->getModelClass();
     $forTemplate = new ArrayData(array());
     $forTemplate->Fields = new FieldList();
     $searchField = new TextField('gridfield_relationsearch', _t('GridField.RelationSearch', "Relation search"));
     $searchField->setAttribute('data-search-url', Controller::join_links($gridField->Link('search')));
     $searchField->setAttribute('placeholder', $this->getPlaceholderText($dataClass));
     $searchField->addExtraClass('relation-search no-change-track action_gridfield_relationsearch');
     $findAction = new GridField_FormAction($gridField, 'gridfield_relationfind', _t('GridField.Find', "Find"), 'find', 'find');
     $findAction->setAttribute('data-icon', 'relationfind');
     $findAction->addExtraClass('action_gridfield_relationfind');
     $addAction = new GridField_FormAction($gridField, 'gridfield_relationadd', _t('GridField.LinkExisting', "Link Existing"), 'addto', 'addto');
     $addAction->setAttribute('data-icon', 'chain--plus');
     $addAction->addExtraClass('action_gridfield_relationadd');
     // If an object is not found, disable the action
     if (!is_int($gridField->State->GridFieldAddRelation(null))) {
         $addAction->setReadonly(true);
     }
     $forTemplate->Fields->push($searchField);
     $forTemplate->Fields->push($findAction);
     $forTemplate->Fields->push($addAction);
     if ($form = $gridField->getForm()) {
         $forTemplate->Fields->setForm($form);
     }
     $template = SSViewer::get_templates_by_class($this, '', __CLASS__);
     return array($this->targetFragment => $forTemplate->renderWith($template));
 }
 /**
  * Returns the header row providing titles with sort buttons
  *
  * @param GridField $gridField
  * @return array
  */
 public function getHTMLFragments($gridField)
 {
     $list = $gridField->getList();
     if (!$this->checkDataType($list)) {
         return null;
     }
     /** @var Sortable $list */
     $forTemplate = new ArrayData(array());
     $forTemplate->Fields = new ArrayList();
     $state = $gridField->State->GridFieldSortableHeader;
     $columns = $gridField->getColumns();
     $currentColumn = 0;
     $schema = DataObject::getSchema();
     foreach ($columns as $columnField) {
         $currentColumn++;
         $metadata = $gridField->getColumnMetadata($columnField);
         $fieldName = str_replace('.', '-', $columnField);
         $title = $metadata['title'];
         if (isset($this->fieldSorting[$columnField]) && $this->fieldSorting[$columnField]) {
             $columnField = $this->fieldSorting[$columnField];
         }
         $allowSort = $title && $list->canSortBy($columnField);
         if (!$allowSort && strpos($columnField, '.') !== false) {
             // we have a relation column with dot notation
             // @see DataObject::relField for approximation
             $parts = explode('.', $columnField);
             $tmpItem = singleton($list->dataClass());
             for ($idx = 0; $idx < sizeof($parts); $idx++) {
                 $methodName = $parts[$idx];
                 if ($tmpItem instanceof SS_List) {
                     // It's impossible to sort on a HasManyList/ManyManyList
                     break;
                 } elseif (method_exists($tmpItem, 'hasMethod') && $tmpItem->hasMethod($methodName)) {
                     // The part is a relation name, so get the object/list from it
                     $tmpItem = $tmpItem->{$methodName}();
                 } elseif ($tmpItem instanceof DataObject && $schema->fieldSpec($tmpItem, $methodName, DataObjectSchema::DB_ONLY)) {
                     // Else, if we've found a database field at the end of the chain, we can sort on it.
                     // If a method is applied further to this field (E.g. 'Cost.Currency') then don't try to sort.
                     $allowSort = $idx === sizeof($parts) - 1;
                     break;
                 } else {
                     // If neither method nor field, then unable to sort
                     break;
                 }
             }
         }
         if ($allowSort) {
             $dir = 'asc';
             if ($state->SortColumn(null) == $columnField && $state->SortDirection('asc') == 'asc') {
                 $dir = 'desc';
             }
             $field = GridField_FormAction::create($gridField, 'SetOrder' . $fieldName, $title, "sort{$dir}", array('SortColumn' => $columnField))->addExtraClass('grid-field__sort');
             if ($state->SortColumn(null) == $columnField) {
                 $field->addExtraClass('ss-gridfield-sorted');
                 if ($state->SortDirection('asc') == 'asc') {
                     $field->addExtraClass('ss-gridfield-sorted-asc');
                 } else {
                     $field->addExtraClass('ss-gridfield-sorted-desc');
                 }
             }
         } else {
             if ($currentColumn == count($columns) && $gridField->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldFilterHeader')) {
                 $field = new LiteralField($fieldName, '<button type="button" name="showFilter" class="btn font-icon-search btn--no-text btn--icon-large grid-field__filter-open"></button>');
             } else {
                 $field = new LiteralField($fieldName, '<span class="non-sortable">' . $title . '</span>');
             }
         }
         $forTemplate->Fields->push($field);
     }
     $template = SSViewer::get_templates_by_class($this, '_Row', __CLASS__);
     return array('header' => $forTemplate->renderWith($template));
 }
 /**
  * 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));
     }
 }
 public function getHTMLFragments($gridField)
 {
     $list = $gridField->getList();
     if (!$this->checkDataType($list)) {
         return null;
     }
     /** @var Filterable $list */
     $forTemplate = new ArrayData(array());
     $forTemplate->Fields = new ArrayList();
     $columns = $gridField->getColumns();
     $filterArguments = $gridField->State->GridFieldFilterHeader->Columns->toArray();
     $currentColumn = 0;
     foreach ($columns as $columnField) {
         $currentColumn++;
         $metadata = $gridField->getColumnMetadata($columnField);
         $title = $metadata['title'];
         $fields = new FieldGroup();
         if ($title && $list->canFilterBy($columnField)) {
             $value = '';
             if (isset($filterArguments[$columnField])) {
                 $value = $filterArguments[$columnField];
             }
             $field = new TextField('filter[' . $gridField->getName() . '][' . $columnField . ']', '', $value);
             $field->addExtraClass('grid-field__sort-field');
             $field->addExtraClass('no-change-track');
             $field->setAttribute('placeholder', _t('GridField.FilterBy', "Filter by ") . _t('GridField.' . $metadata['title'], $metadata['title']));
             $fields->push($field);
             $fields->push(GridField_FormAction::create($gridField, 'reset', false, 'reset', null)->addExtraClass('btn font-icon-cancel btn--no-text ss-gridfield-button-reset')->setAttribute('title', _t('GridField.ResetFilter', "Reset"))->setAttribute('id', 'action_reset_' . $gridField->getModelClass() . '_' . $columnField));
         }
         if ($currentColumn == count($columns)) {
             $fields->push(GridField_FormAction::create($gridField, 'filter', false, 'filter', null)->addExtraClass('btn font-icon-search btn--no-text btn--icon-large grid-field__filter-submit ss-gridfield-button-filter')->setAttribute('title', _t('GridField.Filter', "Filter"))->setAttribute('id', 'action_filter_' . $gridField->getModelClass() . '_' . $columnField));
             $fields->push(GridField_FormAction::create($gridField, 'reset', false, 'reset', null)->addExtraClass('btn font-icon-cancel btn--no-text grid-field__filter-clear ss-gridfield-button-close')->setAttribute('title', _t('GridField.ResetFilter', "Reset"))->setAttribute('id', 'action_reset_' . $gridField->getModelClass() . '_' . $columnField));
             $fields->addExtraClass('filter-buttons');
             $fields->addExtraClass('no-change-track');
         }
         $forTemplate->Fields->push($fields);
     }
     $templates = SSViewer::get_templates_by_class($this, '_Row', __CLASS__);
     return array('header' => $forTemplate->renderWith($templates));
 }