findPublishedById() public static method

Find a published page by its ID
public static findPublishedById ( integer $intId, array $arrOptions = [] ) : PageModel | null
$intId integer The page ID
$arrOptions array An optional options array
return PageModel | null The model or null if there is no published page
Example #1
0
 /**
  * Prepare the output
  *
  * @param PageModel|integer $objRootPage
  *
  * @return PageModel
  *
  * @throws AccessDeniedException
  *
  * @internal Do not call this method in your code. It will be made private in Contao 5.0.
  */
 protected function prepare($objRootPage = null)
 {
     // Use the given root page object if available (thanks to Andreas Schempp)
     if ($objRootPage === null) {
         $objRootPage = $this->getRootPageFromUrl();
     } else {
         $objRootPage = \PageModel::findPublishedById(is_integer($objRootPage) ? $objRootPage : $objRootPage->id);
     }
     // Look for a 403 page
     $obj403 = \PageModel::find403ByPid($objRootPage->id);
     // Die if there is no page at all
     if (null === $obj403) {
         throw new AccessDeniedException('Forbidden');
     }
     // Forward to another page
     if ($obj403->autoforward && $obj403->jumpTo) {
         $objNextPage = \PageModel::findPublishedById($obj403->jumpTo);
         if (null === $objNextPage) {
             $this->log('Forward page ID "' . $obj403->jumpTo . '" does not exist', __METHOD__, TL_ERROR);
             throw new ForwardPageNotFoundException('Forward page not found');
         }
         $this->redirect($objNextPage->getFrontendUrl(), $obj403->redirect == 'temporary' ? 302 : 301);
     }
     return $obj403;
 }
 /**
  * Fetch the page model
  *
  * @param array $entry
  *
  * @return PageModel|null
  */
 private function fetchPageModel(array $entry)
 {
     $pageId = $this->fetchPageId($entry);
     if ($pageId === null) {
         return null;
     }
     return PageModel::findPublishedById($pageId);
 }
Example #3
0
 /**
  * Prepare the output
  *
  * @return \PageModel
  *
  * @internal
  */
 protected function prepare()
 {
     // Check the search index (see #3761)
     \Search::removeEntry(\Environment::get('relativeRequest'));
     // Find the matching root page
     $objRootPage = $this->getRootPageFromUrl();
     // Forward if the language should be but is not set (see #4028)
     if (\Config::get('addLanguageToUrl')) {
         // Get the request string without the script name
         $strRequest = \Environment::get('relativeRequest');
         // Only redirect if there is no language fragment (see #4669)
         if ($strRequest != '' && !preg_match('@^[a-z]{2}(\\-[A-Z]{2})?/@', $strRequest)) {
             // Handle language fragments without trailing slash (see #7666)
             if (preg_match('@^[a-z]{2}(\\-[A-Z]{2})?$@', $strRequest)) {
                 $this->redirect(\Environment::get('request') . '/', 301);
             } else {
                 if ($strRequest == \Environment::get('request')) {
                     $strRequest = $objRootPage->language . '/' . $strRequest;
                 } else {
                     $strRequest = \Environment::get('script') . '/' . $objRootPage->language . '/' . $strRequest;
                 }
                 $this->redirect($strRequest, 301);
             }
         }
     }
     // Look for a 404 page
     $obj404 = \PageModel::find404ByPid($objRootPage->id);
     // Die if there is no page at all
     if (null === $obj404) {
         throw new PageNotFoundException('Page not found');
     }
     // Forward to another page
     if ($obj404->autoforward && $obj404->jumpTo) {
         $objNextPage = \PageModel::findPublishedById($obj404->jumpTo);
         if (null === $objNextPage) {
             $this->log('Forward page ID "' . $obj404->jumpTo . '" does not exist', __METHOD__, TL_ERROR);
             throw new ForwardPageNotFoundException('Forward page not found');
         }
         $this->redirect($this->generateFrontendUrl($objNextPage->row(), null, $objRootPage->language), $obj404->redirect == 'temporary' ? 302 : 301);
     }
     return $obj404;
 }
