vendor/contao-themes-net/mate-theme-bundle/src/Resources/contao/modules/Mate/NavBarModule.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Open Source CMS
  4.  *
  5.  * Copyright (c) 2005-2016 Leo Feyer
  6.  *
  7.  * @license LGPL-3.0+
  8.  */
  9. namespace ContaoThemesNet\MateThemeBundle\Mate;
  10. use Contao\Environment;
  11. use Contao\FrontendTemplate;
  12. use Contao\FrontendUser;
  13. use Contao\ModuleSitemap;
  14. use Contao\PageModel;
  15. use Contao\StringUtil;
  16. use Contao\System;
  17. /**
  18.  * Class NavbarModule
  19.  *
  20.  * @author Mathias Arzberger <develop@pdir.de>
  21.  */
  22. class NavBarModule extends \Module
  23. {
  24.     /**
  25.      * Display a wildcard in the back end
  26.      *
  27.      * @return string
  28.      */
  29.     public function generate()
  30.     {
  31.         if (TL_MODE == 'BE')
  32.         {
  33.             /** @var \BackendTemplate|object $objTemplate */
  34.             $objTemplate = new \BackendTemplate('be_wildcard');
  35.             $objTemplate->wildcard '### ' utf8_strtoupper($GLOBALS['TL_LANG']['CTE']['mateTheme']['navbar'][0]) . ' ###';
  36.             $objTemplate->title $this->headline;
  37.             $objTemplate->id $this->id;
  38.             $objTemplate->link $this->name;
  39.             $objTemplate->href 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' $this->id;
  40.             return $objTemplate->parse();
  41.         }
  42.         return parent::generate();
  43.     }
  44.     /**
  45.      * Generate the module
  46.      */
  47.     protected function compile()
  48.     {
  49.         if ($this->mateNavbarType == 'type2') {
  50.             $this->Template->setName('mod_mate_navbar_left');
  51.         } else {
  52.             $this->Template->setName('mod_mate_navbar');
  53.         }
  54.         if($this->mateNavbar_customTpl != "") {
  55.             $this->Template->setName($this->mateNavbar_customTpl);
  56.         }
  57.         $this->Template->navbarType $this->mateNavbarType;
  58.         $this->Template->showLogo $this->mateShowLogo;
  59.         $this->Template->showSearch $this->mateShowSearch;
  60.         $this->Template->showMobileMenu $this->mateShowMobileMenu;
  61.         $this->Template->headline $this->headline;
  62.         /** @var \PageModel $objPage */
  63.         global $objPage;
  64.         // Set the trail and level
  65.         if ($this->defineRoot && $this->rootPage 0)
  66.         {
  67.             $trail = array($this->rootPage);
  68.             $level 0;
  69.         }
  70.         else
  71.         {
  72.             $trail $objPage->trail;
  73.             $level = ($this->levelOffset 0) ? $this->levelOffset 0;
  74.         }
  75.         $lang null;
  76.         $host null;
  77.         // Overwrite the domain and language if the reference page belongs to a differnt root page (see #3765)
  78.         if ($this->defineRoot && $this->rootPage 0)
  79.         {
  80.             $objRootPage \PageModel::findWithDetails($this->rootPage);
  81.             // Set the language
  82.             if (\Config::get('addLanguageToUrl') && $objRootPage->rootLanguage != $objPage->rootLanguage)
  83.             {
  84.                 $lang $objRootPage->rootLanguage;
  85.             }
  86.             // Set the domain
  87.             if ($objRootPage->rootId != $objPage->rootId && $objRootPage->domain != '' && $objRootPage->domain != $objPage->domain)
  88.             {
  89.                 $host $objRootPage->domain;
  90.             }
  91.         }
  92.         $this->Template->request ampersand(\Environment::get('indexFreeRequest'));
  93.         $this->Template->skipId 'skipNavigation' $this->id;
  94.         $this->Template->skipNavigation specialchars($GLOBALS['TL_LANG']['MSC']['skipNavigation']);
  95.         $arrItems $this->getNavigationMenu($trail[$level], 1$host$lang);
  96.         // root layout template fallback
  97.         if ($this->mateNavbarType == 'type1')
  98.         {
  99.             $this->mateRootTpl 'nav_mate_left';
  100.             $this->mateMobileTpl 'nav_mate_mobile';
  101.             $this->mateDropdownTpl '';
  102.             $this->mateDropdownTplLvl3 '';
  103.             $this->mateDropdownTplLvl4 '';
  104.         }
  105.         if ($this->mateNavbarType == 'type2')
  106.         {
  107.             $this->mateRootTpl 'nav_mate_left';
  108.             $this->mateMobileTpl 'nav_mate_mobile';
  109.             $this->mateDropdownTpl '';
  110.             $this->mateDropdownTplLvl3 '';
  111.             $this->mateDropdownTplLvl4 '';
  112.         }
  113.         if ($this->mateNavbarType == 'type5' || $this->mateNavbarType == 'type3')
  114.         {
  115.             $this->mateRootTpl 'nav_mate_root';
  116.             $this->mateMobileTpl 'nav_mate_mobile';
  117.             $this->mateDropdownTpl 'nav_mate_dropdown';
  118.             $this->mateDropdownTplLvl3 'nav_mate_dropdown_lvl3';
  119.             $this->mateDropdownTplLvl4 'nav_mate_dropdown_lvl4';
  120.         }
  121.         if(is_array($arrItems) && count($arrItems) > 0) {
  122.             /** @var \FrontendTemplate|object $objTemplate */
  123.             $objTemplate = new \FrontendTemplate($this->mateRootTpl);
  124.             $objTemplate->items $arrItems;
  125.             $objTemplate->id $this->id;
  126.             $this->Template->rootNav $objTemplate->parse();
  127.             $objTemplate = new \FrontendTemplate($this->mateMobileTpl);
  128.             $objTemplate->items $arrItems;
  129.             $objTemplate->id $this->id;
  130.             $this->Template->mobileNav $objTemplate->parse();
  131.             $strDropdownNav '';
  132.             $strDropdownNavLvl3 '';
  133.             $strDropdownNavLvl4 '';
  134.             if(is_array($arrItems)) {
  135.                 foreach ($arrItems as $item) {
  136.                     if(isset($item['subitems']) && is_array($item['subitems']) && count($item['subitems']) > 0) {
  137.                         /** @var \FrontendTemplate|object $objTemplate */
  138.                         $objTemplate = new \FrontendTemplate($this->mateDropdownTpl);
  139.                         $objTemplate->id $this->id;
  140.                         $objTemplate->itemId $item['id'];
  141.                         $objTemplate->items $item['subitems'];
  142.                         $strDropdownNav .= $objTemplate->parse();
  143.                         foreach ($item['subitems'] as $subitem) {
  144.                             if(isset($subitem['subitems']) && is_array($subitem['subitems']) && count($subitem['subitems']) > 0) {
  145.                                 /** @var \FrontendTemplate|object $objTemplate */
  146.                                 $objTemplate = new \FrontendTemplate($this->mateDropdownTplLvl3);
  147.                                 $objTemplate->id $this->id;
  148.                                 $objTemplate->itemId $subitem['id'];
  149.                                 $objTemplate->items $subitem['subitems'];
  150.                                 $strDropdownNavLvl3 .= $objTemplate->parse();
  151.                                 foreach ($subitem['subitems'] as $subitemLvl4) {
  152.                                     if(isset($subitemLvl4['subitems']) && is_array($subitemLvl4['subitems']) && count($subitemLvl4['subitems']) > 0) {
  153.                                         /** @var \FrontendTemplate|object $objTemplate */
  154.                                         $objTemplate = new \FrontendTemplate($this->mateDropdownTplLvl4);
  155.                                         $objTemplate->id $this->id;
  156.                                         $objTemplate->itemId $subitemLvl4['id'];
  157.                                         $objTemplate->items $subitemLvl4['subitems'];
  158.                                         $strDropdownNavLvl4 .= $objTemplate->parse();
  159.                                     }
  160.                                 }
  161.                             }
  162.                         }
  163.                     }
  164.                 }
  165.             }
  166.             $this->Template->dropdownNav $strDropdownNav;
  167.             $this->Template->dropdownNavLvl3 $strDropdownNavLvl3;
  168.             $this->Template->dropdownNavLvl4 $strDropdownNavLvl4;
  169.         }
  170.         if($this->mateIncludeHeadroom == 1) {
  171.             $this->Template->includeHeadroom " includeHeadroom";
  172.         } else {
  173.             $this->Template->includeHeadroom "";
  174.         }
  175.         if($this->mateNavbarStuck == 1) {
  176.             $this->Template->stuckNavbar " stuckNavbar";
  177.         } else {
  178.             $this->Template->stuckNavbar "";
  179.         }
  180.         $this->Template->pid $trail[$level];
  181.         $this->Template->type get_class($this);
  182.         $this->Template->cssID $this->cssID;
  183.     }
  184.     /**
  185.      * Recursively compile the navigation menu and return it as array
  186.      *
  187.      * @param integer $pid
  188.      * @param integer $level
  189.      * @param string  $host
  190.      * @param string  $language
  191.      *
  192.      * @return string
  193.      */
  194.     protected function getNavigationMenu($pid$level=1$host=null$language=null)
  195.     {
  196.         // Get all active subpages
  197.         if (version_compare(VERSION'4.12''<')) {
  198.             $arrSubpages = static::getPublishedSubpagesWithoutGuestsByPid($pid$this->showHidden$this instanceof ModuleSitemap);
  199.         } else {
  200.             $arrSubpages = static::getPublishedSubpagesByPid($pid$this->showHidden$this instanceof ModuleSitemap);
  201.         }
  202.         if ($arrSubpages === null)
  203.         {
  204.             return '';
  205.         }
  206.         $items = array();
  207.         $security System::getContainer()->get('security.helper');
  208.         $isMember $security->isGranted('ROLE_MEMBER');
  209.         $blnShowUnpublished System::getContainer()->get('contao.security.token_checker')->isPreviewMode();
  210.         $level++;
  211.         /** @var PageModel $objPage */
  212.         global $objPage;
  213.         // Browse subpages
  214.         foreach ($arrSubpages as list('page' => $objSubpage'hasSubpages' => $blnHasSubpages))
  215.         {
  216.             // Skip hidden sitemap pages
  217.             if ($this instanceof ModuleSitemap && $objSubpage->sitemap == 'map_never')
  218.             {
  219.                 continue;
  220.             }
  221.             $objSubpage->loadDetails();
  222.             // Override the domain (see #3765)
  223.             if ($host !== null)
  224.             {
  225.                 $objSubpage->domain $host;
  226.             }
  227.             if ($objSubpage->tabindex 0)
  228.             {
  229.                 trigger_deprecation('contao/core-bundle''4.12''Using a tabindex value greater than 0 has been deprecated and will no longer work in Contao 5.0.');
  230.             }
  231.             // Hide the page if it is not protected and only visible to guests (backwards compatibility)
  232.             if ($objSubpage->guests && !$objSubpage->protected && $isMember)
  233.             {
  234.                 trigger_deprecation('contao/core-bundle''4.12''Using the "show to guests only" feature has been deprecated an will no longer work in Contao 5.0. Use the "protect page" function instead.');
  235.                 continue;
  236.             }
  237.             $subitems '';
  238.             // PageModel->groups is an array after calling loadDetails()
  239.             if ($this->checkPermission($objSubpage$security))
  240.             {
  241.                 // Check whether there will be subpages
  242.                 if ($blnHasSubpages && (!$this->showLevel || $this->showLevel >= $level || (!$this->hardLimit && ($objPage->id == $objSubpage->id || \in_array($objPage->id$this->Database->getChildRecords($objSubpage->id'tl_page'))))))
  243.                 {
  244.                     $subitems $this->getNavigationMenu($objSubpage->id$level$host$language);
  245.                 }
  246.                 // Get href
  247.                 switch ($objSubpage->type)
  248.                 {
  249.                     case 'redirect':
  250.                         $href $objSubpage->url;
  251.                         if (strncasecmp($href'mailto:'7) === 0)
  252.                         {
  253.                             $href StringUtil::encodeEmail($href);
  254.                         }
  255.                         break;
  256.                     case 'forward':
  257.                         if ($objSubpage->jumpTo)
  258.                         {
  259.                             $objNext PageModel::findPublishedById($objSubpage->jumpTo);
  260.                         }
  261.                         else
  262.                         {
  263.                             $objNext PageModel::findFirstPublishedRegularByPid($objSubpage->id);
  264.                         }
  265.                         // Hide the link if the target page is invisible
  266.                         if (!$objNext instanceof PageModel || (!$objNext->loadDetails()->isPublic && !$blnShowUnpublished))
  267.                         {
  268.                             continue 2;
  269.                         }
  270.                         try
  271.                         {
  272.                             $href $objNext->getFrontendUrl();
  273.                         }
  274.                         catch (ExceptionInterface $exception)
  275.                         {
  276.                             continue 2;
  277.                         }
  278.                         break;
  279.                     default:
  280.                         try
  281.                         {
  282.                             $href $objSubpage->getFrontendUrl();
  283.                         }
  284.                         catch (ExceptionInterface $exception)
  285.                         {
  286.                             continue 2;
  287.                         }
  288.                         break;
  289.                 }
  290.                 $items[] = $this->compileNavigationRow($objPage$objSubpage$subitems$href);
  291.             }
  292.         }
  293.         // Add classes first and last
  294.         if (!empty($items))
  295.         {
  296.             $last \count($items) - 1;
  297.             $items[0]['class'] = trim($items[0]['class'] . ' first');
  298.             $items[$last]['class'] = trim($items[$last]['class'] . ' last');
  299.         }
  300.         return $items;
  301.     }
  302.     /**
  303.      * Compile the navigation row and return it as array
  304.      *
  305.      * @param PageModel $objPage
  306.      * @param PageModel $objSubpage
  307.      * @param string    $subitems
  308.      * @param string    $href
  309.      *
  310.      * @return array
  311.      */
  312.     protected function compileNavigationRow(PageModel $objPagePageModel $objSubpage$subitems$href)
  313.     {
  314.         $row $objSubpage->row();
  315.         $trail \in_array($objSubpage->id$objPage->trail);
  316.         // Use the path without query string to check for active pages (see #480)
  317.         list($path) = explode('?'Environment::get('request'), 2);
  318.         // Active page
  319.         if (($objPage->id == $objSubpage->id || ($objSubpage->type == 'forward' && $objPage->id == $objSubpage->jumpTo)) && !($this instanceof ModuleSitemap) && $href == $path)
  320.         {
  321.             // Mark active forward pages (see #4822)
  322.             $strClass = (($objSubpage->type == 'forward' && $objPage->id == $objSubpage->jumpTo) ? 'forward' . ($trail ' trail' '') : 'active') . ($subitems ' submenu' '') . ($objSubpage->protected ' protected' '') . ($objSubpage->cssClass ' ' $objSubpage->cssClass '');
  323.             $row['isActive'] = true;
  324.             $row['isTrail'] = false;
  325.         }
  326.         // Regular page
  327.         else
  328.         {
  329.             $strClass = ($subitems 'submenu' '') . ($objSubpage->protected ' protected' '') . ($trail ' trail' '') . ($objSubpage->cssClass ' ' $objSubpage->cssClass '');
  330.             // Mark pages on the same level (see #2419)
  331.             if ($objSubpage->pid == $objPage->pid)
  332.             {
  333.                 $strClass .= ' sibling';
  334.             }
  335.             $row['isActive'] = false;
  336.             $row['isTrail'] = $trail;
  337.         }
  338.         $row['subitems'] = $subitems;
  339.         $row['class'] = trim($strClass);
  340.         $row['title'] = StringUtil::specialchars($objSubpage->titletrue);
  341.         $row['pageTitle'] = StringUtil::specialchars($objSubpage->pageTitletrue);
  342.         $row['link'] = $objSubpage->title;
  343.         $row['href'] = $href;
  344.         $row['rel'] = '';
  345.         $row['nofollow'] = false// backwards compatibility
  346.         $row['target'] = '';
  347.         $row['description'] = str_replace(array("\n""\r"), array(' '''), (string) $objSubpage->description);
  348.         $arrRel = array();
  349.         // Override the link target
  350.         if ($objSubpage->type == 'redirect' && $objSubpage->target)
  351.         {
  352.             $arrRel[] = 'noreferrer';
  353.             $arrRel[] = 'noopener';
  354.             $row['target'] = ' target="_blank"';
  355.         }
  356.         // Set the rel attribute
  357.         if (!empty($arrRel))
  358.         {
  359.             $row['rel'] = ' rel="' implode(' '$arrRel) . '"';
  360.         }
  361.         // Tag the page
  362.         if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
  363.         {
  364.             $responseTagger System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
  365.             $responseTagger->addTags(array('contao.db.tl_page.' $objSubpage->id));
  366.         }
  367.         return $row;
  368.     }
  369.     protected function checkPermission($objSubpage$security)
  370.     {
  371.         if (version_compare(VERSION'4.12''<'))
  372.         {
  373.             $groups = array();
  374.             $user $security->getUser();
  375.             if ($user instanceof FrontendUser) {
  376.                 $groups StringUtil::deserialize($user->groupstrue);
  377.             }
  378.             $_groups StringUtil::deserialize($objSubpage->groups);
  379.             if (!$objSubpage->protected || BE_USER_LOGGED_IN || (is_array($_groups) && is_array($groups) && count(array_intersect($_groups$groups))) || $this->showProtected || ($this instanceof ModuleSitemap && $objSubpage->sitemap == 'map_always'))
  380.             {
  381.                 return true;
  382.             }
  383.         }
  384.         else
  385.         {
  386.             if(!$objSubpage->protected || $this->showProtected || ($this instanceof ModuleSitemap && $objSubpage->sitemap == 'map_always') || $security->isGranted(\Contao\CoreBundle\Security\ContaoCorePermissions::MEMBER_IN_GROUPS$objSubpage->groups))
  387.             {
  388.                 return true;
  389.             }
  390.         }
  391.         return false;
  392.     }
  393. }