Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
themeEclipse
languagephp
titleDataSource.php
firstline53
linenumberstrue
public function execute($request)
{
	 switch($request->operationType)
	 {
		 case 'fetch':                    
			return $this->fetch($request);
		 case 'add':
			return $this->add($request);
		 case 'update':                                    
			return $this->update($request);
		 case 'remove':                                       
			return $this->remove($request);
	 }
}
private function add($request)
{            
	$ds = $this->data_source;
	// get the table name from data source
	$tbl_name = $ds['ID'];
	$new_data = $request->data;
				
	//DSResponse 	$responsemake =the newquery DSResponse();      
	$response->setData($new_data);      
	$columns = '';
	$values = '';
	$response->setStatus(0);foreach($new_data as $key => $value)
	{   	return $response; } private function update($request) { 	$old_data = $request->oldValues;
	$update_data = $request->data;
	$ds = $this->data_source $columns .=  $key.',';
	
	// update all fields$values which have changed.= they are defined in the data property
	if ( count($request->getDataKeys()) != 0 )
	{
		 foreach($request->data as $key => $value)
		 {
			$old_data[$key] = $value;
		 }
	} "'".$value."',";                
	}         

	 
	// buildremove the criteria
	//DSResponselast comma
	$response$columns = new DSResponse();      
	$response->setData($old_data);            
	$response->setStatus(0);  
	return $response;
}
private function remove($request)
{
	$ds = $this->data_source;
	$new_data = $request->data;
				 substr($columns, 0, strlen($columns) - 1);
	$values = substr($values, 0, strlen($values) - 1);
	// result insert query
	$insert_query = 'INSERT INTO '.$tbl_name.' ('.$columns.') VALUES ('.$values.')';
	
	// Run the insert query
	R::exec($insert_query);
	//DSResponse
	$response = new DSResponse();      
	$response->setData($new_data);            
	$response->setStatus(0);  
	return $response;
}
private function update($request)
{
	$old_data = $request->oldValues;
	$update_data = $request->data;
	$ds = $this->data_source;
	
	// update all fields which have changed. they are defined in the data property
	if ( count($request->getDataKeys()) != 0 )
	{
		 foreach($request->data as $key => $value)
		 {
			$old_data[$key] = $value;
		 }
	}     
	// get the table name from data source
	$tbl_name = $ds['ID'];            
	$set_value = '';
	// make the update query
	foreach($old_data as $key => $value)
	{            
		$set_value .= "$key = '$value',";               
	}
	// remove the last comma
	$set_value = substr($set_value, 0, strlen($set_value) - 1);
	// result insert query
	$update_query = 'UPDATE '.$tbl_name.' SET '.$set_value.' WHERE itemID = '.$old_data['itemID'];
	R::exec($update_query);
	
	// build the criteria
	//DSResponse
	$response = new DSResponse();      
	$response->setData($old_data);            
	$response->setStatus(0);  
	return $response;
}
private function remove($request)
{
	$ds = $this->data_source;            
	// get the table name from data source
	$tbl_name = $ds['ID'];            
	$new_data = $request->data;
	// make the delete query
	$itemID = $new_data['itemID'];
	$delete_query = "DELETE FROM $tbl_name WHERE itemID = $itemID";
	// Run the insert query
	R::exec($delete_query);
										 
	//DSResponse
	$response = new DSResponse();      
	$response->setData($new_data);            
	$response->setStatus(0);  
	return $response;
}
private function fetch($request)
{   
	// get the DataSource
	$ds = $this->data_source;
	$tbl_name = $ds['ID'];
	 
	$query = "select * from $tbl_name s ";
	$query_count = "select count(*) from $tbl_name s";
	
	// build up the query
	if ( count($request->getDataKeys()) != 0 )
	{   
		$query .= ' where ';
		$query_count .= ' where ';
		 
		foreach($request->data as $key => $value)
		{                       
			// get the field    
			$field = $this->getField($key);
			if(!empty($field))                                        
			{
				$type = $field['type'];
				
				if( $type == "text" ||
					$type == "link" ||
					$type == "enum" ||
					$type == "image" ||
					$type == "ntext" )
				{
					$query .= "s." . $key . " like " . " '%" . $value . "%' and " ;        
				} else
				{
					"s." . $key . "=" . " '" . $value . "' and " ;        
				}
			}
								
			$query_count .= "s." . $key . " like " . "'%" . $value . "%' and " ;            
		}
		
		// remove 'and' of the query 
		$query = substr($query, 0, strlen($query)- 4);
		$query_count = substr($query_count, 0, strlen($query_count)- 4);
	}
	 
	// sort by
	if( !empty($request->sortBy) )
	{
		// set the orderBy
		$query .= " order by ";
		
		// we start to build a coma separated list of items. First item won't have coma
		// but every possible next will do
		$seperator = "";
		foreach($request->sortBy as $index => $sort)
		{ 
			
			// if column name is with -, then ordering is descending, otherwise ascending
			if ( strpos($index, '-') === 0 )
			{
				$query .= $seperator . $sort . " ASC";
			} else
			{
				$query .= $seperator . substr($sort, 1) . " DESC";
			}
			$separator  = ',';
		}
	}
	  
	// convert the payload to our DRequest object            
	// get the count        
	$products_count = R::getAll($query_count);
	$count = $products_count[0]['count(*)'];
	
	//DSResponse
	$response = new DSResponse();    
	 
	$products = R::getAll($query);     
	$response->setData($products);
	$response->setStartRow($request->startRow);
	$response->setEndRow($request->endRow);
	$response->setTotalRows($count);
	$response->setStatus(0);  
	
	// sanity check, if no rows, return 0
	if ($response->getEndRow() < 0 ) 
	{
		$response->setEndRow(0);
	}
	
	return $response;
}

...