1
0
Fork 0
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:
lkassianik 2015-05-13 15:58:46 -07:00
parent 61b178f44e
commit 97611958b0
2 changed files with 146 additions and 170 deletions

View file

@ -35,12 +35,6 @@ final class PhabricatorPeopleCalendarController
$month = $request->getInt('month', $month_d);
$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");
$end_epoch = strtotime("{$year}-{$month}-01 next month");
@ -76,7 +70,6 @@ final class PhabricatorPeopleCalendarController
$month_view->setBrowseURI($request->getRequestURI());
$month_view->setUser($user);
$month_view->setHolidays($holidays);
$month_view->setImage($picture);
$phids = mpull($statuses, 'getUserPHID');

View file

@ -7,7 +7,6 @@ final class PHUICalendarMonthView extends AphrontView {
private $day;
private $month;
private $year;
private $holidays = array();
private $events = array();
private $browseURI;
private $image;
@ -36,12 +35,6 @@ final class PHUICalendarMonthView extends AphrontView {
return $this;
}
public function setHolidays(array $holidays) {
assert_instances_of($holidays, 'PhabricatorCalendarHoliday');
$this->holidays = mpull($holidays, null, 'getDay');
return $this;
}
public function __construct(
$range_start,
$range_end,
@ -71,7 +64,7 @@ final class PHUICalendarMonthView extends AphrontView {
$first = reset($days);
$empty = $first->format('w');
$markup = array();
$cell_lists = array();
$empty_cell = array(
'list' => null,
'date' => null,
@ -81,47 +74,28 @@ final class PHUICalendarMonthView extends AphrontView {
);
for ($ii = 0; $ii < $empty; $ii++) {
$markup[] = $empty_cell;
$cell_lists[] = $empty_cell;
}
$show_events = array();
foreach ($days as $day) {
$day_number = $day->format('j');
$holiday = idx($this->holidays, $day->format('Y-m-d'));
$class = 'phui-calendar-month-day';
$weekday = $day->format('w');
if ($holiday || $weekday == 0 || $weekday == 6) {
$class .= ' phui-calendar-not-work-day';
}
$day->setTime(0, 0, 0);
$epoch_start = $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")); // &nbsp;
}
$day_start_epoch = $day->format('U');
$day_end_epoch = id(clone $day)->modify('+1 day')->format('U');
$list_events = array();
$all_day_events = array();
foreach ($events as $event) {
if ($event->getEpochStart() >= $epoch_end) {
// This list is sorted, so we can stop looking.
if ($event->getEpochStart() >= $day_end_epoch) {
break;
}
if ($event->getEpochStart() < $epoch_end &&
$event->getEpochEnd() > $epoch_start) {
if ($event->getEpochStart() < $day_end_epoch &&
$event->getEpochEnd() > $day_start_epoch) {
if ($event->getIsAllDay()) {
$all_day_events[] = $event;
} else {
@ -144,7 +118,7 @@ final class PHUICalendarMonthView extends AphrontView {
$day->format('m').'/'.
$day->format('d').'/';
$markup[] = array(
$cell_lists[] = array(
'list' => $list,
'date' => $day,
'uri' => $uri,
@ -153,48 +127,60 @@ final class PHUICalendarMonthView extends AphrontView {
);
}
$table = array();
$rows = array_chunk($markup, 7);
$rows = array();
$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();
$event_count_badge = null;
while (count($row) < 7) {
$row[] = $empty_cell;
while (count($week_of_cell_lists) < 7) {
$week_of_cell_lists[] = $empty_cell;
}
foreach ($row as $cell) {
$cell_list = $cell['list'];
$class = $cell['class'];
$uri = $cell['uri'];
$count = $cell['count'];
foreach ($week_of_cell_lists as $cell_list) {
$cells[] = $this->getEventListCell($cell_list);
}
$rows[] = phutil_tag('tr', array(), $cells);
$event_count = null;
if ($count > 0) {
$event_count = phutil_tag(
'div',
array(
'class' => 'phui-calendar-month-count-badge',
),
$count);
$cells = array();
foreach ($week_of_cell_lists as $cell_list) {
$cells[] = $this->getDayNumberCell($cell_list);
}
$rows[] = phutil_tag('tr', array(), $cells);
}
$event_count_badge = phutil_tag(
'div',
array(
'class' => 'phui-calendar-month-event-count',
),
$event_count);
$header = $this->getDayNamesHeader();
$cell_day_secret_link = phutil_tag(
'a',
$table = phutil_tag(
'table',
array('class' => 'phui-calendar-view'),
array(
'class' => 'phui-calendar-month-secret-link',
'href' => $uri,
),
null);
$header,
$rows,
));
$cell_div = phutil_tag(
$warnings = $this->getQueryRangeWarning();
$box = id(new PHUIObjectBoxView())
->setHeader($this->renderCalendarHeader($first))
->appendChild($table)
->setFormErrors($warnings);
if ($this->error) {
$box->setInfoView($this->error);
}
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',
@ -202,34 +188,25 @@ final class PHUICalendarMonthView extends AphrontView {
array(
$cell_day_secret_link,
$event_count_badge,
$cell_list,
$list,
));
$cells[] = phutil_tag(
return phutil_tag(
'td',
array(
'class' => 'phui-calendar-month-event-list '.$class,
),
$cell_div);
$cell_data_div);
}
$table[] = phutil_tag('tr', array(), $cells);
$cells = array();
foreach ($row as $cell) {
$class = $cell['class'];
private function getDayNumberCell($event_list) {
$class = $event_list['class'];
$date = $event_list['date'];
$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);
if ($date) {
$uri = $event_list['uri'];
$cell_day_secret_link = $this->getHiddenDayLink($uri);
$cell_day = phutil_tag(
'a',
@ -237,13 +214,12 @@ final class PHUICalendarMonthView extends AphrontView {
'class' => 'phui-calendar-date-number',
'href' => $uri,
),
$cell_day->format('j'));
$date->format('j'));
} else {
$cell_day = null;
}
if ($cell['date'] && $cell['date']->format('j') == $this->day) {
if ($date && $date->format('j') == $this->day) {
$today_class = 'phui-calendar-today-slot phui-calendar-today';
} else {
$today_class = 'phui-calendar-today-slot';
@ -267,17 +243,45 @@ final class PHUICalendarMonthView extends AphrontView {
$today_slot,
));
$cells[] = phutil_tag(
return phutil_tag(
'td',
array(
'class' => 'phui-calendar-date-number-container '.$class,
),
$cell_div);
}
$table[] = phutil_tag('tr', array(), $cells);
private function getEventCountBadge($count) {
$event_count = null;
if ($count > 0) {
$event_count = phutil_tag(
'div',
array(
'class' => 'phui-calendar-month-count-badge',
),
$count);
}
$header = phutil_tag(
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(
@ -289,27 +293,6 @@ final class PHUICalendarMonthView extends AphrontView {
phutil_tag('th', array(), pht('Fri')),
phutil_tag('th', array(), pht('Sat')),
));
$table = phutil_tag(
'table',
array('class' => 'phui-calendar-view'),
array(
$header,
phutil_implode_html("\n", $table),
));
$warnings = $this->getQueryRangeWarning();
$box = id(new PHUIObjectBoxView())
->setHeader($this->renderCalendarHeader($first))
->appendChild($table)
->setFormErrors($warnings);
if ($this->error) {
$box->setInfoView($this->error);
}
return $box;
}
private function renderCalendarHeader(DateTime $date) {