/**
* Prepares builder object before calling get method on it
*
* @throws CollectionException
*/
private function _prepareBuilder()
{
$builder = $this->_builder;
$params = Input::get();
/*
* START filters
*/
if ($this->renderFilterForm()) {
foreach ($this->_columns as $columnData) {
$column = new Column($columnData);
if (!$column->searchable()) {
// Not a searchable column - skip it
continue;
}
if (!isset($params['resource_table_' . $column->index()])) {
// Searched string not found in GET query
continue;
}
$value = $params['resource_table_' . $column->index()];
if (!$value || !is_string($value)) {
// Skip empty values
continue;
}
$value = $this->_prepareFilterValue($columnData, $value);
switch ($column->searchType()) {
// Use simple WHERE = 'value' for selects
case 'select':
if (ResourceTable::ALL_SELECT_VALUES_KEY == $value) {
// Any value in select - skip it
continue;
}
$builder = $this->_addQueryCondition($builder, $column->queryConditionType(), $column->getDatabaseName(), '=', $value);
break;
// Use LIKE '%value%' for strings
// Use LIKE '%value%' for strings
case 'string':
default:
$builder = $this->_addQueryCondition($builder, $column->queryConditionType(), $column->getDatabaseName(), 'LIKE', '%' . $value . '%');
break;
}
}
}
/*
* END filters
*/
/*
* START pagination
*/
if ($this->_paginate) {
if (isset($params['per_page']) && is_numeric($params['per_page']) && $params['per_page'] > 0) {
// Get per_page from GET
$this->_perPage = (int) $params['per_page'];
}
if (isset($params['page']) && is_numeric($params['page']) && $params['page'] > 1) {
// Get page from GET
$this->_page = (int) $params['page'];
}
$toSkip = $this->_page * $this->_perPage - $this->_perPage;
// A the end set pagination
$this->_totalItems = $this->_getCountForPagination($builder);
$builder = $builder->skip($toSkip)->take($this->_perPage);
}
/*
* END pagination
*/
/*
* START sort
*/
if ($this->_validSort($params)) {
// If sort configuration is valid then set it
$this->sort($params['order_by'], strtoupper($params['order_dir']));
}
// Set sort configuration
$sort = $this->getSort();
if ($sort['index'] && $sort['dir']) {
$builder = $builder->orderBy($sort['index'], $sort['dir']);
}
/*
* END sort
*/
$this->_builder = $builder;
}