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}.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
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;
|
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(
|
2012-01-30 16:48:15 +01:00
|
|
|
$gridField,
|
|
|
|
'export',
|
|
|
|
_t('TableListField.CSVEXPORT', 'Export to CSV'),
|
|
|
|
'export',
|
|
|
|
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-14 13:50:50 +02:00
|
|
|
$this->targetFragment => '<p class="grid-bottom-button 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 = '';
|
|
|
|
$columnData = array();
|
|
|
|
$fieldItems = new ArrayList();
|
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";
|
|
|
|
}
|
|
|
|
|
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) {
|
2012-01-30 07:49:10 +01:00
|
|
|
$columnData = array();
|
2013-08-05 09:59:12 +02:00
|
|
|
|
2012-01-30 07:49:10 +01:00
|
|
|
foreach($csvColumns as $columnSource => $columnHeader) {
|
2012-04-19 23:48:14 +02:00
|
|
|
if(!is_string($columnHeader) && is_callable($columnHeader)) {
|
|
|
|
if($item->hasMethod($columnSource)) {
|
|
|
|
$relObj = $item->{$columnSource}();
|
|
|
|
} else {
|
|
|
|
$relObj = $item->relObject($columnSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = $columnHeader($relObj);
|
|
|
|
} else {
|
2012-05-29 00:06:30 +02:00
|
|
|
$value = $gridField->getDataFieldValue($item, $columnSource);
|
2012-04-19 23:48:14 +02:00
|
|
|
}
|
|
|
|
|
2012-01-30 07:49:10 +01:00
|
|
|
$value = str_replace(array("\r", "\n"), "\n", $value);
|
|
|
|
$columnData[] = '"' . str_replace('"', '\"', $value) . '"';
|
|
|
|
}
|
|
|
|
$fileData .= implode($separator, $columnData);
|
|
|
|
$fileData .= "\n";
|
|
|
|
|
|
|
|
$item->destroy();
|
|
|
|
}
|
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
|
|
|
}
|
2012-01-30 07:49:10 +01: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
|
|
|
}
|