From 2d898cab63586caa5d06525658f50d2a61e332c5 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Mon, 30 Jan 2012 19:49:10 +1300 Subject: [PATCH] API CHANGE: Added GridFieldExporter, a GridField component that adds export functionality, and added it to the security admin. --- admin/code/SecurityAdmin.php | 1 + forms/gridfield/GridFieldExporter.php | 95 +++++++++++++++++++++++++++ security/Group.php | 1 + 3 files changed, 97 insertions(+) create mode 100644 forms/gridfield/GridFieldExporter.php diff --git a/admin/code/SecurityAdmin.php b/admin/code/SecurityAdmin.php index 9dd539dd4..b7332a8bc 100755 --- a/admin/code/SecurityAdmin.php +++ b/admin/code/SecurityAdmin.php @@ -108,6 +108,7 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider { function RootForm() { $config = new GridFieldConfig_Base(25); $config->addComponent(new GridFieldPopupForms($this, 'RootForm')); + $config->addComponent(new GridFieldExporter()); $memberList = new GridField('Members', 'All members', DataList::create('Member'), $config); $fields = new FieldList( diff --git a/forms/gridfield/GridFieldExporter.php b/forms/gridfield/GridFieldExporter.php new file mode 100644 index 000000000..47cde9c94 --- /dev/null +++ b/forms/gridfield/GridFieldExporter.php @@ -0,0 +1,95 @@ +exportColumns = $exportColumns; + } + + /** + * Place the export button in a

tag below the field + */ + public function getHTMLFragments($gridField) { + $button = new GridField_Action($gridField, 'export', 'Export to CSV', 'export', null); + return array( + 'after' => '

' . htmlentities($button->Field()) . '

', + ); + } + + /** + * 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($items as $item) { + $columnData = array(); + foreach($csvColumns as $columnSource => $columnHeader) { + $value = $item->$columnSource; + $value = str_replace(array("\r", "\n"), "\n", $value); + $columnData[] = '"' . str_replace('"', '\"', $value) . '"'; + } + $fileData .= implode($separator, $columnData); + $fileData .= "\n"; + + $item->destroy(); + } + + return $fileData; + } + +} \ No newline at end of file diff --git a/security/Group.php b/security/Group.php index 16980747e..90e63d340 100755 --- a/security/Group.php +++ b/security/Group.php @@ -64,6 +64,7 @@ class Group extends DataObject { $config = new GridFieldConfig_ManyManyEditor('FirstName', false, 20); $config->addComponent(new GridFieldPopupForms(Controller::curr(), 'EditForm')); + $config->addComponent(new GridFieldExporter()); $memberList = new GridField('Members','Members', $this->Members(), $config); // @todo Implement permission checking on GridField