@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactor `PHUICalendarMonthView` to be a little more readable

Summary: Refactor `PHUICalendarMonthView` to be a little more readable

Test Plan: Make sure month view still works

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin, epriestley

Differential Revision: https://secure.phabricator.com/D12831

+144 -168
-7
src/applications/people/controller/PhabricatorPeopleCalendarController.php
··· 35 35 $month = $request->getInt('month', $month_d); 36 36 $day = phabricator_format_local_time($now, $user, 'j'); 37 37 38 - 39 - $holidays = id(new PhabricatorCalendarHoliday())->loadAllWhere( 40 - 'day BETWEEN %s AND %s', 41 - "{$year}-{$month}-01", 42 - "{$year}-{$month}-31"); 43 - 44 38 $start_epoch = strtotime("{$year}-{$month}-01"); 45 39 $end_epoch = strtotime("{$year}-{$month}-01 next month"); 46 40 ··· 76 70 77 71 $month_view->setBrowseURI($request->getRequestURI()); 78 72 $month_view->setUser($user); 79 - $month_view->setHolidays($holidays); 80 73 $month_view->setImage($picture); 81 74 82 75 $phids = mpull($statuses, 'getUserPHID');
+144 -161
src/view/phui/calendar/PHUICalendarMonthView.php
··· 7 7 private $day; 8 8 private $month; 9 9 private $year; 10 - private $holidays = array(); 11 10 private $events = array(); 12 11 private $browseURI; 13 12 private $image; ··· 36 35 return $this; 37 36 } 38 37 39 - public function setHolidays(array $holidays) { 40 - assert_instances_of($holidays, 'PhabricatorCalendarHoliday'); 41 - $this->holidays = mpull($holidays, null, 'getDay'); 42 - return $this; 43 - } 44 - 45 38 public function __construct( 46 39 $range_start, 47 40 $range_end, ··· 71 64 $first = reset($days); 72 65 $empty = $first->format('w'); 73 66 74 - $markup = array(); 67 + $cell_lists = array(); 75 68 $empty_cell = array( 76 69 'list' => null, 77 70 'date' => null, ··· 81 74 ); 82 75 83 76 for ($ii = 0; $ii < $empty; $ii++) { 84 - $markup[] = $empty_cell; 77 + $cell_lists[] = $empty_cell; 85 78 } 86 - 87 - $show_events = array(); 88 79 89 80 foreach ($days as $day) { 90 81 $day_number = $day->format('j'); 91 82 92 - $holiday = idx($this->holidays, $day->format('Y-m-d')); 93 83 $class = 'phui-calendar-month-day'; 94 84 $weekday = $day->format('w'); 95 85 96 - if ($holiday || $weekday == 0 || $weekday == 6) { 97 - $class .= ' phui-calendar-not-work-day'; 98 - } 99 - 100 86 $day->setTime(0, 0, 0); 101 - $epoch_start = $day->format('U'); 102 - 103 - 104 - $epoch_end = id(clone $day)->modify('+1 day')->format('U'); 105 - 106 - if ($weekday == 0) { 107 - $show_events = array(); 108 - } else { 109 - $show_events = array_fill_keys( 110 - array_keys($show_events), 111 - phutil_tag_div( 112 - 'phui-calendar-event phui-calendar-event-empty', 113 - "\xC2\xA0")); // &nbsp; 114 - } 87 + $day_start_epoch = $day->format('U'); 88 + $day_end_epoch = id(clone $day)->modify('+1 day')->format('U'); 115 89 116 90 $list_events = array(); 117 91 $all_day_events = array(); 92 + 118 93 foreach ($events as $event) { 119 - if ($event->getEpochStart() >= $epoch_end) { 120 - // This list is sorted, so we can stop looking. 94 + if ($event->getEpochStart() >= $day_end_epoch) { 121 95 break; 122 96 } 123 - if ($event->getEpochStart() < $epoch_end && 124 - $event->getEpochEnd() > $epoch_start) { 97 + if ($event->getEpochStart() < $day_end_epoch && 98 + $event->getEpochEnd() > $day_start_epoch) { 125 99 if ($event->getIsAllDay()) { 126 100 $all_day_events[] = $event; 127 101 } else { ··· 144 118 $day->format('m').'/'. 145 119 $day->format('d').'/'; 146 120 147 - $markup[] = array( 121 + $cell_lists[] = array( 148 122 'list' => $list, 149 123 'date' => $day, 150 124 'uri' => $uri, ··· 153 127 ); 154 128 } 155 129 156 - $table = array(); 157 - $rows = array_chunk($markup, 7); 130 + $rows = array(); 131 + $cell_lists_by_week = array_chunk($cell_lists, 7); 158 132 159 - foreach ($rows as $row) { 133 + foreach ($cell_lists_by_week as $week_of_cell_lists) { 160 134 $cells = array(); 161 - $event_count_badge = null; 135 + while (count($week_of_cell_lists) < 7) { 136 + $week_of_cell_lists[] = $empty_cell; 137 + } 138 + foreach ($week_of_cell_lists as $cell_list) { 139 + $cells[] = $this->getEventListCell($cell_list); 140 + } 141 + $rows[] = phutil_tag('tr', array(), $cells); 162 142 163 - while (count($row) < 7) { 164 - $row[] = $empty_cell; 143 + $cells = array(); 144 + foreach ($week_of_cell_lists as $cell_list) { 145 + $cells[] = $this->getDayNumberCell($cell_list); 165 146 } 166 - foreach ($row as $cell) { 167 - $cell_list = $cell['list']; 168 - $class = $cell['class']; 169 - $uri = $cell['uri']; 170 - $count = $cell['count']; 147 + $rows[] = phutil_tag('tr', array(), $cells); 148 + } 149 + 150 + $header = $this->getDayNamesHeader(); 151 + 152 + $table = phutil_tag( 153 + 'table', 154 + array('class' => 'phui-calendar-view'), 155 + array( 156 + $header, 157 + $rows, 158 + )); 159 + 160 + $warnings = $this->getQueryRangeWarning(); 171 161 172 - $event_count = null; 173 - if ($count > 0) { 174 - $event_count = phutil_tag( 175 - 'div', 176 - array( 177 - 'class' => 'phui-calendar-month-count-badge', 178 - ), 179 - $count); 180 - } 162 + $box = id(new PHUIObjectBoxView()) 163 + ->setHeader($this->renderCalendarHeader($first)) 164 + ->appendChild($table) 165 + ->setFormErrors($warnings); 166 + if ($this->error) { 167 + $box->setInfoView($this->error); 181 168 182 - $event_count_badge = phutil_tag( 183 - 'div', 184 - array( 185 - 'class' => 'phui-calendar-month-event-count', 186 - ), 187 - $event_count); 169 + } 188 170 189 - $cell_day_secret_link = phutil_tag( 190 - 'a', 191 - array( 192 - 'class' => 'phui-calendar-month-secret-link', 193 - 'href' => $uri, 194 - ), 195 - null); 171 + return $box; 172 + } 196 173 197 - $cell_div = phutil_tag( 198 - 'div', 199 - array( 200 - 'class' => 'phui-calendar-month-cell-div', 201 - ), 202 - array( 203 - $cell_day_secret_link, 204 - $event_count_badge, 205 - $cell_list, 206 - )); 174 + private function getEventListCell($event_list) { 175 + $list = $event_list['list']; 176 + $class = $event_list['class']; 177 + $uri = $event_list['uri']; 178 + $count = $event_list['count']; 207 179 208 - $cells[] = phutil_tag( 209 - 'td', 210 - array( 211 - 'class' => 'phui-calendar-month-event-list '.$class, 212 - ), 213 - $cell_div); 214 - } 215 - $table[] = phutil_tag('tr', array(), $cells); 180 + $event_count_badge = $this->getEventCountBadge($count); 181 + $cell_day_secret_link = $this->getHiddenDayLink($uri); 182 + 183 + $cell_data_div = phutil_tag( 184 + 'div', 185 + array( 186 + 'class' => 'phui-calendar-month-cell-div', 187 + ), 188 + array( 189 + $cell_day_secret_link, 190 + $event_count_badge, 191 + $list, 192 + )); 216 193 217 - $cells = array(); 218 - foreach ($row as $cell) { 219 - $class = $cell['class']; 220 - $cell_day_secret_link = null; 194 + return phutil_tag( 195 + 'td', 196 + array( 197 + 'class' => 'phui-calendar-month-event-list '.$class, 198 + ), 199 + $cell_data_div); 200 + } 221 201 222 - if ($cell['date']) { 223 - $cell_day = $cell['date']; 224 - $uri = $cell['uri']; 202 + private function getDayNumberCell($event_list) { 203 + $class = $event_list['class']; 204 + $date = $event_list['date']; 205 + $cell_day_secret_link = null; 225 206 226 - $cell_day_secret_link = phutil_tag( 227 - 'a', 228 - array( 229 - 'class' => 'phui-calendar-month-secret-link', 230 - 'href' => $uri, 231 - ), 232 - null); 207 + if ($date) { 208 + $uri = $event_list['uri']; 209 + $cell_day_secret_link = $this->getHiddenDayLink($uri); 233 210 234 - $cell_day = phutil_tag( 235 - 'a', 236 - array( 237 - 'class' => 'phui-calendar-date-number', 238 - 'href' => $uri, 239 - ), 240 - $cell_day->format('j')); 211 + $cell_day = phutil_tag( 212 + 'a', 213 + array( 214 + 'class' => 'phui-calendar-date-number', 215 + 'href' => $uri, 216 + ), 217 + $date->format('j')); 218 + } else { 219 + $cell_day = null; 220 + } 241 221 242 - } else { 243 - $cell_day = null; 244 - } 222 + if ($date && $date->format('j') == $this->day) { 223 + $today_class = 'phui-calendar-today-slot phui-calendar-today'; 224 + } else { 225 + $today_class = 'phui-calendar-today-slot'; 226 + } 245 227 246 - if ($cell['date'] && $cell['date']->format('j') == $this->day) { 247 - $today_class = 'phui-calendar-today-slot phui-calendar-today'; 248 - } else { 249 - $today_class = 'phui-calendar-today-slot'; 250 - } 228 + $today_slot = phutil_tag ( 229 + 'div', 230 + array( 231 + 'class' => $today_class, 232 + ), 233 + null); 251 234 252 - $today_slot = phutil_tag ( 253 - 'div', 254 - array( 255 - 'class' => $today_class, 256 - ), 257 - null); 235 + $cell_div = phutil_tag( 236 + 'div', 237 + array( 238 + 'class' => 'phui-calendar-month-cell-div', 239 + ), 240 + array( 241 + $cell_day_secret_link, 242 + $cell_day, 243 + $today_slot, 244 + )); 258 245 259 - $cell_div = phutil_tag( 260 - 'div', 261 - array( 262 - 'class' => 'phui-calendar-month-cell-div', 263 - ), 264 - array( 265 - $cell_day_secret_link, 266 - $cell_day, 267 - $today_slot, 268 - )); 246 + return phutil_tag( 247 + 'td', 248 + array( 249 + 'class' => 'phui-calendar-date-number-container '.$class, 250 + ), 251 + $cell_div); 252 + } 269 253 270 - $cells[] = phutil_tag( 271 - 'td', 272 - array( 273 - 'class' => 'phui-calendar-date-number-container '.$class, 274 - ), 275 - $cell_div); 276 - } 277 - $table[] = phutil_tag('tr', array(), $cells); 254 + private function getEventCountBadge($count) { 255 + $event_count = null; 256 + if ($count > 0) { 257 + $event_count = phutil_tag( 258 + 'div', 259 + array( 260 + 'class' => 'phui-calendar-month-count-badge', 261 + ), 262 + $count); 278 263 } 279 264 280 - $header = phutil_tag( 265 + return phutil_tag( 266 + 'div', 267 + array( 268 + 'class' => 'phui-calendar-month-event-count', 269 + ), 270 + $event_count); 271 + } 272 + 273 + private function getHiddenDayLink($uri) { 274 + return phutil_tag( 275 + 'a', 276 + array( 277 + 'class' => 'phui-calendar-month-secret-link', 278 + 'href' => $uri, 279 + ), 280 + null); 281 + } 282 + 283 + private function getDayNamesHeader() { 284 + return phutil_tag( 281 285 'tr', 282 286 array('class' => 'phui-calendar-day-of-week-header'), 283 287 array( ··· 289 293 phutil_tag('th', array(), pht('Fri')), 290 294 phutil_tag('th', array(), pht('Sat')), 291 295 )); 292 - 293 - $table = phutil_tag( 294 - 'table', 295 - array('class' => 'phui-calendar-view'), 296 - array( 297 - $header, 298 - phutil_implode_html("\n", $table), 299 - )); 300 - 301 - $warnings = $this->getQueryRangeWarning(); 302 - 303 - $box = id(new PHUIObjectBoxView()) 304 - ->setHeader($this->renderCalendarHeader($first)) 305 - ->appendChild($table) 306 - ->setFormErrors($warnings); 307 - if ($this->error) { 308 - $box->setInfoView($this->error); 309 - 310 - } 311 - 312 - return $box; 313 296 } 314 297 315 298 private function renderCalendarHeader(DateTime $date) {