Skip to content

Commit

Permalink
Update DataTableHelper
Browse files Browse the repository at this point in the history
Correção do bug de array_merge no parametros
  • Loading branch information
Jefferson Simão Gonçalves committed Aug 26, 2019
1 parent 1a43098 commit 0a4c70c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 91 deletions.
58 changes: 28 additions & 30 deletions src/Controller/Component/DataTablesComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,38 @@ class DataTablesComponent extends Component
CallbackTrait;

protected $_defaultConfig = [
'start' => 0,
'length' => 10,
'order' => [],
'search' => '',
'prefixSearch' => true, // use "LIKE …%" instead of "LIKE %…%" conditions
'conditionsOr' => [], // table-wide search conditions
'start' => 0,
'length' => 10,
'order' => [],
'search' => '',
'prefixSearch' => true, // use "LIKE …%" instead of "LIKE %…%" conditions
'conditionsOr' => [], // table-wide search conditions
'conditionsAnd' => [], // column search conditions
'matching' => [], // column search conditions for foreign tables
'comparison' => [], // per-column comparison definition
'matching' => [], // column search conditions for foreign tables
'comparison' => [], // per-column comparison definition
];

protected $_defaultComparison = [
'string' => 'LIKE',
'text' => 'LIKE',
'uuid' => 'LIKE',
'integer' => '=',
'string' => 'LIKE',
'text' => 'LIKE',
'uuid' => 'LIKE',
'integer' => '=',
'biginteger' => '=',
'float' => '=',
'decimal' => '=',
'boolean' => '=',
'binary' => 'LIKE',
'date' => 'LIKE',
'datetime' => 'LIKE',
'timestamp' => 'LIKE',
'time' => 'LIKE',
'json' => 'LIKE',
'float' => '=',
'decimal' => '=',
'boolean' => '=',
'binary' => 'LIKE',
'date' => 'LIKE',
'datetime' => 'LIKE',
'timestamp' => 'LIKE',
'time' => 'LIKE',
'json' => 'LIKE',
];

protected $_viewVars = [
'recordsTotal' => 0,
'recordsTotal' => 0,
'recordsFiltered' => 0,
'draw' => 0,
'draw' => 0,
];

