From a4bca24a6f7e26404b04f1b4308f96e17c472b4b Mon Sep 17 00:00:00 2001 From: mzf Date: Thu, 4 Feb 2016 00:07:50 +0700 Subject: [PATCH 1/5] Added new feature: using model methods for table render with custom formatting. --- src/Adapters/AdapterInterface.php | 204 ++++++++++++++++-------------- src/Adapters/ArrayAdapter.php | 171 +++++++++++++------------ src/Adapters/QueryBuilder.php | 193 +++++++++++++++++++++------- src/Adapters/ResultSet.php | 178 +++++++++++++------------- src/DataTable.php | 133 ++++++++++--------- src/ParamsParser.php | 169 ++++++++++++++----------- 6 files changed, 606 insertions(+), 442 deletions(-) diff --git a/src/Adapters/AdapterInterface.php b/src/Adapters/AdapterInterface.php index 290f309..8a0f74a 100644 --- a/src/Adapters/AdapterInterface.php +++ b/src/Adapters/AdapterInterface.php @@ -4,112 +4,128 @@ use DataTables\ParamsParser; -abstract class AdapterInterface { - - protected $parser = null; - protected $columns = []; - protected $lentgh = 30; - - public function __construct($length) { - $this->length = $length; - } - - abstract public function getResponse(); - - public function setParser(ParamsParser $parser) { - $this->parser = $parser; - } - - public function setColumns(array $columns) { - $this->columns = $columns; - } - - public function getColumns() { - return $this->columns; - } - - public function columnExists($column) { - return in_array($column, $this->columns); - } - - public function getParser() { - return $this->parser; - } - - public function formResponse($options) { - $defaults = [ - 'total' => 0, - 'filtered' => 0, - 'data' => [] - ]; - $options += $defaults; - - $response = []; - $response['draw'] = $this->parser->getDraw(); - $response['recordsTotal'] = $options['total']; - $response['recordsFiltered'] = $options['filtered']; - - if (count($options['data'])) { - foreach($options['data'] as $item) { - if (isset($item['id'])) { - $item['DT_RowId'] = $item['id']; - } +abstract class AdapterInterface +{ - $response['data'][] = $item; - } - } else { - $response['data'] = []; - } + protected $parser = null; + protected $columns = []; + protected $lentgh = 30; - return $response; - } + public function __construct($length) + { + $this->length = $length; + } - public function sanitaze($string) { - return mb_substr($string, 0, $this->length); - } + abstract public function getResponse(); - public function bind($case, $closure) { - switch($case) { - case "global_search": - $search = $this->parser->getSearchValue(); - if (!mb_strlen($search)) return; + public function setParser(ParamsParser $parser) + { + $this->parser = $parser; + } - foreach($this->parser->getSearchableColumns() as $column) { - if (!$this->columnExists($column)) continue; - $closure($column, $this->sanitaze($search)); - } - break; - case "column_search": - $columnSearch = $this->parser->getColumnsSearch(); - if (!$columnSearch) return; - - foreach($columnSearch as $key => $column) { - if (!$this->columnExists($column['data'])) continue; - $closure($column['data'], $this->sanitaze($column['search']['value'])); - } - break; - case "order": - $order = $this->parser->getOrder(); - if (!$order) return; + public function setColumns(array $columns) + { + $this->columns = $columns; + } - $orderArray = []; + public function getColumns() + { + return $this->columns; + } - foreach($order as $orderBy) { - if (!isset($orderBy['dir']) || !isset($orderBy['column'])) continue; - $orderDir = $orderBy['dir']; + public function columnExists($column) + { + return in_array($column, $this->columns); + } - $column = $this->parser->getColumnById($orderBy['column']); - if (is_null($column) || !$this->columnExists($column)) continue; + public function getParser() + { + return $this->parser; + } - $orderArray[] = "{$column} {$orderDir}"; + public function formResponse($options) + { + $defaults = [ + 'total' => 0, + 'filtered' => 0, + 'data' => [] + ]; + $options += $defaults; + + $response = []; + $response['draw'] = $this->parser->getDraw(); + $response['recordsTotal'] = $options['total']; + $response['recordsFiltered'] = $options['filtered']; + + if (count($options['data'])) { + foreach ($options['data'] as $item) { + if (isset($item['id'])) { + $item['DT_RowId'] = $item['id']; + } + + $response['data'][] = $item; + } + } else { + $response['data'] = []; } - $closure($orderArray); - break; - default: - throw new \Exception('Unknown bind type'); + return $response; } - } + public function sanitaze($string) + { + return mb_substr($string, 0, $this->length); + } + + public function bind($case, $closure) + { + switch ($case) { + case "global_search": + $search = $this->parser->getSearchValue(); + if (!mb_strlen($search)) + return; + + foreach ($this->parser->getSearchableColumns() as $column) { + if (!$this->columnExists($column)) + continue; + $closure($column, $this->sanitaze($search)); + } + break; + case "column_search": + $columnSearch = $this->parser->getColumnsSearch(); + if (!$columnSearch) + return; + + foreach ($columnSearch as $key => $column) { + if (!$this->columnExists($column['data'])) + continue; + $closure($column['data'], $this->sanitaze($column['search']['value'])); + } + break; + case "order": + $order = $this->parser->getOrder(); + if (!$order) + return; + + $orderArray = []; + + foreach ($order as $orderBy) { + if (!isset($orderBy['dir']) || !isset($orderBy['column'])) + continue; + $orderDir = $orderBy['dir']; + + $column = $this->parser->getColumnById($orderBy['column']); + if (is_null($column) || !$this->columnExists($column)) + continue; + + $orderArray[] = "{$column} {$orderDir}"; + } + + $closure($orderArray); + break; + default: + throw new \Exception('Unknown bind type'); + } + } } diff --git a/src/Adapters/ArrayAdapter.php b/src/Adapters/ArrayAdapter.php index 3f3086c..2be492d 100644 --- a/src/Adapters/ArrayAdapter.php +++ b/src/Adapters/ArrayAdapter.php @@ -2,101 +2,106 @@ namespace DataTables\Adapters; -class ArrayAdapter extends AdapterInterface { - - protected $array = []; - protected $column = []; - protected $global = []; - protected $order = []; - - public function setArray(array $array) { - $this->array = $array; - } - - public function getResponse() { - $limit = $this->parser->getLimit(); - $offset = $this->parser->getOffset(); - $total = count($this->array); - - $this->bind('global_search', function($column, $search) { - $this->global[$column][] = $search; - }); - - $this->bind('column_search', function($column, $search) { - $this->column[$column][] = $search; - }); - - $this->bind('order', function($order) { - $this->order = $order; - }); - - if(count($this->global) || count($this->column)) { - $items = array_filter($this->array, function($item) { - $check = false; - - if (count($this->global)) { - foreach($this->global as $column=>$filters) { - foreach($filters as $search) { - $check = (strpos($item[$column], $search) !== false); - if ($check) break 2; - } - } - } else { - $check = true; - } +class ArrayAdapter extends AdapterInterface +{ - if (count($this->column) && $check) { - foreach($this->column as $column=>$filters) { - foreach($filters as $search) { - $check = (strpos($item[$column], $search) !== false); - if (!$check) break 2; - } - } - } + protected $array = []; + protected $column = []; + protected $global = []; + protected $order = []; - if ($check) { - return $item; - } - }); - } else { - $items = $this->array; + public function setArray(array $array) + { + $this->array = $array; } - $filtered = count($items); + public function getResponse() + { + $limit = $this->parser->getLimit(); + $offset = $this->parser->getOffset(); + $total = count($this->array); + + $this->bind('global_search', function($column, $search) { + $this->global[$column][] = $search; + }); + + $this->bind('column_search', function($column, $search) { + $this->column[$column][] = $search; + }); + + $this->bind('order', function($order) { + $this->order = $order; + }); + + if (count($this->global) || count($this->column)) { + $items = array_filter($this->array, function($item) { + $check = false; + + if (count($this->global)) { + foreach ($this->global as $column => $filters) { + foreach ($filters as $search) { + $check = (strpos($item[$column], $search) !== false); + if ($check) + break 2; + } + } + } else { + $check = true; + } + + if (count($this->column) && $check) { + foreach ($this->column as $column => $filters) { + foreach ($filters as $search) { + $check = (strpos($item[$column], $search) !== false); + if (!$check) + break 2; + } + } + } + + if ($check) { + return $item; + } + }); + } else { + $items = $this->array; + } - if ($this->order) { - $args = []; + $filtered = count($items); - foreach($this->order as $order) { - $tmp = []; - list($column, $dir) = explode(' ', $order); + if ($this->order) { + $args = []; - foreach($items as $key=>$item) { - $tmp[$key] = $item[$column]; - } + foreach ($this->order as $order) { + $tmp = []; + list($column, $dir) = explode(' ', $order); - $args[] = $tmp; - $args[] = ($dir == 'desc') ? SORT_DESC : SORT_ASC; - } + foreach ($items as $key => $item) { + $tmp[$key] = $item[$column]; + } + $args[] = $tmp; + $args[] = ($dir == 'desc') ? SORT_DESC : SORT_ASC; + } - $args[] = &$items; - call_user_func_array('array_multisort', $args); - } - if ($offset > 1) { - $items = array_slice($items, $offset); - } + $args[] = &$items; + call_user_func_array('array_multisort', $args); + } - if ($limit) { - $items = array_slice($items, 0, $limit); - } + if ($offset > 1) { + $items = array_slice($items, $offset); + } + + if ($limit) { + $items = array_slice($items, 0, $limit); + } - return $this->formResponse([ - 'total' => (int)$total, - 'filtered' => (int)$filtered, - 'data' => $items, - ]); - } + return $this->formResponse([ + 'total' => (int) $total, + 'filtered' => (int) $filtered, + 'data' => $items, + ]); + } } diff --git a/src/Adapters/QueryBuilder.php b/src/Adapters/QueryBuilder.php index fd394db..30cd228 100644 --- a/src/Adapters/QueryBuilder.php +++ b/src/Adapters/QueryBuilder.php @@ -1,49 +1,154 @@ builder = $builder; - } - - public function getResponse() { - $builder = new PQueryBuilder([ - 'builder' => $this->builder, - 'limit' => 1, - 'page' => 1, - ]); - - $total = $builder->getPaginate(); - - $this->bind('global_search', function($column, $search) { - $this->builder->orWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]); - }); - - $this->bind('column_search', function($column, $search) { - $this->builder->andWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]); - }); - - $this->bind('order', function($order) { - if (!empty($order)) { - $this->builder->orderBy(implode(', ', $order)); - } - }); - - $builder = new PQueryBuilder([ - 'builder' => $this->builder, - 'limit' => $this->parser->getLimit(), - 'page' => $this->parser->getPage(), - ]); - - $filtered = $builder->getPaginate(); - - return $this->formResponse([ - 'total' => $total->total_items, - 'filtered' => $filtered->total_items, - 'data' => $filtered->items->toArray(), - ]); - } +class QueryBuilder extends AdapterInterface +{ + + /** + * + * @var \Phalcon\Mvc\Model\Query\Builder + */ + protected $builder; + protected $originalColumns; + + public function setBuilder($builder) + { + $this->builder = $builder; + } + + public function setColumns(array $columns) + { + $this->originalColumns = $columns; + + foreach ($columns as $i => $column) { + if (is_array($column)) { + $columns[$i] = array_keys($column)[0]; + } + } + + $this->columns = $columns; + } + + public function getResponse() + { + $builder = new PQueryBuilder([ + 'builder' => $this->builder, + 'limit' => 1, + 'page' => 1, + ]); + + $total = $builder->getPaginate(); + + $this->bind('global_search', function($column, $search) { + $this->builder->orWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]); + }); + + $this->bind('column_search', function($column, $search) { + $this->builder->andWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]); + }); + + $this->bind('order', function($order) { + + if (!empty($order)) { + $this->builder->orderBy(implode(', ', $order)); + } + }); + + $builder = new PQueryBuilder([ + 'builder' => $this->builder, + 'limit' => $this->parser->getLimit(), + 'page' => $this->parser->getPage(), + ]); + + + /* @var $filtered \Phalcon\Mvc\Model\Resultset */ + $filtered = $builder->getPaginate(); + + /* @var $metadata \Phalcon\Mvc\Model\MetaData */ + $metadata = \Phalcon\Di::getDefault()->get('modelsMetadata'); + + $item = $filtered->items->getFirst(); + if ($item instanceof \Phalcon\Mvc\Model) { + $filtered->items->rewind(); + $columnMap = $metadata->getColumnMap($item); + $columnMap = array_combine($columnMap, $columnMap); + + $extractMethods = function ($item) { + $reflection = new \ReflectionClass($item); + $itemMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); + $itemMethods = array_map(function(\ReflectionMethod $reflectionMethod) { + return $reflectionMethod->getName(); + }, $itemMethods); + return array_combine($itemMethods, $itemMethods); + }; + + // if use array_diff we can catch error, because $this->originalColumns can have array item + $attributes = $methods = []; + foreach ($this->originalColumns as $itemColumn) { + $itemData = []; + if (is_string($itemColumn)) { + // check that it is item attribute + if (isset($columnMap[$itemColumn])) { + $attributes[] = $itemColumn; + } + } elseif (is_array($itemColumn)) { + /** + * Possible variants + * itemColumn => [methodName => [param1, param2]] - method with parameters + * itemColumn => methodName] - method without parameters + * + */ + $columnName = array_keys($itemColumn)[0]; + $methodData = $itemColumn[$columnName]; + + if (!isset($columnMap[$columnName])) { + // undefined columnName + continue; + } + $parameters = null; + if (is_array($methodData)) { + $methodName = array_keys($methodData)[0]; + $parameters = $methodData[$methodName]; + } else { + $methodName = $methodData; + } + // check that it is existed method + if (empty($itemMethods)) { + $itemMethods = $extractMethods($item); + } + + if (isset($itemMethods[$methodName])) { + $methods[$columnName] = compact('methodName', 'parameters'); + } + } + } + + $data = []; + foreach ($filtered->items as $item) { + $itemData = []; + foreach ($attributes as $attr) { + $itemData[$attr] = $item->readAttribute($attr); + } + + foreach ($methods as $columnName => $method) { + $parameters = !empty($method['parameters']) ? $method['parameters'] : null; + $itemData[$columnName] = call_user_func_array([$item, $method['methodName']], $parameters); + } + + $data[] = $itemData; + } + } else { + $data = $filtered->items->toArray(); + } + + return $this->formResponse([ + 'total' => $total->total_items, + 'filtered' => $filtered->total_items, + 'data' => $data, + ]); + } + } diff --git a/src/Adapters/ResultSet.php b/src/Adapters/ResultSet.php index 5a50e7e..713044c 100644 --- a/src/Adapters/ResultSet.php +++ b/src/Adapters/ResultSet.php @@ -1,108 +1,114 @@ parser->getLimit(); - $offset = $this->parser->getOffset(); - $total = $this->resultSet->count(); - - $this->bind('global_search', function($column, $search) { - $this->global[$column][] = $search; - }); +class ResultSet extends AdapterInterface +{ + + protected $resultSet; + protected $column = []; + protected $global = []; + protected $order = []; + + public function getResponse() + { + $limit = $this->parser->getLimit(); + $offset = $this->parser->getOffset(); + $total = $this->resultSet->count(); + + $this->bind('global_search', function($column, $search) { + $this->global[$column][] = $search; + }); + + $this->bind('column_search', function($column, $search) { + $this->column[$column][] = $search; + }); + + $this->bind('order', function($order) { + $this->order = $order; + }); + + if (count($this->global) || count($this->column)) { + $filter = $this->resultSet->filter(function($item) { + $check = false; + + if (count($this->global)) { + foreach ($this->global as $column => $filters) { + foreach ($filters as $search) { + $check = (strpos($item->$column, $search) !== false); + if ($check) + break 2; + } + } + } else { + $check = true; + } + + if (count($this->column) && $check) { + foreach ($this->column as $column => $filters) { + foreach ($filters as $search) { + $check = (strpos($item->$column, $search) !== false); + if (!$check) + break 2; + } + } + } + + if ($check) { + return $item; + } + }); + + $filtered = count($filter); + $items = array_map(function($item) { + return $item->toArray(); + }, $filter); + } else { + $filtered = $total; + $items = $this->resultSet->filter(function($item) { + return $item->toArray(); + }); + } - $this->bind('column_search', function($column, $search) { - $this->column[$column][] = $search; - }); + if ($this->order) { + $args = []; - $this->bind('order', function($order) { - $this->order = $order; - }); + foreach ($this->order as $order) { + $tmp = []; + list($column, $dir) = explode(' ', $order); - if(count($this->global) || count($this->column)) { - $filter = $this->resultSet->filter(function($item){ - $check = false; + foreach ($items as $key => $item) { + $tmp[$key] = $item[$column]; + } - if (count($this->global)) { - foreach($this->global as $column=>$filters) { - foreach($filters as $search) { - $check = (strpos($item->$column, $search) !== false); - if ($check) break 2; + $args[] = $tmp; + $args[] = ($dir == 'desc') ? SORT_DESC : SORT_ASC; } - } - } else { - $check = true; - } - if (count($this->column) && $check) { - foreach($this->column as $column=>$filters) { - foreach($filters as $search) { - $check = (strpos($item->$column, $search) !== false); - if (!$check) break 2; - } - } + $args[] = &$items; + call_user_func_array('array_multisort', $args); } - if ($check) { - return $item; + if ($offset > 1) { + $items = array_slice($items, ($offset - 1)); } - }); - - $filtered = count($filter); - $items = array_map(function($item) { - return $item->toArray(); - }, $filter); - } else { - $filtered = $total; - $items = $this->resultSet->filter(function($item) { - return $item->toArray(); - }); - } - - if ($this->order) { - $args = []; - foreach($this->order as $order) { - $tmp = []; - list($column, $dir) = explode(' ', $order); - - foreach($items as $key=>$item) { - $tmp[$key] = $item[$column]; + if ($limit) { + $items = array_slice($items, 0, $limit); } - $args[] = $tmp; - $args[] = ($dir == 'desc') ? SORT_DESC : SORT_ASC; - } - - $args[] = &$items; - call_user_func_array('array_multisort', $args); + return $this->formResponse([ + 'total' => (int) $total, + 'filtered' => (int) $filtered, + 'data' => $items, + ]); } - if ($offset > 1) { - $items = array_slice($items, ($offset - 1)); + public function setResultSet($resultSet) + { + $this->resultSet = $resultSet; } - if ($limit) { - $items = array_slice($items, 0, $limit); - } - - return $this->formResponse([ - 'total' => (int)$total, - 'filtered' => (int)$filtered, - 'data' => $items, - ]); - } - - public function setResultSet($resultSet) { - $this->resultSet = $resultSet; - } - } diff --git a/src/DataTable.php b/src/DataTable.php index ad4bdd2..42ae149 100644 --- a/src/DataTable.php +++ b/src/DataTable.php @@ -1,4 +1,5 @@ 20, - 'length' => 50, - ]; + /** + * + * @var ParamsParser + */ + public $parser; - $this->options = $options + $default; - $this->parser = new ParamsParser($this->options['limit']); - } + public function __construct($options = []) + { + $default = [ + 'limit' => 20, + 'length' => 50, + ]; - public function getParams() { - return $this->parser->getParams(); - } + $this->options = $options + $default; + $this->parser = new ParamsParser($this->options['limit']); + } - public function getResponse() { - return !empty($this->response) ? $this->response : []; - } + public function getParams() + { + return $this->parser->getParams(); + } - public function sendResponse() { - if ($this->di->has('view')) { - $this->di->get('view')->disable(); + public function getResponse() + { + return !empty($this->response) ? $this->response : []; } - $response = new Response(); - $response->setContentType('application/json', 'utf8'); - $response->setJsonContent($this->getResponse()); - $response->send(); - } + public function sendResponse() + { + if ($this->di->has('view')) { + $this->di->get('view')->disable(); + } - public function fromBuilder($builder, $columns = []) { - if (empty($columns)) { - $columns = $builder->getColumns(); - $columns = (is_array($columns)) ? $columns : array_map('trim', explode(',', $columns)); + $response = new Response(); + $response->setContentType('application/json', 'utf8'); + $response->setJsonContent($this->getResponse()); + $response->send(); } - $adapter = new QueryBuilder($this->options['length']); - $adapter->setBuilder($builder); - $adapter->setParser($this->parser); - $adapter->setColumns($columns); - $this->response = $adapter->getResponse(); + public function fromBuilder($builder, $columns = []) + { + if (empty($columns)) { + $columns = $builder->getColumns(); + $columns = (is_array($columns)) ? $columns : array_map('trim', explode(',', $columns)); + } - return $this; - } + $adapter = new QueryBuilder($this->options['length']); + $adapter->setBuilder($builder); + $adapter->setParser($this->parser); + $adapter->setColumns($columns); + $this->response = $adapter->getResponse(); - public function fromResultSet($resultSet, $columns = []) { - if(empty($columns) && $resultSet->count() > 0) { - $columns = array_keys($resultSet->getFirst()->toArray()); - $resultSet->rewind(); + + return $this; } - $adapter = new ResultSet($this->options['length']); - $adapter->setResultSet($resultSet); - $adapter->setParser($this->parser); - $adapter->setColumns($columns); - $this->response = $adapter->getResponse(); + public function fromResultSet($resultSet, $columns = []) + { + if (empty($columns) && $resultSet->count() > 0) { + $columns = array_keys($resultSet->getFirst()->toArray()); + $resultSet->rewind(); + } - return $this; - } + $adapter = new ResultSet($this->options['length']); + $adapter->setResultSet($resultSet); + $adapter->setParser($this->parser); + $adapter->setColumns($columns); + $this->response = $adapter->getResponse(); - public function fromArray($array, $columns = []) { - if(empty($columns) && count($array) > 0) { - $columns = array_keys(current($array)); + return $this; } - $adapter = new ArrayAdapter($this->options['length']); - $adapter->setArray($array); - $adapter->setParser($this->parser); - $adapter->setColumns($columns); - $this->response = $adapter->getResponse(); + public function fromArray($array, $columns = []) + { + if (empty($columns) && count($array) > 0) { + $columns = array_keys(current($array)); + } + + $adapter = new ArrayAdapter($this->options['length']); + $adapter->setArray($array); + $adapter->setParser($this->parser); + $adapter->setColumns($columns); + $this->response = $adapter->getResponse(); - return $this; - } + return $this; + } } diff --git a/src/ParamsParser.php b/src/ParamsParser.php index 36e6a07..c3d4bee 100644 --- a/src/ParamsParser.php +++ b/src/ParamsParser.php @@ -1,82 +1,99 @@ null, - 'start' => 1, - 'length' => $limit, - 'columns' => [], - 'search' => [], - 'order' => [] - ]; - - $request = $this->di->get('request'); - $requestParams = $request->isPost() ? $request->getPost() : $request->getQuery(); - $this->params = (array)$requestParams + $params; - $this->setPage(); - } - - public function getParams() { - return $this->params; - } - - public function setPage() { - $this->page = (int)(floor($this->params['start'] / $this->params['length']) + 1); - } - - public function getPage() { - return $this->page; - } - - public function getColumnsSearch() { - return array_filter(array_map(function($item) { - return (isset($item['search']['value']) && strlen($item['search']['value'])) ? $item : null; - }, $this->params['columns'])); - } - - public function getSearchableColumns() { - return array_filter(array_map(function($item) { - return (isset($item['searchable']) && $item['searchable'] === "true") ? $item['data'] : null; - }, $this->params['columns'])); - } - - public function getDraw() { - return $this->params['draw']; - } - - public function getLimit() { - return $this->params['length']; - } - - public function getOffset() { - return $this->params['start']; - } - - public function getColumns() { - return $this->params['columns']; - } - - public function getColumnById($id) { - return isset($this->params['columns'][$id]['data']) ? $this->params['columns'][$id]['data'] : null; - } - - public function getSearch() { - return $this->params['search']; - } - - public function getOrder() { - return $this->params['order']; - } - - public function getSearchValue() { - return isset($this->params['search']['value']) ? $this->params['search']['value'] : ''; - } +class ParamsParser extends Component +{ + + protected $params = []; + protected $page = 1; + + public function __construct($limit) + { + $params = [ + 'draw' => null, + 'start' => 1, + 'length' => $limit, + 'columns' => [], + 'search' => [], + 'order' => [] + ]; + + $request = $this->di->get('request'); + $requestParams = $request->isPost() ? $request->getPost() : $request->getQuery(); + $this->params = (array) $requestParams + $params; + $this->setPage(); + } + + public function getParams() + { + return $this->params; + } + + public function setPage() + { + $this->page = (int) (floor($this->params['start'] / $this->params['length']) + 1); + } + + public function getPage() + { + return $this->page; + } + + public function getColumnsSearch() + { + return array_filter(array_map(function($item) { + return (isset($item['search']['value']) && strlen($item['search']['value'])) ? $item : null; + }, $this->params['columns'])); + } + + public function getSearchableColumns() + { + return array_filter(array_map(function($item) { + return (isset($item['searchable']) && $item['searchable'] === "true") ? $item['data'] : null; + }, $this->params['columns'])); + } + + public function getDraw() + { + return $this->params['draw']; + } + + public function getLimit() + { + return $this->params['length']; + } + + public function getOffset() + { + return $this->params['start']; + } + + public function getColumns() + { + return $this->params['columns']; + } + + public function getColumnById($id) + { + return isset($this->params['columns'][$id]['data']) ? $this->params['columns'][$id]['data'] : null; + } + + public function getSearch() + { + return $this->params['search']; + } + + public function getOrder() + { + return $this->params['order']; + } + + public function getSearchValue() + { + return isset($this->params['search']['value']) ? $this->params['search']['value'] : ''; + } + } From 72bdd88489f19f609d1ff23fb24a9cfb205d7df5 Mon Sep 17 00:00:00 2001 From: mzf Date: Thu, 4 Feb 2016 11:46:54 +0700 Subject: [PATCH 2/5] Added possibility to getting values only from item method, thats not assigned to any field Example['actions' => ['tableActions' => ['param']], 'fake' => true] --- src/Adapters/QueryBuilder.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Adapters/QueryBuilder.php b/src/Adapters/QueryBuilder.php index 30cd228..4e38d9c 100644 --- a/src/Adapters/QueryBuilder.php +++ b/src/Adapters/QueryBuilder.php @@ -25,7 +25,16 @@ public function setColumns(array $columns) foreach ($columns as $i => $column) { if (is_array($column)) { - $columns[$i] = array_keys($column)[0]; + $columnName = array_keys($column)[0]; + // check thats not fake field + // if column faked and get values from item method we dont add column + if (!empty($column['fake'])) { + unset($columns[$i]); + } else { + $columns[$i] = $columnName; + } + } else { + $columns[$i] = $column; } } @@ -106,7 +115,7 @@ public function getResponse() if (!isset($columnMap[$columnName])) { // undefined columnName - continue; + //continue; } $parameters = null; if (is_array($methodData)) { @@ -134,7 +143,7 @@ public function getResponse() } foreach ($methods as $columnName => $method) { - $parameters = !empty($method['parameters']) ? $method['parameters'] : null; + $parameters = !empty($method['parameters']) ? $method['parameters'] : []; $itemData[$columnName] = call_user_func_array([$item, $method['methodName']], $parameters); } From 8a2b8f424edabab66cd02d0b741ca095d8dedf7d Mon Sep 17 00:00:00 2001 From: mzf Date: Tue, 9 Feb 2016 17:03:20 +0700 Subject: [PATCH 3/5] Changed a method of obtaining list of columns. --- src/Adapters/QueryBuilder.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Adapters/QueryBuilder.php b/src/Adapters/QueryBuilder.php index 4e38d9c..0959b4c 100644 --- a/src/Adapters/QueryBuilder.php +++ b/src/Adapters/QueryBuilder.php @@ -82,7 +82,8 @@ public function getResponse() $item = $filtered->items->getFirst(); if ($item instanceof \Phalcon\Mvc\Model) { $filtered->items->rewind(); - $columnMap = $metadata->getColumnMap($item); + + $columnMap = $metadata->getAttributes($item); $columnMap = array_combine($columnMap, $columnMap); $extractMethods = function ($item) { From 1341d83be7aa75eb58a0912a1237e30abb836339 Mon Sep 17 00:00:00 2001 From: mzf Date: Thu, 18 Feb 2016 11:20:51 +0700 Subject: [PATCH 4/5] Fixed error arising by search in the table that having fake field --- src/Adapters/AdapterInterface.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Adapters/AdapterInterface.php b/src/Adapters/AdapterInterface.php index 8a0f74a..784841d 100644 --- a/src/Adapters/AdapterInterface.php +++ b/src/Adapters/AdapterInterface.php @@ -35,7 +35,19 @@ public function getColumns() public function columnExists($column) { - return in_array($column, $this->columns); + $notFakeField = true; + if (is_array($this->originalColumns)) { + foreach ($this->originalColumns as $columnDefinition) { + if (!is_array($columnDefinition) || array_keys($columnDefinition)[0] != $column) { + continue; + } + if (!empty($columnDefinition[$column]['fake'])) { + $notFakeField = false; + } + } + } + + return in_array($column, $this->columns) && $notFakeField; } public function getParser() From ef8affb892b5213566b9c424873957dc86899fb8 Mon Sep 17 00:00:00 2001 From: mzf Date: Thu, 18 Feb 2016 18:32:44 +0700 Subject: [PATCH 5/5] Travis-CI fix --- src/Adapters/AdapterInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/AdapterInterface.php b/src/Adapters/AdapterInterface.php index 784841d..586eb1b 100644 --- a/src/Adapters/AdapterInterface.php +++ b/src/Adapters/AdapterInterface.php @@ -36,7 +36,7 @@ public function getColumns() public function columnExists($column) { $notFakeField = true; - if (is_array($this->originalColumns)) { + if (isset($this->originalColumns) && is_array($this->originalColumns)) { foreach ($this->originalColumns as $columnDefinition) { if (!is_array($columnDefinition) || array_keys($columnDefinition)[0] != $column) { continue;