diff --git a/forms/gridfield/GridFieldPrintButton.php b/forms/gridfield/GridFieldPrintButton.php new file mode 100644 index 000000000..281571dea --- /dev/null +++ b/forms/gridfield/GridFieldPrintButton.php @@ -0,0 +1,169 @@ +targetFragment = $targetFragment; + $this->printColumns = $printColumns; + } + + /** + * Place the print button in a

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( + $this->targetFragment => '

' . $button->Field() . '

', + ); + } + + /** + * 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(); + $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 + ) + )); + } + $ret = new ArrayData( + array( + "Header" => $header, + "ItemRows" => $itemRows, + ) + ); + + + $item->destroy(); + + 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; + } +} diff --git a/javascript/GridField.js b/javascript/GridField.js index f5ce69eb0..1ced97eec 100644 --- a/javascript/GridField.js +++ b/javascript/GridField.js @@ -111,7 +111,49 @@ } } }); + + $('.ss-gridfield .action.gridfield-button-print').entwine({ + UUID: null, + onmatch: function() { + this._super(); + this.setUUID(new Date().getTime()); + }, + onclick: function(e){ + var self = this, iframeID = 'gridfield-print-iframe' + this.getUUID(); + var printIframe = $('#' + iframeID); + + if(!printIframe.length){ + printIframe = $('