<?php
/**
* Copyright (C) 2021 Rhyme Digital, LLC.
*
* @author Blair Winans <blair@rhyme.digital>
* @author Adam Fisher <adam@rhyme.digital>
* @link https://rhyme.digital
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
*/
namespace Rhyme\WMassArtsHub\Module\Profile;
use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
use Contao\Database;
use Contao\PageModel;
use Contao\StringUtil;
use Contao\Environment;
use Contao\PageError404;
use Contao\BackendTemplate;
use Contao\System;
use Haste\Http\Response\HtmlResponse;
use Haste\Input\Input;
use Rhyme\WMassArtsHub\Model\Profile\Item as ProfileItemModel;
/**
* Class Reader
* @package Rhyme\WMassArtsHub\Module\Collection
*/
class Reader extends Module
{
/**
* Template
* @var string
*/
protected $strTemplate = 'mod_artshub_profile_reader';
/**
* Item
* @var ProfileItemModel
*/
protected $objItem;
/**
* Display a wildcard in the back end
* @return string
*/
public function generate()
{
if ('BE' === TL_MODE) {
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ' . \strtoupper($GLOBALS['TL_LANG']['FMD'][$this->type][0]) . ' ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id;
return $objTemplate->parse();
}
// Return if no item has been specified
if (Input::getAutoItem('detail', false, true) == '') {
if ($this->iso_display404Page) {
$this->generate404();
} else {
return '';
}
}
return parent::generate();
}
/**
* Generate module
* @return void
*/
protected function compile()
{
global $objPage;
$objItem = ProfileItemModel::findAvailableByIdOrAlias(Input::getAutoItem('detail'));
if (null === $objItem) {
$this->generate404();
}
$arrConfig = array(
'module' => $this,
'template' => $this->artshub_profile_reader_layout ?: 'artshub_profile_reader_default',
'jumpTo' => $objPage,
'imgSize' => $this->imgSize,
'gallery_imgSize' => $this->gallery_imgSize
);
$GLOBALS['TL_JAVASCRIPT']['jquerymosaic'] = 'web/bundles/rhymewmassartshub/assets/js/vendor/jquerymosaic/jquery.mosaic.js|static';
$GLOBALS['TL_CSS']['jquerymosaic'] = 'web/bundles/rhymewmassartshub/assets/js/vendor/jquerymosaic/jquery.mosaic.css|static';
$this->addMetaTags($objItem);
$this->Template->item = $objItem->generate($arrConfig);
$this->Template->item_id = $this->getCssId($objItem);
$this->Template->item_class = $this->getCssClass($objItem);
$this->Template->referer = 'javascript:history.go(-1)';
$this->Template->back = $GLOBALS['TL_LANG']['MSC']['goBack'];
}
/**
* Add meta header fields to the current page
*
* @param ProfileItemModel $objItem
*/
protected function addMetaTags(ProfileItemModel $objItem)
{
// Overwrite the page meta data (see #2853, #4955 and #87)
$responseContext = System::getContainer()->get(ResponseContextAccessor::class)->getResponseContext();
if ($responseContext && $responseContext->has(HtmlHeadBag::class))
{
/** @var HtmlHeadBag $htmlHeadBag */
$htmlHeadBag = $responseContext->get(HtmlHeadBag::class);
$htmlHeadBag->setTitle($objItem->getPageTitle());
$htmlHeadBag->setMetaDescription(StringUtil::inputEncodedToPlainText($objItem->getPageDescription()));
}
}
/**
* Gets the CSS ID for this item
*
* @param ProfileItemModel $objItem
*
* @return string|null
*/
protected function getCssId(ProfileItemModel $objItem)
{
$css = StringUtil::deserialize(\strval($objItem->cssID), true);
return $css[0] ? ' id="' . $css[0] . '"' : null;
}
/**
* Gets the CSS classes for this item
*
* @param ProfileItemModel $objItem
*
* @return string
*/
protected function getCssClass(ProfileItemModel $objItem)
{
$classes = ['item'];
$arrCSS = StringUtil::deserialize($objItem->cssID, true);
if (\count($arrCSS) > 1 && '' !== (string) $arrCSS[1]) {
$classes[] = (string) $arrCSS[1];
}
return \implode(' ', $classes);
}
/**
* Generates a 404 page and stops page output.
*/
private function generate404()
{
/** @var PageError404 $objHandler */
$objHandler = new $GLOBALS['TL_PTY']['error_404']();
$objHandler->generate();
exit;
}
}