src/Rhyme/WMassArtsHub/ContentElement/PagesNav.php line 64

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (C) 2021 Rhyme Digital, LLC.
  4.  *
  5.  * @link        https://rhyme.digital
  6.  * @license        http://www.gnu.org/licenses/lgpl-3.0.html LGPL
  7.  */
  8. namespace Rhyme\WMassArtsHub\ContentElement;
  9. use Contao\BackendTemplate;
  10. use Contao\ContentText;
  11. use Contao\Environment;
  12. use Contao\FrontendTemplate;
  13. use Contao\FrontendUser;
  14. use Contao\PageModel;
  15. use Contao\StringUtil;
  16. use Contao\System;
  17. use Contao\Template;
  18. use Patchwork\Utf8;
  19. /**
  20.  * Generic PagesNav Element
  21.  * This is essentially the same as the custom nav module
  22.  */
  23. class PagesNav extends ContentText
  24. {
  25.     /**
  26.      * Template
  27.      * @var string
  28.      */
  29.     protected $strTemplate 'ce_pagesnav';
  30.     /**
  31.      * Display a wildcard in the back end
  32.      * @return string
  33.      */
  34.     public function generate()
  35.     {
  36.         $request System::getContainer()->get('request_stack')->getCurrentRequest();
  37.         if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
  38.         {
  39.             $objTemplate = new BackendTemplate('be_wildcard');
  40.             $objTemplate->wildcard '### ' Utf8::strtoupper($GLOBALS['TL_LANG']['CTE']['pagesnav'][0]) . ' ###';
  41.             $objTemplate->title $this->headline;
  42.             $objTemplate->id $this->id;
  43.             $objTemplate->link $this->name;
  44.             return $objTemplate->parse();
  45.         }
  46.         // Always return an array (see #4616)
  47.         $this->pages StringUtil::deserialize($this->pagestrue);
  48.         if (empty($this->pages) || !$this->pages[0])
  49.         {
  50.             return '';
  51.         }
  52.         $strBuffer parent::generate();
  53.         return $this->Template->items $strBuffer '';
  54.     }
  55.     /**
  56.      * Generate content element
  57.      */
  58.     protected function compile()
  59.     {
  60.         /** @var PageModel $objPage */
  61.         global $objPage;
  62.         $items = array();
  63.         $user null;
  64.         if (System::getContainer()->get('contao.security.token_checker')->hasFrontendUser())
  65.         {
  66.             $user FrontendUser::getInstance();
  67.         }
  68.         // Get all active pages and also include root pages if the language is added to the URL (see #72)
  69.         $objPages PageModel::findPublishedRegularWithoutGuestsByIds($this->pages, array('includeRoot'=>true));
  70.         // Return if there are no pages
  71.         if ($objPages === null)
  72.         {
  73.             return;
  74.         }
  75.         $objTemplate = new FrontendTemplate($this->navigationTpl ?: 'nav_pagesnav');
  76.         $objTemplate->type = static::class;
  77.         $objTemplate->cssID $this->cssID// see #4897 and 6129
  78.         $objTemplate->level 'level_1';
  79.         $objTemplate->module $this// see #155
  80.         /** @var PageModel[] $objPages */
  81.         foreach ($objPages as $objModel)
  82.         {
  83.             // Do not show protected pages unless a front end user is logged in
  84.             if (!$objModel->protected || $this->showProtected || ($user && $user->isMemberOf(StringUtil::deserialize($objModel->groups))))
  85.             {
  86.                 // Get href
  87.                 switch ($objModel->type)
  88.                 {
  89.                     case 'redirect':
  90.                         $href $objModel->url;
  91.                         break;
  92.                     case 'root':
  93.                         // Overwrite the alias to link to the empty URL or language URL (see #1641)
  94.                         $objModel->alias 'index';
  95.                         $href $objModel->getFrontendUrl();
  96.                         break;
  97.                     case 'forward':
  98.                         if ($objModel->jumpTo)
  99.                         {
  100.                             $objNext PageModel::findPublishedById($objModel->jumpTo);
  101.                         }
  102.                         else
  103.                         {
  104.                             $objNext PageModel::findFirstPublishedRegularByPid($objModel->id);
  105.                         }
  106.                         if ($objNext instanceof PageModel)
  107.                         {
  108.                             $href $objNext->getFrontendUrl();
  109.                             break;
  110.                         }
  111.                     // no break
  112.                     default:
  113.                         $href $objModel->getFrontendUrl();
  114.                         break;
  115.                 }
  116.                 $trail = \in_array($objModel->id$objPage->trail);
  117.                 // Use the path without query string to check for active pages (see #480)
  118.                 list($path) = explode('?'Environment::get('request'), 2);
  119.                 // Active page
  120.                 if ($objPage->id == $objModel->id && $href == $path)
  121.                 {
  122.                     $strClass trim($objModel->cssClass);
  123.                     $row $objModel->row();
  124.                     $row['isActive'] = true;
  125.                     $row['isTrail'] = false;
  126.                     $row['class'] = trim('active ' $strClass);
  127.                     $row['title'] = StringUtil::specialchars($objModel->titletrue);
  128.                     $row['pageTitle'] = StringUtil::specialchars($objModel->pageTitletrue);
  129.                     $row['link'] = $objModel->title;
  130.                     $row['href'] = $href;
  131.                     $row['rel'] = '';
  132.                     $row['nofollow'] = (strncmp($objModel->robots'noindex,nofollow'16) === 0);
  133.                     $row['target'] = '';
  134.                     $row['description'] = str_replace(array("\n""\r"), array(' '''), $objModel->description);
  135.                     $arrRel = array();
  136.                     if (strncmp($objModel->robots'noindex,nofollow'16) === 0)
  137.                     {
  138.                         $arrRel[] = 'nofollow';
  139.                     }
  140.                     // Override the link target
  141.                     if ($objModel->type == 'redirect' && $objModel->target)
  142.                     {
  143.                         $arrRel[] = 'noreferrer';
  144.                         $arrRel[] = 'noopener';
  145.                         $row['target'] = ' target="_blank"';
  146.                     }
  147.                     // Set the rel attribute
  148.                     if (!empty($arrRel))
  149.                     {
  150.                         $row['rel'] = ' rel="' implode(' '$arrRel) . '"';
  151.                     }
  152.                     $items[] = $row;
  153.                 }
  154.                 // Regular page
  155.                 else
  156.                 {
  157.                     $strClass trim($objModel->cssClass . ($trail ' trail' ''));
  158.                     $row $objModel->row();
  159.                     $row['isActive'] = false;
  160.                     $row['isTrail'] = $trail;
  161.                     $row['class'] = $strClass;
  162.                     $row['title'] = StringUtil::specialchars($objModel->titletrue);
  163.                     $row['pageTitle'] = StringUtil::specialchars($objModel->pageTitletrue);
  164.                     $row['link'] = $objModel->title;
  165.                     $row['href'] = $href;
  166.                     $row['rel'] = '';
  167.                     $row['nofollow'] = (strncmp($objModel->robots'noindex,nofollow'16) === 0);
  168.                     $row['target'] = '';
  169.                     $row['description'] = str_replace(array("\n""\r"), array(' '''), $objModel->description);
  170.                     $arrRel = array();
  171.                     if (strncmp($objModel->robots'noindex,nofollow'16) === 0)
  172.                     {
  173.                         $arrRel[] = 'nofollow';
  174.                     }
  175.                     // Override the link target
  176.                     if ($objModel->type == 'redirect' && $objModel->target)
  177.                     {
  178.                         $arrRel[] = 'noreferrer';
  179.                         $arrRel[] = 'noopener';
  180.                         $row['target'] = ' target="_blank"';
  181.                     }
  182.                     // Set the rel attribute
  183.                     if (!empty($arrRel))
  184.                     {
  185.                         $row['rel'] = ' rel="' implode(' '$arrRel) . '"';
  186.                     }
  187.                     $items[] = $row;
  188.                 }
  189.             }
  190.         }
  191.         // Add classes first and last
  192.         $items[0]['class'] = trim($items[0]['class'] . ' first');
  193.         $last = \count($items) - 1;
  194.         $items[$last]['class'] = trim($items[$last]['class'] . ' last');
  195.         $objTemplate->items $items;
  196.         $this->Template->addImage false;
  197.         parent::compile();
  198.         // Add an image
  199.         if ($this->addImage)
  200.         {
  201.             $this->Template->addImage true;
  202.         }
  203.         $extraClassesArr StringUtil::deserialize($this->classOptionstrue);
  204.         $extraClasses '';
  205.         foreach($extraClassesArr as $class) {
  206.             $extraClasses .= ' ' $class;
  207.         }
  208.         if($this->addImage) {
  209.             $extraClasses .= ' hasImage';
  210.         }
  211.         $this->Template->extraClasses $extraClasses;
  212.         $this->Template->request Environment::get('indexFreeRequest');
  213.         $this->Template->skipId 'skipNavigation' $this->id;
  214.         $this->Template->skipNavigation StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['skipNavigation']);
  215.         $this->Template->items = !empty($items) ? $objTemplate->parse() : '';
  216.     }
  217. }