2012-04-03 06:56:43 +02:00
|
|
|
<?php
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms\GridField;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\SS_HTTPRequest;
|
|
|
|
use SilverStripe\Core\Convert;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
|
|
|
use SilverStripe\Security\Member;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
use SilverStripe\View\ArrayData;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
/**
|
2013-03-04 22:29:27 +01:00
|
|
|
* Adds an "Print" button to the bottom or top of a GridField.
|
2012-04-03 06:56:43 +02:00
|
|
|
*/
|
|
|
|
class GridFieldPrintButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler {
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @var array Map of a property name on the printed objects, with values
|
2013-03-04 22:29:27 +01:00
|
|
|
* being the column title in the CSV file.
|
|
|
|
*
|
|
|
|
* Note that titles are only used when {@link $csvHasHeader} is set to TRUE
|
2012-04-03 06:56:43 +02:00
|
|
|
*/
|
|
|
|
protected $printColumns;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $printHasHeader = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
/**
|
2013-03-04 22:29:27 +01:00
|
|
|
* Fragment to write the button to.
|
|
|
|
*
|
|
|
|
* @var string
|
2012-04-03 06:56:43 +02:00
|
|
|
*/
|
|
|
|
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
|
2013-03-04 22:29:27 +01:00
|
|
|
*
|
|
|
|
* @param GridField
|
|
|
|
*
|
|
|
|
* @return array
|
2012-04-03 06:56:43 +02:00
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField) {
|
|
|
|
$button = new GridField_FormAction(
|
2014-08-15 08:53:05 +02:00
|
|
|
$gridField,
|
|
|
|
'print',
|
2012-04-03 06:56:43 +02:00
|
|
|
_t('TableListField.Print', 'Print'),
|
2014-08-15 08:53:05 +02:00
|
|
|
'print',
|
2012-04-03 06:56:43 +02:00
|
|
|
null
|
|
|
|
);
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
$button->setAttribute('data-icon', 'grid_print');
|
|
|
|
$button->addExtraClass('gridfield-button-print');
|
2016-02-21 22:03:03 +01:00
|
|
|
$button->addExtraClass('font-icon-print');
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
return array(
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->targetFragment => '<p class="grid-print-button">' . $button->Field() . '</p>',
|
2012-04-03 06:56:43 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-04 22:29:27 +01:00
|
|
|
* Print is an action button.
|
|
|
|
*
|
|
|
|
* @param GridField
|
|
|
|
*
|
|
|
|
* @return array
|
2012-04-03 06:56:43 +02:00
|
|
|
*/
|
|
|
|
public function getActions($gridField) {
|
|
|
|
return array('print');
|
|
|
|
}
|
|
|
|
|
2013-03-04 22:29:27 +01:00
|
|
|
/**
|
|
|
|
* Handle the print action.
|
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @param string $actionName
|
|
|
|
* @param array $arguments
|
|
|
|
* @param array $data
|
|
|
|
* @return DBHTMLText
|
2013-03-04 22:29:27 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
2012-04-03 06:56:43 +02:00
|
|
|
if($actionName == 'print') {
|
|
|
|
return $this->handlePrint($gridField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-04 22:29:27 +01:00
|
|
|
* Print is accessible via the url
|
|
|
|
*
|
|
|
|
* @param GridField
|
|
|
|
* @return array
|
2012-04-03 06:56:43 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getURLHandlers($gridField) {
|
2012-04-03 06:56:43 +02:00
|
|
|
return array(
|
|
|
|
'print' => 'handlePrint',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the print, for both the action button and the URL
|
2016-06-23 01:37:22 +02:00
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return DBHTMLText
|
|
|
|
*/
|
2012-04-03 06:56:43 +02:00
|
|
|
public function handlePrint($gridField, $request = null) {
|
|
|
|
set_time_limit(60);
|
|
|
|
Requirements::clear();
|
2016-04-19 11:04:43 +02:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/client/dist/styles/GridField_print.css');
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
if($data = $this->generatePrintData($gridField)){
|
2016-08-23 04:32:26 +02:00
|
|
|
return $data->renderWith(get_class($gridField)."_print");
|
2012-04-03 06:56:43 +02:00
|
|
|
}
|
2016-08-23 04:32:26 +02:00
|
|
|
|
|
|
|
return null;
|
2012-04-03 06:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-04 22:29:27 +01:00
|
|
|
* Return the columns to print
|
|
|
|
*
|
|
|
|
* @param GridField
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-04 22:29:27 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getPrintColumnsForGridField(GridField $gridField) {
|
2013-01-14 16:32:31 +01:00
|
|
|
if($this->printColumns) {
|
2016-08-19 00:51:35 +02:00
|
|
|
return $this->printColumns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var GridFieldDataColumns $dataCols */
|
|
|
|
$dataCols = $gridField->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns');
|
|
|
|
if($dataCols) {
|
|
|
|
return $dataCols->getDisplayFields($gridField);
|
2013-01-14 16:32:31 +01:00
|
|
|
}
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
return DataObject::singleton($gridField->getModelClass())->summaryFields();
|
2013-03-04 22:29:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the title of the printed page
|
|
|
|
*
|
|
|
|
* @param GridField
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTitle(GridField $gridField) {
|
|
|
|
$form = $gridField->getForm();
|
2014-06-12 05:31:03 +02:00
|
|
|
$currentController = $gridField->getForm()->getController();
|
2013-03-04 22:29:27 +01:00
|
|
|
$title = '';
|
|
|
|
|
|
|
|
if(method_exists($currentController, 'Title')) {
|
|
|
|
$title = $currentController->Title();
|
|
|
|
} else {
|
|
|
|
if ($currentController->Title) {
|
|
|
|
$title = $currentController->Title;
|
2014-12-18 14:00:09 +01:00
|
|
|
} elseif ($form->getName()) {
|
|
|
|
$title = $form->getName();
|
2013-03-04 22:29:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($fieldTitle = $gridField->Title()) {
|
|
|
|
if($title) {
|
|
|
|
$title .= " - ";
|
|
|
|
}
|
|
|
|
|
|
|
|
$title .= $fieldTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export core.
|
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @return ArrayData
|
|
|
|
*/
|
2013-03-04 22:29:27 +01:00
|
|
|
public function generatePrintData(GridField $gridField) {
|
|
|
|
$printColumns = $this->getPrintColumnsForGridField($gridField);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-10 01:13:39 +02:00
|
|
|
$header = null;
|
2013-03-04 22:29:27 +01:00
|
|
|
|
|
|
|
if($this->printHasHeader) {
|
2012-04-03 06:56:43 +02:00
|
|
|
$header = new ArrayList();
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
foreach($printColumns as $field => $label){
|
2013-03-04 22:29:27 +01:00
|
|
|
$header->push(new ArrayData(array(
|
|
|
|
"CellString" => $label,
|
|
|
|
)));
|
2012-04-03 07:04:19 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-04 22:27:15 +01:00
|
|
|
$items = $gridField->getManipulatedList();
|
2012-04-03 06:56:43 +02:00
|
|
|
$itemRows = new ArrayList();
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var DataObject $item */
|
2015-11-12 02:59:08 +01:00
|
|
|
foreach($items->limit(null) as $item) {
|
2012-04-03 06:56:43 +02:00
|
|
|
$itemRow = new ArrayList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
foreach($printColumns as $field => $label) {
|
2012-05-29 00:06:30 +02:00
|
|
|
$value = $gridField->getDataFieldValue($item, $field);
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2015-02-10 06:01:19 +01:00
|
|
|
if($item->escapeTypeForField($field) != 'xml') {
|
2015-02-08 00:00:36 +01:00
|
|
|
$value = Convert::raw2xml($value);
|
|
|
|
}
|
|
|
|
|
2013-03-04 22:29:27 +01:00
|
|
|
$itemRow->push(new ArrayData(array(
|
|
|
|
"CellString" => $value,
|
|
|
|
)));
|
2012-04-03 06:56:43 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-04 22:29:27 +01:00
|
|
|
$itemRows->push(new ArrayData(array(
|
|
|
|
"ItemRow" => $itemRow
|
|
|
|
)));
|
2015-04-01 10:55:24 +02:00
|
|
|
if ($item->hasMethod('destroy')) {
|
|
|
|
$item->destroy();
|
|
|
|
}
|
2012-04-03 06:56:43 +02:00
|
|
|
}
|
2013-03-04 22:29:27 +01:00
|
|
|
|
|
|
|
$ret = new ArrayData(array(
|
|
|
|
"Title" => $this->getTitle($gridField),
|
|
|
|
"Header" => $header,
|
|
|
|
"ItemRows" => $itemRows,
|
2015-08-30 07:02:55 +02:00
|
|
|
"Datetime" => DBDatetime::now(),
|
2013-03-04 22:29:27 +01:00
|
|
|
"Member" => Member::currentUser(),
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getPrintColumns() {
|
2012-04-03 06:56:43 +02:00
|
|
|
return $this->printColumns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param array $cols
|
|
|
|
* @return $this
|
2012-04-03 06:56:43 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setPrintColumns($cols) {
|
2012-04-03 06:56:43 +02:00
|
|
|
$this->printColumns = $cols;
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getPrintHasHeader() {
|
2012-04-03 06:56:43 +02:00
|
|
|
return $this->printHasHeader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param bool $bool
|
|
|
|
* @return $this
|
2012-04-03 06:56:43 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setPrintHasHeader($bool) {
|
2012-04-03 06:56:43 +02:00
|
|
|
$this->printHasHeader = $bool;
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2014-06-12 05:31:03 +02:00
|
|
|
}
|