/** * Sets the root node. * * @param Page $root * * @return Page */ public function setRoot(Page $root) { if ($this->hasMainSection()) { $this->getMainSection()->setRoot($root->getRoot()->getMainSection()); } return $this; }
/** * Checks for the uniqueness of the URL and postfixe it if need. * * @param \BackBee\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\\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 page p LEFT JOIN 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; }
/** * @covers BackBee\NestedNode\Page::getRoot() */ public function testGetRoot() { $this->assertEquals($this->page, $this->page->getRoot()); $child = new Page('child'); $child->setSection($this->page->getSection()); $this->assertEquals($this->page, $child->getRoot()); }