Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / view / phui / calendar / PHUICalendarWeekView.php
blob6237e5b569f01ee045d898ed231282179483ba28
1 <?php
3 final class PHUICalendarWeekView extends AphrontView {
4 private $events;
5 private $dateTime;
6 private $weekLength = 7;
7 private $view = 'day';
9 public function setEvents($events) {
10 $this->events = $events;
11 return $this;
14 public function setDateTime($date_time) {
15 $this->dateTime = $date_time;
16 return $this;
19 private function getDateTime() {
20 if ($this->dateTime) {
21 return $this->dateTime;
23 return $this->getDefaultDateTime();
26 public function setWeekLength($week_length) {
27 $this->weekLength = $week_length;
28 return $this;
31 public function setView($view) {
32 $this->view = $view;
33 return $this;
36 private function getView() {
37 return $this->view;
40 public function render() {
41 $this->events = msort($this->events, 'getEpochStart');
42 $week_of_boxes = $this->getWeekOfBoxes();
43 $filled_boxes = array();
45 foreach ($week_of_boxes as $day_box) {
46 $box_start = $day_box['start'];
47 $box_end = id(clone $box_start)->modify('+1 day');
49 $box_start = $box_start->format('U');
50 $box_end = $box_end->format('U');
52 $box_events = array();
54 foreach ($this->events as $event) {
55 $event_start = $event->getEpochStart();
56 $event_end = $event->getEpochEnd();
58 if ($event_start < $box_end && $event_end > $box_start) {
59 $box_events[] = $event;
63 $filled_boxes[] = $this->renderSidebarBox(
64 $box_events,
65 $day_box['title']);
68 return $filled_boxes;
71 private function renderSidebarBox($events, $title) {
72 $widget = id(new PHUICalendarWidgetView())
73 ->addClass('calendar-day-view-sidebar');
75 $list = id(new PHUICalendarListView())
76 ->setUser($this->getViewer())
77 ->setView($this->getView());
79 if (count($events) == 0) {
80 $list->showBlankState(true);
81 } else {
82 $sorted_events = msort($events, 'getEpochStart');
83 foreach ($sorted_events as $event) {
84 $list->addEvent($event);
88 $widget
89 ->setCalendarList($list)
90 ->setHeader($title);
91 return $widget;
94 private function getWeekOfBoxes() {
95 $day_boxes = array();
96 $week_length = $this->weekLength - 1;
98 $display_start_day = $this->getDateTime();
99 $display_end_day = id(clone $display_start_day)
100 ->modify('+'.$week_length.' day');
102 $box_start_time = clone $display_start_day;
104 $today_time = PhabricatorTime::getTodayMidnightDateTime($this->getViewer());
105 $tomorrow_time = clone $today_time;
106 $tomorrow_time->modify('+1 day');
108 while ($box_start_time <= $display_end_day) {
109 if ($box_start_time == $today_time) {
110 $title = pht('Today');
111 } else if ($box_start_time == $tomorrow_time) {
112 $title = pht('Tomorrow');
113 } else {
114 $title = $box_start_time->format('l');
117 $day_boxes[] = array(
118 'title' => $title,
119 'start' => clone $box_start_time,
122 $box_start_time->modify('+1 day');
124 return $day_boxes;
127 private function getDefaultDateTime() {
128 return PhabricatorTime::getTodayMidnightDateTime($this->getViewer());