2012-04-03 06:56:43 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2013-03-04 22:29:27 +01:00
|
|
|
* Adds an "Print" button to the bottom or top of a GridField.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2012-04-03 06:56:43 +02:00
|
|
|
* @subpackage gridfield
|
|
|
|
*/
|
|
|
|
|
|
|
|
class GridFieldPrintButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler {
|
|
|
|
|
|
|
|
/**
|
2013-03-04 22:29:27 +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
|
2012-04-03 06:56:43 +02:00
|
|
|
*/
|
|
|
|
protected $printColumns;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $printHasHeader = true;
|
|
|
|
|
|
|
|
/**
|
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(
|
|
|
|
$gridField,
|
|
|
|
'print',
|
|
|
|
_t('TableListField.Print', 'Print'),
|
|
|
|
'print',
|
|
|
|
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');
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
return array(
|
2012-04-04 04:08:19 +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.
|
|
|
|
*
|
|
|
|
* @param GridField
|
|
|
|
* @param string
|
|
|
|
* @param array
|
|
|
|
* @param array
|
|
|
|
*/
|
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
|
|
|
|
*/
|
|
|
|
public function handlePrint($gridField, $request = null) {
|
|
|
|
set_time_limit(60);
|
|
|
|
Requirements::clear();
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/GridField_print.css');
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
if($data = $this->generatePrintData($gridField)){
|
|
|
|
return $data->renderWith("GridField_print");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-04 22:29:27 +01:00
|
|
|
* Return the columns to print
|
|
|
|
*
|
|
|
|
* @param GridField
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getPrintColumnsForGridField(GridField $gridField) {
|
2013-01-14 16:32:31 +01:00
|
|
|
if($this->printColumns) {
|
|
|
|
$printColumns = $this->printColumns;
|
|
|
|
} else if($dataCols = $gridField->getConfig()->getComponentByType('GridFieldDataColumns')) {
|
|
|
|
$printColumns = $dataCols->getDisplayFields($gridField);
|
|
|
|
} else {
|
|
|
|
$printColumns = singleton($gridField->getModelClass())->summaryFields();
|
|
|
|
}
|
2013-03-04 22:29:27 +01:00
|
|
|
|
|
|
|
return $printColumns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the title of the printed page
|
|
|
|
*
|
|
|
|
* @param GridField
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTitle(GridField $gridField) {
|
|
|
|
$form = $gridField->getForm();
|
|
|
|
$currentController = Controller::curr();
|
|
|
|
$title = '';
|
|
|
|
|
|
|
|
if(method_exists($currentController, 'Title')) {
|
|
|
|
$title = $currentController->Title();
|
|
|
|
} else {
|
|
|
|
if ($currentController->Title) {
|
|
|
|
$title = $currentController->Title;
|
|
|
|
} else {
|
|
|
|
if($form->Name()){
|
|
|
|
$title = $form->Name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($fieldTitle = $gridField->Title()) {
|
|
|
|
if($title) {
|
|
|
|
$title .= " - ";
|
|
|
|
}
|
|
|
|
|
|
|
|
$title .= $fieldTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export core.
|
|
|
|
*
|
|
|
|
* @param GridField
|
|
|
|
*/
|
|
|
|
public function generatePrintData(GridField $gridField) {
|
|
|
|
$printColumns = $this->getPrintColumnsForGridField($gridField);
|
2013-01-14 16:32:31 +01: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
foreach($items as $item) {
|
|
|
|
$itemRow = new ArrayList();
|
2013-03-04 22:29:27 +01: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
|
|
|
|
|
|
|
$itemRow->push(new ArrayData(array(
|
|
|
|
"CellString" => $value,
|
|
|
|
)));
|
2012-04-03 06:56:43 +02:00
|
|
|
}
|
2013-03-04 22:29:27 +01:00
|
|
|
|
|
|
|
$itemRows->push(new ArrayData(array(
|
|
|
|
"ItemRow" => $itemRow
|
|
|
|
)));
|
|
|
|
|
2012-04-04 01:29:25 +02:00
|
|
|
$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,
|
|
|
|
"Datetime" => SS_Datetime::now(),
|
|
|
|
"Member" => Member::currentUser(),
|
|
|
|
));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param boolean
|
|
|
|
*/
|
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;
|
|
|
|
}
|
2013-03-04 22:29:27 +01:00
|
|
|
}
|