/** * Custom patch process for Page's sibling or parent node. * * @param Page $page * @param array $operations passed by reference */ private function patchSiblingAndParentOperation(Page $page, array &$operations) { $sibling_operation = null; $parent_operation = null; foreach ($operations as $key => $operation) { $op = array('key' => $key, 'op' => $operation); if ('/sibling_uid' === $operation['path']) { $sibling_operation = $op; } elseif ('/parent_uid' === $operation['path']) { $parent_operation = $op; } } if (null !== $sibling_operation || null !== $parent_operation) { if ($page->isRoot()) { throw new BadRequestHttpException('Cannot move root node of a site.'); } if ($page->isOnline(true)) { $this->granted('PUBLISH', $page); // user must have publish permission on the page } } try { if (null !== $sibling_operation) { unset($operations[$sibling_operation['key']]); $sibling = $this->getPageByUid($sibling_operation['op']['value']); $this->granted('EDIT', $sibling->getParent()); $this->getPageRepository()->moveAsPrevSiblingOf($page, $sibling); } elseif (null !== $parent_operation) { unset($operations[$parent_operation['key']]); $parent = $this->getPageByUid($parent_operation['op']['value']); if ($this->isFinal($parent)) { throw new BadRequestHttpException('Can\'t create children of ' . $parent->getLayout()->getLabel() . ' layout'); } $this->moveAsFirstChildOf($page, $parent); } } catch (InvalidArgumentException $e) { throw new BadRequestHttpException('Invalid node move action: ' . $e->getMessage()); } }
/** * Computes the URL of a page according to a scheme. * * @param array $scheme The scheme to apply * @param Page $page The page * @param AbstractClassContent $content The optionnal main content of the page * @return string The generated URL */ private function doGenerate($scheme, Page $page, AbstractClassContent $content = null) { $replacement = ['$parent' => $page->isRoot() ? '' : $page->getParent()->getUrl(false), '$title' => StringUtils::urlize($page->getTitle()), '$datetime' => $page->getCreated()->format('ymdHis'), '$date' => $page->getCreated()->format('ymd'), '$time' => $page->getCreated()->format('His')]; $matches = []; if (preg_match_all('/(\\$content->[a-z]+)/i', $scheme, $matches)) { foreach ($matches[1] as $pattern) { $property = explode('->', $pattern); $property = array_pop($property); try { $replacement[$pattern] = StringUtils::urlize($content->{$property}); } catch (\Exception $e) { $replacement[$pattern] = ''; } } } $matches = []; if (preg_match_all('/(\\$ancestor\\[([0-9]+)\\])/i', $scheme, $matches)) { foreach ($matches[2] as $level) { $ancestor = $this->application->getEntityManager()->getRepository('BackBee\\NestedNode\\Page')->getAncestor($page, $level); if (null !== $ancestor && $page->getLevel() > $level) { $replacement['$ancestor[' . $level . ']'] = $ancestor->getUrl(false); } else { $replacement['$ancestor[' . $level . ']'] = ''; } } } $url = preg_replace('/\\/+/', '/', str_replace(array_keys($replacement), array_values($replacement), $scheme)); return $this->getUniqueness($page, $url); }
/** * Add query part to select pages from their tree root. * * @param Page $root The root page to check * * @return PageQueryBuilder * * @throws \InvalidArgumentException Raises if $root is not a tree root */ public function andRootIs(Page $root) { if (!$root->isRoot()) { throw new \InvalidArgumentException('Provided page is not a tree root.'); } $suffix = $this->getSuffix(); return $this->andWhere($this->getSectionAlias() . '._root = :root' . $suffix)->setParameter('root' . $suffix, $root->getSection()); }
/** * @covers BackBee\NestedNode\Page::isRoot() */ public function testIsRoot() { $this->assertTrue($this->page->isRoot()); $subsection = new Section('sub-section'); $subsection->setRoot($this->page->getSection())->setParent($this->page->getSection()); $child = new Page('child', array('main_section' => $subsection)); $this->assertFalse($child->isRoot()); $subchild = new Page('child'); $subchild->setSection($child->getSection()); $this->assertFalse($subchild->isRoot()); }
/** * Set a page to filter the query on a nested portion. * * @param BackBee\NestedNode\Page $page */ public function addPageFilter(Page $page) { if ($page && !$page->isRoot()) { $this->leftJoin('cc._mainnode', 'p')->leftJoin('p._section', 'sp')->andWhere('sp._root = :selectedPageRoot')->andWhere('sp._leftnode >= :selectedPageLeftnode')->andWhere('sp._rightnode <= :selectedPageRightnode')->setParameters(['selectedPageRoot' => $page->getSection()->getRoot(), 'selectedPageLeftnode' => $page->getLeftnode(), 'selectedPageRightnode' => $page->getRightnode()]); } }