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
|
|
|
|
2017-11-29 03:27:36 +01:00
|
|
|
/**
|
|
|
|
* Set to true to disable XLS sanitisation
|
|
|
|
* [SS-2017-007] Ensure all cells with leading [@=+] have a leading tab
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $xls_export_disabled = false;
|
|
|
|
|
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');
|
2016-05-02 07:37:03 +02:00
|
|
|
$button->addExtraClass('no-ajax action_export');
|
|
|
|
$button->setForm($gridField->getForm());
|
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
|
|
|
}
|
|
|
|
}
|
2017-11-29 03:27:36 +01:00
|
|
|
|
2016-07-18 22:26:07 +02:00
|
|
|
/**
|
|
|
|
* Return the columns to export
|
2017-11-29 03:27:36 +01:00
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
*
|
2016-07-18 22:26:07 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getExportColumnsForGridField(GridField $gridField) {
|
|
|
|
if($this->exportColumns) {
|
|
|
|
$exportColumns = $this->exportColumns;
|
|
|
|
} else if($dataCols = $gridField->getConfig()->getComponentByType('GridFieldDataColumns')) {
|
|
|
|
$exportColumns = $dataCols->getDisplayFields($gridField);
|
|
|
|
} else {
|
|
|
|
$exportColumns = singleton($gridField->getModelClass())->summaryFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $exportColumns;
|
|
|
|
}
|
2012-01-30 07:49:10 +01:00
|
|
|
|
|
|
|
/**
|
2012-04-19 23:48:14 +02:00
|
|
|
* Generate export fields for CSV.
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
2015-07-27 03:38:11 +02:00
|
|
|
* @return string
|
2012-04-19 23:48:14 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function generateExportFileData($gridField) {
|
2015-07-27 03:38:11 +02:00
|
|
|
$separator = $this->getCsvSeparator();
|
2016-07-18 22:26:07 +02:00
|
|
|
$csvColumns = $this->getExportColumnsForGridField($gridField);
|
2015-07-27 03:38:11 +02:00
|
|
|
$fileData = array();
|
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
|
2017-02-15 23:20:00 +01:00
|
|
|
|
2012-04-19 23:48:14 +02:00
|
|
|
foreach($csvColumns as $columnSource => $columnHeader) {
|
2017-02-15 23:20:00 +01:00
|
|
|
if (is_array($columnHeader) && array_key_exists('title', $columnHeader)) {
|
|
|
|
$headers[] = $columnHeader['title'];
|
|
|
|
} else {
|
|
|
|
$headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader;
|
|
|
|
}
|
2012-04-19 23:48:14 +02:00
|
|
|
}
|
|
|
|
|
2015-07-27 03:38:11 +02:00
|
|
|
$fileData[] = $headers;
|
2012-01-30 07:49:10 +01:00
|
|
|
}
|
2016-05-02 07:37:03 +02: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-05-02 07:37:03 +02: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
|
|
|
|
2016-05-16 01:38:15 +02:00
|
|
|
if($value === null) {
|
2015-05-06 22:42:22 +02:00
|
|
|
$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);
|
2017-11-29 03:27:36 +01:00
|
|
|
|
|
|
|
// [SS-2017-007] Sanitise XLS executable column values with a leading tab
|
|
|
|
if (!Config::inst()->get(get_class($this), 'xls_export_disabled')
|
|
|
|
&& preg_match('/^[-@=+].*/', $value)
|
|
|
|
) {
|
|
|
|
$value = "\t" . $value;
|
|
|
|
}
|
2015-07-27 03:38:11 +02:00
|
|
|
$columnData[] = $value;
|
2012-04-19 23:48:14 +02:00
|
|
|
}
|
2015-05-06 22:42:22 +02:00
|
|
|
|
2015-07-27 03:38:11 +02:00
|
|
|
$fileData[] = $columnData;
|
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
|
|
|
|
2015-07-27 03:38:11 +02:00
|
|
|
// Convert the $fileData array into csv by capturing fputcsv's output
|
|
|
|
$csv = fopen('php://temp', 'r+');
|
|
|
|
foreach($fileData as $line) {
|
|
|
|
fputcsv($csv, $line, $separator);
|
|
|
|
}
|
|
|
|
rewind($csv);
|
|
|
|
return stream_get_contents($csv);
|
2012-01-30 07:49:10 +01:00
|
|
|
}
|
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
|
|
|
}
|