<?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\Module\Event;
use Contao\ArticleModel;
use Contao\Calendar;
use Contao\CalendarEventsModel;
use Contao\CalendarModel;
use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
use Contao\Date;
use Contao\Input;
use Contao\PageModel;
use Contao\StringUtil;
use Contao\Environment;
use Contao\BackendTemplate;
use Contao\ModuleEventReader;
use Contao\CoreBundle\Exception\InternalServerErrorException;
use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\CoreBundle\Exception\RedirectResponseException;
use Contao\System;
use Contao\Template;
use Contao\UserModel;
use FOS\HttpCache\ResponseTagger;
use Patchwork\Utf8;
use Haste\Ajax\ReloadHelper;
use Isotope\Isotope;
use Isotope\Model\Product;
use Isotope\Model\Product\AbstractProduct;
use Rhyme\WMassArtsHub\Model\Event;
/**
* Class Reader
* @package Rhyme\WMassArtsHub\Module\Event
*/
class AjaxReader extends ModuleEventReader
{
/**
* Display a wildcard in the back end
*
* @throws InternalServerErrorException
*
* @return string
*/
public function generate()
{
if (TL_MODE == 'BE')
{
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ' . Utf8::strtoupper($GLOBALS['TL_LANG']['FMD']['eventreader'][0]) . ' (AJAX) ###';
$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 parent::generate();
}
/**
* Generate the module
*/
protected function compile()
{
// Haste AJAX reload
ReloadHelper::subscribe(
ReloadHelper::getUniqid(ReloadHelper::TYPE_MODULE, $this->id),
['reloadEventReader-'.$this->id]
);
// Form stuff
$this->Template->action = StringUtil::ampersand(Environment::get('request'), true);
$this->Template->formSubmit = 'eventReader'.$this->id;
// Make sure this module has an ID
$arrCssID = (array)$this->cssID;
$arrCssID[0] = $arrCssID[0] ?: 'eventReader'.$this->id;
$this->cssID = $arrCssID;
$this->handleSubmission();
// Add scripts
$GLOBALS['TL_JAVASCRIPT']['eventReader'] = 'web/bundles/rhymewmassartshub/assets/js/frontend/event/reader.js|static';
$GLOBALS['TL_BODY']['eventReader'.$this->id] = "
<script>
jQuery(document).ready(function(){
ArtsHub.Event.Reader.create(jQuery('#".$arrCssID[0]."'), {
});
});
</script>";
/** @var PageModel $objPage */
global $objPage;
$this->Template->event = '';
$this->Template->referer = 'javascript:history.go(-1)';
$this->Template->back = $GLOBALS['TL_LANG']['MSC']['goBack'];
// Get the current event
$objEvent = Event::findPublishedByParentAndIdOrAlias(Input::get('events'), $this->cal_calendar);
// The event does not exist (see #33)
if ($objEvent === null)
{
throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
}
// Redirect if the event has a target URL (see #1498)
switch ($objEvent->source) {
case 'internal':
if ($page = PageModel::findPublishedById($objEvent->jumpTo))
{
throw new RedirectResponseException($page->getAbsoluteUrl(), 301);
}
throw new InternalServerErrorException('Invalid "jumpTo" value or target page not public');
case 'article':
if (($article = ArticleModel::findByPk($objEvent->articleId)) && ($page = PageModel::findPublishedById($article->pid)))
{
throw new RedirectResponseException($page->getAbsoluteUrl('/articles/' . ($article->alias ?: $article->id)), 301);
}
throw new InternalServerErrorException('Invalid "articleId" value or target page not public');
case 'external':
if ($objEvent->url)
{
throw new RedirectResponseException($objEvent->url, 301);
}
throw new InternalServerErrorException('Empty target URL');
}
// 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);
if ($objEvent->pageTitle)
{
$htmlHeadBag->setTitle($objEvent->pageTitle); // Already stored decoded
}
elseif ($objEvent->title)
{
$htmlHeadBag->setTitle(StringUtil::inputEncodedToPlainText($objEvent->title));
}
if ($objEvent->description)
{
$htmlHeadBag->setMetaDescription(StringUtil::inputEncodedToPlainText($objEvent->description));
}
elseif ($objEvent->teaser)
{
$htmlHeadBag->setMetaDescription(StringUtil::htmlToPlainText($objEvent->teaser));
}
if ($objEvent->robots)
{
$htmlHeadBag->setMetaRobots($objEvent->robots);
}
}
$intStartTime = $objEvent->startTime;
$intEndTime = $objEvent->endTime;
$span = Calendar::calculateSpan($intStartTime, $intEndTime);
// Do not show dates in the past if the event is recurring (see #923)
if ($objEvent->recurring)
{
$arrRange = StringUtil::deserialize($objEvent->repeatEach);
if (isset($arrRange['unit'], $arrRange['value']))
{
while (($this->cal_hideRunning ? $intStartTime : $intEndTime) < time() && $intEndTime < $objEvent->repeatEnd)
{
$intStartTime = strtotime('+' . $arrRange['value'] . ' ' . $arrRange['unit'], $intStartTime);
$intEndTime = strtotime('+' . $arrRange['value'] . ' ' . $arrRange['unit'], $intEndTime);
}
}
}
// Mark past and upcoming events (see #187)
if ($intEndTime < strtotime('00:00:00'))
{
$objEvent->cssClass .= ' bygone';
}
elseif ($intStartTime > strtotime('23:59:59'))
{
$objEvent->cssClass .= ' upcoming';
}
else
{
$objEvent->cssClass .= ' current';
}
list($strDate, $strTime) = $this->getDateAndTime($objEvent, $objPage, $intStartTime, $intEndTime, $span);
$until = '';
$recurring = '';
$arrRange = array();
// Recurring event
if ($objEvent->recurring)
{
$arrRange = StringUtil::deserialize($objEvent->repeatEach);
if (isset($arrRange['unit'], $arrRange['value']))
{
if ($arrRange['value'] == 1)
{
$repeat = $GLOBALS['TL_LANG']['MSC']['cal_single_' . $arrRange['unit']];
}
else
{
$repeat = sprintf($GLOBALS['TL_LANG']['MSC']['cal_multiple_' . $arrRange['unit']], $arrRange['value']);
}
if ($objEvent->recurrences > 0)
{
$until = ' ' . sprintf($GLOBALS['TL_LANG']['MSC']['cal_until'], Date::parse($objPage->dateFormat, $objEvent->repeatEnd));
}
if ($objEvent->recurrences > 0 && $intEndTime <= time())
{
$recurring = sprintf($GLOBALS['TL_LANG']['MSC']['cal_repeat_ended'], $repeat, $until);
}
elseif ($objEvent->addTime)
{
$recurring = sprintf($GLOBALS['TL_LANG']['MSC']['cal_repeat'], $repeat, $until, date('Y-m-d\TH:i:sP', $intStartTime), $strDate . ($strTime ? ' ' . $strTime : ''));
}
else
{
$recurring = sprintf($GLOBALS['TL_LANG']['MSC']['cal_repeat'], $repeat, $until, date('Y-m-d', $intStartTime), $strDate);
}
}
}
$this->Template->event = $objEvent->generate($this->getModel());
// Tag the event (see #2137)
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
$responseTagger->addTags(array('contao.db.tl_calendar_events.' . $objEvent->id));
}
$bundles = System::getContainer()->getParameter('kernel.bundles');
// HOOK: comments extension required
if ($objEvent->noComments || !isset($bundles['ContaoCommentsBundle']))
{
$this->Template->allowComments = false;
return;
}
/** @var CalendarModel $objCalendar */
$objCalendar = $objEvent->getRelated('pid');
$this->Template->allowComments = $objCalendar->allowComments;
// Comments are not allowed
if (!$objCalendar->allowComments)
{
return;
}
// Adjust the comments headline level
$intHl = min((int) str_replace('h', '', $this->hl), 5);
$this->Template->hlc = 'h' . ($intHl + 1);
$this->import(Comments::class, 'Comments');
$arrNotifies = array();
// Notify the system administrator
if ($objCalendar->notify != 'notify_author')
{
$arrNotifies[] = $GLOBALS['TL_ADMIN_EMAIL'];
}
/** @var UserModel $objAuthor */
if ($objCalendar->notify != 'notify_admin' && ($objAuthor = $objEvent->getRelated('author')) instanceof UserModel && $objAuthor->email)
{
$arrNotifies[] = $objAuthor->email;
}
$objConfig = new \stdClass();
$objConfig->perPage = $objCalendar->perPage;
$objConfig->order = $objCalendar->sortOrder;
$objConfig->template = $this->com_template;
$objConfig->requireLogin = $objCalendar->requireLogin;
$objConfig->disableCaptcha = $objCalendar->disableCaptcha;
$objConfig->bbcode = $objCalendar->bbcode;
$objConfig->moderate = $objCalendar->moderate;
$this->Comments->addCommentsToTemplate($this->Template, $objConfig, 'tl_calendar_events', $objEvent->id, $arrNotifies);
}
protected function handleSubmission()
{
}
/**
* Return the date and time strings
*
* @param CalendarEventsModel $objEvent
* @param PageModel $objPage
* @param integer $intStartTime
* @param integer $intEndTime
* @param integer $span
*
* @return array
*/
private function getDateAndTime(CalendarEventsModel $objEvent, PageModel $objPage, $intStartTime, $intEndTime, $span)
{
$strDate = Date::parse($objPage->dateFormat, $intStartTime);
if ($span > 0)
{
$strDate = Date::parse($objPage->dateFormat, $intStartTime) . $GLOBALS['TL_LANG']['MSC']['cal_timeSeparator'] . Date::parse($objPage->dateFormat, $intEndTime);
}
$strTime = '';
if ($objEvent->addTime)
{
if ($span > 0)
{
$strDate = Date::parse($objPage->datimFormat, $intStartTime) . $GLOBALS['TL_LANG']['MSC']['cal_timeSeparator'] . Date::parse($objPage->datimFormat, $intEndTime);
}
elseif ($intStartTime == $intEndTime)
{
$strTime = Date::parse($objPage->timeFormat, $intStartTime);
}
else
{
$strTime = Date::parse($objPage->timeFormat, $intStartTime) . $GLOBALS['TL_LANG']['MSC']['cal_timeSeparator'] . Date::parse($objPage->timeFormat, $intEndTime);
}
}
return array($strDate, $strTime);
}
}