2012-04-03 06:56:43 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage gridfield
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an "Print" button to the bottom or top of a GridField.
|
|
|
|
*
|
|
|
|
* WARNING: This is experimental and its API is subject to change. Feel free to use it as long as you are happy of
|
|
|
|
* refactoring your code in the future.
|
|
|
|
*/
|
|
|
|
class GridFieldPrintButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField) {
|
|
|
|
$button = new GridField_FormAction(
|
|
|
|
$gridField,
|
|
|
|
'print',
|
|
|
|
_t('TableListField.Print', 'Print'),
|
|
|
|
'print',
|
|
|
|
null
|
|
|
|
);
|
|
|
|
$button->setAttribute('data-icon', 'grid_print');
|
|
|
|
$button->addExtraClass('gridfield-button-print');
|
|
|
|
//$button->addExtraClass('no-ajax');
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* print is an action button
|
|
|
|
*/
|
|
|
|
public function getActions($gridField) {
|
|
|
|
return array('print');
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
|
|
|
if($actionName == 'print') {
|
|
|
|
return $this->handlePrint($gridField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* it is also a URL
|
|
|
|
*/
|
|
|
|
function getURLHandlers($gridField) {
|
|
|
|
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();
|
|
|
|
Requirements::css(SAPPHIRE_DIR . '/css/GridField_print.css');
|
|
|
|
if($data = $this->generatePrintData($gridField)){
|
|
|
|
return $data->renderWith("GridField_print");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export core.
|
|
|
|
*/
|
|
|
|
function generatePrintData($gridField) {
|
|
|
|
$printColumns = ($this->printColumns) ? $this->printColumns : $gridField->getDisplayFields();
|
|
|
|
if($this->printHasHeader){
|
|
|
|
$header = new ArrayList();
|
|
|
|
foreach($printColumns as $field => $label){
|
|
|
|
$header->push(
|
|
|
|
new ArrayData(array(
|
|
|
|
"CellString" => $label,
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$items = $gridField->getList();
|
2012-04-03 07:04:19 +02:00
|
|
|
foreach($gridField->getConfig()->getComponents() as $component){
|
|
|
|
if($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) {
|
|
|
|
$items = $component->getManipulatedData($gridField, $items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
$itemRows = new ArrayList();
|
|
|
|
foreach($items as $item) {
|
|
|
|
$itemRow = new ArrayList();
|
|
|
|
foreach($printColumns as $field => $label) {
|
|
|
|
$value = $item->relField($field);
|
|
|
|
$itemRow->push(
|
|
|
|
new ArrayData(array(
|
|
|
|
"CellString" => $value,
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$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
|
|
|
}
|
2012-04-04 04:06:47 +02:00
|
|
|
|
|
|
|
//get title for the print view
|
|
|
|
$form = $gridField->getForm();
|
|
|
|
$currentController = Controller::curr();
|
2012-04-05 01:02:42 +02:00
|
|
|
$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;
|
|
|
|
}
|
2012-04-04 04:06:47 +02:00
|
|
|
|
2012-04-03 06:56:43 +02:00
|
|
|
$ret = new ArrayData(
|
|
|
|
array(
|
2012-04-04 04:06:47 +02:00
|
|
|
"Title" => $title,
|
2012-04-03 06:56:43 +02:00
|
|
|
"Header" => $header,
|
|
|
|
"ItemRows" => $itemRows,
|
2012-04-04 04:06:47 +02:00
|
|
|
"Datetime" => SS_Datetime::now(),
|
|
|
|
"Member" => Member::currentUser(),
|
2012-04-03 06:56:43 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getPrintColumns() {
|
|
|
|
return $this->printColumns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
function setPrintColumns($cols) {
|
|
|
|
$this->printColumns = $cols;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function getPrintHasHeader() {
|
|
|
|
return $this->printHasHeader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param boolean
|
|
|
|
*/
|
|
|
|
function setPrintHasHeader($bool) {
|
|
|
|
$this->printHasHeader = $bool;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|