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

Source for file ScTrees.php

Documentation is available at ScTrees.php

  1. <?php
  2. /**
  3.  * SCPHP
  4.  *
  5.  * An open source SmartClient library for PHP
  6.  *
  7.  * @package        SCPHP
  8.  * @author        Fernando Marcelo Morgenstern <fernando@consultorpc.com>
  9.  * @copyright    Copyright (c) 2009, ConsultorPC
  10.  * @license        http://www.gnu.org/licenses/lgpl-3.0-standalone.html
  11.  * @link        http://smartclientphp.com/
  12.  * @since        Version 0.1b
  13.  * @filesource
  14.  */
  15.  
  16.  
  17. /**
  18.  * Smart Client Trees Helper
  19.  *
  20.  */
  21. class ScTrees extends ScGeneral 
  22. {
  23.  
  24.     /**
  25.      * @var Array 
  26.      */
  27.     protected $_fields;
  28.     
  29.     /**
  30.      * 
  31.      * Add Field
  32.      *
  33.      * @param mixed $data If string, it must be the id of the field. If array, them we will loop on it
  34.      * @param string $title Title of the field
  35.      * @param array $options Additional options
  36.      * @return string 
  37.      * 
  38.      */
  39.     public function addField$data $title '' $options '' )
  40.     {
  41.         $newField array();
  42.         
  43.         // Check if it is an array
  44.         if is_array$data ) )
  45.         {
  46.             $newField $data;
  47.         }
  48.         else
  49.         {
  50.             // Add name and title to the new field
  51.             $newField['name'$data;
  52.             $newField['title'$title;
  53.             
  54.             // Check if options is array and do the loop
  55.             if is_array$options ) )
  56.             {
  57.                 foreach $options as $name => $value )
  58.                 {
  59.                     $newField[$name$value;
  60.                 }
  61.             }
  62.         }
  63.         
  64.         // TODO: Check if at least name and title are set on the new field
  65.         // Add field to the array
  66.         $this->_fields[$newField;
  67.     }
  68.     
  69.     /**
  70.      * Add Fields
  71.      *
  72.      * Get an array and do a loop using the addField function
  73.      * 
  74.      * @param array $data Array containing multiple fields
  75.      *  
  76.      */
  77.     public function addFields$data )
  78.     {
  79.         if is_array$data ) )
  80.         {
  81.             foreach $data as $row )
  82.             {
  83.                 $this->addField$row );
  84.             }
  85.         }
  86.     }
  87.     
  88.     /**
  89.      * Get Fields
  90.      *
  91.      * Return the current fields array
  92.      * 
  93.      * @return array 
  94.      *  
  95.      */
  96.     public function getFields()
  97.     {
  98.         return $this->_fields;
  99.     }
  100.     
  101.     /**
  102.      * 
  103.      * Clear fields
  104.      *
  105.      * Clear Fields array
  106.      *  
  107.      */
  108.     public function clearFields()
  109.     {
  110.         unset$this->_fields );
  111.     }
  112.  
  113.     /**
  114.      * 
  115.      * Create
  116.      * 
  117.      * Creates a new tree
  118.      *
  119.      * @param mixed $data If string, it must be the ID of the Tree. If array, them we will loop through it
  120.      * @param string $dataSource Datasource
  121.      * @param string $width Width of the button
  122.      * @param string $height Height of the button
  123.      * @param array $options Additional options
  124.      * @return string 
  125.      * 
  126.      */
  127.     public function create$data $dataSource '' $width '' $height '' $options '' )
  128.     {
  129.         $newTree array();
  130.         
  131.         // Check if it is an array
  132.         if is_array$data ) )
  133.         {
  134.             $newTree $data;
  135.         }
  136.         else
  137.         {
  138.             // Add vars to the new button
  139.             $newTree['ID'$data;
  140.             $newTree['dataSource'$this->addQuotesStr$dataSource );
  141.             $newTree['width'$width;
  142.             $newTree['height'$height;
  143.             
  144.             // Set default value for autoFetchData if it is not set
  145.             if empty$options['autoFetchData') )
  146.             {
  147.                 $options['autoFetchData'true;
  148.             }
  149.             
  150.             // Set fields
  151.             $newTree['fields'$this->_fields;
  152.             
  153.             // Check if options is array and do the loop
  154.             if is_array$options ) )
  155.             {
  156.                 foreach $options as $name => $value )
  157.                 {
  158.                     $newTree[$name$value;
  159.                 }
  160.             }
  161.         }
  162.         
  163.         // Encode all data
  164.         $dataEncoded json_encode$newTree );
  165.         
  166.         // Remove vars that shouldn't be quoted
  167.         $dataEncoded $this->removeQuotes$dataEncoded );
  168.         
  169.         // Generate js code
  170.         $js '
  171.         <script type="text/javascript">
  172.             isc.TreeGrid.create(
  173.                 '$dataEncoded .'
  174.             );
  175.         </script>
  176.         ';
  177.         
  178.         return $js;
  179.     }
  180. }

Documentation generated on Wed, 29 Jul 2009 22:37:15 -0300 by phpDocumentor 1.4.1