Cnz_Html
[ class tree: Cnz_Html ] [ index: Cnz_Html ] [ all elements ]

Source for file Csv.php

Documentation is available at Csv.php

  1. <?php
  2. /**
  3.  * CNZ Framework
  4.  *
  5.  * A Framework for Creating and Using Complex Web Elements
  6.  *
  7.  * The purpose of this framework is to provide a library of high-level objects
  8.  * to facilitate common HTML coding tasks, such as menus, tables, and forms.
  9.  * The intent is to reduce repetitive HTML coding as much as possible, replacing
  10.  * it with a combination of configuration files and style sheets with
  11.  * standardized naming conventions.
  12.  *
  13.  * This framework is built on and requires the
  14.  * {@link http://framework.zend.com/ Zend Framework}.
  15.  *
  16.  * @category Cnz
  17.  * @package Cnz_Html
  18.  * @subpackage Table
  19.  *
  20.  * @author    Lyle Frost <lfrost@cnz.com>
  21.  * @copyright Copyright (c) 2006-2007 Citadel Network <{@link http://www.citadelnetwork.com/}>
  22.  * @filesource
  23.  * @license   http://www.citadelnetwork.com/license/cnzframework New BSD License
  24.  * @version   $Id: Csv.php 27 2007-07-19 18:47:54Z lfrost $
  25.  */
  26.  
  27. /**
  28.  * Required classes.
  29.  *
  30.  * @ignore
  31.  */
  32. Zend_Loader::loadClass('Cnz_Html_Table');
  33.  
  34. /**
  35.  * HTML Table Csv class
  36.  *
  37.  * The data file is <i>name</i>.csv.  The file is searched for first in the
  38.  * current directory, then in $_SERVER['DOCUMENT_ROOT'] . '/../dat/table/'
  39.  *
  40.  * @category Cnz
  41.  * @package Cnz_Html
  42.  * @subpackage Table
  43.  *
  44.  * @todo Add limit/offset functionality.
  45.  */
  46. {
  47.     const FILE_EXT        = 'csv';
  48.  
  49.     /**#@+ @var string */
  50.     private $filename    NULL;
  51.     /**#@-*/
  52.  
  53.     /* Methods ===========================================================*/
  54.  
  55.     /**
  56.      * Option fields:
  57.      *   none
  58.      *
  59.      * Configuration fields:
  60.      *   action   Form action for button press
  61.      *   label    Button label
  62.      *   method   Form method for button press
  63.      *   param    Parameter to pass with button press
  64.      *
  65.      * @param  array       $options Options
  66.      * @param  Zend_Config $config  Configuration
  67.      */
  68.     public function __construct(array $options array())
  69.     {
  70.         parent::__construct($options);
  71.  
  72.         $filename $this->name . '.' self::FILE_EXT;
  73.         if (!is_file($filename))
  74.         {
  75.             $filename $_SERVER['DOCUMENT_ROOT''/../dat/table/' $filename;
  76.         }
  77.         $this->filename $filename;
  78.         return;
  79.     }
  80.  
  81.     /**
  82.      * @param  mixed $v1 Value 1
  83.      * @param  mixed $v2 Value 2
  84.      * @return int   <, =, > 0 if $v1 <, =, > $v2
  85.      */
  86.     public function sortCallback($v1$v2)
  87.     {
  88.         $result 0;
  89.  
  90.         $a array_keys($this->orderByArray);
  91.         $columnName $a[0];
  92.         $columnOrder self::ORDER_ASC;
  93.         if ($this->orderByArray[$columnName== self::ORDER_DESC$columnOrder self::ORDER_DESC;
  94.  
  95.         $columnNumber $this->columnNameToNumber($columnName);
  96.         if ($columnNumber === false)
  97.         {
  98.             trigger_error("Unknown column name $columnName in sort callback."E_USER_ERROR);
  99.         }
  100.         switch ($this->columnArray[$columnNumber]->getType())
  101.         {
  102.         case 'boolean':
  103.         case 'int':
  104.             $result = (int)$v1[$columnName- (int)$v2[$columnName];
  105.             break;
  106.         default:
  107.             $result strcmp($v1[$columnName]$v2[$columnName]);
  108.             break;
  109.         }
  110.  
  111.         if ($columnOrder == self::ORDER_DESC$result = -$result;
  112.  
  113.         return $result;
  114.  
  115.     }
  116.  
  117.     /**
  118.      * Read table head and data from a CSV file into an array of table
  119.      * items.  The column delimiter must be a comma, and the column
  120.      * enclosure character a double quotation mark.
  121.      *
  122.      * @return boolean Success
  123.      */
  124.     protected function readData()
  125.     {
  126.         $cna $this->genColumnNameArray();
  127.  
  128.         $fp fopen($this->filename'r');
  129.         if ($fp == NULL)
  130.         {
  131.             return false;
  132.         }
  133.         $rowNum 0;
  134.         while (($data fgetcsv($fp1000",")) !== FALSE)
  135.         {
  136.             $this->rowArray[array_combine($cna$data);
  137.             $rowNum++;
  138.         }
  139.         fclose($fp);
  140.         if (!empty($this->orderByArray))
  141.         {
  142.             if (!usort($this->rowArrayarray($this'sortCallback')))
  143.             {
  144.                 trigger_error('SORT ERROR');
  145.             }
  146.         }
  147.  
  148.         return true;
  149.     }
  150. }

Documentation generated on Thu, 19 Jul 2007 15:01:58 -0400 by phpDocumentor 1.4.0RC2