vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Sortable/PropelQuerySubscriber.php line 11

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Sortable;
  3. use Knp\Component\Pager\PaginatorInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Knp\Component\Pager\Event\ItemsEvent;
  6. class PropelQuerySubscriber implements EventSubscriberInterface
  7. {
  8.     public function items(ItemsEvent $event)
  9.     {
  10.         // Check if the result has already been sorted by an other sort subscriber
  11.         $customPaginationParameters $event->getCustomPaginationParameters();
  12.         if (!empty($customPaginationParameters['sorted']) ) {
  13.             return;
  14.         }
  15.         $query $event->target;
  16.         if ($query instanceof \ModelCriteria) {
  17.             $event->setCustomPaginationParameter('sorted'true);
  18.             if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
  19.                 $part $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
  20.                 $directionParam $event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME];
  21.                 $direction = (isset($_GET[$directionParam]) && strtolower($_GET[$directionParam]) === 'asc')
  22.                                 ? 'asc' 'desc';
  23.                 if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
  24.                     if (!in_array($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]], $event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
  25.                         throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]]}] this field is not in whitelist");
  26.                     }
  27.                 }
  28.                 $query->orderBy($part$direction);
  29.             }
  30.         }
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return array(
  35.             'knp_pager.items' => array('items'1)
  36.         );
  37.     }
  38. }