Esempio n. 1
0
 public function items(ItemsEvent $event)
 {
     if ($event->target instanceof Query) {
         if (!isset($_GET[$event->options['filterValueParameterName']]) || empty($_GET[$event->options['filterValueParameterName']]) && $_GET[$event->options['filterValueParameterName']] !== "0") {
             return;
         }
         if (!empty($_GET[$event->options['filterFieldParameterName']])) {
             $columns = $_GET[$event->options['filterFieldParameterName']];
         } elseif (!empty($event->options['defaultFilterFields'])) {
             $columns = $event->options['defaultFilterFields'];
         } else {
             return;
         }
         $value = $_GET[$event->options['filterValueParameterName']];
         if (false !== strpos($value, '*')) {
             $value = str_replace('*', '%', $value);
         }
         if (is_string($columns) && false !== strpos($columns, ',')) {
             $columns = explode(',', $columns);
         }
         $columns = (array) $columns;
         if (isset($event->options['filterFieldWhitelist'])) {
             foreach ($columns as $column) {
                 if (!in_array($column, $event->options['filterFieldWhitelist'])) {
                     throw new \UnexpectedValueException("Cannot filter by: [{$column}] this field is not in whitelist");
                 }
             }
         }
         $event->target->setHint(WhereWalker::HINT_PAGINATOR_FILTER_VALUE, $value)->setHint(WhereWalker::HINT_PAGINATOR_FILTER_COLUMNS, $columns);
         QueryHelper::addCustomTreeWalker($event->target, 'Knp\\Component\\Pager\\Event\\Subscriber\\Filtration\\Doctrine\\ORM\\Query\\WhereWalker');
     }
 }
Esempio n. 2
0
 public function items(ItemsEvent $event)
 {
     if ($event->target instanceof Query) {
         if (isset($_GET[$event->options['sortFieldParameterName']])) {
             $dir = isset($_GET[$event->options['sortDirectionParameterName']]) && strtolower($_GET[$event->options['sortDirectionParameterName']]) === 'asc' ? 'asc' : 'desc';
             if (isset($event->options['sortFieldWhitelist'])) {
                 if (!in_array($_GET[$event->options['sortFieldParameterName']], $event->options['sortFieldWhitelist'])) {
                     throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options['sortFieldParameterName']]}] this field is not in whitelist");
                 }
             }
             $sortFieldParameterNames = $_GET[$event->options['sortFieldParameterName']];
             $fields = array();
             $aliases = array();
             foreach (explode('+', $sortFieldParameterNames) as $sortFieldParameterName) {
                 $parts = explode('.', $sortFieldParameterName);
                 // We have to prepend the field. Otherwise OrderByWalker will add
                 // the order-by items in the wrong order
                 array_unshift($fields, end($parts));
                 array_unshift($aliases, 2 <= count($parts) ? reset($parts) : false);
             }
             $event->target->setHint(OrderByWalker::HINT_PAGINATOR_SORT_DIRECTION, $dir)->setHint(OrderByWalker::HINT_PAGINATOR_SORT_FIELD, $fields)->setHint(OrderByWalker::HINT_PAGINATOR_SORT_ALIAS, $aliases);
             QueryHelper::addCustomTreeWalker($event->target, 'Knp\\Component\\Pager\\Event\\Subscriber\\Sortable\\Doctrine\\ORM\\Query\\OrderByWalker');
         }
     }
 }
 public function items(ItemsEvent $event)
 {
     if ($event->target instanceof Query) {
         // process count
         $useDoctrineWalkers = version_compare(\Doctrine\ORM\Version::VERSION, '2.2.0', '>=');
         if (($count = $event->target->getHint(self::HINT_COUNT)) !== false) {
             $event->count = intval($count);
         } else {
             $countQuery = QueryHelper::cloneQuery($event->target);
             QueryHelper::addCustomTreeWalker($countQuery, $useDoctrineWalkers ? 'Doctrine\\ORM\\Tools\\Pagination\\CountWalker' : 'Knp\\Component\\Pager\\Event\\Subscriber\\Paginate\\Doctrine\\ORM\\Query\\CountWalker');
             $countQuery->setHint($useDoctrineWalkers ? DoctrineCountWalker::HINT_DISTINCT : CountWalker::HINT_DISTINCT, $event->options['distinct'])->setFirstResult(null)->setMaxResults(null);
             $countResult = $countQuery->getResult(Query::HYDRATE_ARRAY);
             if (count($countResult) > 1) {
                 $countResult = count($countResult);
             } else {
                 $countResult = current($countResult);
                 $countResult = $countResult ? current($countResult) : 0;
             }
             $event->count = intval($countResult);
         }
         // process items
         $result = null;
         if ($event->count) {
             if ($event->options['distinct']) {
                 $limitSubQuery = QueryHelper::cloneQuery($event->target);
                 $limitSubQuery->setFirstResult($event->getOffset())->setMaxResults($event->getLimit())->useQueryCache(false);
                 QueryHelper::addCustomTreeWalker($limitSubQuery, $useDoctrineWalkers ? 'Doctrine\\ORM\\Tools\\Pagination\\LimitSubqueryWalker' : 'Knp\\Component\\Pager\\Event\\Subscriber\\Paginate\\Doctrine\\ORM\\Query\\LimitSubqueryWalker');
                 $ids = array_map('current', $limitSubQuery->getScalarResult());
                 // create where-in query
                 $whereInQuery = QueryHelper::cloneQuery($event->target);
                 QueryHelper::addCustomTreeWalker($whereInQuery, $useDoctrineWalkers ? 'Doctrine\\ORM\\Tools\\Pagination\\WhereInWalker' : 'Knp\\Component\\Pager\\Event\\Subscriber\\Paginate\\Doctrine\\ORM\\Query\\WhereInWalker');
                 $whereInQuery->setHint($useDoctrineWalkers ? DoctrineWhereInWalker::HINT_PAGINATOR_ID_COUNT : WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids))->setFirstResult(null)->setMaxResults(null);
                 $type = $limitSubQuery->getHint($useDoctrineWalkers ? DoctrineLimitSubqueryWalker::IDENTIFIER_TYPE : LimitSubqueryWalker::IDENTIFIER_TYPE);
                 $idAlias = $useDoctrineWalkers ? DoctrineWhereInWalker::PAGINATOR_ID_ALIAS : WhereInWalker::PAGINATOR_ID_ALIAS;
                 foreach ($ids as $i => $id) {
                     $whereInQuery->setParameter($idAlias . '_' . ++$i, $id, $type->getName());
                 }
                 $result = $whereInQuery->execute();
             } else {
                 $event->target->setFirstResult($event->getOffset())->setMaxResults($event->getLimit());
                 $result = $event->target->execute();
             }
         } else {
             $result = array();
             // count is 0
         }
         $event->items = $result;
         $event->stopPropagation();
     }
 }
