src/Rhyme/WMassArtsHub/Module/Event/AjaxReader.php line 253

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\Module\Event;
  9. use Contao\ArticleModel;
  10. use Contao\Calendar;
  11. use Contao\CalendarEventsModel;
  12. use Contao\CalendarModel;
  13. use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
  14. use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
  15. use Contao\Date;
  16. use Contao\Input;
  17. use Contao\PageModel;
  18. use Contao\StringUtil;
  19. use Contao\Environment;
  20. use Contao\BackendTemplate;
  21. use Contao\ModuleEventReader;
  22. use Contao\CoreBundle\Exception\InternalServerErrorException;
  23. use Contao\CoreBundle\Exception\PageNotFoundException;
  24. use Contao\CoreBundle\Exception\RedirectResponseException;
  25. use Contao\System;
  26. use Contao\Template;
  27. use Contao\UserModel;
  28. use FOS\HttpCache\ResponseTagger;
  29. use Patchwork\Utf8;
  30. use Haste\Ajax\ReloadHelper;
  31. use Isotope\Isotope;
  32. use Isotope\Model\Product;
  33. use Isotope\Model\Product\AbstractProduct;
  34. use Rhyme\WMassArtsHub\Model\Event;
  35. /**
  36.  * Class Reader
  37.  * @package Rhyme\WMassArtsHub\Module\Event
  38.  */
  39. class AjaxReader extends ModuleEventReader
  40. {
  41.     /**
  42.      * Display a wildcard in the back end
  43.      *
  44.      * @throws InternalServerErrorException
  45.      *
  46.      * @return string
  47.      */
  48.     public function generate()
  49.     {
  50.         if (TL_MODE == 'BE')
  51.         {
  52.             $objTemplate = new BackendTemplate('be_wildcard');
  53.             $objTemplate->wildcard '### ' Utf8::strtoupper($GLOBALS['TL_LANG']['FMD']['eventreader'][0]) . ' (AJAX) ###';
  54.             $objTemplate->title $this->headline;
  55.             $objTemplate->id $this->id;
  56.             $objTemplate->link $this->name;
  57.             $objTemplate->href 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' $this->id;
  58.             return $objTemplate->parse();
  59.         }
  60.         return parent::generate();
  61.     }
  62.     /**
  63.      * Generate the module
  64.      */
  65.     protected function compile()
  66.     {
  67.         // Haste AJAX reload
  68.         ReloadHelper::subscribe(
  69.             ReloadHelper::getUniqid(ReloadHelper::TYPE_MODULE$this->id),
  70.             ['reloadEventReader-'.$this->id]
  71.         );
  72.         // Form stuff
  73.         $this->Template->action StringUtil::ampersand(Environment::get('request'), true);
  74.         $this->Template->formSubmit 'eventReader'.$this->id;
  75.         // Make sure this module has an ID
  76.         $arrCssID = (array)$this->cssID;
  77.         $arrCssID[0] = $arrCssID[0] ?: 'eventReader'.$this->id;
  78.         $this->cssID $arrCssID;
  79.         $this->handleSubmission();
  80.         // Add scripts
  81.         $GLOBALS['TL_JAVASCRIPT']['eventReader'] = 'web/bundles/rhymewmassartshub/assets/js/frontend/event/reader.js|static';
  82.         $GLOBALS['TL_BODY']['eventReader'.$this->id] = "
  83.             <script>
  84.                 jQuery(document).ready(function(){
  85.                     ArtsHub.Event.Reader.create(jQuery('#".$arrCssID[0]."'), {
  86.                     });
  87.                 });
  88.             </script>";
  89.         /** @var PageModel $objPage */
  90.         global $objPage;
  91.         $this->Template->event '';
  92.         $this->Template->referer 'javascript:history.go(-1)';
  93.         $this->Template->back $GLOBALS['TL_LANG']['MSC']['goBack'];
  94.         // Get the current event
  95.         $objEvent Event::findPublishedByParentAndIdOrAlias(Input::get('events'), $this->cal_calendar);
  96.         // The event does not exist (see #33)
  97.         if ($objEvent === null)
  98.         {
  99.             throw new PageNotFoundException('Page not found: ' Environment::get('uri'));
  100.         }
  101.         // Redirect if the event has a target URL (see #1498)
  102.         switch ($objEvent->source) {
  103.             case 'internal':
  104.                 if ($page PageModel::findPublishedById($objEvent->jumpTo))
  105.                 {
  106.                     throw new RedirectResponseException($page->getAbsoluteUrl(), 301);
  107.                 }
  108.                 throw new InternalServerErrorException('Invalid "jumpTo" value or target page not public');
  109.             case 'article':
  110.                 if (($article ArticleModel::findByPk($objEvent->articleId)) && ($page PageModel::findPublishedById($article->pid)))
  111.                 {
  112.                     throw new RedirectResponseException($page->getAbsoluteUrl('/articles/' . ($article->alias ?: $article->id)), 301);
  113.                 }
  114.                 throw new InternalServerErrorException('Invalid "articleId" value or target page not public');
  115.             case 'external':
  116.                 if ($objEvent->url)
  117.                 {
  118.                     throw new RedirectResponseException($objEvent->url301);
  119.                 }
  120.                 throw new InternalServerErrorException('Empty target URL');
  121.         }
  122.         // Overwrite the page meta data (see #2853, #4955 and #87)
  123.         $responseContext System::getContainer()->get(ResponseContextAccessor::class)->getResponseContext();
  124.         if ($responseContext && $responseContext->has(HtmlHeadBag::class))
  125.         {
  126.             /** @var HtmlHeadBag $htmlHeadBag */
  127.             $htmlHeadBag $responseContext->get(HtmlHeadBag::class);
  128.             if ($objEvent->pageTitle)
  129.             {
  130.                 $htmlHeadBag->setTitle($objEvent->pageTitle); // Already stored decoded
  131.             }
  132.             elseif ($objEvent->title)
  133.             {
  134.                 $htmlHeadBag->setTitle(StringUtil::inputEncodedToPlainText($objEvent->title));
  135.             }
  136.             if ($objEvent->description)
  137.             {
  138.                 $htmlHeadBag->setMetaDescription(StringUtil::inputEncodedToPlainText($objEvent->description));
  139.             }
  140.             elseif ($objEvent->teaser)
  141.             {
  142.                 $htmlHeadBag->setMetaDescription(StringUtil::htmlToPlainText($objEvent->teaser));
  143.             }
  144.             if ($objEvent->robots)
  145.             {
  146.                 $htmlHeadBag->setMetaRobots($objEvent->robots);
  147.             }
  148.         }
  149.         $intStartTime $objEvent->startTime;
  150.         $intEndTime $objEvent->endTime;
  151.         $span Calendar::calculateSpan($intStartTime$intEndTime);
  152.         // Do not show dates in the past if the event is recurring (see #923)
  153.         if ($objEvent->recurring)
  154.         {
  155.             $arrRange StringUtil::deserialize($objEvent->repeatEach);
  156.             if (isset($arrRange['unit'], $arrRange['value']))
  157.             {
  158.                 while (($this->cal_hideRunning $intStartTime $intEndTime) < time() && $intEndTime $objEvent->repeatEnd)
  159.                 {
  160.                     $intStartTime strtotime('+' $arrRange['value'] . ' ' $arrRange['unit'], $intStartTime);
  161.                     $intEndTime strtotime('+' $arrRange['value'] . ' ' $arrRange['unit'], $intEndTime);
  162.                 }
  163.             }
  164.         }
  165.         // Mark past and upcoming events (see #187)
  166.         if ($intEndTime strtotime('00:00:00'))
  167.         {
  168.             $objEvent->cssClass .= ' bygone';
  169.         }
  170.         elseif ($intStartTime strtotime('23:59:59'))
  171.         {
  172.             $objEvent->cssClass .= ' upcoming';
  173.         }
  174.         else
  175.         {
  176.             $objEvent->cssClass .= ' current';
  177.         }
  178.         list($strDate$strTime) = $this->getDateAndTime($objEvent$objPage$intStartTime$intEndTime$span);
  179.         $until '';
  180.         $recurring '';
  181.         $arrRange = array();
  182.         // Recurring event
  183.         if ($objEvent->recurring)
  184.         {
  185.             $arrRange StringUtil::deserialize($objEvent->repeatEach);
  186.             if (isset($arrRange['unit'], $arrRange['value']))
  187.             {
  188.                 if ($arrRange['value'] == 1)
  189.                 {
  190.                     $repeat $GLOBALS['TL_LANG']['MSC']['cal_single_' $arrRange['unit']];
  191.                 }
  192.                 else
  193.                 {
  194.                     $repeat sprintf($GLOBALS['TL_LANG']['MSC']['cal_multiple_' $arrRange['unit']], $arrRange['value']);
  195.                 }
  196.                 if ($objEvent->recurrences 0)
  197.                 {
  198.                     $until ' ' sprintf($GLOBALS['TL_LANG']['MSC']['cal_until'], Date::parse($objPage->dateFormat$objEvent->repeatEnd));
  199.                 }
  200.                 if ($objEvent->recurrences && $intEndTime <= time())
  201.                 {
  202.                     $recurring sprintf($GLOBALS['TL_LANG']['MSC']['cal_repeat_ended'], $repeat$until);
  203.                 }
  204.                 elseif ($objEvent->addTime)
  205.                 {
  206.                     $recurring sprintf($GLOBALS['TL_LANG']['MSC']['cal_repeat'], $repeat$untildate('Y-m-d\TH:i:sP'$intStartTime), $strDate . ($strTime ' ' $strTime ''));
  207.                 }
  208.                 else
  209.                 {
  210.                     $recurring sprintf($GLOBALS['TL_LANG']['MSC']['cal_repeat'], $repeat$untildate('Y-m-d'$intStartTime), $strDate);
  211.                 }
  212.             }
  213.         }
  214.         $this->Template->event $objEvent->generate($this->getModel());
  215.         // Tag the event (see #2137)
  216.         if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
  217.         {
  218.             $responseTagger System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
  219.             $responseTagger->addTags(array('contao.db.tl_calendar_events.' $objEvent->id));
  220.         }
  221.         $bundles System::getContainer()->getParameter('kernel.bundles');
  222.         // HOOK: comments extension required
  223.         if ($objEvent->noComments || !isset($bundles['ContaoCommentsBundle']))
  224.         {
  225.             $this->Template->allowComments false;
  226.             return;
  227.         }
  228.         /** @var CalendarModel $objCalendar */
  229.         $objCalendar $objEvent->getRelated('pid');
  230.         $this->Template->allowComments $objCalendar->allowComments;
  231.         // Comments are not allowed
  232.         if (!$objCalendar->allowComments)
  233.         {
  234.             return;
  235.         }
  236.         // Adjust the comments headline level
  237.         $intHl min((int) str_replace('h'''$this->hl), 5);
  238.         $this->Template->hlc 'h' . ($intHl 1);
  239.         $this->import(Comments::class, 'Comments');
  240.         $arrNotifies = array();
  241.         // Notify the system administrator
  242.         if ($objCalendar->notify != 'notify_author')
  243.         {
  244.             $arrNotifies[] = $GLOBALS['TL_ADMIN_EMAIL'];
  245.         }
  246.         /** @var UserModel $objAuthor */
  247.         if ($objCalendar->notify != 'notify_admin' && ($objAuthor $objEvent->getRelated('author')) instanceof UserModel && $objAuthor->email)
  248.         {
  249.             $arrNotifies[] = $objAuthor->email;
  250.         }
  251.         $objConfig = new \stdClass();
  252.         $objConfig->perPage $objCalendar->perPage;
  253.         $objConfig->order $objCalendar->sortOrder;
  254.         $objConfig->template $this->com_template;
  255.         $objConfig->requireLogin $objCalendar->requireLogin;
  256.         $objConfig->disableCaptcha $objCalendar->disableCaptcha;
  257.         $objConfig->bbcode $objCalendar->bbcode;
  258.         $objConfig->moderate $objCalendar->moderate;
  259.         $this->Comments->addCommentsToTemplate($this->Template$objConfig'tl_calendar_events'$objEvent->id$arrNotifies);
  260.     }
  261.     protected function handleSubmission()
  262.     {
  263.     }
  264.     /**
  265.      * Return the date and time strings
  266.      *
  267.      * @param CalendarEventsModel $objEvent
  268.      * @param PageModel           $objPage
  269.      * @param integer             $intStartTime
  270.      * @param integer             $intEndTime
  271.      * @param integer             $span
  272.      *
  273.      * @return array
  274.      */
  275.     private function getDateAndTime(CalendarEventsModel $objEventPageModel $objPage$intStartTime$intEndTime$span)
  276.     {
  277.         $strDate Date::parse($objPage->dateFormat$intStartTime);
  278.         if ($span 0)
  279.         {
  280.             $strDate Date::parse($objPage->dateFormat$intStartTime) . $GLOBALS['TL_LANG']['MSC']['cal_timeSeparator'] . Date::parse($objPage->dateFormat$intEndTime);
  281.         }
  282.         $strTime '';
  283.         if ($objEvent->addTime)
  284.         {
  285.             if ($span 0)
  286.             {
  287.                 $strDate Date::parse($objPage->datimFormat$intStartTime) . $GLOBALS['TL_LANG']['MSC']['cal_timeSeparator'] . Date::parse($objPage->datimFormat$intEndTime);
  288.             }
  289.             elseif ($intStartTime == $intEndTime)
  290.             {
  291.                 $strTime Date::parse($objPage->timeFormat$intStartTime);
  292.             }
  293.             else
  294.             {
  295.                 $strTime Date::parse($objPage->timeFormat$intStartTime) . $GLOBALS['TL_LANG']['MSC']['cal_timeSeparator'] . Date::parse($objPage->timeFormat$intEndTime);
  296.             }
  297.         }
  298.         return array($strDate$strTime);
  299.     }
  300. }