/** * {@inheritdoc} */ public function findCollectionSet($depth = 0, $filter = [], CollectionInterface $collection = null, $sortBy = [], UserInterface $user = null, $permission = null) { try { $queryBuilder = $this->createQueryBuilder('collection')->addSelect('collectionMeta')->addSelect('defaultMeta')->addSelect('collectionType')->addSelect('collectionParent')->addSelect('parentMeta')->addSelect('collectionChildren')->leftJoin('collection.meta', 'collectionMeta')->leftJoin('collection.defaultMeta', 'defaultMeta')->leftJoin('collection.type', 'collectionType')->leftJoin('collection.parent', 'collectionParent')->leftJoin('collection.children', 'collectionChildren')->leftJoin('collectionParent.meta', 'parentMeta')->where('collection.depth <= :depth1 OR collectionChildren.depth <= :depth2'); if ($collection !== null) { $queryBuilder->andWhere('collection.lft BETWEEN :lft AND :rgt AND collection.id != :id'); } if (array_key_exists('search', $filter) && $filter['search'] !== null) { $queryBuilder->andWhere('collectionMeta.title LIKE :search'); } if (array_key_exists('locale', $filter)) { $queryBuilder->andWhere('collectionMeta.locale = :locale OR defaultMeta.locale != :locale'); } if ($sortBy !== null && is_array($sortBy) && count($sortBy) > 0) { foreach ($sortBy as $column => $order) { $queryBuilder->addOrderBy('collectionMeta.' . $column, strtolower($order) === 'asc' ? 'ASC' : 'DESC'); } } if ($user !== null && $permission != null) { $this->addAccessControl($queryBuilder, $user, $permission, Collection::class, 'collection'); } $collectionDepth = $collection !== null ? $collection->getDepth() : 0; $query = $queryBuilder->getQuery(); $query->setParameter('depth1', $collectionDepth + $depth); $query->setParameter('depth2', $depth + 1); if ($collection !== null) { $query->setParameter('lft', $collection->getLft()); $query->setParameter('rgt', $collection->getRgt()); $query->setParameter('id', $collection->getId()); } if (array_key_exists('search', $filter) && $filter['search'] !== null) { $query->setParameter('search', '%' . $filter['search'] . '%'); } if (array_key_exists('limit', $filter)) { $query->setMaxResults($filter['limit']); } if (array_key_exists('offset', $filter)) { $query->setFirstResult($filter['offset']); } if (array_key_exists('locale', $filter)) { $query->setParameter('locale', $filter['locale']); } return new Paginator($query); } catch (NoResultException $ex) { return []; } }
/** * Returns the basic query which selects the ids of a collection for a given * set of parameters. * * @param int $depth * @param array $filter * @param array $sortBy * @param CollectionInterface|null $collection * @param string $select * * @return Query */ private function getIdsQuery($depth = 0, $filter = [], $sortBy = [], CollectionInterface $collection = null, $select = 'collection.id') { $queryBuilder = $this->createQueryBuilder('collection')->select($select)->where('collection.depth <= :depth'); $collectionDepth = $collection !== null ? $collection->getDepth() : 0; $queryBuilder->setParameter('depth', $collectionDepth + $depth); if ($collection !== null) { $queryBuilder->andWhere('collection.lft BETWEEN :lft AND :rgt AND collection.id != :id'); $queryBuilder->setParameter('lft', $collection->getLft()); $queryBuilder->setParameter('rgt', $collection->getRgt()); $queryBuilder->setParameter('id', $collection->getId()); } if (array_key_exists('search', $filter) && $filter['search'] !== null || array_key_exists('locale', $filter) || count($sortBy) > 0) { $queryBuilder->leftJoin('collection.meta', 'collectionMeta'); $queryBuilder->leftJoin('collection.defaultMeta', 'defaultMeta'); } if (array_key_exists('search', $filter) && $filter['search'] !== null) { $queryBuilder->andWhere('collectionMeta.title LIKE :search OR defaultMeta.locale != :locale'); $queryBuilder->setParameter('search', '%' . $filter['search'] . '%'); } if (array_key_exists('locale', $filter)) { $queryBuilder->andWhere('collectionMeta.locale = :locale OR defaultMeta.locale != :locale'); $queryBuilder->setParameter('locale', $filter['locale']); } if (array_key_exists('systemCollections', $filter) && !$filter['systemCollections']) { $queryBuilder->leftJoin('collection.type', 'collectionType'); $queryBuilder->andWhere('collectionType.key != :type'); $queryBuilder->setParameter('type', SystemCollectionManagerInterface::COLLECTION_TYPE); } if (count($sortBy) > 0) { foreach ($sortBy as $column => $order) { $queryBuilder->addOrderBy('collectionMeta.' . $column, strtolower($order) === 'asc' ? 'ASC' : 'DESC'); } } $queryBuilder->addOrderBy('collection.id', 'ASC'); if (array_key_exists('limit', $filter)) { $queryBuilder->setMaxResults($filter['limit']); } if (array_key_exists('offset', $filter)) { $queryBuilder->setFirstResult($filter['offset']); } return $queryBuilder->getQuery(); }
/** * prepare an api entity. * * @param CollectionEntity $entity * @param string $locale * @param CollectionEntity[] $entities nested set * @param array $breadcrumbEntities * * @return Collection */ protected function getApiEntity(CollectionInterface $entity, $locale, $entities = null, $breadcrumbEntities = null) { $apiEntity = new Collection($entity, $locale); $children = []; if ($entities !== null) { foreach ($entities as $possibleChild) { if (($parent = $possibleChild->getParent()) !== null && $parent->getId() === $entity->getId()) { $children[] = $this->getApiEntity($possibleChild, $locale, $entities); } } } $apiEntity->setChildren($children); if ($entity->getParent() !== null) { $apiEntity->setParent($this->getApiEntity($entity->getParent(), $locale)); } if ($breadcrumbEntities !== null) { $breadcrumbApiEntities = []; foreach ($breadcrumbEntities as $entity) { $breadcrumbApiEntities[] = $this->getApiEntity($entity, $locale); } $apiEntity->setBreadcrumb($breadcrumbApiEntities); } return $this->addPreview($apiEntity); }
/** * @VirtualProperty * @SerializedName("id") * * @return int */ public function getId() { return $this->entity->getId(); }
/** * {@inheritdoc} */ public function findCollectionSet($depth = 0, $filter = [], CollectionInterface $collection = null, $sortBy = []) { try { $dql = sprintf('SELECT n, collectionMeta, defaultMeta, collectionType, collectionParent, parentMeta, collectionChildren FROM %s AS n LEFT OUTER JOIN n.meta AS collectionMeta LEFT JOIN n.defaultMeta AS defaultMeta LEFT JOIN n.type AS collectionType LEFT JOIN n.parent AS collectionParent LEFT JOIN n.children AS collectionChildren LEFT JOIN collectionParent.meta AS parentMeta WHERE (n.depth <= :depth + :maxDepth OR collectionChildren.depth <= :maxDepthPlusOne)', $this->_entityName); if ($collection !== null) { $dql .= ' AND n.lft BETWEEN :lft AND :rgt AND n.id != :id'; } if (array_key_exists('search', $filter) && $filter['search'] !== null) { $dql .= ' AND collectionMeta.title LIKE :search'; } if (array_key_exists('locale', $filter)) { $dql .= ' AND (collectionMeta.locale = :locale OR defaultMeta != :locale)'; } if ($sortBy !== null && is_array($sortBy) && sizeof($sortBy) > 0) { $orderBy = []; foreach ($sortBy as $column => $order) { $orderBy[] = 'collectionMeta.' . $column . ' ' . (strtolower($order) === 'asc' ? 'ASC' : 'DESC'); } $dql .= ' ORDER BY ' . implode(', ', $orderBy); } $query = new Query($this->_em); $query->setDQL($dql); $query->setParameter('maxDepth', intval($depth)); $query->setParameter('maxDepthPlusOne', intval($depth) + 1); $query->setParameter('depth', $collection !== null ? $collection->getDepth() : 0); if ($collection !== null) { $query->setParameter('lft', $collection->getLft()); $query->setParameter('rgt', $collection->getRgt()); $query->setParameter('id', $collection->getId()); } if (array_key_exists('search', $filter) && $filter['search'] !== null) { $query->setParameter('search', '%' . $filter['search'] . '%'); } if (array_key_exists('limit', $filter)) { $query->setMaxResults($filter['limit']); } if (array_key_exists('offset', $filter)) { $query->setFirstResult($filter['offset']); } if (array_key_exists('locale', $filter)) { $query->setParameter('locale', $filter['locale']); } return new Paginator($query); } catch (NoResultException $ex) { return []; } }