vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ORM/QuerySubscriber.php line 14

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Knp\Component\Pager\Event\ItemsEvent;
  5. use Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM\Query\OrderByWalker;
  6. use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper as QueryHelper;
  7. use Doctrine\ORM\Query;
  8. use Knp\Component\Pager\PaginatorInterface;
  9. class QuerySubscriber implements EventSubscriberInterface
  10. {
  11.     public function items(ItemsEvent $event)
  12.     {
  13.         // Check if the result has already been sorted by an other sort subscriber
  14.         $customPaginationParameters $event->getCustomPaginationParameters();
  15.         if (!empty($customPaginationParameters['sorted']) ) {
  16.             return;
  17.         }
  18.         if ($event->target instanceof Query) {
  19.             $event->setCustomPaginationParameter('sorted'true);
  20.             if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
  21.                 $dir = isset($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) && strtolower($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) === 'asc' 'asc' 'desc';
  22.                 if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
  23.                     if (!in_array($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]], $event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
  24.                         throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]]}] this field is not in whitelist");
  25.                     }
  26.                 }
  27.                 $sortFieldParameterNames $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
  28.                 $fields = array();
  29.                 $aliases = array();
  30.                 foreach (explode('+'$sortFieldParameterNames) as $sortFieldParameterName) {
  31.                     $parts explode('.'$sortFieldParameterName2);
  32.                     // We have to prepend the field. Otherwise OrderByWalker will add
  33.                     // the order-by items in the wrong order
  34.                     array_unshift($fieldsend($parts));
  35.                     array_unshift($aliases<= count($parts) ? reset($parts) : false);
  36.                 }
  37.                 $event->target
  38.                     ->setHint(OrderByWalker::HINT_PAGINATOR_SORT_DIRECTION$dir)
  39.                     ->setHint(OrderByWalker::HINT_PAGINATOR_SORT_FIELD$fields)
  40.                     ->setHint(OrderByWalker::HINT_PAGINATOR_SORT_ALIAS$aliases)
  41.                 ;
  42.                 QueryHelper::addCustomTreeWalker($event->target'Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM\Query\OrderByWalker');
  43.             }
  44.         }
  45.     }
  46.     public static function getSubscribedEvents()
  47.     {
  48.         return array(
  49.             'knp_pager.items' => array('items'1)
  50.         );
  51.     }
  52. }