Source of file GridFieldExcelExportButton.php
Size: 7,133 Bytes - Last Modified: 2021-12-24T06:47:15+00:00
/var/www/docs.ssmods.com/process/src/code/GridFieldExcelExportButton.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 | <?php /** * Enhanced GridField export button that allows the list to be exported to: * * Excel 2007, * * Excel 5, * * CSV * * The button appears has a Split button exposing the 3 possible export format. * * @author Firebrand <hello@firebrand.nz> * @license MIT * @package silverstripe-excel-export */ class GridFieldExcelExportButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler { /** * Whatever to override the default $useFieldLabelsAsHeaders value for the DataFormatter. * @var bool */ protected $useLabelsAsHeaders = null; /** * Fragment to write the button to */ protected $targetFragment; /** * Instanciate GridFieldExcelExportButton. * @param string $targetFragment */ public function __construct($targetFragment = "before") { $this->targetFragment = $targetFragment; } /** * @inheritdoc * * Create the split button with all the export options. * * @param GridField $gridField * @return array */ public function getHTMLFragments($gridField) { // Set up the split button $splitButton = new SplitButton('Export', 'Export'); $splitButton->setAttribute('data-icon', 'download-csv'); // XLSX option $button = new GridField_FormAction( $gridField, 'xlsxexport', _t('firebrandhq.EXCELEXPORT', 'Export to Excel (XLSX)'), 'xlsxexport', null ); $button->addExtraClass('no-ajax'); $splitButton->push($button); // XLS option $button = new GridField_FormAction( $gridField, 'xlsexport', _t('firebrandhq.EXCELEXPORT', 'Export to Excel (XLS)'), 'xlsexport', null ); $button->addExtraClass('no-ajax'); $splitButton->push($button); // CSV option $button = new GridField_FormAction( $gridField, 'csvexport', _t('firebrandhq.EXCELEXPORT', 'Export to CSV'), 'csvexport', null ); $button->addExtraClass('no-ajax'); $splitButton->push($button); // Return the fragment return array( $this->targetFragment => $splitButton->Field() ); } /** * @inheritdoc */ public function getActions($gridField) { return array('xlsxexport', 'xlsexport', 'csvexport'); } /** * @inheritdoc */ public function handleAction( GridField $gridField, $actionName, $arguments, $data ) { if ($actionName == 'xlsxexport') { return $this->handleXlsx($gridField); } if ($actionName == 'xlsexport') { return $this->handleXls($gridField); } if ($actionName == 'csvexport') { return $this->handleCsv($gridField); } } /** * @inheritdoc */ public function getURLHandlers($gridField) { return array( 'xlsxexport' => 'handleXlsx', 'xlsexport' => 'handleXls', 'csvexport' => 'handleCsv', ); } /** * Action to export the GridField list to an Excel 2007 file. * @param GridField $gridField * @param SS_HTTPRequest $request * @return string */ public function handleXlsx(GridField $gridField, $request = null) { return $this->genericHandle('ExcelDataFormatter', 'xlsx', $gridField, $request); } /** * Action to export the GridField list to an Excel 5 file. * @param GridField $gridField * @param SS_HTTPRequest $request * @return string */ public function handleXls(GridField $gridField, $request = null) { return $this->genericHandle('OldExcelDataFormatter', 'xls', $gridField, $request); } /** * Action to export the GridField list to an CSV file. * @param GridField $gridField * @param SS_HTTPRequest $request * @return string */ public function handleCsv(GridField $gridField, $request = null) { return $this->genericHandle('CsvDataFormatter', 'csv', $gridField, $request); } /** * Generic Handle request that will return a Spread Sheet in the requested format * @param string $dataFormatterClass * @param string $ext * @param GridField $gridField * @param SS_HTTPRequest $request * @return string */ protected function genericHandle($dataFormatterClass, $ext, GridField $gridField, $request = null) { $items = $this->getItems($gridField); $this->setHeader($gridField, $ext); $formater = new $dataFormatterClass(); $formater->setUseLabelsAsHeaders($this->useLabelsAsHeaders); $fileData = $formater->convertDataObjectSet($items); return $fileData; } /** * Set the HTTP header to force a download and set the filename. * @param GridField $gridField * @param string $ext Extension to use in the filename. */ protected function setHeader($gridField, $ext) { $do = singleton($gridField->getModelClass()); Controller::curr()->getResponse() ->addHeader( "Content-Disposition", 'attachment; filename="' . $do->i18n_plural_name() . '.' . $ext . '"' ); } /** * Helper function to extract the item list out of the GridField. * @param GridField $gridField * @return SS_list */ protected function getItems(GridField $gridField) { $gridField->getConfig()->removeComponentsByType('GridFieldPaginator'); $items = $gridField->getManipulatedList(); foreach ($gridField->getConfig()->getComponents() as $component) { if ($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { $items = $component->getManipulatedData($gridField, $items); } } $arrayList = new ArrayList(); foreach ($items->limit(null) as $item) { if (!$item->hasMethod('canView') || $item->canView()) { $arrayList->add($item); } } return $arrayList; } /** * 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; } } |