Source for file ScDataSource.php
Documentation is available at ScDataSource.php
* An open source SmartClient library for PHP
* @author Fernando Marcelo Morgenstern <fernando@consultorpc.com>
* @copyright Copyright (c) 2009, ConsultorPC
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html
* @link http://smartclientphp.com/
* Smart Client Data Source Helper
* @param mixed $data If string, it must be the id of the field. If array, them we will loop on it
* @param string $title Title of the field
* @param boolean $primaryKey True if this field is a primary key
* @param string $foreignKey Name of the foreign key, if necessary
* @param integer $rootValue Root value
* @param boolean $canEdit True if this field is editable
* @param array $options Additional options
public function addField( $data , $title = '' , $primaryKey = false , $foreignKey = '' , $rootValue = 0 , $canEdit = true , $options = '' )
// Check if it is an array
// Add name and title to the new field
$newField['name'] = $data;
$newField['title'] = $title;
$newField['primaryKey'] = $primaryKey;
$newField['foreignKey'] = $foreignKey;
$newField['rootValue'] = $rootValue;
$newField['canEdit'] = $canEdit;
// Check if options is array and do the loop
foreach ( $options as $name => $value )
$newField[$name] = $value;
// TODO: Check if at least name and title are set on the new field
// Add field to the array
* Get an array and do a loop using the addField function
* @param array $data Array containing multiple fields
foreach ( $data as $row )
* Return the current fields array
* @param mixed $data If string, it must be the id of the data source. If array, them we will loop on it
* @param string $dataFormat Data format
* @param string $fetchURL
* @param string $updateURL
* @param string $removeURL
* @param array $options Additional options
public function create( $data , $fetchURL = '' , $addURL = '' , $updateURL = '' , $removeURL = '' , $dataFormat = 'json' , $options = '' )
$newDataSource = array();
// Check if we have an array
// Add var to the new data soure
$newDataSource['ID'] = $data;
$newDataSource['dataFormat'] = $dataFormat;
$newDataSource['fields'] = $this->_fields;
// Set operationBindings for fetch, add, update and remove
$operationBindings = array();
$operationBindings[0]['dataURL'] = $fetchURL;
$operationBindings[0]['operationType'] = 'fetch';
$operationBindings[1]['dataURL'] = $addURL;
$operationBindings[1]['operationType'] = 'add';
$operationBindings[2]['dataURL'] = $updateURL;
$operationBindings[2]['operationType'] = 'update';
$operationBindings[3]['dataURL'] = $removeURL;
$operationBindings[3]['operationType'] = 'remove';
// Assign operationBindings to our newDataSource array
$newDataSource['operationBindings'] = $operationBindings;
// Set the default value for dataProtocol
if ( !isset ( $options['dataProtocol'] ) )
$options['dataProtocol'] = 'postParams';
// Set the default value for transformResponse
if ( !isset ( $options['transformResponse'] ) )
// Get this function on http://www.smartclient.com/docs/6.5.1/a/system/reference/SmartClient_Explorer.html#jsonServerValidationErrors
$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; } }" );
// Check if options is array and do the loop
foreach ( $options as $name => $value )
$newDataSource[$name] = $value;
// Remove vars that shouldn't be quoted
<script type="text/javascript">
isc.RestDataSource.create(
|