Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed to new paginator component #47

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
322 changes: 161 additions & 161 deletions src/Controller/Component/DatatablesPaginatorComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,170 +3,170 @@

namespace CakeDC\Datatables\Controller\Component;

use Cake\Controller\Component\PaginatorComponent;
use Cake\Datasource\ResultSetInterface;
use Cake\Controller\Component;
use Cake\Datasource\ResultSetDecorator;
use Cake\ORM\ResultSet;

/**
* DatatablesPaginator component
*/
class DatatablesPaginatorComponent extends PaginatorComponent
class DatatablesPaginatorComponent extends Component
{
/**
* Default configuration.
*
* @var array
*/
protected $_defaultConfig = [
];

public function paginate(object $object, array $settings = []): ResultSetInterface
{
$request = $this->getController()->getRequest();
$dtData = $request->is('post') ? $request->getData() : $request->getQueryParams();

$settings = $this->applyOrder($dtData, $settings);
$settings = $this->applyLimits($dtData, $settings);
$resultSet = parent::paginate($object, $settings);

return $resultSet;
}

/**
* Translate between datatables and CakePHP pagination order
*
* @param \Cake\Http\ServerRequest $request
* @param array $settings
* @return array
*/
protected function applyOrder(array $data, array $settings): array
{
$dtColumns = $data['columns'] ?? [];
$dtOrders = $data['order'] ?? [];

foreach ($dtOrders as $dtOrder) {
$colIndex = (int)($dtOrder['column'] ?? 0);
$colOrder = $dtOrder['dir'] ?? 'asc';
$colOrderable = $dtColumns[$colIndex]['orderable'] ?? null;
if (!$colOrderable === 'true') {
continue;
}
$colName = $dtColumns[$colIndex]['data'];

$settings['order'] = array_merge($settings['order'] ?? [], $this->filterOrder($colName, $colOrder));
}

return $settings;
}

/**
* @param string $colName
* @param string $colOrder
* @return array
*/
protected function filterOrder(string $colName, string $colOrder): array
{
$filters = $this->getConfig('filterOrder') ?? [];
if (isset($filters[$colName])) {
return array_fill_keys($filters[$colName] ?? [], $colOrder);
}

return [$colName => $colOrder];
}

/**
* Translate limit and offset from datatables
*
* @param \Cake\Http\ServerRequest $request
* @param array $settings
* @return array
*/
protected function applyLimits(array $data, array $settings): array
{
$dtStart = (int)$data['start'] ?? 0;
$dtLength = (int)$data['length'] ?? 0;

$settings['limit'] = $dtLength;
if ($dtStart === 0) {
$settings['page'] = 1;
} else {
$settings['page'] = intdiv($dtStart, $dtLength) + 1;
}

return $settings;
}

/**
* Translate the CakePHP pagination paging attribute into response keys for the datatables to consume
*
* @param $resultSet
* @return void
*/
public function prepareResponse($resultSet): void
{
$pagingData = $this->getController()->getRequest()->getAttribute('paging');
if (is_array($pagingData) && count($pagingData) === 1) {
$pagingData = reset($pagingData);
}
$this->getController()->set(
[
'data' => $resultSet,
'recordsTotal' => $pagingData['count'] ?? 0,
'recordsFiltered' => $pagingData['count'] ?? 0,
]
);
$this->getController()->viewBuilder()->setOption(
'serialize', [
'data',
'draw',
'recordsTotal',
'recordsFiltered',
]
);
}

/**
* Translate request query params from datatables into search queries for cakephp
* Extracting parameters from the structure provided in datatables like
* {"data":"title","name":"","searchable":"true","orderable":"true","search":{"value":"((((a))))","regex":"false"}}
* into ?title=a
*
* @return void
*/
public function prepareRequestQueryParams(): void
{
// translate ordering
$request = $this->getController()->getRequest();

$dtColumns = $request->is('post') ?
$request->getData('columns', []) : $request->getQuery('columns', []);

$newQueryParams = [];
foreach ($dtColumns as $dtColumn) {
if (($dtColumn['searchable'] ?? null) !== 'true') {
continue;
}
if (!array_key_exists('data',$dtColumn)) {
continue;
}

$colName = $dtColumn['data'];

if ($request->is('post') ? $request->getData($colName): $request->getQuery($colName)) {
// we already have a query param with values, ignoring this column
continue;
}
$colSearch = $dtColumn['search']['value'] ?? null;
$colSearch = ltrim($colSearch, '(');
$colSearch = rtrim($colSearch, ')');
$colSearch = trim($colSearch);
//@todo: enable regexp based search option
if (!$colSearch) {
continue;
}
$newQueryParams[$colName] = $colSearch;
}

$request = $request->withQueryParams($request->getQueryParams() + $newQueryParams);
$this->getController()->setRequest($request);
}
/**
* Default configuration.
*
* @var array
*/
protected $_defaultConfig = [
];

public function paginate(object $object, array $settings = []): ResultSetDecorator|ResultSet
{
$request = $this->getController()->getRequest();
$dtData = $request->is('post') ? $request->getData() : $request->getQueryParams();

$settings = $this->applyOrder($dtData, $settings);
$settings = $this->applyLimits($dtData, $settings);

return $this->getController()->paginate($object, $settings);
}

/**
* Translate between datatables and CakePHP pagination order
*
* @param \Cake\Http\ServerRequest $request
* @param array $settings
* @return array
*/
protected function applyOrder(array $data, array $settings): array
{
$dtColumns = $data['columns'] ?? [];
$dtOrders = $data['order'] ?? [];

foreach ($dtOrders as $dtOrder) {
$colIndex = (int)($dtOrder['column'] ?? 0);
$colOrder = $dtOrder['dir'] ?? 'asc';
$colOrderable = $dtColumns[$colIndex]['orderable'] ?? null;
if (!$colOrderable === 'true') {
continue;
}
$colName = $dtColumns[$colIndex]['data'];

$settings['order'] = array_merge($settings['order'] ?? [], $this->filterOrder($colName, $colOrder));
}

return $settings;
}

/**
* @param string $colName
* @param string $colOrder
* @return array
*/
protected function filterOrder(string $colName, string $colOrder): array
{
$filters = $this->getConfig('filterOrder') ?? [];
if (isset($filters[$colName])) {
return array_fill_keys($filters[$colName] ?? [], $colOrder);
}

return [$colName => $colOrder];
}

/**
* Translate limit and offset from datatables
*
* @param \Cake\Http\ServerRequest $request
* @param array $settings
* @return array
*/
protected function applyLimits(array $data, array $settings): array
{
$dtStart = (int)$data['start'] ?? 0;
$dtLength = (int)$data['length'] ?? 0;

$settings['limit'] = $dtLength;
if ($dtStart === 0) {
$settings['page'] = 1;
} else {
$settings['page'] = intdiv($dtStart, $dtLength) + 1;
}

return $settings;
}

/**
* Translate the CakePHP pagination paging attribute into response keys for the datatables to consume
*
* @param $resultSet
* @return void
*/
public function prepareResponse($resultSet): void
{
$pagingData = $this->getController()->getRequest()->getAttribute('paging');
if (is_array($pagingData) && count($pagingData) === 1) {
$pagingData = reset($pagingData);
}
$this->getController()->set(
[
'data' => $resultSet,
'recordsTotal' => $pagingData['count'] ?? 0,
'recordsFiltered' => $pagingData['count'] ?? 0,
]
);
$this->getController()->viewBuilder()->setOption(
'serialize', [
'data',
'draw',
'recordsTotal',
'recordsFiltered',
]
);
}

/**
* Translate request query params from datatables into search queries for cakephp
* Extracting parameters from the structure provided in datatables like
* {"data":"title","name":"","searchable":"true","orderable":"true","search":{"value":"((((a))))","regex":"false"}}
* into ?title=a
*
* @return void
*/
public function prepareRequestQueryParams(): void
{
// translate ordering
$request = $this->getController()->getRequest();

$dtColumns = $request->is('post') ?
$request->getData('columns', []) : $request->getQuery('columns', []);

$newQueryParams = [];
foreach ($dtColumns as $dtColumn) {
if (($dtColumn['searchable'] ?? null) !== 'true') {
continue;
}
if (!array_key_exists('data',$dtColumn)) {
continue;
}

$colName = $dtColumn['data'];

if ($request->is('post') ? $request->getData($colName): $request->getQuery($colName)) {
// we already have a query param with values, ignoring this column
continue;
}
$colSearch = $dtColumn['search']['value'] ?? null;
$colSearch = ltrim($colSearch, '(');
$colSearch = rtrim($colSearch, ')');
$colSearch = trim($colSearch);
//@todo: enable regexp based search option
if (!$colSearch) {
continue;
}
$newQueryParams[$colName] = $colSearch;
}

$request = $request->withQueryParams($request->getQueryParams() + $newQueryParams);
$this->getController()->setRequest($request);
}
}
2 changes: 1 addition & 1 deletion src/Datatable/Datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ protected function processColumnTypeSearch(): string
if (!empty($parVal) && is_array($parVal)) {
$dataPars = [];
foreach ($parVal as $v) {
$dataPars[] = "{'id': '" . $v['id'] . "', 'name': '" . $v['name'] . "'}";
$dataPars[] = "{'id': '" . $v['id'] . "', 'name': '" . addslashes($v['name']) . "'}";
}
$data = '[' . implode(',', $dataPars) . ']';
} else {
Expand Down