/** @var \Cake\ORM\Table */
Expand Down Expand Up @@ -96,8 +96,7 @@ public function columns()
*
* @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
public function find(string $tableName, string $finder = 'all', array $options = [], array $columns = []): Query
{
$delegateSearch = $options['delegateSearch'] ?? false;
if (empty($columns))
Expand Down Expand Up @@ -224,8 +223,7 @@ private function _order(array &$options, &$columns)
*
* @return bool : true if additional filtering takes place
*/
private function _filter(array &$options, &$columns)
: bool
private function _filter(array &$options, &$columns): bool
{
$queryParams = $this->getController()->getRequest()->getQueryParams();

Expand Down Expand Up @@ -358,8 +356,7 @@ private function _addCondition($column, $value, $type = 'and')
*
* @return string : Database comparison operator
*/
protected function _getComparison($table, string $column)
: string
protected function _getComparison($table, string $column): string
{
$config = new Collection($this->getConfig('comparison'));

Expand All @@ -381,8 +378,9 @@ protected function _getComparison($table, string $column)
private function _setViewVars()
{
$controller = $this->getController();
$view = $controller->createView();

$_serialize = $controller->viewVars['_serialize'] ?? [];
$_serialize = $view->get('_serialize', []);
$_serialize = array_merge($_serialize, array_keys($this->_viewVars));

$controller->set($this->_viewVars);
Expand Down
18 changes: 6 additions & 12 deletions src/Lib/ColumnDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public function __construct(array $template, ColumnDefinitions $owner)
*
* @return \DataTables\Lib\ColumnDefinition
*/
public function add(...$args)
: ColumnDefinition
public function add(...$args): ColumnDefinition
{
return $this->owner->add(...$args);
}
Expand All @@ -66,8 +65,7 @@ public function add(...$args)
*
* @return ColumnDefinition
*/
public function set($key, $value = null)
: ColumnDefinition
public function set($key, $value = null): ColumnDefinition
{
if (is_array($key)) {
if (!empty($value))
Expand All @@ -88,8 +86,7 @@ public function set($key, $value = null)
*
* @return \DataTables\Lib\ColumnDefinition
*/
public function __call($name, $arguments)
: ColumnDefinition
public function __call($name, $arguments): ColumnDefinition
{
if (in_array($name, $this->switchesPositive)) {
if (!empty($arguments))
Expand All @@ -113,8 +110,7 @@ public function __call($name, $arguments)
*
* @return \DataTables\Lib\ColumnDefinition
*/
public function unset(string $key)
: ColumnDefinition
public function unset(string $key): ColumnDefinition
{
unset($this->content[$key]);

Expand All @@ -127,8 +123,7 @@ public function unset(string $key)
*
* @return ColumnDefinition
*/
public function render(string $name, array $args = [])
: ColumnDefinition
public function render(string $name, array $args = []): ColumnDefinition
{
$this->content['render'] = new CallbackFunction($name, $args);

Expand All @@ -138,8 +133,7 @@ public function render(string $name, array $args = [])
/**
* @return array
*/
public function jsonSerialize()
: array
public function jsonSerialize(): array
{
return $this->content;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Lib/ColumnDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class ColumnDefinitions implements \JsonSerializable, \ArrayAccess, \IteratorAgg
*
* @return ColumnDefinition
*/
public function add($column, string $fieldName = null)
: ColumnDefinition
public function add($column, string $fieldName = null): ColumnDefinition
{
if (!is_array($column))
$column = [
Expand Down Expand Up @@ -72,8 +71,7 @@ public function setTitles(array $titles)
*
* @return array: column definitions
*/
public function jsonSerialize()
: array
public function jsonSerialize(): array
{
return array_values($this->columns);
}
Expand All @@ -83,8 +81,7 @@ public function jsonSerialize()
*
* @return bool
*/
public function offsetExists($offset)
: bool
public function offsetExists($offset): bool
{
if (is_numeric($offset))
return isset($this->columns[$offset]);
Expand Down
83 changes: 40 additions & 43 deletions src/View/Helper/DataTablesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class DataTablesHelper extends Helper
public $helpers = ['Html', 'Url'];

protected $_defaultConfig = [
'searching' => true,
'processing' => true,
'serverSide' => true,
'searching' => true,
'processing' => true,
'serverSide' => true,
'deferRender' => true,
];

Expand All @@ -33,8 +33,7 @@ class DataTablesHelper extends Helper
*
* @return string
*/
public function renderTableUtility(TableUtility $tableUtility)
: string
public function renderTableUtility(TableUtility $tableUtility): string
{
return $this->table($tableUtility->getTable(), $tableUtility->getOptions(), $tableUtility->getClass());
}
Expand All @@ -48,15 +47,14 @@ public function renderTableUtility(TableUtility $tableUtility)
*
* @return string containing a <table> and a <script> element
*/
public function table($id = 'datatable', array $dtOptions = [], array $htmlOptions = [])
: string
public function table($id = 'datatable', array $dtOptions = [], array $htmlOptions = []): string
{
$htmlOptions = array_merge($htmlOptions, [
'id' => $id,
$htmlOptions = array_merge([
'id' => $id,
'class' => 'dataTable ' . ($htmlOptions['class'] ?? ''),
'style' => 'width:100%;',
'block' => false,
]);
], $htmlOptions);

$blockScript = $htmlOptions['block'];
unset($htmlOptions['block']);
Expand All @@ -78,8 +76,7 @@ public function table($id = 'datatable', array $dtOptions = [], array $htmlOptio
*
* @return string
*/
public function draw(string $selector, array $options = [])
: string
public function draw(string $selector, array $options = []): string
{
// incorporate any defaults set earlier
$options += $this->getConfig();
Expand All @@ -101,6 +98,37 @@ public function draw(string $selector, array $options = [])
return "dt.initDataTables('{$selector}', {$json});\n";
}

/**
* @param array $config
*/
public function initialize(array $config)
{
/* set default i18n (not possible in _$defaultConfig due to use of __d() */
if (empty($this->getConfig('language'))) {
// defaults from datatables.net/reference/option/language
$this->setConfig('language', [
'emptyTable' => __d('data_tables', 'No data available in table'),
'info' => __d('data_tables', 'Showing _START_ to _END_ of _TOTAL_ entries'),
'infoEmpty' => __d('data_tables', 'No entries to show'),
'infoFiltered' => __d('data_tables', '(filtered from _MAX_ total entries)'),
'lengthMenu' => __d('data_tables', 'Show _MENU_ entries'),
'processing' => __d('data_tables', 'Processing...'),
'search' => __d('data_tables', 'Search:'),
'zeroRecords' => __d('data_tables', 'No matching records found'),
'paginate' => [
'first' => __d('data_tables', 'First'),
'last' => __d('data_tables', 'Last'),
'next' => __d('data_tables', 'Next'),
'previous' => __d('data_tables', 'Previous'),
],
'aria' => [
'sortAscending' => __d('data_tables', ': activate to sort column ascending'),
'sortDescending' => __d('data_tables', ': activate to sort column descending'),
],
]);
}
}

/**
* @param array $order
* @param $columns
Expand Down Expand Up @@ -135,35 +163,4 @@ protected function translateOrder(array &$order, &$columns)
}
}
}

/**
* @param array $config
*/
public function initialize(array $config)
{
/* set default i18n (not possible in _$defaultConfig due to use of __d() */
if (empty($this->getConfig('language'))) {
// defaults from datatables.net/reference/option/language
$this->setConfig('language', [
'emptyTable' => __d('data_tables', 'No data available in table'),
'info' => __d('data_tables', 'Showing _START_ to _END_ of _TOTAL_ entries'),
'infoEmpty' => __d('data_tables', 'No entries to show'),
'infoFiltered' => __d('data_tables', '(filtered from _MAX_ total entries)'),
'lengthMenu' => __d('data_tables', 'Show _MENU_ entries'),
'processing' => __d('data_tables', 'Processing...'),
'search' => __d('data_tables', 'Search:'),
'zeroRecords' => __d('data_tables', 'No matching records found'),
'paginate' => [
'first' => __d('data_tables', 'First'),
'last' => __d('data_tables', 'Last'),
'next' => __d('data_tables', 'Next'),
'previous' => __d('data_tables', 'Previous'),
],
'aria' => [
'sortAscending' => __d('data_tables', ': activate to sort column ascending'),
'sortDescending' => __d('data_tables', ': activate to sort column descending'),
],
]);
}
}
}

0 comments on commit 0a4c70c

Please sign in to comment.