Advertisement
Guest User

GridFieldPrintButtonExtension.php

a guest
Jun 26th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.54 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Adds an "Print" button to the bottom or top of a GridField.
  5.  *
  6.  * @package forms
  7.  * @subpackage fields-gridfield
  8.  */
  9. class GridFieldPrintButtonExtension implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler {
  10.  
  11.     /**
  12.      * @var array Map of a property name on the printed objects, with values
  13.      * being the column title in the CSV file.
  14.      *
  15.      * Note that titles are only used when {@link $csvHasHeader} is set to TRUE
  16.      */
  17.     protected $printColumns;
  18.  
  19.     /**
  20.      * @var boolean
  21.      */
  22.     protected $printHasHeader = true;
  23.  
  24.     /**
  25.      * Fragment to write the button to.
  26.      *
  27.      * @var string
  28.      */
  29.     protected $targetFragment;
  30.  
  31.     /**
  32.      * @param string $targetFragment The HTML fragment to write the button into
  33.      * @param array $printColumns The columns to include in the print view
  34.      */
  35.     public function __construct($targetFragment = "before", $printColumns = null) {
  36.         $this->targetFragment = $targetFragment;
  37.         $this->printColumns = $printColumns;
  38.     }
  39.  
  40.     /**
  41.      * Place the print button in a <p> tag below the field
  42.      *
  43.      * @param GridField
  44.      *
  45.      * @return array
  46.      */
  47.     public function getHTMLFragments($gridField) {
  48.         $button = new GridField_FormAction(
  49.             $gridField,
  50.             'print2',
  51.             'Print',
  52.             'print2',
  53.             null
  54.         );
  55.  
  56.         $button->setAttribute('data-icon', 'grid_print');
  57.         $button->addExtraClass('gridfield-button-print');
  58.  
  59.         return array(
  60.             $this->targetFragment => '<p class="grid-print-button">' . $button->Field() . '</p>',
  61.         );
  62.     }
  63.  
  64.     /**
  65.      * Print is an action button.
  66.      *
  67.      * @param GridField
  68.      *
  69.      * @return array
  70.      */
  71.     public function getActions($gridField) {
  72.         return array('print2');
  73.     }
  74.  
  75.     /**
  76.      * Handle the print action.
  77.      *
  78.      * @param GridField
  79.      * @param string
  80.      * @param array
  81.      * @param array
  82.      */
  83.     public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
  84.         if($actionName == 'print2') {
  85.             return $this->handlePrint($gridField);
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Print is accessible via the url
  91.      *
  92.      * @param GridField
  93.      * @return array
  94.      */
  95.     public function getURLHandlers($gridField) {
  96.         return array(
  97.             'print2' => 'handlePrint',
  98.         );
  99.     }
  100.  
  101.     /**
  102.      * Handle the print, for both the action button and the URL
  103.      */
  104.     public function handlePrint($gridField, $request = null) {
  105.     //echo 'daaa';die();
  106.         set_time_limit(60);
  107.         Requirements::clear();
  108.         Requirements::css(FRAMEWORK_DIR . '/css/GridField_print.css');
  109.         Requirements::css('mysite/css/CustomPagePrint.css');
  110.         Requirements::css('mysite/css/font-awesome/css/font-awesome.min.css');
  111.  
  112.         if($data = $this->generatePrintData($gridField)){
  113.             return $data->renderWith("GridField_print");
  114.         }
  115.     }
  116.  
  117.     /**
  118.      * Return the columns to print
  119.      *
  120.      * @param GridField
  121.      *
  122.      * @return array
  123.      */
  124.     protected function getPrintColumnsForGridField(GridField $gridField) {
  125.         if($this->printColumns) {
  126.             $printColumns = $this->printColumns;
  127.         } else if($dataCols = $gridField->getConfig()->getComponentByType('GridFieldDataColumns')) {
  128.             $printColumns = $dataCols->getDisplayFields($gridField);
  129.         } else {
  130.             $printColumns = singleton($gridField->getModelClass())->summaryFields();
  131.         }
  132.  
  133.         return $printColumns;
  134.     }
  135.  
  136.     /**
  137.      * Return the title of the printed page
  138.      *
  139.      * @param GridField
  140.      *
  141.      * @return array
  142.      */
  143.     public function getTitle(GridField $gridField) {
  144.         $form = $gridField->getForm();
  145.         $currentController = $gridField->getForm()->getController();
  146.         $title = '';
  147.  
  148.         if(method_exists($currentController, 'Title')) {
  149.             $title = $currentController->Title();
  150.         } else {
  151.             if ($currentController->Title) {
  152.                 $title = $currentController->Title;
  153.             } elseif ($form->getName()) {
  154.                 $title = $form->getName();
  155.             }
  156.         }
  157.  
  158.         if($fieldTitle = $gridField->Title()) {
  159.             if($title) {
  160.                 $title .= " - ";
  161.             }
  162.  
  163.             $title .= $fieldTitle;
  164.         }
  165.  
  166.         return $title;
  167.     }
  168.  
  169.     /**
  170.      * Export core.
  171.      *
  172.      * @param GridField
  173.      */
  174.     public function generatePrintData(GridField $gridField) {
  175.         $printColumns = $this->getPrintColumnsForGridField($gridField);
  176.  
  177.         $header = null;
  178.  
  179.         if($this->printHasHeader) {
  180.             $header = new ArrayList();
  181.  
  182.             foreach($printColumns as $field => $label){
  183.                 $header->push(new ArrayData(array(
  184.                     "CellString" => $label,
  185.                 )));
  186.             }
  187.         }
  188.  
  189.         $items = $gridField->getManipulatedList();
  190.         $itemRows = new ArrayList();
  191.  
  192.         foreach($items->limit(null) as $item) {
  193.             $itemRow = new ArrayList();
  194.  
  195.             foreach($printColumns as $field => $label) {
  196.                 $value = $gridField->getDataFieldValue($item, $field);
  197.  
  198.                 if($item->escapeTypeForField($field) != 'xml') {
  199.                     $value = Convert::raw2xml($value);
  200.                 }
  201.  
  202.                 $itemRow->push(new ArrayData(array(
  203.                     "CellString" => $value,
  204.                 )));
  205.             }
  206.  
  207.             $itemRows->push(new ArrayData(array(
  208.                 "ItemRow" => $itemRow
  209.             )));
  210.             if ($item->hasMethod('destroy')) {
  211.                 $item->destroy();
  212.             }
  213.         }
  214.  
  215.         $ret = new ArrayData(array(
  216.             "Title" => $this->getTitle($gridField),
  217.             "Header" => $header,
  218.             "ItemRows" => $itemRows,
  219.             "Datetime" => SS_Datetime::now(),
  220.             "Member" => Member::currentUser(),
  221.         ));
  222.  
  223.         return $ret;
  224.     }
  225.  
  226.     /**
  227.      * @return array
  228.      */
  229.     public function getPrintColumns() {
  230.         return $this->printColumns;
  231.     }
  232.  
  233.     /**
  234.      * @param array
  235.      */
  236.     public function setPrintColumns($cols) {
  237.         $this->printColumns = $cols;
  238.  
  239.         return $this;
  240.     }
  241.  
  242.     /**
  243.      * @return boolean
  244.      */
  245.     public function getPrintHasHeader() {
  246.         return $this->printHasHeader;
  247.     }
  248.  
  249.     /**
  250.      * @param boolean
  251.      */
  252.     public function setPrintHasHeader($bool) {
  253.         $this->printHasHeader = $bool;
  254.  
  255.         return $this;
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement