* @copyright Copyright (c) 2010, ConsultorPC * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html * @link http://smartclientphp.com/ * @since Version 0.1b * @filesource */ /** * Smart Client Data Source Helper * */ class ScDataSource extends ScGeneral { /** * @var Array */ protected $_fields; /** * * Add Field * * @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 * @return string * */ public function addField( $data , $title = '' , $primaryKey = false , $foreignKey = '' , $rootValue = 0 , $canEdit = true , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // 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 if ( is_array( $options ) ) { 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 $this->_fields[] = $newField; } /** * Add Fields * * Get an array and do a loop using the addField function * * @param array $data Array containing multiple fields * */ public function addFields( $data ) { if ( is_array( $data ) ) { foreach ( $data as $row ) { $this->addField( $row ); } } } /** * Get Fields * * Return the current fields array * * @return array * */ public function getFields() { return $this->_fields; } /** * * Clear fields * * Clear Fields array * */ public function clearFields() { unset( $this->_fields ); } /** * Create * * Create a data source * * @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 $addURL * @param string $updateURL * @param string $removeURL * @param array $options Additional options * @return string * */ public function create( $data , $fetchURL = '' , $addURL = '' , $updateURL = '' , $removeURL = '' , $dataFormat = 'json' , $options = '' ) { $newDataSource = array(); // Check if we have an array if ( is_array( $data ) ) { $newDataSource = $data; } else { // Add var to the new data soure $newDataSource['ID'] = $data; $newDataSource['dataFormat'] = $dataFormat; // Set Fields $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'; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newDataSource[$name] = $value; } } } // Encode data $dataEncoded = $this->encode( $newDataSource ); // Generate js code $js = 'isc.RestDataSource.create( '. $dataEncoded .' );'; // Add script tag $js = $this->addScriptTags( $js ); return $js; } }