2012-01-30 07:49:10 +01:00
|
|
|
<?php
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2012-01-30 16:48:15 +01:00
|
|
|
/**
|
2013-05-20 12:18:07 +02:00
|
|
|
* Adds an "Export list" button to the bottom of a {@link GridField}.
|
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2012-01-30 16:48:15 +01:00
|
|
|
*/
|
2012-01-30 07:49:10 +01:00
|
|
|
|
2012-03-09 00:54:02 +01:00
|
|
|
class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler {
|
2012-01-30 16:48:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Map of a property name on the exported objects, with values being the column title in the CSV file.
|
|
|
|
* Note that titles are only used when {@link $csvHasHeader} is set to TRUE.
|
|
|
|
*/
|
2012-01-30 07:49:10 +01:00
|
|
|
protected $exportColumns;
|
2012-01-30 16:48:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-01-30 07:49:10 +01:00
|
|
|
protected $csvSeparator = ",";
|
2012-01-30 16:48:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2012-01-30 07:49:10 +01:00
|
|
|
protected $csvHasHeader = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 04:48:08 +01:00
|
|
|
/**
|
|
|
|
* Fragment to write the button to
|
|
|
|
*/
|
|
|
|
protected $targetFragment;
|
2012-01-30 07:49:10 +01:00
|
|
|
|
2012-01-30 16:48:15 +01:00
|
|
|
/**
|
2012-03-09 04:48:08 +01:00
|
|
|
* @param string $targetFragment The HTML fragment to write the button into
|
|
|
|
* @param array $exportColumns The columns to include in the export
|
2012-01-30 16:48:15 +01:00
|
|
|
*/
|
2012-03-09 04:48:08 +01:00
|
|
|
public function __construct($targetFragment = "after", $exportColumns = null) {
|
|
|
|
$this->targetFragment = $targetFragment;
|
2012-01-30 07:49:10 +01:00
|
|
|
$this->exportColumns = $exportColumns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Place the export button in a <p> tag below the field
|
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField) {
|
2012-03-06 12:51:57 +01:00
|
|
|
$button = new GridField_FormAction(
|
2014-08-15 08:53:05 +02:00
|
|
|
$gridField,
|
|
|
|
'export',
|
2012-01-30 16:48:15 +01:00
|
|
|
_t('TableListField.CSVEXPORT', 'Export to CSV'),
|
2014-08-15 08:53:05 +02:00
|
|
|
'export',
|
2012-01-30 16:48:15 +01:00
|
|
|
null
|
|
|
|
);
|
2012-03-07 03:28:00 +01:00
|
|
|
$button->setAttribute('data-icon', 'download-csv');
|
2012-03-06 13:36:33 +01:00
|
|
|
$button->addExtraClass('no-ajax');
|
2012-01-30 07:49:10 +01:00
|
|
|
return array(
|
2013-06-21 15:22:00 +02:00
|
|
|
$this->targetFragment => '<p class="grid-csv-button">' . $button->Field() . '</p>',
|
2012-01-30 07:49:10 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* export is an action button
|
|
|
|
*/
|
|
|
|
public function getActions($gridField) {
|
|
|
|
return array('export');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
2012-01-30 07:49:10 +01:00
|
|
|
if($actionName == 'export') {
|
|
|
|
return $this->handleExport($gridField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* it is also a URL
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getURLHandlers($gridField) {
|
2012-01-30 07:49:10 +01:00
|
|
|
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)){
|
2012-05-17 04:52:03 +02:00
|
|
|
return SS_HTTPRequest::send_file($fileData, $fileName, 'text/csv');
|
2012-01-30 07:49:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-19 23:48:14 +02:00
|
|
|
* Generate export fields for CSV.
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function generateExportFileData($gridField) {
|
2012-01-30 07:49:10 +01:00
|
|
|
$separator = $this->csvSeparator;
|
2012-09-26 23:34:00 +02:00
|
|
|
$csvColumns = ($this->exportColumns)
|
|
|
|
? $this->exportColumns
|
|
|
|
: singleton($gridField->getModelClass())->summaryFields();
|
2012-01-30 07:49:10 +01:00
|
|
|
$fileData = '';
|
2012-04-19 23:48:14 +02:00
|
|
|
|
2012-01-30 07:49:10 +01:00
|
|
|
if($this->csvHasHeader) {
|
2012-04-19 23:48:14 +02:00
|
|
|
$headers = array();
|
|
|
|
|
|
|
|
// determine the CSV headers. If a field is callable (e.g. anonymous function) then use the
|
|
|
|
// source name as the header instead
|
|
|
|
foreach($csvColumns as $columnSource => $columnHeader) {
|
|
|
|
$headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fileData .= "\"" . implode("\"{$separator}\"", array_values($headers)) . "\"";
|
2012-01-30 07:49:10 +01:00
|
|
|
$fileData .= "\n";
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-10-17 05:22:30 +02:00
|
|
|
//Remove GridFieldPaginator as we're going to export the entire list.
|
|
|
|
$gridField->getConfig()->removeComponentsByType('GridFieldPaginator');
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-03-04 22:27:15 +01:00
|
|
|
$items = $gridField->getManipulatedList();
|
2012-04-19 23:48:14 +02:00
|
|
|
|
|
|
|
// @todo should GridFieldComponents change behaviour based on whether others are available in the config?
|
2012-04-03 06:48:49 +02:00
|
|
|
foreach($gridField->getConfig()->getComponents() as $component){
|
|
|
|
if($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) {
|
|
|
|
$items = $component->getManipulatedData($gridField, $items);
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 23:48:14 +02:00
|
|
|
|
2013-08-05 09:59:12 +02:00
|
|
|
foreach($items->limit(null) as $item) {
|
2015-05-06 22:42:22 +02:00
|
|
|
if(!$item->hasMethod('canView') || $item->canView()) {
|
2015-02-03 04:21:37 +01:00
|
|
|
$columnData = array();
|
|
|
|
|
|
|
|
foreach($csvColumns as $columnSource => $columnHeader) {
|
|
|
|
if(!is_string($columnHeader) && is_callable($columnHeader)) {
|
|
|
|
if($item->hasMethod($columnSource)) {
|
|
|
|
$relObj = $item->{$columnSource}();
|
|
|
|
} else {
|
|
|
|
$relObj = $item->relObject($columnSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = $columnHeader($relObj);
|
2012-04-19 23:48:14 +02:00
|
|
|
} else {
|
2015-02-03 04:21:37 +01:00
|
|
|
$value = $gridField->getDataFieldValue($item, $columnSource);
|
2015-05-06 22:42:22 +02:00
|
|
|
|
|
|
|
if(!$value) {
|
|
|
|
$value = $gridField->getDataFieldValue($item, $columnHeader);
|
|
|
|
}
|
2012-04-19 23:48:14 +02:00
|
|
|
}
|
|
|
|
|
2015-02-03 04:21:37 +01:00
|
|
|
$value = str_replace(array("\r", "\n"), "\n", $value);
|
2015-02-13 11:10:52 +01:00
|
|
|
$columnData[] = '"' . str_replace('"', '""', $value) . '"';
|
2012-04-19 23:48:14 +02:00
|
|
|
}
|
2015-05-06 22:42:22 +02:00
|
|
|
|
2015-02-03 04:21:37 +01:00
|
|
|
$fileData .= implode($separator, $columnData);
|
|
|
|
$fileData .= "\n";
|
2012-01-30 07:49:10 +01:00
|
|
|
}
|
|
|
|
|
2015-05-06 22:42:22 +02:00
|
|
|
if($item->hasMethod('destroy')) {
|
2015-02-21 11:03:34 +01:00
|
|
|
$item->destroy();
|
|
|
|
}
|
2012-01-30 07:49:10 +01:00
|
|
|
}
|
2012-04-19 23:48:14 +02:00
|
|
|
|
2012-01-30 07:49:10 +01:00
|
|
|
return $fileData;
|
|
|
|
}
|
2012-01-30 16:48:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getExportColumns() {
|
2012-01-30 17:29:35 +01:00
|
|
|
return $this->exportColumns;
|
2012-01-30 16:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setExportColumns($cols) {
|
2012-01-30 16:48:15 +01:00
|
|
|
$this->exportColumns = $cols;
|
2012-03-06 12:51:57 +01:00
|
|
|
return $this;
|
2012-01-30 16:48:15 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-30 16:48:15 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCsvSeparator() {
|
2012-01-30 16:48:15 +01:00
|
|
|
return $this->csvSeparator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCsvSeparator($separator) {
|
2012-01-30 16:48:15 +01:00
|
|
|
$this->csvSeparator = $separator;
|
2012-03-06 12:51:57 +01:00
|
|
|
return $this;
|
2012-01-30 16:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCsvHasHeader() {
|
2012-01-30 16:48:15 +01:00
|
|
|
return $this->csvHasHeader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setCsvHasHeader($bool) {
|
2012-01-30 16:48:15 +01:00
|
|
|
$this->csvHasHeader = $bool;
|
2012-03-06 12:51:57 +01:00
|
|
|
return $this;
|
2012-01-30 16:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-25 08:54:38 +02:00
|
|
|
}
|