mirror of
https://we.phorge.it/source/phorge.git
synced 2025-01-18 10:41:08 +01:00
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
This commit is contained in:
parent
61b178f44e
commit
97611958b0
2 changed files with 146 additions and 170 deletions
|
@ -35,12 +35,6 @@ final class PhabricatorPeopleCalendarController
|
||||||
$month = $request->getInt('month', $month_d);
|
$month = $request->getInt('month', $month_d);
|
||||||
$day = phabricator_format_local_time($now, $user, 'j');
|
$day = phabricator_format_local_time($now, $user, 'j');
|
||||||
|
|
||||||
|
|
||||||
$holidays = id(new PhabricatorCalendarHoliday())->loadAllWhere(
|
|
||||||
'day BETWEEN %s AND %s',
|
|
||||||
"{$year}-{$month}-01",
|
|
||||||
"{$year}-{$month}-31");
|
|
||||||
|
|
||||||
$start_epoch = strtotime("{$year}-{$month}-01");
|
$start_epoch = strtotime("{$year}-{$month}-01");
|
||||||
$end_epoch = strtotime("{$year}-{$month}-01 next month");
|
$end_epoch = strtotime("{$year}-{$month}-01 next month");
|
||||||
|
|
||||||
|
@ -76,7 +70,6 @@ final class PhabricatorPeopleCalendarController
|
||||||
|
|
||||||
$month_view->setBrowseURI($request->getRequestURI());
|
$month_view->setBrowseURI($request->getRequestURI());
|
||||||
$month_view->setUser($user);
|
$month_view->setUser($user);
|
||||||
$month_view->setHolidays($holidays);
|
|
||||||
$month_view->setImage($picture);
|
$month_view->setImage($picture);
|
||||||
|
|
||||||
$phids = mpull($statuses, 'getUserPHID');
|
$phids = mpull($statuses, 'getUserPHID');
|
||||||
|
|
|
@ -7,7 +7,6 @@ final class PHUICalendarMonthView extends AphrontView {
|
||||||
private $day;
|
private $day;
|
||||||
private $month;
|
private $month;
|
||||||
private $year;
|
private $year;
|
||||||
private $holidays = array();
|
|
||||||
private $events = array();
|
private $events = array();
|
||||||
private $browseURI;
|
private $browseURI;
|
||||||
private $image;
|
private $image;
|
||||||
|
@ -36,12 +35,6 @@ final class PHUICalendarMonthView extends AphrontView {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setHolidays(array $holidays) {
|
|
||||||
assert_instances_of($holidays, 'PhabricatorCalendarHoliday');
|
|
||||||
$this->holidays = mpull($holidays, null, 'getDay');
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$range_start,
|
$range_start,
|
||||||
$range_end,
|
$range_end,
|
||||||
|
@ -71,7 +64,7 @@ final class PHUICalendarMonthView extends AphrontView {
|
||||||
$first = reset($days);
|
$first = reset($days);
|
||||||
$empty = $first->format('w');
|
$empty = $first->format('w');
|
||||||
|
|
||||||
$markup = array();
|
$cell_lists = array();
|
||||||
$empty_cell = array(
|
$empty_cell = array(
|
||||||
'list' => null,
|
'list' => null,
|
||||||
'date' => null,
|
'date' => null,
|
||||||
|
@ -81,47 +74,28 @@ final class PHUICalendarMonthView extends AphrontView {
|
||||||
);
|
);
|
||||||
|
|
||||||
for ($ii = 0; $ii < $empty; $ii++) {
|
for ($ii = 0; $ii < $empty; $ii++) {
|
||||||
$markup[] = $empty_cell;
|
$cell_lists[] = $empty_cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
$show_events = array();
|
|
||||||
|
|
||||||
foreach ($days as $day) {
|
foreach ($days as $day) {
|
||||||
$day_number = $day->format('j');
|
$day_number = $day->format('j');
|
||||||
|
|
||||||
$holiday = idx($this->holidays, $day->format('Y-m-d'));
|
|
||||||
$class = 'phui-calendar-month-day';
|
$class = 'phui-calendar-month-day';
|
||||||
$weekday = $day->format('w');
|
$weekday = $day->format('w');
|
||||||
|
|
||||||
if ($holiday || $weekday == 0 || $weekday == 6) {
|
|
||||||
$class .= ' phui-calendar-not-work-day';
|
|
||||||
}
|
|
||||||
|
|
||||||
$day->setTime(0, 0, 0);
|
$day->setTime(0, 0, 0);
|
||||||
$epoch_start = $day->format('U');
|
$day_start_epoch = $day->format('U');
|
||||||
|
$day_end_epoch = id(clone $day)->modify('+1 day')->format('U');
|
||||||
|
|
||||||
$epoch_end = id(clone $day)->modify('+1 day')->format('U');
|
|
||||||
|
|
||||||
if ($weekday == 0) {
|
|
||||||
$show_events = array();
|
|
||||||
} else {
|
|
||||||
$show_events = array_fill_keys(
|
|
||||||
array_keys($show_events),
|
|
||||||
phutil_tag_div(
|
|
||||||
'phui-calendar-event phui-calendar-event-empty',
|
|
||||||
"\xC2\xA0")); //
|
|
||||||
}
|
|
||||||
|
|
||||||
$list_events = array();
|
$list_events = array();
|
||||||
$all_day_events = array();
|
$all_day_events = array();
|
||||||
|
|
||||||
foreach ($events as $event) {
|
foreach ($events as $event) {
|
||||||
if ($event->getEpochStart() >= $epoch_end) {
|
if ($event->getEpochStart() >= $day_end_epoch) {
|
||||||
// This list is sorted, so we can stop looking.
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ($event->getEpochStart() < $epoch_end &&
|
if ($event->getEpochStart() < $day_end_epoch &&
|
||||||
$event->getEpochEnd() > $epoch_start) {
|
$event->getEpochEnd() > $day_start_epoch) {
|
||||||
if ($event->getIsAllDay()) {
|
if ($event->getIsAllDay()) {
|
||||||
$all_day_events[] = $event;
|
$all_day_events[] = $event;
|
||||||
} else {
|
} else {
|
||||||
|
@ -144,7 +118,7 @@ final class PHUICalendarMonthView extends AphrontView {
|
||||||
$day->format('m').'/'.
|
$day->format('m').'/'.
|
||||||
$day->format('d').'/';
|
$day->format('d').'/';
|
||||||
|
|
||||||
$markup[] = array(
|
$cell_lists[] = array(
|
||||||
'list' => $list,
|
'list' => $list,
|
||||||
'date' => $day,
|
'date' => $day,
|
||||||
'uri' => $uri,
|
'uri' => $uri,
|
||||||
|
@ -153,149 +127,34 @@ final class PHUICalendarMonthView extends AphrontView {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$table = array();
|
$rows = array();
|
||||||
$rows = array_chunk($markup, 7);
|
$cell_lists_by_week = array_chunk($cell_lists, 7);
|
||||||
|
|
||||||
foreach ($rows as $row) {
|
foreach ($cell_lists_by_week as $week_of_cell_lists) {
|
||||||
$cells = array();
|
$cells = array();
|
||||||
$event_count_badge = null;
|
while (count($week_of_cell_lists) < 7) {
|
||||||
|
$week_of_cell_lists[] = $empty_cell;
|
||||||
while (count($row) < 7) {
|
|
||||||
$row[] = $empty_cell;
|
|
||||||
}
|
}
|
||||||
foreach ($row as $cell) {
|
foreach ($week_of_cell_lists as $cell_list) {
|
||||||
$cell_list = $cell['list'];
|
$cells[] = $this->getEventListCell($cell_list);
|
||||||
$class = $cell['class'];
|
|
||||||
$uri = $cell['uri'];
|
|
||||||
$count = $cell['count'];
|
|
||||||
|
|
||||||
$event_count = null;
|
|
||||||
if ($count > 0) {
|
|
||||||
$event_count = phutil_tag(
|
|
||||||
'div',
|
|
||||||
array(
|
|
||||||
'class' => 'phui-calendar-month-count-badge',
|
|
||||||
),
|
|
||||||
$count);
|
|
||||||
}
|
|
||||||
|
|
||||||
$event_count_badge = phutil_tag(
|
|
||||||
'div',
|
|
||||||
array(
|
|
||||||
'class' => 'phui-calendar-month-event-count',
|
|
||||||
),
|
|
||||||
$event_count);
|
|
||||||
|
|
||||||
$cell_day_secret_link = phutil_tag(
|
|
||||||
'a',
|
|
||||||
array(
|
|
||||||
'class' => 'phui-calendar-month-secret-link',
|
|
||||||
'href' => $uri,
|
|
||||||
),
|
|
||||||
null);
|
|
||||||
|
|
||||||
$cell_div = phutil_tag(
|
|
||||||
'div',
|
|
||||||
array(
|
|
||||||
'class' => 'phui-calendar-month-cell-div',
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
$cell_day_secret_link,
|
|
||||||
$event_count_badge,
|
|
||||||
$cell_list,
|
|
||||||
));
|
|
||||||
|
|
||||||
$cells[] = phutil_tag(
|
|
||||||
'td',
|
|
||||||
array(
|
|
||||||
'class' => 'phui-calendar-month-event-list '.$class,
|
|
||||||
),
|
|
||||||
$cell_div);
|
|
||||||
}
|
}
|
||||||
$table[] = phutil_tag('tr', array(), $cells);
|
$rows[] = phutil_tag('tr', array(), $cells);
|
||||||
|
|
||||||
$cells = array();
|
$cells = array();
|
||||||
foreach ($row as $cell) {
|
foreach ($week_of_cell_lists as $cell_list) {
|
||||||
$class = $cell['class'];
|
$cells[] = $this->getDayNumberCell($cell_list);
|
||||||
$cell_day_secret_link = null;
|
|
||||||
|
|
||||||
if ($cell['date']) {
|
|
||||||
$cell_day = $cell['date'];
|
|
||||||
$uri = $cell['uri'];
|
|
||||||
|
|
||||||
$cell_day_secret_link = phutil_tag(
|
|
||||||
'a',
|
|
||||||
array(
|
|
||||||
'class' => 'phui-calendar-month-secret-link',
|
|
||||||
'href' => $uri,
|
|
||||||
),
|
|
||||||
null);
|
|
||||||
|
|
||||||
$cell_day = phutil_tag(
|
|
||||||
'a',
|
|
||||||
array(
|
|
||||||
'class' => 'phui-calendar-date-number',
|
|
||||||
'href' => $uri,
|
|
||||||
),
|
|
||||||
$cell_day->format('j'));
|
|
||||||
|
|
||||||
} else {
|
|
||||||
$cell_day = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($cell['date'] && $cell['date']->format('j') == $this->day) {
|
|
||||||
$today_class = 'phui-calendar-today-slot phui-calendar-today';
|
|
||||||
} else {
|
|
||||||
$today_class = 'phui-calendar-today-slot';
|
|
||||||
}
|
|
||||||
|
|
||||||
$today_slot = phutil_tag (
|
|
||||||
'div',
|
|
||||||
array(
|
|
||||||
'class' => $today_class,
|
|
||||||
),
|
|
||||||
null);
|
|
||||||
|
|
||||||
$cell_div = phutil_tag(
|
|
||||||
'div',
|
|
||||||
array(
|
|
||||||
'class' => 'phui-calendar-month-cell-div',
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
$cell_day_secret_link,
|
|
||||||
$cell_day,
|
|
||||||
$today_slot,
|
|
||||||
));
|
|
||||||
|
|
||||||
$cells[] = phutil_tag(
|
|
||||||
'td',
|
|
||||||
array(
|
|
||||||
'class' => 'phui-calendar-date-number-container '.$class,
|
|
||||||
),
|
|
||||||
$cell_div);
|
|
||||||
}
|
}
|
||||||
$table[] = phutil_tag('tr', array(), $cells);
|
$rows[] = phutil_tag('tr', array(), $cells);
|
||||||
}
|
}
|
||||||
|
|
||||||
$header = phutil_tag(
|
$header = $this->getDayNamesHeader();
|
||||||
'tr',
|
|
||||||
array('class' => 'phui-calendar-day-of-week-header'),
|
|
||||||
array(
|
|
||||||
phutil_tag('th', array(), pht('Sun')),
|
|
||||||
phutil_tag('th', array(), pht('Mon')),
|
|
||||||
phutil_tag('th', array(), pht('Tue')),
|
|
||||||
phutil_tag('th', array(), pht('Wed')),
|
|
||||||
phutil_tag('th', array(), pht('Thu')),
|
|
||||||
phutil_tag('th', array(), pht('Fri')),
|
|
||||||
phutil_tag('th', array(), pht('Sat')),
|
|
||||||
));
|
|
||||||
|
|
||||||
$table = phutil_tag(
|
$table = phutil_tag(
|
||||||
'table',
|
'table',
|
||||||
array('class' => 'phui-calendar-view'),
|
array('class' => 'phui-calendar-view'),
|
||||||
array(
|
array(
|
||||||
$header,
|
$header,
|
||||||
phutil_implode_html("\n", $table),
|
$rows,
|
||||||
));
|
));
|
||||||
|
|
||||||
$warnings = $this->getQueryRangeWarning();
|
$warnings = $this->getQueryRangeWarning();
|
||||||
|
@ -312,6 +171,130 @@ final class PHUICalendarMonthView extends AphrontView {
|
||||||
return $box;
|
return $box;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getEventListCell($event_list) {
|
||||||
|
$list = $event_list['list'];
|
||||||
|
$class = $event_list['class'];
|
||||||
|
$uri = $event_list['uri'];
|
||||||
|
$count = $event_list['count'];
|
||||||
|
|
||||||
|
$event_count_badge = $this->getEventCountBadge($count);
|
||||||
|
$cell_day_secret_link = $this->getHiddenDayLink($uri);
|
||||||
|
|
||||||
|
$cell_data_div = phutil_tag(
|
||||||
|
'div',
|
||||||
|
array(
|
||||||
|
'class' => 'phui-calendar-month-cell-div',
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
$cell_day_secret_link,
|
||||||
|
$event_count_badge,
|
||||||
|
$list,
|
||||||
|
));
|
||||||
|
|
||||||
|
return phutil_tag(
|
||||||
|
'td',
|
||||||
|
array(
|
||||||
|
'class' => 'phui-calendar-month-event-list '.$class,
|
||||||
|
),
|
||||||
|
$cell_data_div);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getDayNumberCell($event_list) {
|
||||||
|
$class = $event_list['class'];
|
||||||
|
$date = $event_list['date'];
|
||||||
|
$cell_day_secret_link = null;
|
||||||
|
|
||||||
|
if ($date) {
|
||||||
|
$uri = $event_list['uri'];
|
||||||
|
$cell_day_secret_link = $this->getHiddenDayLink($uri);
|
||||||
|
|
||||||
|
$cell_day = phutil_tag(
|
||||||
|
'a',
|
||||||
|
array(
|
||||||
|
'class' => 'phui-calendar-date-number',
|
||||||
|
'href' => $uri,
|
||||||
|
),
|
||||||
|
$date->format('j'));
|
||||||
|
} else {
|
||||||
|
$cell_day = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($date && $date->format('j') == $this->day) {
|
||||||
|
$today_class = 'phui-calendar-today-slot phui-calendar-today';
|
||||||
|
} else {
|
||||||
|
$today_class = 'phui-calendar-today-slot';
|
||||||
|
}
|
||||||
|
|
||||||
|
$today_slot = phutil_tag (
|
||||||
|
'div',
|
||||||
|
array(
|
||||||
|
'class' => $today_class,
|
||||||
|
),
|
||||||
|
null);
|
||||||
|
|
||||||
|
$cell_div = phutil_tag(
|
||||||
|
'div',
|
||||||
|
array(
|
||||||
|
'class' => 'phui-calendar-month-cell-div',
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
$cell_day_secret_link,
|
||||||
|
$cell_day,
|
||||||
|
$today_slot,
|
||||||
|
));
|
||||||
|
|
||||||
|
return phutil_tag(
|
||||||
|
'td',
|
||||||
|
array(
|
||||||
|
'class' => 'phui-calendar-date-number-container '.$class,
|
||||||
|
),
|
||||||
|
$cell_div);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getEventCountBadge($count) {
|
||||||
|
$event_count = null;
|
||||||
|
if ($count > 0) {
|
||||||
|
$event_count = phutil_tag(
|
||||||
|
'div',
|
||||||
|
array(
|
||||||
|
'class' => 'phui-calendar-month-count-badge',
|
||||||
|
),
|
||||||
|
$count);
|
||||||
|
}
|
||||||
|
|
||||||
|
return phutil_tag(
|
||||||
|
'div',
|
||||||
|
array(
|
||||||
|
'class' => 'phui-calendar-month-event-count',
|
||||||
|
),
|
||||||
|
$event_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getHiddenDayLink($uri) {
|
||||||
|
return phutil_tag(
|
||||||
|
'a',
|
||||||
|
array(
|
||||||
|
'class' => 'phui-calendar-month-secret-link',
|
||||||
|
'href' => $uri,
|
||||||
|
),
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getDayNamesHeader() {
|
||||||
|
return phutil_tag(
|
||||||
|
'tr',
|
||||||
|
array('class' => 'phui-calendar-day-of-week-header'),
|
||||||
|
array(
|
||||||
|
phutil_tag('th', array(), pht('Sun')),
|
||||||
|
phutil_tag('th', array(), pht('Mon')),
|
||||||
|
phutil_tag('th', array(), pht('Tue')),
|
||||||
|
phutil_tag('th', array(), pht('Wed')),
|
||||||
|
phutil_tag('th', array(), pht('Thu')),
|
||||||
|
phutil_tag('th', array(), pht('Fri')),
|
||||||
|
phutil_tag('th', array(), pht('Sat')),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
private function renderCalendarHeader(DateTime $date) {
|
private function renderCalendarHeader(DateTime $date) {
|
||||||
$button_bar = null;
|
$button_bar = null;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue