targetFragment = $targetFragment; $this->exportColumns = $exportColumns; } /** * Place the export button in a
tag below the field */ 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'); return array( $this->targetFragment => '
', ); } /** * export is an action button */ public function getActions($gridField) { return array('export'); } function handleAction(GridField $gridField, $actionName, $arguments, $data) { if($actionName == 'export') { return $this->handleExport($gridField); } } /** * it is also a URL */ function getURLHandlers($gridField) { return array( 'export' => 'handleExport', ); } /** * Handle the export, for both the action button and the URL */ public function handleExport($gridField, $request = null) { $now = Date("d-m-Y-H-i"); $fileName = "export-$now.csv"; if($fileData = $this->generateExportFileData($gridField)){ return SS_HTTPRequest::send_file($fileData, $fileName); } } /** * Export core. */ function generateExportFileData($gridField) { $separator = $this->csvSeparator; $csvColumns = ($this->exportColumns) ? $this->exportColumns : $gridField->getDisplayFields(); $fileData = ''; $columnData = array(); $fieldItems = new ArrayList(); if($this->csvHasHeader) { $fileData .= "\"" . implode("\"{$separator}\"", array_values($csvColumns)) . "\""; $fileData .= "\n"; } $items = $gridField->getList(); foreach($gridField->getConfig()->getComponents() as $component){ if($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { $items = $component->getManipulatedData($gridField, $items); } } foreach($items as $item) { $columnData = array(); foreach($csvColumns as $columnSource => $columnHeader) { $value = $item->relField($columnSource); $value = str_replace(array("\r", "\n"), "\n", $value); $columnData[] = '"' . str_replace('"', '\"', $value) . '"'; } $fileData .= implode($separator, $columnData); $fileData .= "\n"; $item->destroy(); } return $fileData; } /** * @return array */ function getExportColumns() { return $this->exportColumns; } /** * @param array */ function setExportColumns($cols) { $this->exportColumns = $cols; return $this; } /** * @return string */ function getCsvSeparator() { return $this->csvSeparator; } /** * @param string */ function setCsvSeparator($separator) { $this->csvSeparator = $separator; return $this; } /** * @return boolean */ function getCsvHasHeader() { return $this->csvHasHeader; } /** * @param boolean */ function setCsvHasHeader($bool) { $this->csvHasHeader = $bool; return $this; } }