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

Source for file CSVFiles.class.php

Documentation is available at CSVFiles.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package filesystem
  9.  */
  10. if!defined"CLASSPATH" ) )
  11. {
  12.   echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
  13.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
  14.   exit();
  15. }
  16.  
  17. /**
  18.  *
  19.  */
  20. include_onceCLASSPATH."filesystem/Files.class.php" );
  21.  
  22. /**
  23.  *
  24.  */
  25. include_onceCLASSPATH."core/Arrays.class.php" );
  26.  
  27. /**
  28.  *
  29.  */
  30. include_onceCLASSPATH."core/Strings.class.php" );
  31.  
  32. /**
  33.  * A class to manage files with comma separated values.
  34.  *
  35.  * @package   filesystem
  36.  * @version   0.1.22
  37.  * @since     0.9.0.7
  38.  * @author    Daniel Plücken <daniel@debakel.net>
  39.  * @license   http://www.gnu.org/copyleft/lesser.html
  40.  *             GNU Lesser General Public License
  41.  * @copyright Copyright (c) 2006 Daniel Plücken <daniel@debakel.net>
  42.  *
  43.  *  This library is free software; you can redistribute it and/or
  44.  *  modify it under the terms of the GNU Lesser General Public
  45.  *  License as published by the Free Software Foundation; either
  46.  *  version 2.1 of the License.
  47.  *
  48.  *  This library is distributed in the hope that it will be useful,
  49.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  50.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  51.  *  GNU Lesser General Public License for more details.
  52.  *
  53.  *  You should have received a copy of the GNU Lesser General
  54.  *  Public License along with this library; if not, write to the
  55.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  56.  *  Boston, MA 02111-1307 USA
  57.  */
  58. class CSVFiles extends Files
  59. {
  60.   /**
  61.    * @access public
  62.    * @var    char   $separator_chart 
  63.    */
  64.   var $separator_char = ";";
  65.  
  66.   /**
  67.    * @access public
  68.    * @var    char   $enclosing_char 
  69.    */
  70.   var $enclosing_char = "\"";
  71.  
  72.   /**
  73.    * @access public
  74.    * @var    char   $escape_char 
  75.    */
  76.   var $escape_char = "\\\\";
  77.  
  78.   /**
  79.    * @access public
  80.    * @var    char   line_terminator_char
  81.    */
  82.   var $line_terminator_char = "\r\n";
  83.  
  84.   /**
  85.    * @access public
  86.    * @var    array  $content_arr 
  87.    */
  88.   var $content_arr = array();
  89.  
  90.   /**
  91.    * @access public
  92.    * @var    integer $cols 
  93.    */
  94.   var $cols = 0;
  95.  
  96.  
  97.   /**
  98.    * Constructor
  99.    *
  100.    * @access  public
  101.    * @version 1.0
  102.    * @since   0.1.0
  103.    *
  104.    * @param   string  $path_and_filename 
  105.    * @or
  106.    * @param   string  $path 
  107.    * @param   string  $filename 
  108.    *
  109.    * @return  CSVFiles 
  110.    */
  111.   function CSVFiles$path$filename "" )
  112.   {
  113.       ifempty$filename ) )
  114.       {
  115.         $tmpArr FilesystemToolkit
  116.                 ::separatePathAndFile$path );
  117.         $path $tmpArr[0];
  118.         $filename $tmpArr[1];
  119.       }
  120.  
  121.       $this->path     = $path;
  122.       $this->filename = $filename;
  123.   }
  124.  
  125.  
  126.  
  127.   /**
  128.    * Stores the number of columns of this csv-file object.
  129.    *
  130.    * @access  public
  131.    * @version 1.0
  132.    * @since   0.1.2
  133.    *
  134.    * @return  void 
  135.    */
  136.   function setCols$int_cols )
  137.   $this->cols = $int_cols}
  138.  
  139.  
  140.  
  141.   /**
  142.    * Returns the number of columns of this csvfile object.
  143.    *
  144.    * @access  public
  145.    * @version 1.0
  146.    * @since   0.1.2
  147.    *
  148.    * @return  integer 
  149.    */
  150.   function getCols()
  151.   return $this->cols}
  152.  
  153.  
  154.  
  155.   /**
  156.    * Stores the number of columns of this csv-file object.
  157.    *
  158.    * @access  public
  159.    * @version 1.0
  160.    * @since   0.1.2
  161.    *
  162.    * @param   string $str_val 
  163.    *
  164.    * @return  void 
  165.    */
  166.   function addCol$str_val )
  167.   {
  168.      if empty$this->cols ) )
  169.         echo "To insert a value in a cell of an csv-file, set the number of "
  170.             ."columns in this file first!";
  171.      else
  172.      {
  173.         $int_actual_row count$this->content_arr 1;
  174.         if $int_actual_row )
  175.            $int_actual_row 0;
  176.  
  177.         $int_next_col count$this->content_arr[$int_actual_row);
  178.         if $int_next_col >= $this->cols )
  179.         {
  180.            $int_next_col 0;
  181.            $int_actual_row++;
  182.         }
  183.  
  184.         $this->content_arr[$int_actual_row][$int_next_col$str_val;
  185.      }
  186.   }
  187.  
  188.  
  189.  
  190.   /**
  191.    * Sets the header for an excel output.
  192.    *
  193.    * @access  public
  194.    * @version 1.0
  195.    * @since   0.1.22
  196.    *
  197.    * @return void 
  198.    */
  199.   function headerForExcel()
  200.   {
  201.     header"Content-type: application/x-msexcel" );
  202.     header"Content-disposition: attachment; filename=".$this->filename );
  203.     header"Pragma: no-cache" );
  204.     header"Expires: 0" );
  205.   }
  206.  
  207.  
  208.  
  209.   /**
  210.    * Returns the content of this file object.
  211.    *
  212.    * @access  public
  213.    * @version 1.0
  214.    * @since   0.1.2
  215.    *
  216.    * @return  string 
  217.    */
  218.   function &getContent ()
  219.   {
  220.     if(
  221.         empty$this->content )
  222.      || Arrays::is_empty$this->content_arr )
  223.       )
  224.       $this->load();
  225.  
  226.     for $i 0$i count$this->content_arr )$i++ )
  227.     {
  228.         if $i )
  229.            $this->content .= $this->line_terminator_char;
  230.  
  231.         for $k 0$k count$this->content_arr[$i)$k++ )
  232.         {
  233.             if $k )
  234.                $this->content .= $this->separator_char;
  235.  
  236.            $this->content .= $this->enclosing_char
  237.                             .Strings::escapeLineBreaks(
  238.                                addslashes$this->content_arr[$i][$k)
  239.                                                       )
  240.                             .$this->enclosing_char;
  241.         }
  242.     }
  243.  
  244.     return $this->content;
  245.   }
  246. // End of class CSVFiles
  247. ?>

Documentation generated on Thu, 05 Jun 2008 19:10:30 +0200 by phpDocumentor 1.4.1