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

Source for file ScDataSource.php

Documentation is available at ScDataSource.php

  1. <?php
  2. /**
  3.  * SCZF
  4.  *
  5.  * An open source SmartClient View Helper for Zend Framework
  6.  *
  7.  * @package        SCZF
  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 Data Source Helper
  19.  *
  20.  * @uses viewHelper SmartClient
  21.  */
  22. {
  23.  
  24.     /**
  25.      * @var Zend_View_Interface 
  26.      */
  27.     public $view;
  28.  
  29.     /**
  30.      * @var Array 
  31.      */
  32.     protected $_fields;
  33.  
  34.     /**
  35.      * 
  36.      * Return instance
  37.      * 
  38.      */
  39.     public function ScDataSource()
  40.     {
  41.         return $this;
  42.     }
  43.  
  44.     /**
  45.      * 
  46.      * Sets the view field
  47.      * @param $view Zend_View_Interface
  48.      * 
  49.      */
  50.     public function setViewZend_View_Interface $view )
  51.     {
  52.         $this->view = $view;
  53.     }
  54.  
  55.     /**
  56.      * 
  57.      * Add Field
  58.      *
  59.      * @param mixed $data If string, it must be the id of the field. If array, them we will loop on it
  60.      * @param string $title Title of the field
  61.      * @param boolean $primaryKey True if this field is a primary key
  62.      * @param string $foreignKey Name of the foreign key, if necessary
  63.      * @param integer $rootValue Root value
  64.      * @param boolean $canEdit True if this field is editable
  65.      * @param array $options Additional options
  66.      * @return string 
  67.      * 
  68.      */
  69.     public function addField$data $title '' $primaryKey false $foreignKey '' $rootValue $canEdit true $options '' )
  70.     {
  71.         $newField array();
  72.         
  73.         // Check if it is an array
  74.         if is_array$data ) )
  75.         {
  76.             $newField $data;
  77.         }
  78.         else
  79.         {
  80.             // Add name and title to the new field
  81.             $newField['name'$data;
  82.             $newField['title'$title;
  83.             $newField['primaryKey'$primaryKey;
  84.             $newField['foreignKey'$foreignKey;
  85.             $newField['rootValue'$rootValue;
  86.             $newField['canEdit'$canEdit;
  87.             
  88.             // Check if options is array and do the loop
  89.             if is_array$options ) )
  90.             {
  91.                 foreach $options as $name => $value )
  92.                 {
  93.                     $newField[$name$value;
  94.                 }
  95.             }
  96.         }
  97.         
  98.         // TODO: Check if at least name and title are set on the new field
  99.         // Add field to the array
  100.         $this->_fields[$newField;
  101.     }
  102.  
  103.     /**
  104.      * Add Fields
  105.      *
  106.      * Get an array and do a loop using the addField function
  107.      * 
  108.      * @param array $data Array containing multiple fields
  109.      *  
  110.      */
  111.     public function addFields$data )
  112.     {
  113.         if is_array$data ) )
  114.         {
  115.             foreach $data as $row )
  116.             {
  117.                 $this->addField$row );
  118.             }
  119.         }
  120.     }
  121.     
  122.     /**
  123.      * Get Fields
  124.      *
  125.      * Return the current fields array
  126.      * 
  127.      * @return array 
  128.      *  
  129.      */
  130.     public function getFields()
  131.     {
  132.         return $this->_fields;
  133.     }
  134.     
  135.     /**
  136.      * 
  137.      * Clear fields
  138.      *
  139.      * Clear Fields array
  140.      *  
  141.      */
  142.     public function clearFields()
  143.     {
  144.         unset$this->_fields );
  145.     }
  146.         
  147.     /**
  148.      * Create
  149.      *
  150.      * Create a data source
  151.      * 
  152.      * @param mixed $data If string, it must be the id of the data source. If array, them we will loop on it
  153.      * @param string $dataFormat Data format
  154.      * @param string $fetchURL 
  155.      * @param string $addURL 
  156.      * @param string $updateURL 
  157.      * @param string $removeURL 
  158.      * @param array $options Additional options
  159.      * @return string 
  160.      *  
  161.      */
  162.     public function create$data $fetchURL '' $addURL '' $updateURL '' $removeURL '' $dataFormat 'json' ,  $options '' )
  163.     {
  164.         $newDataSource array();
  165.         
  166.         // Check if we have an array
  167.         if is_array$data ) )
  168.         {
  169.             $newDataSource $data;
  170.         }
  171.         else
  172.         {
  173.             // Add var to the new data soure
  174.             $newDataSource['ID'$data;
  175.             $newDataSource['dataFormat'$dataFormat;
  176.             
  177.             // Set Fields
  178.             $newDataSource['fields'$this->_fields;
  179.             
  180.             // Set operationBindings for fetch, add, update and remove
  181.             $operationBindings array();
  182.             $operationBindings[0]['dataURL'$fetchURL;
  183.             $operationBindings[0]['operationType''fetch';
  184.             
  185.             $operationBindings[1]['dataURL'$addURL;
  186.             $operationBindings[1]['operationType''add';
  187.             
  188.             $operationBindings[2]['dataURL'$updateURL;
  189.             $operationBindings[2]['operationType''update';
  190.             
  191.             $operationBindings[3]['dataURL'$removeURL;
  192.             $operationBindings[3]['operationType''remove';
  193.             
  194.             // Assign operationBindings to our newDataSource array
  195.             $newDataSource['operationBindings'$operationBindings;
  196.             
  197.             // Set the default value for dataProtocol
  198.             if !isset$options['dataProtocol') )
  199.             {
  200.                 $options['dataProtocol''postParams';
  201.             }
  202.             
  203.             // Set the default value for transformResponse
  204.             if !isset$options['transformResponse') )
  205.             {
  206.                 // Get this function on http://www.smartclient.com/docs/6.5.1/a/system/reference/SmartClient_Explorer.html#jsonServerValidationErrors
  207.                 $options['transformResponse'$this->addQuotesStr"function (dsResponse, dsRequest, jsonData) { var status = isc.XMLTools.selectObjects(jsonData, '/response/status'); if (status != 0) { dsResponse.status = isc.RPCResponse.STATUS_VALIDATION_ERROR; var errors = isc.XMLTools.selectObjects(jsonData, '/response/errors'); dsResponse.errors = errors; } }" );
  208.             }
  209.             
  210.             // Check if options is array and do the loop
  211.             if is_array$options ) )
  212.             {
  213.                 foreach $options as $name => $value )
  214.                 {
  215.                     $newDataSource[$name$value;
  216.                 }
  217.             }
  218.         }
  219.         
  220.         // Encode all data
  221.         $dataEncoded Zend_Json::encode$newDataSource );
  222.         
  223.         // Remove vars that shouldn't be quoted
  224.         $dataEncoded $this->removeQuotes$dataEncoded );
  225.         
  226.         // Generate js code
  227.         $js '
  228.         <script type="text/javascript">
  229.             isc.RestDataSource.create(
  230.                 '$dataEncoded .'
  231.             );
  232.         </script>
  233.         ';
  234.         
  235.         return $js;
  236.     }
  237. }

Documentation generated on Mon, 20 Jul 2009 16:51:56 -0300 by phpDocumentor 1.4.1