2012-05-22 15:23:51 +02:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Subscriber;
|
2012-05-22 15:23:51 +02:00
|
|
|
|
2014-02-24 13:47:26 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2012-05-22 15:23:51 +02:00
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
|
use Knp\Component\Pager\Event\ItemsEvent;
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface;
|
2013-03-27 19:58:34 +01:00
|
|
|
use FOS\ElasticaBundle\Paginator\PartialResultsInterface;
|
2012-05-22 15:23:51 +02:00
|
|
|
|
|
|
|
class PaginateElasticaQuerySubscriber implements EventSubscriberInterface
|
|
|
|
{
|
2014-02-24 13:47:26 +01:00
|
|
|
private $request;
|
2013-09-13 15:34:59 +02:00
|
|
|
|
2014-02-24 13:47:26 +01:00
|
|
|
public function setRequest(Request $request = null)
|
2013-09-13 15:34:59 +02:00
|
|
|
{
|
2014-02-24 13:47:26 +01:00
|
|
|
$this->request = $request;
|
2013-09-13 15:34:59 +02:00
|
|
|
}
|
|
|
|
|
2012-05-22 15:23:51 +02:00
|
|
|
public function items(ItemsEvent $event)
|
|
|
|
{
|
|
|
|
if ($event->target instanceof PaginatorAdapterInterface) {
|
2013-09-13 15:34:59 +02:00
|
|
|
// Add sort to query
|
2014-02-24 13:47:26 +01:00
|
|
|
$this->setSorting($event);
|
2013-09-13 15:34:59 +02:00
|
|
|
|
2013-03-02 12:23:44 +01:00
|
|
|
/** @var $results PartialResultsInterface */
|
2012-05-22 15:23:51 +02:00
|
|
|
$results = $event->target->getResults($event->getOffset(), $event->getLimit());
|
|
|
|
|
|
|
|
$event->count = $results->getTotalHits();
|
|
|
|
$event->items = $results->toArray();
|
|
|
|
$facets = $results->getFacets();
|
|
|
|
if (null != $facets) {
|
|
|
|
$event->setCustomPaginationParameter('facets', $facets);
|
|
|
|
}
|
2014-10-08 19:33:15 +02:00
|
|
|
$aggregations = $results->getAggregations();
|
|
|
|
if (null != $aggregations) {
|
|
|
|
$event->setCustomPaginationParameter('aggregations', $aggregations);
|
|
|
|
}
|
2012-05-22 15:23:51 +02:00
|
|
|
|
|
|
|
$event->stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 15:34:59 +02:00
|
|
|
/**
|
|
|
|
* Adds knp paging sort to query
|
|
|
|
*
|
2014-02-24 13:47:26 +01:00
|
|
|
* @param ItemsEvent $event
|
2013-09-13 15:34:59 +02:00
|
|
|
*/
|
2014-02-24 13:47:26 +01:00
|
|
|
protected function setSorting(ItemsEvent $event)
|
2013-09-13 15:34:59 +02:00
|
|
|
{
|
|
|
|
$options = $event->options;
|
2014-02-24 13:47:26 +01:00
|
|
|
$sortField = $this->request->get($options['sortFieldParameterName']);
|
2013-09-13 15:34:59 +02:00
|
|
|
|
|
|
|
if (!empty($sortField)) {
|
|
|
|
// determine sort direction
|
|
|
|
$dir = 'asc';
|
2014-02-24 13:47:26 +01:00
|
|
|
$sortDirection = $this->request->get($options['sortDirectionParameterName']);
|
2013-09-13 15:34:59 +02:00
|
|
|
if ('desc' === strtolower($sortDirection)) {
|
|
|
|
$dir = 'desc';
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the requested sort field is in the sort whitelist
|
|
|
|
if (isset($options['sortFieldWhitelist']) && !in_array($sortField, $options['sortFieldWhitelist'])) {
|
|
|
|
throw new \UnexpectedValueException(sprintf('Cannot sort by: [%s] this field is not in whitelist', $sortField));
|
|
|
|
}
|
|
|
|
|
|
|
|
// set sort on active query
|
|
|
|
$event->target->getQuery()->setSort(array(
|
|
|
|
$sortField => array('order' => $dir),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-22 15:23:51 +02:00
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'knp_pager.items' => array('items', 1)
|
|
|
|
);
|
|
|
|
}
|
2014-10-08 19:33:15 +02:00
|
|
|
}
|