Source of file Calendar.php
Size: 7,070 Bytes - Last Modified: 2021-12-23T10:04:07+00:00
/var/www/docs.ssmods.com/process/src/code/service/Calendar.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 | <?php /** * Generator for (monthly) calendars * * Class Calendar */ class Calendar { /** * Constants response keys */ const RESPONSE_DATE = 'Date'; const RESPONSE_EVENTS = 'Events'; const RESPONSE_MONTH = 'Month'; const RESPONSE_MONTH_TEXT = 'Text'; const RESPONSE_MONTH_NUMERIC = 'Numeric'; const RESPONSE_YEAR = 'Year'; const RESPONSE_DAYS = 'Days'; const RESPONSE_NEXT_MONTH = 'NextMonth'; const RESPONSE_PREVIOUS_MONTH = 'PreviousMonth'; /** * Start date for this calendar * * @var DateTimeHelper */ protected $start; /** * End date for this calendar * * @var DateTimeHelper */ protected $end; /** * The days in this calendar * * @var ArrayData */ protected $days; /** * Holder of (this month's) Agenda items * * @var DataList */ protected $agenda; /** * Holds the representation of an empty day * * @var ArrayData */ protected $emptyDay; /** * Counter for the number of events * * @var int */ protected $events = 0; /** * Create a fully functional calendar. Can immediately be used in templates. * * @param \DateTimeHelper $startDateTime * @param \DateTimeHelper $endDateTime * @param \DataList $agenda * * @return ArrayData */ public static function create(DateTimeHelper $startDateTime, DateTimeHelper $endDateTime, DataList $agenda) { $calendar = new static(); $calendar->setStart($startDateTime); $calendar->setEnd($endDateTime); $calendar->setAgenda($agenda); $calendar->generate(); $nextMonth = clone $startDateTime; $nextMonth->modify('last day of next month'); $prevMonth = clone $endDateTime; $prevMonth->modify('first day of previous month'); return ArrayData::create([ static::RESPONSE_MONTH => ArrayData::create([ static::RESPONSE_MONTH_TEXT => _t( sprintf( 'YearCalendar.Month.%1$s', $calendar->getStart() ->format('F') ), $calendar->getStart() ->format('F') ), static::RESPONSE_MONTH_NUMERIC => $calendar->getStart() ->format('n'), ]), static::RESPONSE_YEAR => $calendar->getStart() ->format('Y'), static::RESPONSE_DAYS => $calendar->days, static::RESPONSE_EVENTS => $calendar->events, static::RESPONSE_NEXT_MONTH => _t( sprintf( 'YearCalendar.Month.%1$s', $nextMonth->format('F') ), $nextMonth->format('F') ), static::RESPONSE_PREVIOUS_MONTH => _t( sprintf( 'YearCalendar.Month.%1$s', $prevMonth->format('F') ), $prevMonth->format('F') ), ]); } /** * Calendar constructor. * */ public function __construct() { $this->days = ArrayList::create(); } /** * Set the start date for this calendar * * @param \DateTimeHelper $startDateTime */ public function setStart(DateTimeHelper $startDateTime) { $this->start = $startDateTime; } /** * Retrieve the start date for this calendar * * @return \DateTimeHelper */ public function getStart() { return $this->start; } /** * Set the end date for this calendar * * @param \DateTimeHelper $endDateTime */ public function setEnd(DateTimeHelper $endDateTime) { $this->end = $endDateTime; } /** * Set the Agenda items for this calendar * * @param \DataList $agenda */ public function setAgenda(DataList $agenda) { $this->agenda = $agenda; } /** * Generate the calendar * */ public function generate() { $this->generateStartDuds(); $this->generateDays(); $this->generateEndDuds(); } /** * Generate the duds needed before the actual calendar starts * */ protected function generateStartDuds() { $firstDay = (int) $this->start->format('w'); for ($i = 0; $i < $firstDay; ++$i) { $this->days->push($this->getEmptyDay()); } } /** * Generate the actual calendar days * */ protected function generateDays() { /** @var DateTimeHelper $day */ $day = clone $this->start; do { $dayEntry = ArrayData::create([ static::RESPONSE_DATE => $day->format('d'), static::RESPONSE_EVENTS => $this->findEventsForDay($day), ]); $this->days->push($dayEntry); $day->modify('+1 day'); } while ($day <= $this->end); } /** * Generate the duds needed after the actual calendar ends * */ protected function generateEndDuds() { $lastDay = (int) $this->end->format('w'); for ($i = $lastDay + 1; $i < 7; ++$i) { $this->days->push($this->getEmptyDay()); } } /** * Find the Agenda items for a given day * * @param \DateTimeHelper $day * * @return static */ protected function findEventsForDay(DateTimeHelper $day) { $return = ArrayList::create(); /** @var Agenda $item */ foreach ($this->agenda as $item) { if (!$item->WholeDay) { if ($day->isDateEqualTo($item->FromDateTime())) { ++$this->events; $return->push($item); } } elseif ($day->isBetween($item->FromDateTime(), $item->ToDateTime())) { if ($day->isDateEqualTo($item->FromDateTime())) { $item->FirstDay = true; } if ($day->isDateEqualTo($item->ToDateTime())) { $item->LastDay = true; } if (($day->format('N') >= 6) && ($item->ExcludeWeekend)) { $item->NoWeekend = true; } ++$this->events; $return->push($item); } } return $return; } /** * Retrieve the empty day representation * * @return \ArrayData|static */ protected function getEmptyDay() { if (!$this->emptyDay) { $this->emptyDay = ArrayData::create([ static::RESPONSE_DATE => null, static::RESPONSE_EVENTS => ArrayList::create(), ]); } return $this->emptyDay; } } |