Example #4
0
 /**
  * Redirect to a jumpTo page or reload the current page
  *
  * @param integer|array $intId
  * @param string        $strParams
  * @param string        $strForceLang
  */
 protected function jumpToOrReload($intId, $strParams = null, $strForceLang = null)
 {
     if ($strForceLang !== null) {
         @trigger_error('Using Frontend::jumpToOrReload() with $strForceLang has been deprecated and will no longer work in Contao 5.0.', E_USER_DEPRECATED);
     }
     /** @var PageModel $objPage */
     global $objPage;
     // Always redirect if there are additional arguments (see #5734)
     $blnForceRedirect = $strParams !== null || $strForceLang !== null;
     if (is_array($intId)) {
         if ($intId['id'] != '') {
             if ($intId['id'] != $objPage->id || $blnForceRedirect) {
                 $this->redirect($this->generateFrontendUrl($intId, $strParams, $strForceLang, true));
             }
         }
     } elseif ($intId > 0) {
         if ($intId != $objPage->id || $blnForceRedirect) {
             if (($objNextPage = \PageModel::findPublishedById($intId)) !== null) {
                 $this->redirect($objNextPage->getFrontendUrl($strParams, $strForceLang));
             }
         }
     }
     $this->reload();
 }
Example #5
0
 /**
  * Generate the module
  */
 protected function compile()
 {
     // Get all active pages
     $objPages = \PageModel::findPublishedRegularWithoutGuestsByIds($this->pages);
     // Return if there are no pages
     if ($objPages === null) {
         return;
     }
     $arrPages = array();
     // Sort the array keys according to the given order
     if ($this->orderPages != '') {
         $tmp = deserialize($this->orderPages);
         if (!empty($tmp) && is_array($tmp)) {
             $arrPages = array_map(function () {
             }, array_flip($tmp));
         }
     }
     // Add the items to the pre-sorted array
     while ($objPages->next()) {
         /** @var PageModel $objModel */
         $objModel = $objPages->current();
         $arrPages[$objPages->id] = $objModel->loadDetails()->row();
         // see #3765
     }
     $items = array();
     foreach ($arrPages as $arrPage) {
         $arrPage['title'] = strip_insert_tags($arrPage['title']);
         $arrPage['pageTitle'] = strip_insert_tags($arrPage['pageTitle']);
         // Get href
         switch ($arrPage['type']) {
             case 'redirect':
                 $href = $arrPage['url'];
                 break;
             case 'forward':
                 if (($objNext = \PageModel::findPublishedById($arrPage['jumpTo'])) !== null) {
                     $strForceLang = null;
                     $objNext->loadDetails();
                     // Check the target page language (see #4706)
                     if (\Config::get('addLanguageToUrl')) {
                         $strForceLang = $objNext->language;
                     }
                     $href = $this->generateFrontendUrl($objNext->row(), null, $strForceLang, true);
                     break;
                 }
                 // DO NOT ADD A break; STATEMENT
             // DO NOT ADD A break; STATEMENT
             default:
                 $href = $this->generateFrontendUrl($arrPage, null, $arrPage['rootLanguage'], true);
                 break;
         }
         $items[] = array('href' => $href, 'title' => specialchars($arrPage['pageTitle'] ?: $arrPage['title']), 'link' => $arrPage['title']);
     }
     $this->Template->items = $items;
     $this->Template->formId = 'tl_quicklink_' . $this->id;
     $this->Template->request = ampersand(\Environment::get('request'), true);
     $this->Template->title = $this->customLabel ?: $GLOBALS['TL_LANG']['MSC']['quicklink'];
     $this->Template->button = specialchars($GLOBALS['TL_LANG']['MSC']['go']);
 }
