/** * Prepare the page object and redirect URL * * @param integer $rootPageId * * @return PageModel * * @throws NoActivePageFoundException */ protected function getNextPage($rootPageId) { $objNextPage = \PageModel::findFirstPublishedByPid($rootPageId); // No published pages yet if (null === $objNextPage) { $this->log('No active page found under root page "' . $rootPageId . '")', __METHOD__, TL_ERROR); throw new NoActivePageFoundException('No active page found under root page.'); } return $objNextPage; }
/** * Redirect to the first active regular page * * @param integer $rootPageId * @param boolean $blnReturn * @param boolean $blnPreferAlias * * @return integer */ public function generate($rootPageId, $blnReturn = false, $blnPreferAlias = false) { $objNextPage = \PageModel::findFirstPublishedByPid($rootPageId); // No published pages yet if (null === $objNextPage) { $this->log('No active page found under root page "' . $rootPageId . '")', __METHOD__, TL_ERROR); throw new NoActivePageFoundException('No active page found under root page.'); } if (!$blnReturn) { /** @var \PageModel $objPage */ global $objPage; $this->redirect($this->generateFrontendUrl($objNextPage->row(), null, $objPage->language)); } if ($blnPreferAlias && $objNextPage->alias != '') { return $objNextPage->alias; } return $objNextPage->id; }
/** * Try to find a root page based on language and URL * * @return PageModel */ public static function getRootPageFromUrl() { // HOOK: add custom logic if (isset($GLOBALS['TL_HOOKS']['getRootPageFromUrl']) && is_array($GLOBALS['TL_HOOKS']['getRootPageFromUrl'])) { foreach ($GLOBALS['TL_HOOKS']['getRootPageFromUrl'] as $callback) { /** @var PageModel $objRootPage */ if (is_object($objRootPage = static::importStatic($callback[0])->{$callback[1]}())) { return $objRootPage; } } } $host = \Environment::get('host'); // The language is set in the URL if (\Config::get('addLanguageToUrl') && !empty($_GET['language'])) { $objRootPage = \PageModel::findFirstPublishedRootByHostAndLanguage($host, \Input::get('language')); // No matching root page found if ($objRootPage === null) { \System::log('No root page found (host "' . $host . '", language "' . \Input::get('language') . '")', __METHOD__, TL_ERROR); throw new NoRootPageFoundException('No root page found'); } } else { $accept_language = \Environment::get('httpAcceptLanguage'); // Always load the language fall back root if "doNotRedirectEmpty" is enabled if (\Config::get('addLanguageToUrl') && \Config::get('doNotRedirectEmpty')) { $accept_language = '-'; } // Find the matching root pages (thanks to Andreas Schempp) $objRootPage = \PageModel::findFirstPublishedRootByHostAndLanguage($host, $accept_language); // No matching root page found if ($objRootPage === null) { \System::log('No root page found (host "' . \Environment::get('host') . '", languages "' . implode(', ', \Environment::get('httpAcceptLanguage')) . '")', __METHOD__, TL_ERROR); throw new NoRootPageFoundException('No root page found'); } // Redirect to the website root or language root (e.g. en/) if (\Environment::get('relativeRequest') == '') { if (\Config::get('addLanguageToUrl') && !\Config::get('doNotRedirectEmpty')) { $arrParams = array('_locale' => $objRootPage->language); $strUrl = \System::getContainer()->get('router')->generate('contao_index', $arrParams); $strUrl = substr($strUrl, strlen(\Environment::get('path')) + 1); static::redirect($strUrl, 301); } else { $objPage = \PageModel::findFirstPublishedByPid($objRootPage->id); // Redirect if it is not the language fall back page and the alias is "index" (see #8498) if ($objPage !== null && (!$objRootPage->fallback || $objPage->alias != 'index')) { static::redirect($objPage->getFrontendUrl(), 302); } } } } return $objRootPage; }
/** * Generate the module */ protected function compile() { /** @var PageModel $objPage */ global $objPage; $type = null; $pageId = $objPage->id; $pages = array($objPage); $items = array(); // Get all pages up to the root page $objPages = \PageModel::findParentsById($objPage->pid); if ($objPages !== null) { while ($pageId > 0 && $type != 'root' && $objPages->next()) { $type = $objPages->type; $pageId = $objPages->pid; $pages[] = $objPages->current(); } } // Get the first active regular page and display it instead of the root page if ($type == 'root') { $objFirstPage = \PageModel::findFirstPublishedByPid($objPages->id); $items[] = array('isRoot' => true, 'isActive' => false, 'href' => $objFirstPage !== null ? $objFirstPage->getFrontendUrl() : \Environment::get('base'), 'title' => \StringUtil::specialchars($objPages->pageTitle ?: $objPages->title, true), 'link' => $objPages->title, 'data' => $objFirstPage->row(), 'class' => ''); array_pop($pages); } /** @var PageModel[] $pages */ for ($i = count($pages) - 1; $i > 0; $i--) { if ($pages[$i]->hide && !$this->showHidden || !$pages[$i]->published && !BE_USER_LOGGED_IN) { continue; } // Get href switch ($pages[$i]->type) { case 'redirect': $href = $pages[$i]->url; if (strncasecmp($href, 'mailto:', 7) === 0) { $href = \StringUtil::encodeEmail($href); } break; case 'forward': if (($objNext = $pages[$i]->getRelated('jumpTo')) instanceof PageModel || ($objNext = \PageModel::findFirstPublishedRegularByPid($pages[$i]->id)) instanceof PageModel) { /** @var PageModel $objNext */ $href = $objNext->getFrontendUrl(); break; } // DO NOT ADD A break; STATEMENT // DO NOT ADD A break; STATEMENT default: $href = $pages[$i]->getFrontendUrl(); break; } $items[] = array('isRoot' => false, 'isActive' => false, 'href' => $href, 'title' => \StringUtil::specialchars($pages[$i]->pageTitle ?: $pages[$i]->title, true), 'link' => $pages[$i]->title, 'data' => $pages[$i]->row(), 'class' => ''); } // Active article if (isset($_GET['articles'])) { $items[] = array('isRoot' => false, 'isActive' => false, 'href' => $pages[0]->getFrontendUrl(), 'title' => \StringUtil::specialchars($pages[0]->pageTitle ?: $pages[0]->title, true), 'link' => $pages[0]->title, 'data' => $pages[0]->row(), 'class' => ''); list($strSection, $strArticle) = explode(':', \Input::get('articles')); if ($strArticle === null) { $strArticle = $strSection; } $objArticle = \ArticleModel::findByIdOrAlias($strArticle); $strAlias = $objArticle->alias ?: $objArticle->id; if ($objArticle->inColumn != 'main') { $strAlias = $objArticle->inColumn . ':' . $strAlias; } if ($objArticle !== null) { $items[] = array('isRoot' => false, 'isActive' => true, 'href' => $pages[0]->getFrontendUrl('/articles/' . $strAlias), 'title' => \StringUtil::specialchars($objArticle->title, true), 'link' => $objArticle->title, 'data' => $objArticle->row(), 'class' => ''); } } else { $items[] = array('isRoot' => false, 'isActive' => true, 'href' => $pages[0]->getFrontendUrl(), 'title' => \StringUtil::specialchars($pages[0]->pageTitle ?: $pages[0]->title), 'link' => $pages[0]->title, 'data' => $pages[0]->row(), 'class' => ''); } // Mark the first element (see #4833) $items[0]['class'] = 'first'; // HOOK: add custom logic if (isset($GLOBALS['TL_HOOKS']['generateBreadcrumb']) && is_array($GLOBALS['TL_HOOKS']['generateBreadcrumb'])) { foreach ($GLOBALS['TL_HOOKS']['generateBreadcrumb'] as $callback) { $this->import($callback[0]); $items = $this->{$callback[0]}->{$callback[1]}($items, $this); } } $this->Template->items = $items; }