<?php
/**
* Copyright (C) 2021 Rhyme Digital, LLC.
*
* @link https://rhyme.digital
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
*/
namespace Rhyme\WMassArtsHub\ContentElement;
use Contao\BackendTemplate;
use Contao\ContentText;
use Contao\Environment;
use Contao\FrontendTemplate;
use Contao\FrontendUser;
use Contao\PageModel;
use Contao\StringUtil;
use Contao\System;
use Contao\Template;
use Patchwork\Utf8;
/**
* Generic PagesNav Element
* This is essentially the same as the custom nav module
*/
class PagesNav extends ContentText
{
/**
* Template
* @var string
*/
protected $strTemplate = 'ce_pagesnav';
/**
* Display a wildcard in the back end
* @return string
*/
public function generate()
{
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
{
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ' . Utf8::strtoupper($GLOBALS['TL_LANG']['CTE']['pagesnav'][0]) . ' ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
return $objTemplate->parse();
}
// Always return an array (see #4616)
$this->pages = StringUtil::deserialize($this->pages, true);
if (empty($this->pages) || !$this->pages[0])
{
return '';
}
$strBuffer = parent::generate();
return $this->Template->items ? $strBuffer : '';
}
/**
* Generate content element
*/
protected function compile()
{
/** @var PageModel $objPage */
global $objPage;
$items = array();
$user = null;
if (System::getContainer()->get('contao.security.token_checker')->hasFrontendUser())
{
$user = FrontendUser::getInstance();
}
// Get all active pages and also include root pages if the language is added to the URL (see #72)
$objPages = PageModel::findPublishedRegularWithoutGuestsByIds($this->pages, array('includeRoot'=>true));
// Return if there are no pages
if ($objPages === null)
{
return;
}
$objTemplate = new FrontendTemplate($this->navigationTpl ?: 'nav_pagesnav');
$objTemplate->type = static::class;
$objTemplate->cssID = $this->cssID; // see #4897 and 6129
$objTemplate->level = 'level_1';
$objTemplate->module = $this; // see #155
/** @var PageModel[] $objPages */
foreach ($objPages as $objModel)
{
// Do not show protected pages unless a front end user is logged in
if (!$objModel->protected || $this->showProtected || ($user && $user->isMemberOf(StringUtil::deserialize($objModel->groups))))
{
// Get href
switch ($objModel->type)
{
case 'redirect':
$href = $objModel->url;
break;
case 'root':
// Overwrite the alias to link to the empty URL or language URL (see #1641)
$objModel->alias = 'index';
$href = $objModel->getFrontendUrl();
break;
case 'forward':
if ($objModel->jumpTo)
{
$objNext = PageModel::findPublishedById($objModel->jumpTo);
}
else
{
$objNext = PageModel::findFirstPublishedRegularByPid($objModel->id);
}
if ($objNext instanceof PageModel)
{
$href = $objNext->getFrontendUrl();
break;
}
// no break
default:
$href = $objModel->getFrontendUrl();
break;
}
$trail = \in_array($objModel->id, $objPage->trail);
// Use the path without query string to check for active pages (see #480)
list($path) = explode('?', Environment::get('request'), 2);
// Active page
if ($objPage->id == $objModel->id && $href == $path)
{
$strClass = trim($objModel->cssClass);
$row = $objModel->row();
$row['isActive'] = true;
$row['isTrail'] = false;
$row['class'] = trim('active ' . $strClass);
$row['title'] = StringUtil::specialchars($objModel->title, true);
$row['pageTitle'] = StringUtil::specialchars($objModel->pageTitle, true);
$row['link'] = $objModel->title;
$row['href'] = $href;
$row['rel'] = '';
$row['nofollow'] = (strncmp($objModel->robots, 'noindex,nofollow', 16) === 0);
$row['target'] = '';
$row['description'] = str_replace(array("\n", "\r"), array(' ', ''), $objModel->description);
$arrRel = array();
if (strncmp($objModel->robots, 'noindex,nofollow', 16) === 0)
{
$arrRel[] = 'nofollow';
}
// Override the link target
if ($objModel->type == 'redirect' && $objModel->target)
{
$arrRel[] = 'noreferrer';
$arrRel[] = 'noopener';
$row['target'] = ' target="_blank"';
}
// Set the rel attribute
if (!empty($arrRel))
{
$row['rel'] = ' rel="' . implode(' ', $arrRel) . '"';
}
$items[] = $row;
}
// Regular page
else
{
$strClass = trim($objModel->cssClass . ($trail ? ' trail' : ''));
$row = $objModel->row();
$row['isActive'] = false;
$row['isTrail'] = $trail;
$row['class'] = $strClass;
$row['title'] = StringUtil::specialchars($objModel->title, true);
$row['pageTitle'] = StringUtil::specialchars($objModel->pageTitle, true);
$row['link'] = $objModel->title;
$row['href'] = $href;
$row['rel'] = '';
$row['nofollow'] = (strncmp($objModel->robots, 'noindex,nofollow', 16) === 0);
$row['target'] = '';
$row['description'] = str_replace(array("\n", "\r"), array(' ', ''), $objModel->description);
$arrRel = array();
if (strncmp($objModel->robots, 'noindex,nofollow', 16) === 0)
{
$arrRel[] = 'nofollow';
}
// Override the link target
if ($objModel->type == 'redirect' && $objModel->target)
{
$arrRel[] = 'noreferrer';
$arrRel[] = 'noopener';
$row['target'] = ' target="_blank"';
}
// Set the rel attribute
if (!empty($arrRel))
{
$row['rel'] = ' rel="' . implode(' ', $arrRel) . '"';
}
$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->addImage = false;
parent::compile();
// Add an image
if ($this->addImage)
{
$this->Template->addImage = true;
}
$extraClassesArr = StringUtil::deserialize($this->classOptions, true);
$extraClasses = '';
foreach($extraClassesArr as $class) {
$extraClasses .= ' ' . $class;
}
if($this->addImage) {
$extraClasses .= ' hasImage';
}
$this->Template->extraClasses = $extraClasses;
$this->Template->request = Environment::get('indexFreeRequest');
$this->Template->skipId = 'skipNavigation' . $this->id;
$this->Template->skipNavigation = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['skipNavigation']);
$this->Template->items = !empty($items) ? $objTemplate->parse() : '';
}
}