Beispiel #1
0
 /**
  * Copy a page to a new one.
  *
  * @param  Page                 $page               The page to be copied.
  * @param  string|null          $title              Optional, the title of the copy, if null (default) the title of the copied page.
  * @param  Page|null            $parent             Optional, the parent of the copy, if null (default) the parent of the copied page.
  *
  * @return Page                                     The copy of the page.
  *
  * @throws InvalidArgumentException                 Raises if the page is deleted.
  */
 private function copy(Page $page, $title = null, Page $parent = null)
 {
     if (Page::STATE_DELETED & $page->getState()) {
         throw new InvalidArgumentException('Cannot duplicate a deleted page.');
     }
     // Cloning the page
     $new_page = clone $page;
     $new_page->setTitle(null === $title ? $page->getTitle() : $title)->setLayout($page->getLayout());
     // Setting the clone as first child of the parent
     if (null !== $parent) {
         $new_page = $this->insertNodeAsFirstChildOf($new_page, $parent, $new_page->hasMainSection());
     }
     // Persisting entities
     $this->_em->persist($new_page);
     return $new_page;
 }
Beispiel #2
0
 /**
  * @covers BackBee\NestedNode\Page::setTitle
  */
 public function testSetTitle()
 {
     $this->assertEquals($this->page, $this->page->setTitle('new-title'));
     $this->assertEquals('new-title', $this->page->getTitle());
 }
Beispiel #3
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\\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);
 }
Beispiel #4
0
 /**
  * Parses definition and resolve value for $metadata.
  *
  * @param array     $definition
  * @param MetaData  $metadata
  * @param Page|NULL $page
  */
 private function parseDefinition(array $definition, MetaData $metadata, Page $page = null)
 {
     foreach ($definition as $attrname => $attrvalue) {
         if (!is_array($attrvalue)) {
             $attrvalue = [$attrvalue];
         }
         if (!$metadata->isComputed($attrname)) {
             continue;
         }
         $scheme = '' === $metadata->getAttribute($attrname) ? reset($attrvalue) : $metadata->getAttribute($attrname);
         if ('title' === $metadata->getName() && null !== $page && null !== $page->getTitle() && array_key_exists('default', $attrvalue)) {
             $scheme = $page->getTitle();
         } elseif (array_key_exists('default', $attrvalue)) {
             $scheme = $attrvalue['default'];
         }
         if (null !== $page && null !== $page->getLayout() && array_key_exists('layout', $attrvalue) && array_key_exists($page->getLayout()->getUid(), $attrvalue['layout'])) {
             $scheme = $attrvalue['layout'][$page->getLayout()->getUid()];
         }
         if (null !== $page && null !== $page->getLayout() && array_key_exists('layout', $attrvalue) && array_key_exists($page->getLayout()->getLabel(), $attrvalue['layout'])) {
             $scheme = $attrvalue['layout'][$page->getLayout()->getLabel()];
         }
         $matches = [];
         $isComputed = true;
         if (null !== $page && preg_match(self::MATCH_PATTERN, $scheme)) {
             $value = $this->resolveScheme($scheme, $page);
         } elseif (preg_match(self::CONST_PATTERN, $scheme, $matches)) {
             $value = $this->resolveConst($matches[1], $page);
         } else {
             $value = $scheme;
             $scheme = null;
             $isComputed = false;
         }
         $metadata->setAttribute($attrname, $value, $scheme, $isComputed);
     }
 }