Esempio n. 4
0
 public function serveQuery(ItemsEvent $event)
 {
     $pagination = $this->paginationService->getPagination();
     $partialResponse = $this->paginationService->getPartialResponse();
     if ($event->target instanceof Query) {
         if ($pagination->getSort()) {
             $event->target->setHint('newscoop.api.sort', $pagination->getSort());
             QueryHelper::addCustomTreeWalker($event->target, 'Newscoop\\GimmeBundle\\EventListener\\Sortable\\Doctrine\\ORM\\Query\\OrderByWalker');
         }
         if ($partialResponse->getFields()) {
             $event->target->setHint('newscoop.api.fields', $partialResponse->getFields());
             QueryHelper::addCustomTreeWalker($event->target, 'Newscoop\\GimmeBundle\\EventListener\\Selectable\\Doctrine\\ORM\\Query\\SelectWalker');
         }
     }
 }
 public function items(ItemsEvent $event)
 {
     if ($event->target instanceof Query) {
         if (isset($_GET[$event->options['sortFieldParameterName']])) {
             $dir = isset($_GET[$event->options['sortDirectionParameterName']]) && strtolower($_GET[$event->options['sortDirectionParameterName']]) === 'asc' ? 'asc' : 'desc';
             $parts = explode('.', $_GET[$event->options['sortFieldParameterName']]);
             if (isset($event->options['sortFieldWhitelist'])) {
                 if (!in_array($_GET[$event->options['sortFieldParameterName']], $event->options['sortFieldWhitelist'])) {
                     throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options['sortFieldParameterName']]}] this field is not in whitelist");
                 }
             }
             $event->target->setHint(OrderByWalker::HINT_PAGINATOR_SORT_DIRECTION, $dir)->setHint(OrderByWalker::HINT_PAGINATOR_SORT_FIELD, end($parts));
             if (2 <= count($parts)) {
                 $event->target->setHint(OrderByWalker::HINT_PAGINATOR_SORT_ALIAS, reset($parts));
             }
             QueryHelper::addCustomTreeWalker($event->target, 'Knp\\Component\\Pager\\Event\\Subscriber\\Sortable\\Doctrine\\ORM\\Query\\OrderByWalker');
         }
     }
 }
 public function items(ItemsEvent $event)
 {
     if ($event->target instanceof Query) {
         $alias = $event->options['alias'];
         if (isset($_GET[$alias . 'sort'])) {
             $dir = strtolower($_GET[$alias . 'direction']) === 'asc' ? 'asc' : 'desc';
             $parts = explode('.', $_GET[$alias . 'sort']);
             if (count($parts) != 2) {
                 throw new \UnexpectedValueException('Invalid sort key came by request, should be example "entityAlias.field" like: "article.title"');
             }
             if (isset($event->options['whitelist'])) {
                 if (!in_array($_GET[$alias . 'sort'], $event->options['whitelist'])) {
                     throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$alias . 'sort']}] this field is not in whitelist");
                 }
             }
             $event->target->setHint(OrderByWalker::HINT_PAGINATOR_SORT_ALIAS, current($parts))->setHint(OrderByWalker::HINT_PAGINATOR_SORT_DIRECTION, $dir)->setHint(OrderByWalker::HINT_PAGINATOR_SORT_FIELD, end($parts));
             QueryHelper::addCustomTreeWalker($event->target, 'Knp\\Component\\Pager\\Event\\Subscriber\\Sortable\\Doctrine\\ORM\\Query\\OrderByWalker');
         }
     }
 }
