public function parser(ListorEvent $event) { $target = $event->getTarget(); if ($target instanceof ListorableInterface) { $event->setTarget($target->getListorable()); } }
public function parser(ListorEvent $event) { $target = $event->getTarget(); if ($target instanceof \ArrayObject) { $target = $target->getArrayCopy(); } if (is_array($target)) { // TODO Sort // TODO Filter $params = $event->getParams(); // Pagination. $event->setCount(count($target)); $event->setItems(array_slice($target, $params['currentPage'] * $params['itemsPerPage'], $params['itemsPerPage'])); $event->stopPropagation(); } }
public function parser(ListorEvent $event) { $target = $event->getTarget(); if ($target instanceof EntityRepository) { $qb = $target->createQueryBuilder('a'); $metadata = $this->em->getClassMetadata($target->getClassName()); // Handle only filtering of ORM Relations like OneToOne, etc... foreach ($event->getFilters() as $field => $filter) { if (!$filter['parsed'] && array_key_exists('operator', $filter) && array_key_exists('value', $filter)) { switch ($filter['operator']) { case Operations::CHOICE_AND: case Operations::CHOICE_OR: case Operations::CHOICE_NAND: if ($metadata->hasAssociation($field)) { if (!is_array($filter['value']) && !(is_object($filter['value']) && $filter['value'] instanceof \Traversable)) { $filter['value'] = array($filter['value']); } if (count($filter['value']) == 0) { continue; } $method = $filter['operator'] == Operations::CHOICE_NAND ? 'neq' : 'eq'; $qb->join('a.' . $field, 'c'); $exprs = array(); foreach ($filter['value'] as $one) { $name = $event->getUniqueName($field); $exprs[] = $qb->expr()->{$method}('c', ':' . $name); $qb->setParameter($name, $one); } $op = $filter['operator'] == Operations::CHOICE_OR ? 'orX' : 'andX'; $qb->andWhere(call_user_func_array(array($qb->expr(), $op), $exprs)); $event->filterParsed($field); } break; } } } $event->setTarget($qb); } }
public function parser(ListorEvent $event) { $target = $event->getTarget(); if ($target instanceof QueryBuilder) { $sort = array(); foreach ($event->getFilters() as $field => $filter) { // Prepare sort. if (!$filter['sorted'] && array_key_exists('sort', $filter)) { $priority = $filter['sort_priority']; if (!array_key_exists($priority, $sort)) { $sort[$priority] = array(); } $sort[$priority][$field] = $filter['sort']; } // Operator. if (!$filter['parsed']) { if (array_key_exists('operator', $filter) && array_key_exists('value', $filter)) { $name = $event->getUniqueName($field); switch ($filter['operator']) { case Operations::TEXT_EQUAL: $target->andWhere($target->expr()->eq('a.' . $field, ':' . $name))->setParameter($name, $filter['value']); break; case Operations::TEXT_NOT_EQUAL: $target->andWhere($target->expr()->neq('a.' . $field, ':' . $name))->setParameter($name, $filter['value']); break; case Operations::TEXT_LIKE: $target->andWhere($target->expr()->like('a.' . $field, ':' . $name))->setParameter($name, '%' . str_replace('%', '\\%', $filter['value']) . '%'); break; case Operations::TEXT_NOT_LIKE: $target->andWhere($target->expr()->notLike('a.' . $field, ':' . $name))->setParameter($name, '%' . str_replace('%', '\\%', $filter['value']) . '%'); break; case Operations::TEXT_START_BY: $target->andWhere($target->expr()->like('a.' . $field, ':' . $name))->setParameter($name, str_replace('%', '\\%', $filter['value']) . '%'); break; case Operations::TEXT_END_BY: $target->andWhere($target->expr()->like('a.' . $field, ':' . $name))->setParameter($name, '%' . str_replace('%', '\\%', $filter['value'])); break; case Operations::DATETIME_BETWEEN: if (empty($filter['value']['start']) || empty($filter['value']['end'])) { continue; } $name2 = $event->getUniqueName($field); $target->andWhere($target->expr()->gte('a.' . $field, ':' . $name))->andWhere($target->expr()->lte('a.' . $field, ':' . $name2))->setParameter($name, $filter['value']['start'])->setParameter($name2, $filter['value']['end']); break; case Operations::DATETIME_EXACT: // TODO break; case Operations::DATETIME_BEFORE: if (empty($filter['value']['start'])) { continue; } $target->andWhere($target->expr()->lte('a.' . $field, ':' . $name))->setParameter($name, $filter['value']['start']); break; case Operations::DATETIME_AFTER: if (empty($filter['value']['start'])) { continue; } $target->andWhere($target->expr()->gte('a.' . $field, ':' . $name))->setParameter($name, $filter['value']['start']); break; case Operations::CHOICE_AND: case Operations::CHOICE_OR: case Operations::CHOICE_NAND: if (!is_array($filter['value']) && !(is_object($filter['value']) && $filter['value'] instanceof \Traversable)) { $filter['value'] = array($filter['value']); } if (count($filter['value']) == 0) { break; } $method = $filter['operator'] == Operations::CHOICE_NAND ? 'neq' : 'eq'; $exprs = array(); foreach ($filter['value'] as $one) { $exprs[] = $target->expr()->{$method}('a.' . $field, ':' . $name); $target->setParameter($name, $one); $name = $event->getUniqueName($field); } $op = $filter['operator'] == Operations::CHOICE_OR ? 'orX' : 'andX'; $target->andWhere(call_user_func_array(array($target->expr(), $op), $exprs)); break; default: throw new ListorException(sprintf('The operator "%s" is not handle for "%s".', $filter['operator'], get_class($target))); } } } } // Count. $count = clone $target; $count->select('count(*) as count'); $event->setCount($count->execute()->fetchColumn()); // Query. if ($event->count() > 0) { // Pagination. $params = $event->getParams(); $target->setFirstResult($params['currentPage'] * $params['itemsPerPage']); $target->setMaxResults($params['itemsPerPage']); // Apply sort. foreach ($sort as $priority) { foreach ($priority as $field => $direction) { $target->addOrderBy($field, $direction); } } $event->setItems($target->execute()->fetchAll()); } $event->stopPropagation(); } }