Example #1
0
 /**
  * Generates and returns url for the provided page.
  *
  * @param Page                 $page    The page to generate its url
  * @param AbstractClassContent $content The optional main content of the page
  * @return string
  */
 public function generate(Page $page, AbstractClassContent $content = null, $force = false, $exceptionOnMissingScheme = true)
 {
     if (!is_bool($force)) {
         throw new \InvalidArgumentException(sprintf('%s method expect `force parameter` to be type of boolean, %s given', __METHOD__, gettype($force)));
     }
     if (null !== $page->getUrl(false) && $page->getState() & Page::STATE_ONLINE && (!$force && $this->preserveOnline)) {
         return $page->getUrl(false);
     }
     if ($page->isRoot() && array_key_exists('_root_', $this->schemes)) {
         return $this->doGenerate($this->schemes['_root_'], $page, $content);
     }
     if (isset($this->schemes['_layout_']) && is_array($this->schemes['_layout_'])) {
         if (array_key_exists($page->getLayout()->getUid(), $this->schemes['_layout_'])) {
             return $this->doGenerate($this->schemes['_layout_'][$page->getLayout()->getUid()], $page);
         }
     }
     if (null !== $content && array_key_exists('_content_', $this->schemes)) {
         $shortClassname = str_replace(AbstractContent::CLASSCONTENT_BASE_NAMESPACE, '', get_class($content));
         if (array_key_exists($shortClassname, $this->schemes['_content_'])) {
             return $this->doGenerate($this->schemes['_content_'][$shortClassname], $page, $content);
         }
     }
     if (array_key_exists('_default_', $this->schemes)) {
         return $this->doGenerate($this->schemes['_default_'], $page, $content);
     }
     $url = $page->getUrl(false);
     if (!empty($url)) {
         return $url;
     }
     if (true === $exceptionOnMissingScheme) {
         throw new RewritingException(sprintf('No rewriting scheme found for Page (#%s)', $page->getUid()), RewritingException::MISSING_SCHEME);
     }
     return '/' . $page->getUid();
 }
Example #2
0
 /**
  * Resolves constant value.
  *
  * @param  string    $const
  * @param  Page|NULL $page
  *
  * @return string
  */
 protected function resolveConst($const, Page $page = null)
 {
     $value = '';
     switch (strtolower($const)) {
         case 'url':
             $value = null !== $page ? $page->getUrl() : '';
         default:
             break;
     }
     return $value;
 }
Example #3
0
 /**
  * @covers BackBee\NestedNode\Page::setUrl
  */
 public function testSetUrl()
 {
     $this->assertEquals($this->page, $this->page->setUrl('new-url'));
     $this->assertEquals('new-url', $this->page->getUrl());
 }
Example #4
0
 /**
  * Compute values of attributes according to the AbstractClassContent provided.
  *
  * @param  \BackBee\ClassContent\AbstractClassContent $content
  *
  * @return \BackBee\MetaData\MetaData
  */
 public function computeAttributes(AbstractClassContent $content, Page $page = null)
 {
     foreach ($this->attributes as $attribute => $value) {
         if (true === $this->is_computed[$attribute] && true === array_key_exists($attribute, $this->scheme)) {
             try {
                 $functions = explode('||', $value);
                 $matches = array();
                 if (false !== preg_match_all('/(\\$([a-z_\\/\\\\]+)(\\[([0-9]+)\\]){0,1}(->){0,1})+/i', $this->scheme[$attribute], $matches, PREG_PATTERN_ORDER)) {
                     $this->attributes[$attribute] = $this->scheme[$attribute];
                     $initial_content = $content;
                     $count = count($matches[0]);
                     for ($i = 0; $i < $count; $i++) {
                         $content = $initial_content;
                         foreach (explode('->', $matches[0][$i]) as $scheme) {
                             $draft = null;
                             if (true === is_object($content)) {
                                 if (null !== ($draft = $content->getDraft())) {
                                     $content->releaseDraft();
                                 }
                             }
                             $newcontent = $content;
                             $m = array();
                             if (preg_match('/\\$([a-z\\/\\\\]+)(\\[([0-9]+)\\]){0,1}/i', $scheme, $m)) {
                                 if (3 < count($m) && $content instanceof ContentSet && 'ContentSet' === $m[1]) {
                                     $newcontent = $content->item($m[3]);
                                 } elseif (3 < count($m) && $content instanceof ContentSet) {
                                     $index = intval($m[3]);
                                     $classname = 'BackBee\\ClassContent\\' . str_replace('/', NAMESPACE_SEPARATOR, $m[1]);
                                     foreach ($content as $subcontent) {
                                         if (get_class($subcontent) == $classname) {
                                             if (0 === $index) {
                                                 $newcontent = $subcontent;
                                             } else {
                                                 $index--;
                                             }
                                         }
                                     }
                                 } elseif (true === is_object($content) && 1 < count($m)) {
                                     $property = $m[1];
                                     try {
                                         $newcontent = $content->{$property};
                                     } catch (\Exception $e) {
                                         $newcontent = new Text();
                                     }
                                 }
                             }
                             if (null !== $draft) {
                                 $content->setDraft($draft);
                             }
                             $content = $newcontent;
                         }
                         if ($content instanceof AbstractClassContent && $content->isElementContent()) {
                             if (null !== ($draft = $content->getDraft())) {
                                 $content->releaseDraft();
                             }
                             if ($content instanceof File) {
                                 $new_value = $content->path;
                             } else {
                                 $new_value = trim(str_replace(array("\n", "\r"), '', strip_tags('' . $content)));
                             }
                             $this->attributes[$attribute] = str_replace($matches[0][$i], $new_value, $this->attributes[$attribute]);
                             if (null !== $draft) {
                                 $content->setDraft($draft);
                             }
                         } elseif (true === is_array($content)) {
                             $v = array();
                             foreach ($content as $c) {
                                 if ($c instanceof Keyword) {
                                 }
                                 $v[] = trim(str_replace(array("\n", "\r"), '', strip_tags('' . $c)));
                             }
                             $this->attributes[$attribute] = str_replace($matches[0][$i], implode(',', $v), $this->attributes[$attribute]);
                         } else {
                             $new_value = trim(str_replace(array("\n", "\r"), '', strip_tags($content)));
                             $this->attributes[$attribute] = str_replace($matches[0][$i], $new_value, $this->attributes[$attribute]);
                         }
                     }
                 }
                 array_shift($functions);
                 if (0 < count($functions)) {
                     foreach ($functions as $fct) {
                         $parts = explode(':', $fct);
                         $functionName = array_shift($parts);
                         array_unshift($parts, $this->attributes[$attribute]);
                         $this->attributes[$attribute];
                         $this->attributes[$attribute] = call_user_func_array($functionName, $parts);
                     }
                 }
             } catch (\Exception $e) {
             }
         } elseif (preg_match('/^\\#([a-z]+)$/i', $value, $matches)) {
             switch (strtolower($matches[1])) {
                 case 'url':
                     if (null !== $page) {
                         $this->attributes[$attribute] = $page->getUrl();
                     }
                 default:
                     break;
             }
         }
     }
     return $this;
 }