2012-01-30 07:49:10 +01:00
|
|
|
<?php
|
2012-01-30 16:48:15 +01:00
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage gridfield
|
|
|
|
*/
|
2012-01-30 07:49:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an "Export list" button to the bottom of a GridField.
|
|
|
|
*
|
|
|
|
* WARNING: This is experimental and its API is subject to change. Feel free to use it as long as you are happy of
|
|
|
|
* refactoring your code in the future.
|
|
|
|
*/
|
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(
|
2012-03-09 04:48:08 +01:00
|
|
|
$this->targetFragment => '<p>' . $button->Field() . '</p>',
|
2012-01-30 07:49:10 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2012-01-30 17:29:35 +01:00
|
|
|
$csvColumns = ($this->exportColumns) ? $this->exportColumns : $gridField->getDisplayFields();
|
2012-01-30 07:49:10 +01:00
|
|
|
$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;
|
|
|
|
}
|
2012-01-30 16:48:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getExportColumns() {
|
2012-01-30 17:29:35 +01:00
|
|
|
return $this->exportColumns;
|
2012-01-30 16:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
function setExportColumns($cols) {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function getCsvSeparator() {
|
|
|
|
return $this->csvSeparator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
function setCsvSeparator($separator) {
|
|
|
|
$this->csvSeparator = $separator;
|
2012-03-06 12:51:57 +01:00
|
|
|
return $this;
|
2012-01-30 16:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function getCsvHasHeader() {
|
|
|
|
return $this->csvHasHeader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param boolean
|
|
|
|
*/
|
|
|
|
function setCsvHasHeader($bool) {
|
|
|
|
$this->csvHasHeader = $bool;
|
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
|
|
|
}
|