Grido@master
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Grido
    • Components
      • Actions
      • Columns
      • Filters
    • DataSources
    • PropertyAccessors
    • Translations

Classes

  • Grido\Translations\FileTranslator
  1 <?php
  2 
  3 /**
  4  * This file is part of the Grido (https://github.com/o5/grido)
  5  *
  6  * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
  7  *
  8  * For the full copyright and license information, please view
  9  * the file LICENSE.md that was distributed with this source code.
 10  */
 11 
 12 namespace Grido\Components;
 13 
 14 /**
 15  * Exporting data to CSV.
 16  *
 17  * @package     Grido
 18  * @subpackage  Components
 19  * @author      Petr Bugyík
 20  */
 21 class Export extends Component implements \Nette\Application\IResponse
 22 {
 23     const ID = 'export';
 24 
 25     /**
 26      * @param \Grido\Grid $grid
 27      * @param string $label
 28      */
 29     public function __construct(\Grido\Grid $grid, $label = NULL)
 30     {
 31         $this->grid = $grid;
 32         $this->label = $label === NULL
 33             ? ucfirst($this->grid->getName())
 34             : $label;
 35 
 36         $grid->addComponent($this, self::ID);
 37     }
 38 
 39     /**
 40      * @param array $data
 41      * @param \Nette\ComponentModel\RecursiveComponentIterator $columns
 42      * @return string
 43      */
 44     protected function generateCsv($data, \Nette\ComponentModel\RecursiveComponentIterator $columns)
 45     {
 46         $head = array();
 47         foreach ($columns as $column) {
 48             $head[] = $column->getLabel();
 49         }
 50 
 51         $resource = fopen('php://temp/maxmemory:' . (5 * 1024 * 1024), 'r+'); // 5MB of memory allocated
 52         fputcsv($resource, $head);
 53 
 54         foreach ($data as $item) {
 55             $items = array();
 56             foreach ($columns as $column) {
 57                 $items[] = $column->renderExport($item);
 58             }
 59 
 60             fputcsv($resource, $items);
 61         }
 62 
 63         rewind($resource);
 64         $output = stream_get_contents($resource);
 65         fclose($resource);
 66 
 67         return $output;
 68     }
 69 
 70     /**
 71      * @internal
 72      */
 73     public function handleExport()
 74     {
 75         $this->grid->onRegistered && $this->grid->onRegistered($this->grid);
 76         $this->grid->presenter->sendResponse($this);
 77     }
 78 
 79     /*************************** interface \Nette\Application\IResponse ***************************/
 80 
 81     /**
 82      * Sends response to output.
 83      * @param \Nette\Http\IRequest $httpRequest
 84      * @param \Nette\Http\IResponse $httpResponse
 85      * @return void
 86      */
 87     public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $httpResponse)
 88     {
 89         $file = $this->label . '.csv';
 90         $data = $this->grid->getData(FALSE);
 91         $columns = $this->grid[\Grido\Components\Columns\Column::ID]->getComponents();
 92         $source = $this->generateCsv($data, $columns);
 93 
 94         $charset = 'UTF-16LE';
 95         $source = mb_convert_encoding($source, $charset, 'UTF-8');
 96         $source = "\xFF\xFE" . $source; //add BOM
 97 
 98         $httpResponse->setHeader('Content-Encoding', $charset);
 99         $httpResponse->setHeader('Content-Length', strlen($source));
100         $httpResponse->setHeader('Content-Type', "text/csv; charset=$charset");
101         $httpResponse->setHeader('Content-Disposition', "attachment; filename=\"$file\"; filename*=utf-8''$file");
102 
103         print $source;
104     }
105 }
106 
Grido@master API documentation generated by ApiGen