...
Code Block |
---|
language | php |
---|
title | RPCManager.php |
---|
|
private function processTransaction()
{
// retrieve the requests with data in form of supplyItem
// store transaction num, we'll use it later to see if there was a transaction or not
$transactionNum = $transaction_request['transactionNum'];
$idx = 0;
$queueFailed = false;
$res_list = array();
$operations = $transaction_request['operations'];
// create transaction
R::begin();
try{
foreach($operations as $op)
{
$res = $op;
// parse advanced criterias, if any
$advancedCriteria = $this->parseAdvancedCriterias($op);
... ... ... ... ...
}
// if there were no errors, commit the transaction
if (!$queueFailed)
{
R::commit();
}
}
catch(Exception $e) {
R::rollback();
}
... ... ... ... ...
} |
...
Code Block |
---|
language | php |
---|
title | DataSource.php |
---|
|
private function fetch($request)
{
// get the DataSource
$ds = $this->data_source;
$tbl_name = $ds['ID'];
// check the advanced cretira
if(empty($request->advancedCriteria))
{
$query_result = $this->buildStandardCriteria($request);
} else
{
$query_result = $this->buildAdvancedCriteria($request);
}
$query = "select * from $tbl_name s ";
$query .= $query_result;
// 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 = ',';
}
}
//DSResponse
$response = new DSResponse();
$products = R::getAll($query);
// get the count
$count = count($products);
$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;
}
private function buildStandardCriteria($request)
{
$query = '';
$query_count = '';
if ( count($request->getDataKeys()) != 0 )
{
$query .= ' 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, strrpos($query, 'and'));
}
return $query;
} |
Note a new method called buildAdvancedCriteria() is required which builds the query criteria from the AdvancedCriteria delivered by the FilterBuilder:
Code Block |
---|
language | php |
---|
title | RPCManager.php |
---|
|
private function buildAdvancedCriteria($request)
{
$advancedCriteria = $request->getAdvancedCriteria();
if(!empty($advancedCriteria))
{
$criteria_query = $this->buildCriterion($advancedCriteria);
$query = ' where '.$criteria_query;
return $query;
}
return null;
} |
For parsing the tree itself, introduce a recursive function:
Code Block |
---|
|
private function buildCriterion($advancedCriteria)
{
$criterias = $advancedCriteria['criteria'];
$operator = $advancedCriteria['operator'];
$result = '';
foreach($criterias as $c)
{
if(isset($c['fieldName']))
$fn = $c['fieldName'];
if(isset($c['operator']))
$op = $c['operator'];
if(isset($c['value']))
{
if ($c['value'] === TRUE )
$val = '1';
else if($c['value'] === FALSE)
$val = '0';
else
$val = $c['value'];
}
if(isset($c['start']))
$start = $c['start'];
if(isset($c['end']))
$end = $c['end'];
if(isset($c['criteria']))
$criteria = $c['criteria'];
else
$criteria = null;
if(empty($criteria))
{
switch($op)
{
case 'equals':
$query = "$fn = '$val'";
break;
case 'notEqual':
$query = "$fn != '$val'";
break;
case 'iEquals':
$query = "UPPER($fn) = UPPER('$val')";
break;
case 'iNotEqual':
$query = "UPPER($fn) != UPPER('$val')";
break;
case 'greaterThan':
$query = "$fn > '$val'";
break;
case 'lessThan':
$query = "$fn < '$val'";
break;
case 'greaterOrEqual':
$query = "$fn >= '$val'";
break;
case 'lessOrEqual':
$query = "$fn <= '$val'";
break;
case 'contains':
$query = "$fn LIKE '%$val%'";
break;
case 'startsWith':
$query = "$fn LIKE '$val%'";
break;
case 'endsWith':
$query = "$fn LIKE '%$val'";
break;
case 'iContains':
$query = "UPPER($fn) LIKE UPPER('%$val%')";
break;
case 'iStartsWith':
$query = "UPPER($fn) LIKE UPPER('$val%')";
break;
case 'iEndsWith':
$query = "UPPER($fn) LIKE UPPER('%$val')";
break;
case 'notContains':
$query = "$fn NOT LIKE '%$val%'";
break;
case 'notStartsWith':
$query = "$fn NOT LIKE '$val%'";
break;
case 'notEndsWith':
$query = "$fn NOT LIKE '%$val'";
break;
case 'iNotContains':
$query = "UPPER($fn) NOT LIKE UPPER('%$val%')";
break;
case 'iNotStartsWith':
$query = "UPPER($fn) NOT LIKE UPPER('$val%')";
break;
case 'iNotEndsWith':
$query = "UPPER($fn) NOT LIKE UPPER('%$val')";
break;
case 'isNull':
$query = "$fn IS NULL";
break;
case 'notNull':
$query = "$fn IS NOT NULL";
break;
case 'equalsField':
$query = "$fn LIKE CONCAT($val, '%')";
break;
case 'iEqualsField':
$query = "UPPER($fn) LIKE UPPER(CONCAT($val, '%'))";
break;
case 'iNotEqualField':
$query = "UPPER($fn) LIKE UPPER(CONCAT($val, '%'))";
break;
case 'notEqualField':
$query = "$fn NOT LIKE CONCAT($val, '%')";
break;
case 'greaterThanField':
$query = "$fn > CONCAT($val, '%')";
break;
case 'lessThanField':
$query = "$fn < CONCAT($val, '%')";
break;
case 'greaterOrEqualField':
$query = "$fn >= CONCAT($val, '%')";
break;
case 'lessOrEqualField':
$query = "$fn <= CONCAT($val, '%')";
break;
case 'iBetweenInclusive':
$query = "$fn BETWEEM $start AND $end";
break;
case 'betweenInclusive':
$query = "$fn BETWEEM $start AND $end";
break;
}
$result .= " ".$query." ".$operator." ";
}else
{
// build the list of subcriterias or criterions
$temp = $result;
$result1 = $this->buildCriterion($c);
$result = $temp . "(".$result1.") ".$operator." ";
}
}
$result_query = substr($result, 0, strrpos($result, $operator));
return $result_query;
} |
At this point you should be able to run the sample and use the FilterBuilder and see it affect the grid entries.