/** * Creates fake localization * @param string $locale * @return GroupLocalization */ public function getLocalization($locale) { $localization = parent::getLocalization($locale); // Create fake localization if not persisted if (is_null($localization)) { $localization = $this->createLocalization($locale); } return $localization; }
/** * @param EntityManager $em * @param ClassMetadata $class */ public function __construct(EntityManager $em, ClassMetadata $class) { parent::__construct($em, $class); // Bind additional conditions to the nested set repository $entities = array(Page::CN(), ApplicationPage::CN(), GroupPage::CN()); $orList = array(); foreach ($entities as $entityName) { $orList[] = "e INSTANCE OF {$entityName}"; } $additionalCondition = implode(' OR ', $orList); $additionalConditionSql = "discr IN ('page', 'application', 'group')"; $this->nestedSetRepository->setAdditionalCondition($additionalCondition, $additionalConditionSql); }
public function getAncestors(Page $page) { $left = $page->getLeftValue(); $right = $page->getRightValue(); $searchCondition = $this->getSearchCondition(); $repository = $this->getNestedSetRepository(); // Will include the self node if required in the end $searchCondition->leftLessThan($left)->rightGreaterThan($right); //$searchCondition->levelGreaterThanOrEqualsTo($level); $orderRule = $repository->createSelectOrderRule()->byLevelDescending(); $ancestors = $repository->search($searchCondition, $orderRule); return $ancestors; }
/** * @param array $data * @return \Supra\Package\Cms\Entity\Abstraction\RedirectTarget */ private function createRedirectTargetFromData(array $data) { $type = isset($data['type']) ? $data['type'] : null; switch ($type) { case 'page': $redirectTarget = new RedirectTargetPage(); if (empty($data['page_id'])) { throw new \InvalidArgumentException('Missing target page id.'); } $page = $this->getEntityManager()->find(Page::CN(), $data['page_id']); if ($page === null) { throw new \UnexpectedValueException(sprintf('Target page [%s] not found.', $data['page_id'])); } break; case 'child': $redirectTarget = new RedirectTargetChild(); if (empty($data['child_position'])) { throw new \InvalidArgumentException('Missing target child position.'); } $redirectTarget->setPage($this->getPageLocalization()->getPage()); $redirectTarget->setChildPosition($data['child_position']); break; case 'url': $redirectTarget = new RedirectTargetUrl(); if (empty($data['url'])) { throw new \InvalidArgumentException('Missing target URL.'); } if (!filter_var($data['url'], FILTER_VALIDATE_URL)) { throw new CmsException(null, sprintf('Provided URL [%s] is not correct.', $data['url'])); } $redirectTarget->setUrl($data['url']); break; default: throw new \UnexpectedValueException(sprintf('Unknown redirect target type [%s].', $type)); } return $redirectTarget; }
/** * Get first page localization ID to show in the CMS. * * @return Localization */ protected function getInitialPageLocalization() { $localization = null; $cookieBag = $this->container->getRequest()->cookies; if ($cookieBag->has(self::INITIAL_PAGE_ID_COOKIE)) { $localization = $this->getEntityManager()->find(Localization::CN(), $cookieBag->get(self::INITIAL_PAGE_ID_COOKIE)); } // Root page otherwise if ($localization === null) { $pageRepository = $this->getEntityManager()->getRepository(Page::CN()); /* @var $pageRepository PageRepository */ $pages = $pageRepository->getRootNodes(); if (!empty($pages)) { /* @var $pages AbstractPage[] */ return $pages[0]->getLocalization($this->getCurrentLocale()->getId()); } } return null; }
/** * Returns children page array data * @param string $entity * @param Localization $parentLocalization * @param string $filter * @param integer $levels * @param boolean $count * @return mixed */ private function gatherChildrenData($entity, $parentLocalization, $filter = null, $levels = null, $count = false) { $input = $this->getRequestInput(); $localeId = $this->getCurrentLocale()->getId(); // @TODO: existing_only flag is used for LinkManager, when it requests for available pages for linking. // should split that onto two separate methods. $existingOnly = $input->filter('existing_only', false, false, FILTER_VALIDATE_BOOLEAN); $customTitleQuery = $input->get('query', false); $entityManager = $this->getEntityManager(); $pageFinder = $entity === Page::CN() ? new PageFinder($entityManager) : new TemplateFinder($entityManager); // Reading by one level because need specific reading strategy for application pages if (empty($parentLocalization)) { $pageFinder->addLevelFilter(0, 0); } else { $rootNode = $parentLocalization->getMaster(); $pageFinder->addFilterByParent($rootNode, 1, 1); } $queryBuilder = $pageFinder->getQueryBuilder(); if ($existingOnly) { $queryBuilder->leftJoin('e.localizations', 'l_', 'WITH', 'l_.locale = :locale')->setParameter('locale', $localeId)->leftJoin('e.localizations', 'l')->andWhere('l_.id IS NOT NULL OR e INSTANCE OF ' . GroupPage::CN()); } else { $queryBuilder->leftJoin('e.localizations', 'l_', 'WITH', 'l_.locale = :locale')->setParameter('locale', $localeId)->leftJoin('e.localizations', 'l')->andWhere('l_.id IS NOT NULL OR e.global = true OR (e.level = 0 AND e.global = false)'); } if ($customTitleQuery) { $queryBuilder->andWhere('l_.title LIKE :customTitleQuery'); $queryBuilder->setParameter('customTitleQuery', $customTitleQuery . '%'); } $filterFolders = array(); $application = null; if ($parentLocalization instanceof ApplicationLocalization) { // This is bad solution for detecting where the sitemap has been requested from. // Should group by month if sitemap requested in the sidebar. //FIXME: JS should pass preference maybe if (empty($filter) && $existingOnly) { $filter = 'group'; } $application = $this->getPageApplicationManager()->createApplicationFor($parentLocalization, $this->getEntityManager()); $filterFolders = $application->getFilterFolders($queryBuilder, $filter); $application->applyFilters($queryBuilder, $filter); } $query = $queryBuilder->getQuery(); if ($input->has('offset')) { $offset = $input->getInt('offset'); $query->setFirstResult($offset); } if ($input->has('resultsPerRequest')) { $limit = $input->getInt('resultsPerRequest'); $query->setMaxResults($limit); } $paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query, true); // TODO: fix, shouldn't return mixed result depending on arguments if ($count) { // When only the count is needed... $count = count($filterFolders) + count($paginator); return $count; } else { $response = array(); // Prepend the filter folders $pages = array_merge($filterFolders, iterator_to_array($paginator)); if (!is_null($levels)) { $levels--; } foreach ($filterFolders as $filterFolder) { if (!$filterFolder instanceof TemporaryGroupPage) { throw new \LogicException("Application " . get_class($application) . ' returned invalid filter folders'); } } foreach ($pages as $page) { $pageData = $this->convertPageToArray($page, $localeId); // Skipping the page if (is_null($pageData)) { continue; } $pageData['children_count'] = 0; $localization = $page->getLocalization($localeId); if (!empty($localization)) { $filter = null; if ($page instanceof TemporaryGroupPage) { $filter = $page->getTitle(); $localization = $parentLocalization; // TODO: for now it's enabled for all filter folders $pageData['childrenListStyle'] = 'scrollList'; $pageData['selectable'] = false; $pageData['editable'] = false; $pageData['isDraggable'] = false; $pageData['isDropTarget'] = true; $pageData['new_children_first'] = true; } if ($levels === 0 && $page instanceof TemporaryGroupPage && $page->hasCalculatedNumberChildren()) { $pageData['children_count'] = $page->getNumberChildren(); } elseif ($levels === 0) { $pageData['children_count'] = $this->gatherChildrenData($entity, $localization, $filter, $levels, true); } else { $pageData['children'] = $this->gatherChildrenData($entity, $localization, $filter, $levels, false); $pageData['children_count'] = count($pageData['children']); } // // TODO: might be job for JS // if ($pageData['children_count'] > 0 // && ! empty($pageData['icon']) // && $pageData['icon'] === 'page') { // // $pageData['icon'] = 'folder'; // } } $response[] = $pageData; } return $response; } }
/** * Called when page structure is changed * @param Page $master * @return array of changed localizations */ private function pageChange(Page $master, $force = false) { // Run for all children $pageLocalizationEntity = PageLocalization::CN(); $dql = "SELECT l FROM {$pageLocalizationEntity} l JOIN l.master m\n\t\t\t\tWHERE m.left >= :left\n\t\t\t\tAND m.right <= :right\n\t\t\t\tORDER BY l.locale, m.left"; $pageLocalizations = $this->em->createQuery($dql)->setParameters(array('left' => $master->getLeftValue(), 'right' => $master->getRightValue()))->getResult(); $changedLocalizations = array(); foreach ($pageLocalizations as $pageLocalization) { $changedLocalization = $this->generatePath($pageLocalization, $force); if (!is_null($changedLocalization)) { $changedLocalizations[] = $changedLocalization; } } return $changedLocalizations; }
/** * {@inheritDoc} */ public function getRedirectUrl() { $localeId = $this->getPageLocalization()->getId(); $targetLocalization = $this->page->getLocalization($localeId); return $targetLocalization ? $targetLocalization->getPath()->format(Path::FORMAT_BOTH_DELIMITERS) : null; }