...
Refactor the existing fetch() method and move out the existing criteria building code into a separate method called buildStandardCriteria():method:
Code Block | ||||
---|---|---|---|---|
| ||||
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:
...