Skip to content

Commit

Permalink
Atualizando as versões
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonsimaogoncalves committed Nov 9, 2020
1 parent bb2b945 commit bcb72f4
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 167 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
}
],
"require": {
"php": "^7.2",
"cakephp/cakephp": "^4.0",
"jeffersonsimaogoncalves/cakephp-utility": "^4.1.0"
"php": ">=7.0",
"cakephp/cakephp": "^3.6",
"jeffersonsimaogoncalves/cakephp-utility": "^5.0"
},
"support": {
"issues": "https://github.com/jeffersonsimaogoncalves/cakephp-datatables/issues",
Expand Down
90 changes: 35 additions & 55 deletions src/Controller/Component/DataTablesComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\ORM\Query;
use DataTables\Lib\ColumnDefinitions;
use Exception;
use InvalidArgumentException;
use JeffersonSimaoGoncalves\Utils\CallbackTrait;

/**
Expand Down Expand Up @@ -69,15 +67,15 @@ class DataTablesComponent extends Component
private $tables = [];

/**
* @param array $config
* @param array $config
*/
public function initialize(array $config): void
public function initialize(array $config)
{
/* Set default comparison operators for field types */
if (Configure::check('DataTables.ComparisonOperators')) {
$operators = Configure::read('DataTables.ComparisonOperators');
$this->_defaultComparison = array_merge($this->_defaultComparison, $operators);
}
};

/* setup column definitions */
$this->_columns = new ColumnDefinitions();
Expand All @@ -94,19 +92,18 @@ public function columns()
/**
* Find data
*
* @param string $tableName : ORM table name
* @param string $finder : Finder name (as in Table::find())
* @param array $options : Finder options (as in Table::find())
* @param array $columns : Column definitions needed for filter/order operations
* @param string $tableName : ORM table name
* @param string $finder : Finder name (as in Table::find())
* @param array $options : Finder options (as in Table::find())
* @param array $columns : Column definitions needed for filter/order operations
*
* @return Query to be evaluated (Query::count() may have already been called)
*/
public function find(string $tableName, string $finder = 'all', array $options = [], array $columns = []): Query
{
$delegateSearch = $options['delegateSearch'] ?? false;
if (empty($columns)) {
if (empty($columns))
$columns = $this->_columns;
}

// -- get table object
$this->_table = $this->getTableLocator()->get($tableName);
Expand Down Expand Up @@ -158,6 +155,7 @@ public function find(string $tableName, string $finder = 'all', array $options =
$this->_setViewVars();

return $data;

}

/**
Expand All @@ -166,51 +164,43 @@ public function find(string $tableName, string $finder = 'all', array $options =
private function _draw()
{
$drawParam = $this->getController()->getRequest()->getQuery('draw');
if (!$drawParam) {
if (!$drawParam)
return;
}

$this->_viewVars['draw'] = (int) $drawParam;
$this->_viewVars['draw'] = (int)$drawParam;
}

/**
* Process query data of ajax request regarding order
* Alters $options if delegateOrder is set
* In this case, the model needs to handle the 'customOrder' option.
*
* @param $options : Query options
* @param ColumnDefinitions|array Column definitions
* @param $options : Query options
* @param ColumnDefinitions|array Column definitions
*/
private function _order(array &$options, &$columns)
{
$queryParams = $this->getController()->getRequest()->getQueryParams();

if (empty($queryParams['order'])) {
if (empty($queryParams['order']))
return;
}

$order = $this->getConfig('order');
/* extract custom ordering from request */
foreach ($queryParams['order'] as $item) {
if (!count($columns)) // note: empty() does not work on objects
{
throw new InvalidArgumentException('Column ordering requested, but no column definitions provided.');
}
throw new \InvalidArgumentException('Column ordering requested, but no column definitions provided.');

$dir = strtoupper($item['dir']);
if (!in_array($dir, ['ASC', 'DESC'])) {
if (!in_array($dir, ['ASC', 'DESC']))
throw new BadRequestException('Malformed order direction.');
}

$c = $columns[$item['column']] ?? null;
if (!$c || !($c['orderable'] ?? true)) // orderable is true by default
{
throw new BadRequestException('Illegal column ordering.');
}

if (empty($c['field'])) {
throw new InvalidArgumentException('Column description misses field name.');
}
if (empty($c['field']))
throw new \InvalidArgumentException('Column description misses field name.');

$order[$c['field']] = $dir;
}
Expand All @@ -231,8 +221,8 @@ private function _order(array &$options, &$columns)
* Alters $options if delegateSearch is set
* In this case, the model needs to handle the 'globalSearch' option.
*
* @param $options : Query options
* @param ColumnDefinitions|array $columns Column definitions
* @param $options : Query options
* @param ColumnDefinitions|array $columns Column definitions
*
* @return bool : true if additional filtering takes place
*/
Expand All @@ -245,7 +235,7 @@ private function _filter(array &$options, &$columns): bool
$this->setConfig('length', $queryParams['length']);
}
if (!empty($queryParams['start'])) {
$this->setConfig('start', (int) $queryParams['start']);
$this->setConfig('start', (int)$queryParams['start']);
}

$haveFilters = false;
Expand All @@ -258,23 +248,19 @@ private function _filter(array &$options, &$columns): bool
/* add global filter (general search field) */
$globalSearch = $this->getConfig('search');
if ($globalSearch) {
if (empty($columns)) {
throw new InvalidArgumentException('Filtering requested, but no column definitions provided.');
}
if (empty($columns))
throw new \InvalidArgumentException('Filtering requested, but no column definitions provided.');

if ($delegateSearch) {
$options['globalSearch'] = $globalSearch;
$haveFilters = true;
} else {
foreach ($columns as $c) {
if (!($c['searchable'] ?? true)) // searchable is true by default
{
continue;
}

if (empty($c['field'])) {
throw new InvalidArgumentException('Column description misses field name.');
}
if (empty($c['field']))
throw new \InvalidArgumentException('Column description misses field name.');

$this->_addCondition($c['field'], $globalSearch, 'or');
$haveFilters = true;
Expand All @@ -287,19 +273,14 @@ private function _filter(array &$options, &$columns): bool
$localSearch = $column['search']['value'] ?? null;
if (!empty($localSearch)) {
if (!count($columns)) // note: empty() does not work on objects
{
throw new InvalidArgumentException('Filtering requested, but no column definitions provided.');
}
throw new \InvalidArgumentException('Filtering requested, but no column definitions provided.');

$c = $columns[$index] ?? null;
if (!$c || !($c['searchable'] ?? true)) // searchable is true by default
{
throw new BadRequestException('Illegal filter request.');
}

if (empty($c['field'])) {
throw new InvalidArgumentException('Column description misses field name.');
}
if (empty($c['field']))
throw new \InvalidArgumentException('Column description misses field name.');

if ($delegateSearch) {
$options['localSearch'][$c['field']] = $localSearch;
Expand All @@ -316,7 +297,7 @@ private function _filter(array &$options, &$columns): bool
/**
* @param $column
* @param $value
* @param string $type
* @param string $type
*/
private function _addCondition($column, $value, $type = 'and')
{
Expand All @@ -335,12 +316,12 @@ private function _addCondition($column, $value, $type = 'and')
try {
$table = $this->_table->getAssociation($tableAlias);
$this->tables[$tableAlias] = $table;
} catch (Exception $e) {
} catch (\Exception $e) {
foreach ($this->tables as $t) {
try {
$table = $t->getAssociation($tableAlias);
$this->tables[$tableAlias] = $table;
} catch (Exception $e) {
} catch (\Exception $e) {
}
}
}
Expand All @@ -367,7 +348,7 @@ private function _addCondition($column, $value, $type = 'and')
if ($value === null) {
return;
}
} catch (Exception $e) {
} catch (\Exception $e) {
return;
}
}
Expand Down Expand Up @@ -408,8 +389,8 @@ private function _addCondition($column, $value, $type = 'and')
/**
* Get comparison operator by entity and column name.
*
* @param \Cake\ORM\Table|\Cake\ORM\Association $table : Target ORM table
* @param string $column : Database column name (may be in form Table.column)
* @param \Cake\ORM\Table|\Cake\ORM\Association $table : Target ORM table
* @param string $column : Database column name (may be in form Table.column)
*
* @return string : Database comparison operator
*/
Expand All @@ -423,9 +404,8 @@ protected function _getComparison($table, string $column): string

return strtolower($key) === strtolower($wanted);
});
if (!$userConfig->isEmpty()) {
if (!$userConfig->isEmpty())
return $userConfig->first();
}

/* Lookup per-field type configuration for the comparison operator */
$columnDesc = $table->getSchema()->getColumn($column);
Expand Down
Loading

0 comments on commit bcb72f4

Please sign in to comment.