Source for file csv.php

Documentation is available at csv.php

  1. <?php
  2. /**
  3.  * @package     FrameworkOnFramework
  4.  * @subpackage  view
  5.  * @copyright   Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  6.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8. // Protect from unauthorized access
  9. defined('_JEXEC'or die;
  10.  
  11. JLoader::import('joomla.application.component.view');
  12.  
  13. /**
  14.  * FrameworkOnFramework CSV View class. Automatically renders the data in CSV
  15.  * format.
  16.  *
  17.  * @package  FrameworkOnFramework
  18.  * @since    1.0
  19.  */
  20. class FOFViewCsv extends FOFViewHtml
  21. {
  22.     /**
  23.      *  Should I produce a CSV header row.
  24.      *
  25.      * @var  boolean 
  26.      */
  27.     protected $csvHeader = true;
  28.  
  29.     /**
  30.      * The filename of the downloaded CSV file.
  31.      *
  32.      * @var  string 
  33.      */
  34.     protected $csvFilename = null;
  35.  
  36.     /**
  37.      * The columns to include in the CSV output. If it's empty it will be ignored.
  38.      *
  39.      * @var  array 
  40.      */
  41.     protected $csvFields = array();
  42.  
  43.     /**
  44.     * Public constructor. Instantiates a FOFViewCsv object.
  45.     *
  46.     * @param   array  $config  The configuration data array
  47.     */
  48.     public function __construct($config array())
  49.     {
  50.         // Make sure $config is an array
  51.         if (is_object($config))
  52.         {
  53.             $config = (array) $config;
  54.         }
  55.         elseif (!is_array($config))
  56.         {
  57.             $config array();
  58.         }
  59.  
  60.         parent::__construct($config);
  61.  
  62.         if (array_key_exists('csv_header'$config))
  63.         {
  64.             $this->csvHeader = $config['csv_header'];
  65.         }
  66.         else
  67.         {
  68.             $this->csvHeader = $this->input->getBool('csv_header'true);
  69.         }
  70.  
  71.         if (array_key_exists('csv_filename'$config))
  72.         {
  73.             $this->csvFilename = $config['csv_filename'];
  74.         }
  75.         else
  76.         {
  77.             $this->csvFilename = $this->input->getString('csv_filename''');
  78.         }
  79.  
  80.         if (empty($this->csvFilename))
  81.         {
  82.             $view              $this->input->getCmd('view''cpanel');
  83.             $view              FOFInflector::pluralize($view);
  84.             $this->csvFilename = strtolower($view);
  85.         }
  86.  
  87.         if (array_key_exists('csv_fields'$config))
  88.         {
  89.             $this->csvFields = $config['csv_fields'];
  90.         }
  91.     }
  92.  
  93.     /**
  94.     * Executes before rendering a generic page, default to actions necessary for the Browse task.
  95.     *
  96.     * @param   string  $tpl  Subtemplate to use
  97.     *
  98.     * @return  boolean  Return true to allow rendering of the page
  99.     */
  100.     protected function onDisplay($tpl null)
  101.     {
  102.         // Load the model
  103.         $model $this->getModel();
  104.  
  105.         $items $model->getItemList();
  106.         $this->assignRef('items'$items);
  107.  
  108.         $document FOFPlatform::getInstance()->getDocument();
  109.  
  110.         if ($document instanceof JDocument)
  111.         {
  112.             $document->setMimeEncoding('text/csv');
  113.         }
  114.  
  115.         JResponse::setHeader('Pragma''public');
  116.         JResponse::setHeader('Expires''0');
  117.         JResponse::setHeader('Cache-Control''must-revalidate, post-check=0, pre-check=0');
  118.         JResponse::setHeader('Cache-Control''public'false);
  119.         JResponse::setHeader('Content-Description''File Transfer');
  120.         JResponse::setHeader('Content-Disposition''attachment; filename="' $this->csvFilename . '"');
  121.  
  122.         if (is_null($tpl))
  123.         {
  124.             $tpl 'csv';
  125.         }
  126.  
  127.         if (FOFPlatform::getInstance()->checkVersion(JVERSION'3.0''lt'))
  128.         {
  129.             FOFPlatform::getInstance()->setErrorHandling(E_ALL'ignore');
  130.         }
  131.  
  132.         $hasFailed false;
  133.  
  134.         try
  135.         {
  136.             $result $this->loadTemplate($tpltrue);
  137.  
  138.             if ($result instanceof Exception)
  139.             {
  140.                 $hasFailed true;
  141.             }
  142.         }
  143.         catch (Exception $e)
  144.         {
  145.             $hasFailed true;
  146.         }
  147.  
  148.         if (FOFPlatform::getInstance()->checkVersion(JVERSION'3.0''lt'))
  149.         {
  150.             if ($result instanceof Exception)
  151.             {
  152.                 $hasFailed true;
  153.             }
  154.         }
  155.  
  156.         if (!$hasFailed)
  157.         {
  158.             echo $result;
  159.         }
  160.         else
  161.         {
  162.             // Default CSV behaviour in case the template isn't there!
  163.  
  164.             if (empty($items))
  165.             {
  166.                 return;
  167.             }
  168.  
  169.             $item    array_pop($items);
  170.             $keys    get_object_vars($item);
  171.             $keys    array_keys($keys);
  172.             $items[$item;
  173.             reset($items);
  174.  
  175.             if (!empty($this->csvFields))
  176.             {
  177.                 $temp array();
  178.  
  179.                 foreach ($this->csvFields as $f)
  180.                 {
  181.                     if (in_array($f$keys))
  182.                     {
  183.                         $temp[$f;
  184.                     }
  185.                 }
  186.  
  187.                 $keys $temp;
  188.             }
  189.  
  190.             if ($this->csvHeader)
  191.             {
  192.                 $csv array();
  193.  
  194.                 foreach ($keys as $k)
  195.                 {
  196.                     $k str_replace('"''""'$k);
  197.                     $k str_replace("\r"'\\r'$k);
  198.                     $k str_replace("\n"'\\n'$k);
  199.                     $k '"' $k '"';
  200.  
  201.                     $csv[$k;
  202.                 }
  203.  
  204.                 echo implode(","$csv"\r\n";
  205.             }
  206.  
  207.             foreach ($items as $item)
  208.             {
  209.                 $csv  array();
  210.                 $item = (array) $item;
  211.  
  212.                 foreach ($keys as $k)
  213.                 {
  214.                     if (!isset($item[$k]))
  215.                     {
  216.                         $v '';
  217.                     }
  218.                     else
  219.                     {
  220.                         $v $item[$k];
  221.                     }
  222.  
  223.                     if (is_array($v))
  224.                     {
  225.                         $v 'Array';
  226.                     }
  227.                     elseif (is_object($v))
  228.                     {
  229.                         $v 'Object';
  230.                     }
  231.  
  232.                     $v str_replace('"''""'$v);
  233.                     $v str_replace("\r"'\\r'$v);
  234.                     $v str_replace("\n"'\\n'$v);
  235.                     $v '"' $v '"';
  236.  
  237.                     $csv[$v;
  238.                 }
  239.  
  240.                 echo implode(","$csv"\r\n";
  241.             }
  242.         }
  243.  
  244.         return false;
  245.     }
  246. }

Documentation generated on Tue, 19 Nov 2013 14:57:40 +0100 by phpDocumentor 1.4.3