Example #6
0
 /**
  * Generate the module
  */
 protected function compile()
 {
     /** @var PageModel $objPage */
     global $objPage;
     $items = array();
     $groups = array();
     // Get all groups of the current front end user
     if (FE_USER_LOGGED_IN) {
         $this->import('FrontendUser', 'User');
         $groups = $this->User->groups;
     }
     // Get all active pages
     $objPages = \PageModel::findPublishedRegularWithoutGuestsByIds($this->pages);
     // Return if there are no pages
     if ($objPages === null) {
         return;
     }
     $arrPages = array();
     // Sort the array keys according to the given order
     if ($this->orderPages != '') {
         $tmp = deserialize($this->orderPages);
         if (!empty($tmp) && is_array($tmp)) {
             $arrPages = array_map(function () {
             }, array_flip($tmp));
         }
     }
     // Add the items to the pre-sorted array
     while ($objPages->next()) {
         /** @var PageModel $objPages */
         $objModel = $objPages->current();
         $arrPages[$objPages->id] = $objModel->loadDetails()->row();
         // see #3765
     }
     // Set default template
     if ($this->navigationTpl == '') {
         $this->navigationTpl = 'nav_default';
     }
     /** @var FrontendTemplate|object $objTemplate */
     $objTemplate = new \FrontendTemplate($this->navigationTpl);
     $objTemplate->type = get_class($this);
     $objTemplate->cssID = $this->cssID;
     // see #4897 and 6129
     $objTemplate->level = 'level_1';
     foreach ($arrPages as $arrPage) {
         // Skip hidden pages (see #5832)
         if (!is_array($arrPage)) {
             continue;
         }
         $_groups = deserialize($arrPage['groups']);
         // Do not show protected pages unless a back end or front end user is logged in
         if (!$arrPage['protected'] || BE_USER_LOGGED_IN || is_array($_groups) && count(array_intersect($_groups, $groups)) || $this->showProtected) {
             // Get href
             switch ($arrPage['type']) {
                 case 'redirect':
                     $href = $arrPage['url'];
                     break;
                 case 'forward':
                     if (($objNext = \PageModel::findPublishedById($arrPage['jumpTo'])) !== null) {
                         $strForceLang = null;
                         $objNext->loadDetails();
                         // Check the target page language (see #4706)
                         if (\Config::get('addLanguageToUrl')) {
                             $strForceLang = $objNext->language;
                         }
                         $href = $this->generateFrontendUrl($objNext->row(), null, $strForceLang, true);
                         break;
                     }
                     // DO NOT ADD A break; STATEMENT
                 // DO NOT ADD A break; STATEMENT
                 default:
                     $href = $this->generateFrontendUrl($arrPage, null, $arrPage['rootLanguage'], true);
                     break;
             }
             $trail = in_array($arrPage['id'], $objPage->trail);
             // Active page
             if ($objPage->id == $arrPage['id'] && $href == \Environment::get('request')) {
                 $strClass = trim($arrPage['cssClass']);
                 $row = $arrPage;
                 $row['isActive'] = true;
                 $row['isTrail'] = false;
                 $row['class'] = trim('active ' . $strClass);
                 $row['title'] = specialchars($arrPage['title'], true);
                 $row['pageTitle'] = specialchars($arrPage['pageTitle'], true);
                 $row['link'] = $arrPage['title'];
                 $row['href'] = $href;
                 $row['nofollow'] = strncmp($arrPage['robots'], 'noindex', 7) === 0;
                 $row['target'] = '';
                 $row['description'] = str_replace(array("\n", "\r"), array(' ', ''), $arrPage['description']);
                 // Override the link target
                 if ($arrPage['type'] == 'redirect' && $arrPage['target']) {
                     $row['target'] = ' target="_blank"';
                 }
                 $items[] = $row;
             } else {
                 $strClass = trim($arrPage['cssClass'] . ($trail ? ' trail' : ''));
                 $row = $arrPage;
                 $row['isActive'] = false;
                 $row['isTrail'] = $trail;
                 $row['class'] = $strClass;
                 $row['title'] = specialchars($arrPage['title'], true);
                 $row['pageTitle'] = specialchars($arrPage['pageTitle'], true);
                 $row['link'] = $arrPage['title'];
                 $row['href'] = $href;
                 $row['nofollow'] = strncmp($arrPage['robots'], 'noindex', 7) === 0;
                 $row['target'] = '';
                 $row['description'] = str_replace(array("\n", "\r"), array(' ', ''), $arrPage['description']);
                 // Override the link target
                 if ($arrPage['type'] == 'redirect' && $arrPage['target']) {
                     $row['target'] = ' target="_blank"';
                 }
                 $items[] = $row;
             }
         }
     }
     // Add classes first and last
     $items[0]['class'] = trim($items[0]['class'] . ' first');
     $last = count($items) - 1;
     $items[$last]['class'] = trim($items[$last]['class'] . ' last');
     $objTemplate->items = $items;
     $this->Template->request = \Environment::get('indexFreeRequest');
     $this->Template->skipId = 'skipNavigation' . $this->id;
     $this->Template->skipNavigation = specialchars($GLOBALS['TL_LANG']['MSC']['skipNavigation']);
     $this->Template->items = !empty($items) ? $objTemplate->parse() : '';
 }
