Exemplo n.º 1
0
 /**
  * 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\\CoreDomain\\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);
 }
Exemplo n.º 2
0
 /**
  * Persists the page is need and valid
  *
  * @param  Page                 $page               The page to be built.
  */
 private function doPersistIfValid(Page $page)
 {
     if (null === $page->getParent() && !empty($this->persist)) {
         // If root, only persist
         $this->em->persist($page);
         return;
     }
     $method = '';
     if (self::PERSIST_AS_FIRST_CHILD === $this->persist) {
         $method = 'insertNodeAsFirstChildOf';
     } elseif (self::PERSIST_AS_LAST_CHILD === $this->persist) {
         $method = 'insertNodeAsLastChildOf';
     }
     if (!empty($method)) {
         $this->em->getRepository('BackBee\\CoreDomain\\NestedNode\\Page')->{$method}($page, $page->getParent(), $this->isSection);
         $this->em->persist($page);
     }
 }
Exemplo n.º 3
0
 /**
  * Move a page as sibling of $target.
  *
  * @param  Page                 $page               The page to be moved.
  * @param  Page                 $target             The target page.
  * @param  boolean              $asPrevious         Move page as previous sibling of $target if true (default), next sibling elsewhere.
  *
  * @return Page                                     The moved page.
  *
  * @throws InvalidArgumentException                 Raises if $target is a root.
  */
 private function moveAsSiblingOf(Page $page, Page $target, $asPrevious = true)
 {
     if (null === $target->getParent()) {
         throw new InvalidArgumentException('Cannot move a page as sibling of a root.');
     }
     if (!$page->hasMainSection() && $target->hasMainSection()) {
         $this->moveAsFirstChildOf($page, $target->getParent());
     } elseif (!$page->hasMainSection() && !$target->hasMainSection()) {
         $this->movePageAsSiblingOf($page, $target, $asPrevious);
     } elseif ($page->hasMainSection() && !$target->hasMainSection()) {
         $this->moveAsLastChildOf($page, $target->getParent());
     } else {
         $this->moveSectionAsSiblingOf($page, $target, $asPrevious);
     }
     return $page->setParent($target->getParent());
 }
Exemplo n.º 4
0
 /**
  * Is this page is an ancestor of the provided one?
  *
  * @param  Page                 $page
  * @param  boolean              $strict             Optional, if true (default) this page is excluded of ancestors list.
  *
  * @return boolean                                  True if this page is an anscestor of provided page, false otherwise.
  */
 public function isAncestorOf(Page $page, $strict = true)
 {
     if (!$this->hasMainSection()) {
         return $this === $page && false === $strict;
     }
     return $this->getSection()->isAncestorOf($page->getSection(), $strict) || $page->getParent() === $this;
 }
Exemplo n.º 5
0
 /**
  * Add query part to select siblings of page.
  *
  * @param  Page                 $page               The page to look for siblings
  * @param  boolean              $strict             Optional, if false (by default) $node is include to the selection
  * @param  array|null           $order              Optional, ordering spec ( [$field => $sort] )
  * @param  integer|null         $limit              Optional, max number of results, if null no limit
  * @param  integer              $start              Optional, first result index (0 by default)
  *
  * @return PageQueryBuilder
  */
 public function andIsSiblingsOf(Page $page, $strict = false, array $order = null, $limit = null, $start = 0)
 {
     if (null === $page->getParent()) {
         $this->andParentIs(null);
     } else {
         $this->andIsDescendantOf($page->getParent(), false, $page->getLevel());
     }
     if (true === $strict) {
         $suffix = $this->getSuffix();
         $this->andWhere($this->getAlias() . ' != :page' . $suffix)->setParameter('page' . $suffix, $page);
     }
     if (null !== $order) {
         $this->addMultipleOrderBy($order);
     }
     if (null !== $limit) {
         $this->setMaxResults($limit)->setFirstResult($start);
     }
     return $this;
 }