src/Rhyme/WMassArtsHub/Model/Event.php line 127

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\Model;
  9. use Contao\Calendar;
  10. use Contao\ContentModel;
  11. use Contao\Controller;
  12. use Contao\Date;
  13. use Contao\Environment;
  14. use Contao\FilesModel;
  15. use Contao\FrontendTemplate;
  16. use Contao\Model\Collection;
  17. use Contao\CalendarEventsModel;
  18. use Contao\ModuleModel;
  19. use Contao\PageModel;
  20. use Contao\StringUtil;
  21. use Contao\System;
  22. use Rhyme\WMassArtsHub\Helper\EventHelper;
  23. use Rhyme\WMassArtsHub\Helper\ImageToTemplateHelper;
  24. use Rhyme\WMassArtsHub\Model\CreativeGround\Profile;
  25. use Rhyme\WMassArtsHub\Model\Profile\Item as ProfileItemModel;
  26. /**
  27.  * Reads and writes calendar events - extends base class
  28.  *
  29.  * @method static string getAliasKey()
  30.  *
  31.  */
  32. /**
  33.  * Class Event
  34.  * @package Rhyme\WMassArtsHub\Model
  35.  */
  36. class Event extends CalendarEventsModel
  37. {
  38.     protected static $placeholderImagePath 'files/assets/img/placeholders/event-placeholder.jpg';
  39.     /**
  40.      * Get the alias key
  41.      * @return string
  42.      */
  43.     public static function getAliasKey()
  44.     {
  45.         return 'events';
  46.     }
  47.     /**
  48.      * Return the average ranking of the record
  49.      * We can then customize this based on any number of criteria
  50.      *
  51.      * @return int A ranking of 1-5
  52.      */
  53.     public function getRank()
  54.     {
  55.         if($this->featured) {
  56.             return 5;
  57.         }
  58.         return $this->rating;
  59.     }
  60.     /**
  61.      * Return the sorting field for A-Z sorting
  62.      *
  63.      * @return string
  64.      */
  65.     public function getSortingValue()
  66.     {
  67.         return $this->title;
  68.     }
  69.     /**
  70.      * Return the date for this item
  71.      * We can then customize this based on any number of criteria
  72.      *
  73.      * @return int A timestamp for the date of this item
  74.      */
  75.     public function getDate()
  76.     {
  77.         return $this->startDate;
  78.     }
  79.     /**
  80.      * The class type for this element
  81.      *
  82.      * @return string
  83.      */
  84.     public function getClass()
  85.     {
  86.         return 'artshub-event';
  87.     }
  88.     /**
  89.      * Get the page title
  90.      * @return string
  91.      */
  92.     public function getPageTitle()
  93.     {
  94.         return $this->title;
  95.     }
  96.     /**
  97.      * Get the frontend URL for the modal popup
  98.      * @param ModuleModel $objModule
  99.      * @return string
  100.      */
  101.     public function getFrontendUrlFromModule(ModuleModel $objModule) {
  102.         return EventHelper::generateEventUrlFromModule($this$objModule);
  103.     }
  104.     /**
  105.      * Generate the item
  106.      *
  107.      * @param ModuleModel $objModule
  108.      * @return string the parsed item
  109.      * @throws \Exception
  110.      */
  111.     public function generate$objModule )
  112.     {
  113.         /** @var PageModel $objPage */
  114.         global $objPage;
  115.         $strTemplate $objModule->cal_template;
  116.         $objTemplate = new FrontendTemplate($strTemplate);
  117.         $event $this->row();
  118.         $objTemplate->setData($event);
  119.         $rootDir System::getContainer()->getParameter('kernel.project_dir');
  120.         EventHelper::addImageMethodsToTemplate($this$objModule$objTemplate);
  121.         //Get address and location from business if none given
  122.         $objTemplate->location $this->getLocation();
  123.         $objTemplate->address $this->getAddress();
  124.         //Add date related items
  125.         $intStart $this->startTime;
  126.         $intEnd $this->endTime;
  127.         $intDate $intStart;
  128.         $intKey date('Ymd'$intStart);
  129.         $strDate Date::parse($objPage->dateFormat$intStart);
  130.         $strDay $GLOBALS['TL_LANG']['DAYS'][date('w'$intStart)];
  131.         $strMonth $GLOBALS['TL_LANG']['MONTHS'][(date('n'$intStart)-1)];
  132.         // Clean the RTE output
  133.         if ($event['teaser'] != '')
  134.         {
  135.             $objTemplate->hasTeaser true;
  136.             $objTemplate->teaser StringUtil::toHtml5($objTemplate->teaser);
  137.             $objTemplate->teaser StringUtil::encodeEmail($objTemplate->teaser);
  138.         }
  139.         $objTemplate->event $this;
  140.         $objTemplate->customDate $this->getFormattedDate();
  141.         $objTemplate->date $strDate;
  142.         $objTemplate->time $this->getFormattedTime();
  143.         $objTemplate->datetime $this->addTime date('Y-m-d\TH:i:sP'$intStart) : date('Y-m-d'$intStart);
  144.         $objTemplate->day $strDay;
  145.         $objTemplate->month $strMonth;
  146.         $objTemplate->parent $this->pid;
  147.         $objTemplate->calendar $this->getRelated('pid');
  148.         $objTemplate->link EventHelper::generateEventUrlFromModule($this$objModule);
  149.         $objTemplate->link_absolute EventHelper::generateEventUrlFromModule($this$objModuletrue);
  150.         $objTemplate->short_teaser strlen($this->teaser) ? '<p>' StringUtil::substr(strip_tags($this->teaser), 100) . '</p>' '';
  151.         // Display the "read more" button for external/article links
  152.         if ($this->source != 'default' && $this->source != '')
  153.         {
  154.             $objTemplate->details true;
  155.             $objTemplate->hasDetails true;
  156.         }
  157.         // Compile the event text
  158.         else
  159.         {
  160.             $id $this->id;
  161.             $objTemplate->details = function () use ($id)
  162.             {
  163.                 $strDetails '';
  164.                 $objElement ContentModel::findPublishedByPidAndTable($id'tl_calendar_events');
  165.                 if ($objElement !== null)
  166.                 {
  167.                     while ($objElement->next())
  168.                     {
  169.                         $strDetails .= Controller::getContentElement($objElement->current());
  170.                     }
  171.                 }
  172.                 return $strDetails;
  173.             };
  174.             $objTemplate->hasDetails = function () use ($id)
  175.             {
  176.                 return ContentModel::countPublishedByPidAndTable($id'tl_calendar_events') > 0;
  177.             };
  178.         }
  179.         return $objTemplate->parse();
  180.     }
  181.     /**
  182.      * Get the formatted date string
  183.      * @return string
  184.      */
  185.     public function getFormattedDate() {
  186.         if ($this->recurring)
  187.         {
  188.             $arrRange StringUtil::deserialize($this->repeatEach);
  189.             if (\is_array($arrRange) && isset($arrRange['unit']) && isset($arrRange['value']))
  190.             {
  191.                 // Todo: build out more cases
  192.                 if ($arrRange['value'] == && $arrRange['unit'] == 'weeks')
  193.                 {
  194.                     return 'Every '.Date::parse('l'$this->startTime).($this->recurrences && $this->repeatEnd ' Until '.Date::parse('F jS'$this->repeatEnd) : '');
  195.                 }
  196.             }
  197.         }
  198.         $intStart $this->startTime;
  199.         $intEnd $this->endTime;
  200.         $span Calendar::calculateSpan($intStart$intEnd);
  201.         //Custom Date Formatting
  202.         $customDayNameStart Date::parse('l'$intStart);
  203.         $customDayNameEnd Date::parse('l'$intEnd);
  204.         $customMonthNameStart Date::parse('F'$intStart);
  205.         $customMonthNameEnd Date::parse('F'$intEnd);
  206.         $blnMonthSame $customMonthNameStart == $customMonthNameEnd;
  207.         $customDateStart Date::parse('jS'$intStart);
  208.         $customDateEnd Date::parse('jS'$intEnd);
  209.         $customDate $customDayNameStart ',  ' $customMonthNameStart ' ' $customDateStart;
  210.         if($span 0) {
  211.             $customDate $customDayNameStart '-' $customDayNameEnd ', ';
  212.             if(!$blnMonthSame) {
  213.                 $customDate .= $customMonthNameStart ' ' $customDateStart '-' $customMonthNameEnd ' ' $customDateEnd;
  214.             } else {
  215.                 $customDate .= $customMonthNameStart ' ' $customDateStart '-' $customDateEnd;
  216.             }
  217.         }
  218.         return $customDate;
  219.     }
  220.     /**
  221.      * Get the formatted time string
  222.      * @return string
  223.      */
  224.     public function getFormattedTime() {
  225.         /** @var PageModel $objPage */
  226.         global $objPage;
  227.         $strTime '';
  228.         $span Calendar::calculateSpan($this->startTime$this->endTime);
  229.         if ($this->addTime)
  230.         {
  231.             if ($span 0)
  232.             {
  233.                 $strTime Date::parse($objPage->datimFormat$this->startTime) . $GLOBALS['TL_LANG']['MSC']['cal_timeSeparator'] . Date::parse($objPage->datimFormat$this->endTime);
  234.             }
  235.             elseif ($this->startTime == $this->endTime)
  236.             {
  237.                 $strTime Date::parse($objPage->timeFormat$this->startTime);
  238.             }
  239.             else
  240.             {
  241.                 $strTime Date::parse($objPage->timeFormat$this->startTime) . $GLOBALS['TL_LANG']['MSC']['cal_timeSeparator'] . Date::parse($objPage->timeFormat$this->endTime);
  242.             }
  243.         }
  244.         return $strTime;
  245.     }
  246.     /*
  247.      * Get the address string
  248.      */
  249.     public function getAddress()
  250.     {
  251.         // If the address is just the concatenated version of null values, use the first business
  252.         if ($this->address == '')
  253.         {
  254.             // Just get the first one selected in the back end for now
  255.             $arrSelected StringUtil::deserialize($this->profilestrue);
  256.             $objBusiness ProfileItemModel::findByPk($arrSelected[0]);
  257.             return $objBusiness !== null ? ($objBusiness->street ', ' $objBusiness->city ' ' $objBusiness->state ' ' $objBusiness->postal) : 'Western Massachusetts';
  258.         }
  259.         return $this->address;
  260.     }
  261.     /*
  262.      * Get the location string
  263.      */
  264.     public function getLocation()
  265.     {
  266.         // If the location is just the concatenated version of null values, use the first business
  267.         if ($this->location == '')
  268.         {
  269.             // Just get the first one selected in the back end for now
  270.             $arrSelected StringUtil::deserialize($this->profilestrue);
  271.             $objBusiness ProfileItemModel::findByPk($arrSelected[0]);
  272.             return $objBusiness !== null $objBusiness->name '';
  273.         }
  274.         return $this->location;
  275.     }
  276.     /**
  277.      * Find an event post by its ID or alias
  278.      *
  279.      * @param mixed $varId      The numeric ID or alias name
  280.      * @param array $arrOptions An optional options array
  281.      *
  282.      * @return CalendarEventsModel|null The model or null if there is no event
  283.      */
  284.     public static function findPublishedByIdOrAlias($varId, array $arrOptions=array())
  285.     {
  286.         $t = static::$strTable;
  287.         $arrColumns = !is_numeric($varId) ? array("$t.alias=?") : array("$t.id=?");
  288.         if (!static::isPreviewMode($arrOptions))
  289.         {
  290.             $time Date::floorToMinute();
  291.             $arrColumns[] = "($t.start='' OR $t.start<='$time') AND ($t.stop='' OR $t.stop>'" . ($time 60) . "') AND $t.published='1'";
  292.         }
  293.         return static::findOneBy($arrColumns$varId$arrOptions);
  294.     }
  295.     /**
  296.      * Find upcoming events by their Business IDs
  297.      *
  298.      * @param array   $arrIds     An array of business IDs
  299.      * @param integer $intLimit   An optional limit
  300.      * @param array   $arrOptions An optional options array
  301.      *
  302.      * @return Collection|CalendarEventsModel[]|CalendarEventsModel|null A collection of models or null if there are no events
  303.      */
  304.     public static function findUpcomingByProfileIds($arrIds$intLimit=0, array $arrOptions=array())
  305.     {
  306.         if (empty($arrIds) || !\is_array($arrIds))
  307.         {
  308.             return null;
  309.         }
  310.         $t = static::$strTable;
  311.         $time Date::floorToMinute();
  312.         $strWhereBiz "";
  313.         foreach (array_map('\intval'$arrIds) as $bizId)
  314.         {
  315.             $strWhereBiz .= (strlen($strWhereBiz) ? " OR " "") .  "$t.profiles LIKE '%\"".$bizId."\"%'";
  316.         }
  317.         $strWhereBiz strlen($strWhereBiz) ? " AND (" $strWhereBiz ")" "";
  318.         // Get upcoming events using endTime instead of startTime (see #3917)
  319.         $arrColumns = array("($t.endTime>=$time OR ($t.recurring='1' AND ($t.recurrences=0 OR $t.repeatEnd>=$time))) $strWhereBiz AND ($t.start='' OR $t.start<='$time') AND ($t.stop='' OR $t.stop>'" . ($time 60) . "') AND $t.published='1'");
  320.         if ($intLimit 0)
  321.         {
  322.             $arrOptions['limit'] = $intLimit;
  323.         }
  324.         if (!isset($arrOptions['order']))
  325.         {
  326.             $arrOptions['order'] = "$t.startTime";
  327.         }
  328.         return static::findBy($arrColumnsnull$arrOptions);
  329.     }
  330. }