Skip to content

Commit

Permalink
ENH GridFieldExportButton improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
3Dgoo committed Nov 7, 2024
1 parent d871f40 commit 3e90d5b
Showing 1 changed file with 82 additions and 3 deletions.
85 changes: 82 additions & 3 deletions src/Forms/GridField/GridFieldExportButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use LogicException;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\ArrayList;
Expand Down Expand Up @@ -42,6 +43,16 @@ class GridFieldExportButton extends AbstractGridFieldComponent implements GridFi
*/
protected $targetFragment;

/**
* Export file name
*/
protected $exportFileName = '[classname]-export-[timestamp].csv';

/**
* Export file name timestamp format
*/
protected $timeStampFormat = 'Y-m-d-H-i';

/**
* Set to true to disable XLS sanitisation
* [SS-2017-007] Ensure all cells with leading [@=+] have a leading tab
Expand All @@ -54,11 +65,26 @@ class GridFieldExportButton extends AbstractGridFieldComponent implements GridFi
/**
* @param string $targetFragment The HTML fragment to write the button into
* @param array $exportColumns The columns to include in the export
* @param string $exportFileName Export file name
* @param string $timeStampFormat Export file name timestamp format
*/
public function __construct($targetFragment = "after", $exportColumns = null)
public function __construct(
$targetFragment = 'after',
$exportColumns = null,
$exportFileName = null,
$timeStampFormat = null
)
{
$this->targetFragment = $targetFragment;
$this->exportColumns = $exportColumns;

if ($exportFileName) {
$this->exportFileName = $exportFileName;
}

if ($timeStampFormat) {
$this->timeStampFormat = $timeStampFormat;
}
}

/**
Expand Down Expand Up @@ -128,8 +154,7 @@ public function getURLHandlers($gridField)
*/
public function handleExport($gridField, $request = null)
{
$now = date("d-m-Y-H-i");
$fileName = "export-$now.csv";
$fileName = $this->getExportFileName($gridField);

if ($fileData = $this->generateExportFileData($gridField)) {
return HTTPRequest::send_file($fileData, $fileName, 'text/csv');
Expand Down Expand Up @@ -351,4 +376,58 @@ public function setCsvHasHeader($bool)
$this->csvHasHeader = $bool;
return $this;
}

/**
* @return string
*/
public function setExportFileName($exportFileName)
{
$this->exportFileName = $exportFileName;

return $this;
}

/**
* @param GridField $gridField
*
* @return string
*/
public function getExportFileName(GridField $gridField)
{
$className = strtolower(
preg_replace(
'/(?<!^)[A-Z]/',
'-$0',
ClassInfo::shortName(
$gridField->getModelClass()
)
)
);

$timeStamp = date($this->timeStampFormat);

return str_replace(
[
'[classname]',
'[timestamp]',
],
[
$className,
$timeStamp,
],
$this->exportFileName
);
}

/**
* @param string $timeStampFormat
*
* @return $this
*/
public function setTimeStampFormat($timeStampFormat)
{
$this->timeStampFormat = $timeStampFormat;

return $this;
}
}

0 comments on commit 3e90d5b

Please sign in to comment.