public function getRoutesByNames($names) { if (null === $names) { $collection = new RouteCollection(); $pages = $this->entityManager->getRepository('BackBee\\CoreDomain\\NestedNode\\Page')->findBy(array('_site' => $this->siteContext->getSite(), '_state' => Page::getUndeletedStates())); foreach ($pages as $page) { $name = $page->getUrl(); $collection->add($name, $this->createRouteFromEntity($page, $name)); } return $collection; } $routes = array(); foreach ($names as $name) { try { $routes[] = $this->getRouteByName($name); } catch (RouteNotFoundException $e) { // not found } } return $routes; }
/** * Checks for the uniqueness of the URL and postfixe it if need. * * @param \BackBee\CoreDomain\NestedNode\Page $page The page * @param string &$url The reference of the generated URL */ public function getUniqueness(Page $page, $url) { if (!$this->preserveUnicity) { return $url; } $pageRepository = $this->application->getEntityManager()->getRepository('BackBee\\CoreDomain\\NestedNode\\Page'); if (null === $pageRepository->findOneBy(['_url' => $url, '_root' => $page->getRoot(), '_state' => $page->getUndeletedStates()])) { return $url; } $baseUrl = $url . '-%d'; $matches = []; $existings = []; if (preg_match('#(.*)\\/$#', $baseUrl, $matches)) { $baseUrl = $matches[1] . '-%d/'; $existings = $pageRepository->createQueryBuilder('p')->andRootIs($page->getRoot())->andWhere('p._url LIKE :url')->setParameter('url', $matches[1] . '%/')->getQuery()->getResult(); } else { $existings = $this->application->getEntityManager()->getConnection()->executeQuery('SELECT p.uid FROM bb_page p LEFT JOIN bb_section s ON s.uid = p.section_uid WHERE s.root_uid = :root AND p.url REGEXP :regex', ['regex' => str_replace(['+'], ['[+]'], $url) . '(-[0-9]+)?$', 'root' => $page->getRoot()->getUid()])->fetchAll(); $uids = []; foreach ($existings as $existing) { $uids[] = $existing['uid']; } $existings = $pageRepository->findBy(['_uid' => $uids]); } $existingUrls = []; foreach ($existings as $existing) { if (!$existing->isDeleted() && $existing->getUid() !== $page->getUid()) { $existingUrls[] = $existing->getUrl(false); } } $count = 1; while (in_array($url, $existingUrls)) { $url = sprintf($baseUrl, $count++); } return $url; }