Esempio n. 7
0
 public function items(ItemsEvent $event)
 {
     if ($event->target instanceof Query) {
         // process count
         $useDoctrineWalkers = false;
         $useDoctrineOutputWalker = false;
         if (version_compare(\Doctrine\ORM\Version::VERSION, '2.3.0', '>=')) {
             $useDoctrineWalkers = true;
             $useDoctrineOutputWalker = true;
         } else {
             if (version_compare(\Doctrine\ORM\Version::VERSION, '2.2.0', '>=')) {
                 $useDoctrineWalkers = true;
             }
         }
         if (($count = $event->target->getHint(self::HINT_COUNT)) !== false) {
             $event->count = intval($count);
         } else {
             $countQuery = QueryHelper::cloneQuery($event->target);
             if ($useDoctrineOutputWalker) {
                 $treeWalker = 'Doctrine\\ORM\\Tools\\Pagination\\CountOutputWalker';
                 $countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, $treeWalker);
             } else {
                 if ($useDoctrineWalkers) {
                     QueryHelper::addCustomTreeWalker($countQuery, 'Doctrine\\ORM\\Tools\\Pagination\\CountWalker');
                 } else {
                     $treeWalker = 'Knp\\Component\\Pager\\Event\\Subscriber\\Paginate\\Doctrine\\ORM\\Query\\CountWalker';
                     QueryHelper::addCustomTreeWalker($countQuery, $treeWalker);
                 }
             }
             if ($useDoctrineWalkers) {
                 $countQuery->setHint(DoctrineCountWalker::HINT_DISTINCT, $event->options['distinct']);
             } else {
                 $countQuery->setHint(CountWalker::HINT_DISTINCT, $event->options['distinct']);
             }
             $countQuery->setFirstResult(null)->setMaxResults(null);
             $countQuery->getEntityManager()->getConfiguration()->addCustomHydrationMode('asIs', 'Knp\\Component\\Pager\\Event\\Subscriber\\Paginate\\Doctrine\\ORM\\Query\\AsIsHydrator');
             $countResult = $countQuery->getResult('asIs');
             $event->count = intval(current(current($countResult)));
         }
         // process items
         $result = null;
         if ($event->count) {
             if ($event->options['distinct']) {
                 $limitSubQuery = QueryHelper::cloneQuery($event->target);
                 $limitSubQuery->setFirstResult($event->getOffset())->setMaxResults($event->getLimit())->useQueryCache(false);
                 QueryHelper::addCustomTreeWalker($limitSubQuery, $useDoctrineWalkers ? 'Doctrine\\ORM\\Tools\\Pagination\\LimitSubqueryWalker' : 'Knp\\Component\\Pager\\Event\\Subscriber\\Paginate\\Doctrine\\ORM\\Query\\LimitSubqueryWalker');
                 $ids = array_map('current', $limitSubQuery->getScalarResult());
                 // create where-in query
                 $whereInQuery = QueryHelper::cloneQuery($event->target);
                 QueryHelper::addCustomTreeWalker($whereInQuery, $useDoctrineWalkers ? 'Doctrine\\ORM\\Tools\\Pagination\\WhereInWalker' : 'Knp\\Component\\Pager\\Event\\Subscriber\\Paginate\\Doctrine\\ORM\\Query\\WhereInWalker');
                 $whereInQuery->setHint($useDoctrineWalkers ? DoctrineWhereInWalker::HINT_PAGINATOR_ID_COUNT : WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids))->setFirstResult(null)->setMaxResults(null);
                 if (version_compare(\Doctrine\ORM\Version::VERSION, '2.3.0', '>=') && count($ids) > 0) {
                     $whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, $ids);
                 } else {
                     $type = $limitSubQuery->getHint($useDoctrineWalkers ? DoctrineLimitSubqueryWalker::IDENTIFIER_TYPE : LimitSubqueryWalker::IDENTIFIER_TYPE);
                     $idAlias = $useDoctrineWalkers ? DoctrineWhereInWalker::PAGINATOR_ID_ALIAS : WhereInWalker::PAGINATOR_ID_ALIAS;
                     foreach ($ids as $i => $id) {
                         $whereInQuery->setParameter($idAlias . '_' . ++$i, $id, $type->getName());
                     }
                 }
                 $result = $whereInQuery->execute();
             } else {
                 $event->target->setFirstResult($event->getOffset())->setMaxResults($event->getLimit());
                 $result = $event->target->execute();
             }
         } else {
             $result = array();
             // count is 0
         }
         $event->items = $result;
         $event->stopPropagation();
     }
 }