Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spriz/cd4da92395be7fa985ef662e64d97f81 to your computer and use it in GitHub Desktop.
Save Spriz/cd4da92395be7fa985ef662e64d97f81 to your computer and use it in GitHub Desktop.
<?php
namespace App\Controller\Component;
use Cake\Controller\Component\PaginatorComponent;
use Cake\Datasource\RepositoryInterface;
use Cake\Network\Exception\NotFoundException;
use Cake\ElasticSearch\Query;
class ElasticPaginatorComponent extends PaginatorComponent
{
/**
* Events supported by this component.
*
* @return array
*/
public function implementedEvents()
{
return [];
}
public function paginate($object, array $settings = [])
{
if ($object instanceof Query) {
$query = $object;
$object = $query->repository();
}
$alias = $object->alias();
$options = $this->mergeOptions($alias, $settings);
$options = $this->validateSort($object, $options);
$options = $this->checkLimit($options);
$options += ['page' => 1];
$options['page'] = (int)$options['page'] < 1 ? 1 : (int)$options['page'];
list($finder, $options) = $this->_extractFinder($options);
if (empty($query)) {
$query = $object->find($finder, $options);
} else {
$query->applyOptions($options);
}
$results = $query->all();
$numResults = $results->count();
$count = $numResults ? $results->getTotalHits() : 0;
$defaults = $this->getDefaults($alias, $settings);
unset($defaults[0]);
$page = $options['page'];
$limit = $options['limit'];
$pageCount = (int)ceil($count / $limit);
$requestedPage = $page;
$page = max(min($page, $pageCount), 1);
$request = $this->_registry->getController()->request;
$order = (array)$options['order'];
$sortDefault = $directionDefault = false;
if (!empty($defaults['order']) && count($defaults['order']) == 1) {
$sortDefault = key($defaults['order']);
$directionDefault = current($defaults['order']);
}
$paging = [
'finder' => $finder,
'page' => $page,
'current' => $numResults,
'count' => $count,
'perPage' => $limit,
'prevPage' => ($page > 1),
'nextPage' => ($count > ($page * $limit)),
'pageCount' => $pageCount,
'sort' => key($order),
'direction' => current($order),
'limit' => $defaults['limit'] != $limit ? $limit : null,
'sortDefault' => $sortDefault,
'directionDefault' => $directionDefault
];
if (!isset($request['paging'])) {
$request['paging'] = [];
}
$request['paging'] = [$alias => $paging] + (array)$request['paging'];
if ($requestedPage > $page) {
throw new NotFoundException();
}
return $results;
}
/**
* Prefixes the field with the type alias if possible.
*
* @param \Cake\Datasource\RepositoryInterface $object Repository object.
* @param array $order Order array.
* @param bool $whitelisted Whether or not the field was whitelisted
* @return array Final order array.
*/
protected function _prefix(RepositoryInterface $object, $order, $whitelisted = false)
{
return $order;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment