Beispiel #1
0
 public function transform()
 {
     $pageId = $this->getAttribute('pid');
     $attList = null;
     if (empty($pageId)) {
         $pageId = $this->getAttribute('id');
         $attList = array_diff(self::$attributeList, ['id']);
         // remove id from attribute list
     }
     if (empty($pageId)) {
         throw new InvalidArgumentException('No page id defined', E_USER_ERROR);
     }
     /** @var $SMSM SMSManager */
     $SMSM = $this->getDIServiceObject('APF\\extensions\\apfelsms', 'Manager');
     ////
     // evaluate magic page ids
     $magicPageIds = ['__current', '__referer', '__start', '__parent', '__next', '__prev'];
     if (in_array($pageId, $magicPageIds)) {
         switch ($pageId) {
             case '__current':
                 // current page
                 $pageId = $SMSM->getSite()->getCurrentPageId();
                 break;
             case '__referer':
                 // previous visited page
                 // get http referer
                 if ($this->getRequest()->getReferrer() === null) {
                     // fallback on current page
                     $pageId = $SMSM->getSite()->getCurrentPageId();
                     break;
                 }
                 $pageRequestParamName = $SMSM->getPageRequestParamName();
                 $referrerUrl = Url::fromReferrer(true);
                 $currentUrl = Url::fromCurrent(true);
                 // protection against url xss
                 if ($referrerUrl->getHost() == $currentUrl->getHost()) {
                     $pageId = $referrerUrl->getQueryParameter($pageRequestParamName);
                 }
                 if (empty($pageId)) {
                     // safety fallback on current page
                     $pageId = $SMSM->getSite()->getCurrentPageId();
                 }
                 // test for valid page id
                 try {
                     $SMSM->getPage($pageId);
                 } catch (SMSException $e) {
                     $pageId = $SMSM->getSite()->getCurrentPageId();
                 }
                 break;
             case '__start':
                 $pageId = $SMSM->getSite()->getStartPageId();
                 break;
             case '__parent':
                 $currentPage = $SMSM->getSite()->getCurrentPage();
                 $parentPage = $currentPage->getParent();
                 if ($parentPage === null) {
                     $pageId = $SMSM->getSite()->getStartPageId();
                 } else {
                     $pageId = $parentPage->getId();
                 }
                 break;
             case '__next':
             case '__prev':
                 $currentPage = $SMSM->getSite()->getCurrentPage();
                 $siblings = $currentPage->getSiblings(true);
                 if (empty($siblings)) {
                     return '';
                 }
                 /** @var SMSPage[] $visibleSiblings */
                 $visibleSiblings = [];
                 foreach ($siblings as $sibling) {
                     if ($sibling->isHidden()) {
                         continue;
                     }
                     $visibleSiblings[] = $sibling;
                 }
                 $currentIndex = null;
                 $currentId = $currentPage->getId();
                 foreach ($visibleSiblings as $index => $sibling) {
                     if ($sibling->getId() == $currentId) {
                         $currentIndex = $index;
                         break;
                     }
                 }
                 if ($pageId == '__next') {
                     $summand = 1;
                 } else {
                     $summand = -1;
                 }
                 $index = $currentIndex + $summand;
                 if (!isset($visibleSiblings[$index])) {
                     return '';
                 }
                 $pageId = $visibleSiblings[$index]->getId();
                 break;
         }
     }
     // fetch page object
     try {
         $page = $SMSM->getPage($pageId);
     } catch (SMSException $e) {
         // fallback on 404 error page (not found)
         $page = $SMSM->getSite()->get404Page();
     }
     // prepare URL generation
     $url = Url::fromCurrent();
     $url->resetQuery();
     // add anchor to url when set
     $anchor = $this->getAttribute('anchor');
     if (!empty($anchor)) {
         $url->setAnchor($anchor);
     }
     $content = $this->getContent();
     if (empty($content)) {
         $content = StringAssistant::escapeSpecialCharacters($page->getNavTitle());
     }
     $this->setAttribute('title', $this->getAttribute('title', $page->getTitle()));
     $this->setAttribute('href', $page->getLink($url));
     // is there an modified attribute list (without id attribute) ?
     if (!is_array($attList)) {
         $attList = self::$attributeList;
     }
     return str_replace(['{ATTR}', '{TEXT}'], [$this->getAttributesAsString($this->attributes, $attList), $content], self::$template);
 }