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

Source for file ScGrids.php

Documentation is available at ScGrids.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 Grids Helper
  19.  *
  20.  */
  21. class ScGrids 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.      * Create
  115.      *
  116.      * Create the Smart Client grid
  117.      * 
  118.      * @param mixed $data If string, it must be the id of the field. If array, them we will loop on it
  119.      * @return string 
  120.      *  
  121.      */
  122.     public function create$data $dataSource '' $width '' $height '' $options '' )
  123.     {
  124.         $newGrid array();
  125.         
  126.         // Check if we have an array
  127.         if is_array$data ) )
  128.         {
  129.             $newGrid $data;
  130.         }
  131.         else
  132.         {
  133.             // Add vars to the new grid
  134.             $newGrid['ID'$data;
  135.             $newGrid['width'$width;
  136.             $newGrid['height'$height;
  137.             
  138.             // Set fields
  139.             $newGrid['fields'$this->_fields;
  140.             
  141.             // Add remove quotes str, to avoid datasource being quoted
  142.             $newGrid['dataSource'$this->addQuotesStr$dataSource );
  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.             // Check if options is array and do the loop
  151.             if is_array$options ) )
  152.             {
  153.                 foreach $options as $name => $value )
  154.                 {
  155.                     $newGrid[$name$value;
  156.                 }
  157.             }
  158.         }
  159.         
  160.         // Encode all data
  161.         $dataEncoded json_encode$newGrid );
  162.         
  163.         // Remove vars that shouldn't be quoted
  164.         $dataEncoded $this->removeQuotes$dataEncoded );
  165.         
  166.         // Generate js code
  167.         $js '
  168.         <script type="text/javascript">
  169.             isc.ListGrid.create(
  170.                 '$dataEncoded .'
  171.             );
  172.         </script>
  173.         ';
  174.         
  175.         return $js;
  176.     }
  177. }

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