Source of file GridFieldExcelExportAction.php
Size: 6,576 Bytes - Last Modified: 2021-12-24T06:47:15+00:00
/var/www/docs.ssmods.com/process/src/code/GridFieldExcelExportAction.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 | <?php /** * Gridfield component that can be added to a Gridfield to allow a user to export a single DataObject to Excel. * * Based of {@link GridFieldDeleteAction}. */ class GridFieldExcelExportAction implements GridField_ColumnProvider, GridField_ActionProvider { /** * The type of file we will be exporting * @var string */ protected $exportType; /** * Whatever to override the default $useFieldLabelsAsHeaders value for the DataFormatter. * @var bool */ protected $useLabelsAsHeaders = null; /** * Instanciate a new GridFieldExcelExportAction * @param string $exportType The type of file we will be exporting. Defaults to 'xlsx', but 'csv' and 'xls' are also * acceptable. */ public function __construct($exportType = 'xlsx') { $this->exportType = $exportType; } /** * Add a column at the end of the grid field if need be * * @param GridField $gridField * @param array $columns */ public function augmentColumns($gridField, &$columns) { if(!in_array('Actions', $columns)) { $columns[] = 'Actions'; } } /** * Return any special attributes that will be used for FormField::create_tag() * * @param GridField $gridField * @param DataObject $record * @param string $columnName * @return array */ public function getColumnAttributes($gridField, $record, $columnName) { return array('class' => 'col-buttons'); } /** * Add the title * * @param GridField $gridField * @param string $columnName * @return array */ public function getColumnMetadata($gridField, $columnName) { if($columnName == 'Actions') { return array('title' => ''); } } /** * Which columns are handled by this component * * @param GridField $gridField * @return array */ public function getColumnsHandled($gridField) { return array('Actions'); } /** * Which GridField actions are this component handling * * @param GridField $gridField * @return array */ public function getActions($gridField) { return array('exportsingle'); } /** * Return the button to show at the end of the row * @param GridField $gridField * @param DataObject $record * @param string $columnName * @return string - the HTML for the column */ public function getColumnContent($gridField, $record, $columnName) { if(!$record->canView()) return; $field = GridField_FormAction::create($gridField, 'ExportSingle'.$record->ID, false, "exportsingle", array('RecordID' => $record->ID)) ->addExtraClass('gridfield-button-export-single no-ajax') ->setAttribute('title', _t('firebrandhq.EXCELEXPORT', "Export")) ->setAttribute('data-icon', 'download-csv'); return $field->Field(); } /** * Handle the actions and apply any changes to the GridField * * @param GridField $gridField * @param string $actionName * @param mixed $arguments * @param array $data - form data * @return void */ public function handleAction(GridField $gridField, $actionName, $arguments, $data) { if($actionName == 'exportsingle') { // Get the item $item = $gridField->getList()->byID($arguments['RecordID']); if(!$item) { return; } // Make sure th current user is authorised to view the item. if (!$item->canView()) { throw new ValidationException( _t('firebrandhq.EXCELEXPORT', "Can not view record"),0); } // Build a filename $filename = $item->i18n_singular_name() . ' - ID ' . $item->ID; $title = $item->getTitle(); if ($title) { $filename .= ' - ' . $title; } // Pick Converter switch ($this->exportType) { case 'xlsx': $formater = new ExcelDataFormatter(); break; case 'xls': $formater = new OldExcelDataFormatter(); break; case 'csv': $formater = new CsvDataFormatter(); break; default: user_error( "GridFieldExcelExportAction expects \$exportType to be either 'xlsx', 'xls' or 'csv'. " . "'{$this->exportType}' provided", E_USER_ERROR ); } // Set the header that will cause the browser to download and save the file. $this->setHeader($gridField, $this->exportType, $filename); // Export our Data Object $formater->setUseLabelsAsHeaders($this->useLabelsAsHeaders); $fileData = $formater->convertDataObject($item); return $fileData; } } /** * Helper function for building the right header to get the file downloaded. * @param GridField $gridField * @param string $ext Extension the file should have * @param string $filename Optional of the file (without the extension). Defaults to the grid field object type. */ protected function setHeader($gridField, $ext, $filename = '') { $do = singleton($gridField->getModelClass()); if (!$filename) { $filename = $do->i18n_plural_name(); } Controller::curr()->getResponse() ->addHeader( "Content-Disposition", 'attachment; filename="' . $filename . '.' . $ext . '"' ); } /** * Set the DataFormatter's UseFieldLabelsAsHeaders property * @param bool $value * @return GridFieldExcelExportButton */ public function setUseLabelsAsHeaders($value) { if ($value === null) { $this->useLabelsAsHeaders = null; } else { $this->useLabelsAsHeaders = (bool)$value; } return $this; } /** * Return the value that will be assigned to the DataFormatter's UseFieldLabelsAsHeaders property * * If null, will fallback on the default. * * @return bool|null */ public function getUseLabelsAsHeaders() { return $this->useLabelsAsHeaders; } } |