Source for file CsvExport.php

Documentation is available at CsvExport.php

  1. <?php
  2.     /**
  3.      * The CSV export action class
  4.      * @copyright Copyright (c) 2006,2007 Mirchev Ideas Ltd. All rights reserved.
  5.      * @package MIPHPF
  6.      */
  7.     
  8.     /**
  9.      * Support for CSV export
  10.      * @copyright Copyright (c) 2006 Mirchev Ideas Ltd. All rights reserved.
  11.      * @package MIPHPF
  12.      */
  13.     class miCsvExportAction extends miAction {
  14.         const CSV_LINE_DELIMITER = "\n";
  15.         
  16.         /**
  17.          * The filename to suggest to the user
  18.          * If the filename contains special chars it must be escaped in quoted printable format
  19.          * 
  20.          * @access protected
  21.          * @var string 
  22.          */
  23.         protected $_csvFilename = 'export.csv';
  24.         
  25.         /**
  26.          * Print export headers
  27.          * 
  28.          * @access protected
  29.          */
  30.         protected function showCsvExportHeaders()
  31.         {
  32.             header('Pragma: public');
  33.             header('Expires: 0');
  34.             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  35.             header('Content-Type: application/force-download');
  36.             header('Content-Type: application/octet-stream');
  37.             header('Content-Type: application/download');
  38.             header('Content-Disposition: attachment; filename=' $this->_csvFilename);
  39.             header('Content-Transfer-Encoding: binary');
  40.         }
  41.         
  42.         /**
  43.          * Escape a single CSV value
  44.          * If it detects a comma then it double quotes the string. Then double quotes themselves are escaped by doubling
  45.          * 
  46.          * @access protected
  47.          * @param string $value 
  48.          * @return string 
  49.          */
  50.         protected function escapeCsv($value)
  51.         {
  52.             if ($value == '')
  53.                 return $value;
  54.             
  55.             if (strtok($value",\"\n"!== $value{
  56.                 if (strpos($value'"'!== false)    // If there are double quotes, escape them by doubling them
  57.                     $value str_replace('"''""'$value);
  58.                 return '"' $value '"';
  59.             }
  60.             return $value;
  61.         }
  62.         
  63.         /**
  64.          * Exports the records as CSV
  65.          * 
  66.          * @access protected
  67.          * @param array $records 
  68.          */
  69.         protected function exportRecords($records)
  70.         {
  71.             if (count($records== 0)
  72.                 return;
  73.                 
  74.             // Print header line
  75.             $header array();
  76.             foreach ($records[0as $key => $value)
  77.                 $header[$this->escapeCsv($key);
  78.             echo implode(','$header);
  79.             echo miCsvExportAction::CSV_LINE_DELIMITER;
  80.             
  81.             // Print the values
  82.             foreach ($records as $record{
  83.                 $line array();
  84.                 foreach ($record as $value)
  85.                     $line[$this->escapeCsv($value);
  86.                 echo implode(','$line);
  87.                 echo miCsvExportAction::CSV_LINE_DELIMITER;
  88.             }
  89.         }
  90.         
  91.         /**
  92.          * Performs the export as CSV action
  93.          * 
  94.          * @access public
  95.          */
  96.         public function doAction()
  97.         {
  98.             // Init the recordset
  99.             $recordSet $this->_view->getRecordset();
  100.             $recordSet->setOrder(miGetParamDefault(miTableSorter::PARAM_SORTBY'')miGetParamDefault(miTableSorter::PARAM_SORTDIR''));
  101.             
  102.             $filterObjects $this->_view->getTableFilterObj()->getFilterObjs();
  103.             $recordSet->addFilters($filterObjects);
  104.             
  105.             // Perform the export
  106.             if (headers_sent()) {
  107.                 throw new miConfigurationException('Cannot export as CSV. Headers have already been sent'miConfigurationException::EXCEPTION_HEADERS_ALREADY_SENT);
  108.             }
  109.             
  110.             // Remove possible header and do gzip encoding for speeding up the CSV file download
  111.             if (ob_get_level()) {
  112.                 ob_end_clean();
  113.                 header('Content-Encoding: none');
  114.             }
  115.             ob_start('ob_gzhandler');
  116.             
  117.             $this->showCsvExportHeaders();
  118.             $this->exportRecords($recordSet->getAllRecords());
  119.             exit;
  120.         }
  121.     }
  122. ?>

Documentation generated on Thu, 08 May 2008 16:57:18 +0300 by phpDocumentor 1.4.1