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

Source for file ScDataSource.php

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

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