Example #7
0
 /**
  * Redirect to a jumpTo page or reload the current page
  *
  * @param integer|array $intId
  * @param string        $strParams
  * @param string        $strForceLang
  */
 protected function jumpToOrReload($intId, $strParams = null, $strForceLang = null)
 {
     /** @var PageModel $objPage */
     global $objPage;
     // Always redirect if there are additional arguments (see #5734)
     $blnForceRedirect = $strParams !== null || $strForceLang !== null;
     if (is_array($intId)) {
         if ($intId['id'] != '') {
             if ($intId['id'] != $objPage->id || $blnForceRedirect) {
                 $this->redirect($this->generateFrontendUrl($intId, $strParams, $strForceLang));
             }
         }
     } elseif ($intId > 0) {
         if ($intId != $objPage->id || $blnForceRedirect) {
             if (($objNextPage = \PageModel::findPublishedById($intId)) !== null) {
                 $this->redirect($this->generateFrontendUrl($objNextPage->row(), $strParams, $strForceLang));
             }
         }
     }
     $this->reload();
 }
 /**
  * @param PageModel $page
  * @param string    $language
  *
  * @return PageModel
  *
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  */
 public function findAssociatedParentForLanguage(PageModel $page, $language)
 {
     // Stop loop if we're at the top
     if (0 === $page->pid || 'root' === $page->type) {
         $rootPages = $this->findRootPagesForPage($page);
         foreach ($rootPages as $model) {
             if (Language::toLocaleID($model->language) === $language) {
                 return $model;
             }
         }
         throw new \InvalidArgumentException(sprintf('There\'s no language "%s" related to root page ID "%s"', $language, $page->id));
     }
     $parent = PageModel::findPublishedById($page->pid);
     if (!$parent instanceof PageModel) {
         throw new \RuntimeException(sprintf('Parent page for page ID "%s" not found', $page->id));
     }
     return $this->findAssociatedForLanguage($parent, $language);
 }
Example #9
0
 /**
  * Generate the module
  */
 protected function compile()
 {
     /** @var PageModel $objPage */
     global $objPage;
     $type = null;
     $pageId = $objPage->id;
     $pages = array($objPage->row());
     $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->row();
         }
     }
     // 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 ? $this->generateFrontendUrl($objFirstPage->row()) : \Environment::get('base'), 'title' => specialchars($objPages->pageTitle ?: $objPages->title, true), 'link' => $objPages->title, 'data' => $objFirstPage->row(), 'class' => '');
         array_pop($pages);
     }
     // Build the breadcrumb menu
     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 = \PageModel::findPublishedById($pages[$i]['jumpTo'])) !== null) {
                     $strForceLang = null;
                     $objNext->loadDetails();
                     // Check the target page language (see #4706)
                     if (\Config::get('addLanguageToUrl')) {
                         $strForceLang = $objNext->language;
                     }
                     $href = $this->generateFrontendUrl($objNext->row(), null, $strForceLang, true);
                     break;
                 }
                 // DO NOT ADD A break; STATEMENT
             // DO NOT ADD A break; STATEMENT
             default:
                 $href = $this->generateFrontendUrl($pages[$i]);
                 break;
         }
         $items[] = array('isRoot' => false, 'isActive' => false, 'href' => $href, 'title' => specialchars($pages[$i]['pageTitle'] ?: $pages[$i]['title'], true), 'link' => $pages[$i]['title'], 'data' => $pages[$i], 'class' => '');
     }
     // Active article
     if (isset($_GET['articles'])) {
         $items[] = array('isRoot' => false, 'isActive' => false, 'href' => $this->generateFrontendUrl($pages[0]), 'title' => specialchars($pages[0]['pageTitle'] ?: $pages[0]['title'], true), 'link' => $pages[0]['title'], 'data' => $pages[0], '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' => $this->generateFrontendUrl($pages[0], '/articles/' . $strAlias), 'title' => specialchars($objArticle->title, true), 'link' => $objArticle->title, 'data' => $objArticle->row(), 'class' => '');
         }
     } else {
         $items[] = array('isRoot' => false, 'isActive' => true, 'href' => $this->generateFrontendUrl($pages[0]), 'title' => specialchars($pages[0]['pageTitle'] ?: $pages[0]['title']), 'link' => $pages[0]['title'], 'data' => $pages[0], '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;
 }