silverstripe-framework/src/Forms/GridField/GridFieldPrintButton.php

302 lines
7.4 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Forms\GridField;
2016-09-09 08:43:05 +02:00
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Convert;
2017-06-02 06:39:14 +02:00
use SilverStripe\Core\Extensible;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;
2016-06-23 01:37:22 +02:00
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData;
2017-09-13 01:48:42 +02:00
use SilverStripe\View\Requirements;
/**
* Adds an "Print" button to the bottom or top of a GridField.
*/
class GridFieldPrintButton extends AbstractGridFieldComponent implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler
2016-11-29 00:31:16 +01:00
{
2017-06-02 06:39:14 +02:00
use Extensible;
2016-11-29 00:31:16 +01:00
/**
* @var array Map of a property name on the printed objects, with values
* being the column title in the CSV file.
*
* Note that titles are only used when {@link $csvHasHeader} is set to TRUE
*/
protected $printColumns;
/**
* @var boolean
*/
protected $printHasHeader = true;
/**
* Fragment to write the button to.
*
* @var string
*/
protected $targetFragment;
/**
* @param string $targetFragment The HTML fragment to write the button into
* @param array $printColumns The columns to include in the print view
*/
public function __construct($targetFragment = "after", $printColumns = null)
{
$this->targetFragment = $targetFragment;
$this->printColumns = $printColumns;
}
/**
* Place the print button in a <p> tag below the field
*
2020-12-21 22:23:23 +01:00
* @param GridField $gridField
2016-11-29 00:31:16 +01:00
*
* @return array
*/
public function getHTMLFragments($gridField)
{
$button = new GridField_FormAction(
$gridField,
'print',
2017-04-20 03:59:51 +02:00
_t('SilverStripe\\Forms\\GridField\\GridField.Print', 'Print'),
2016-11-29 00:31:16 +01:00
'print',
null
);
2017-09-13 01:48:42 +02:00
$button->setForm($gridField->getForm());
2016-11-29 00:31:16 +01:00
2017-09-13 01:48:42 +02:00
$button->addExtraClass('font-icon-print grid-print-button btn btn-secondary');
2016-11-29 00:31:16 +01:00
return [
2017-09-05 05:08:05 +02:00
$this->targetFragment => $button->Field(),
];
2016-11-29 00:31:16 +01:00
}
/**
* Print is an action button.
*
2020-12-21 22:23:23 +01:00
* @param GridField $gridField
2016-11-29 00:31:16 +01:00
*
* @return array
*/
public function getActions($gridField)
{
return ['print'];
2016-11-29 00:31:16 +01:00
}
/**
* Handle the print action.
*
* @param GridField $gridField
* @param string $actionName
* @param array $arguments
* @param array $data
* @return DBHTMLText
*/
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
if ($actionName == 'print') {
return $this->handlePrint($gridField);
}
}
/**
* Print is accessible via the url
*
2020-12-21 22:23:23 +01:00
* @param GridField $gridField
2016-11-29 00:31:16 +01:00
* @return array
*/
public function getURLHandlers($gridField)
{
return [
2016-11-29 00:31:16 +01:00
'print' => 'handlePrint',
];
2016-11-29 00:31:16 +01:00
}
/**
* Handle the print, for both the action button and the URL
*
* @param GridField $gridField
* @param HTTPRequest $request
* @return DBHTMLText
*/
public function handlePrint($gridField, $request = null)
{
set_time_limit(60);
Requirements::clear();
2017-06-02 06:39:14 +02:00
$data = $this->generatePrintData($gridField);
$this->extend('updatePrintData', $data);
if ($data) {
return $data->renderWith([
get_class($gridField) . '_print',
GridField::class . '_print',
]);
2016-11-29 00:31:16 +01:00
}
return null;
}
/**
* Return the columns to print
*
2020-12-21 22:23:23 +01:00
* @param GridField $gridField
2016-11-29 00:31:16 +01:00
*
* @return array
*/
protected function getPrintColumnsForGridField(GridField $gridField)
{
if ($this->printColumns) {
return $this->printColumns;
}
/** @var GridFieldDataColumns $dataCols */
$dataCols = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class);
2016-11-29 00:31:16 +01:00
if ($dataCols) {
return $dataCols->getDisplayFields($gridField);
}
return DataObject::singleton($gridField->getModelClass())->summaryFields();
}
/**
* Return the title of the printed page
*
2020-12-21 22:23:23 +01:00
* @param GridField $gridField
2016-11-29 00:31:16 +01:00
*
* @return array
*/
public function getTitle(GridField $gridField)
{
$form = $gridField->getForm();
$currentController = $gridField->getForm()->getController();
$title = '';
if (method_exists($currentController, 'Title')) {
$title = $currentController->Title();
} else {
if ($currentController->Title) {
$title = $currentController->Title;
} elseif ($form->getName()) {
$title = $form->getName();
}
}
if ($fieldTitle = $gridField->Title()) {
if ($title) {
$title .= " - ";
}
$title .= $fieldTitle;
}
return $title;
}
/**
* Export core.
*
* @param GridField $gridField
* @return ArrayData
*/
public function generatePrintData(GridField $gridField)
{
$printColumns = $this->getPrintColumnsForGridField($gridField);
$header = null;
if ($this->printHasHeader) {
$header = new ArrayList();
foreach ($printColumns as $field => $label) {
$header->push(new ArrayData([
2016-11-29 00:31:16 +01:00
"CellString" => $label,
]));
2016-11-29 00:31:16 +01:00
}
}
$items = $gridField->getManipulatedList();
$itemRows = new ArrayList();
/** @var GridFieldDataColumns $gridFieldColumnsComponent */
$gridFieldColumnsComponent = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class);
2016-11-29 00:31:16 +01:00
/** @var DataObject $item */
foreach ($items->limit(null) as $item) {
if (!$item->hasMethod('canView') || $item->canView()) {
$itemRow = new ArrayList();
2016-11-29 00:31:16 +01:00
foreach ($printColumns as $field => $label) {
$value = $gridFieldColumnsComponent
? strip_tags($gridFieldColumnsComponent->getColumnContent($gridField, $item, $field))
: $gridField->getDataFieldValue($item, $field);
$itemRow->push(new ArrayData([
"CellString" => $value,
]));
}
2016-11-29 00:31:16 +01:00
$itemRows->push(new ArrayData([
"ItemRow" => $itemRow
]));
2016-11-29 00:31:16 +01:00
}
if ($item->hasMethod('destroy')) {
$item->destroy();
}
}
$ret = new ArrayData([
2016-11-29 00:31:16 +01:00
"Title" => $this->getTitle($gridField),
"Header" => $header,
"ItemRows" => $itemRows,
"Datetime" => DBDatetime::now(),
"Member" => Security::getCurrentUser(),
]);
2016-11-29 00:31:16 +01:00
return $ret;
}
/**
* @return array
*/
public function getPrintColumns()
{
return $this->printColumns;
}
/**
* @param array $cols
* @return $this
*/
public function setPrintColumns($cols)
{
$this->printColumns = $cols;
return $this;
}
/**
* @return boolean
*/
public function getPrintHasHeader()
{
return $this->printHasHeader;
}
/**
* @param bool $bool
* @return $this
*/
public function setPrintHasHeader($bool)
{
$this->printHasHeader = $bool;
return $this;
}
}