public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     // TODO: These should be user-based and navigable in the interface.
     $year = idate('Y');
     $month = idate('m');
     $holidays = id(new PhabricatorCalendarHoliday())->loadAllWhere('day BETWEEN %s AND %s', "{$year}-{$month}-01", "{$year}-{$month}-31");
     $statuses = id(new PhabricatorUserStatus())->loadAllWhere('dateTo >= %d AND dateFrom <= %d', strtotime("{$year}-{$month}-01"), strtotime("{$year}-{$month}-01 next month"));
     $month_view = new AphrontCalendarMonthView($month, $year);
     $month_view->setUser($user);
     $month_view->setHolidays($holidays);
     $phids = mpull($statuses, 'getUserPHID');
     $handles = $this->loadViewerHandles($phids);
     foreach ($statuses as $status) {
         $event = new AphrontCalendarEventView();
         $event->setEpochRange($status->getDateFrom(), $status->getDateTo());
         $name_text = $handles[$status->getUserPHID()]->getName();
         $status_text = $status->getTextStatus();
         $event->setUserPHID($status->getUserPHID());
         $event->setName("{$name_text} ({$status_text})");
         $event->setDescription($status->getStatusDescription($user));
         $month_view->addEvent($event);
     }
     return $this->buildStandardPageResponse(array('<div style="padding: 2em;">', $month_view, '</div>'), array('title' => 'Calendar'));
 }
 public function processRequest()
 {
     $now = time();
     $request = $this->getRequest();
     $user = $request->getUser();
     $year_d = phabricator_format_local_time($now, $user, 'Y');
     $year = $request->getInt('year', $year_d);
     $month_d = phabricator_format_local_time($now, $user, 'm');
     $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");
     $statuses = id(new PhabricatorCalendarEventQuery())->setViewer($user)->withDateRange(strtotime("{$year}-{$month}-01"), strtotime("{$year}-{$month}-01 next month"))->execute();
     if ($month == $month_d && $year == $year_d) {
         $month_view = new PHUICalendarMonthView($month, $year, $day);
     } else {
         $month_view = new PHUICalendarMonthView($month, $year);
     }
     $month_view->setBrowseURI($request->getRequestURI());
     $month_view->setUser($user);
     $month_view->setHolidays($holidays);
     $phids = mpull($statuses, 'getUserPHID');
     $handles = $this->loadViewerHandles($phids);
     /* Assign Colors */
     $unique = array_unique($phids);
     $allblue = false;
     $calcolors = CalendarColors::getColors();
     if (count($unique) > count($calcolors)) {
         $allblue = true;
     }
     $i = 0;
     $eventcolor = array();
     foreach ($unique as $phid) {
         if ($allblue) {
             $eventcolor[$phid] = CalendarColors::COLOR_SKY;
         } else {
             $eventcolor[$phid] = $calcolors[$i];
         }
         $i++;
     }
     foreach ($statuses as $status) {
         $event = new AphrontCalendarEventView();
         $event->setEpochRange($status->getDateFrom(), $status->getDateTo());
         $name_text = $handles[$status->getUserPHID()]->getName();
         $status_text = $status->getHumanStatus();
         $event->setUserPHID($status->getUserPHID());
         $event->setDescription(pht('%s (%s)', $name_text, $status_text));
         $event->setName($status_text);
         $event->setEventID($status->getID());
         $event->setColor($eventcolor[$status->getUserPHID()]);
         $month_view->addEvent($event);
     }
     $date = new DateTime("{$year}-{$month}-01");
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb(pht('All Events'));
     $crumbs->addTextCrumb($date->format('F Y'));
     $nav = $this->buildSideNavView();
     $nav->selectFilter('all/');
     $nav->appendChild(array($crumbs, $month_view));
     return $this->buildApplicationPage($nav, array('title' => pht('Calendar')));
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $this->getViewer();
     $username = $request->getURIData('username');
     $user = id(new PhabricatorPeopleQuery())->setViewer($viewer)->withUsernames(array($username))->needProfileImage(true)->executeOne();
     if (!$user) {
         return new Aphront404Response();
     }
     $this->setUser($user);
     $picture = $user->getProfileImageURI();
     $now = time();
     $request = $this->getRequest();
     $year_d = phabricator_format_local_time($now, $user, 'Y');
     $year = $request->getInt('year', $year_d);
     $month_d = phabricator_format_local_time($now, $user, 'm');
     $month = $request->getInt('month', $month_d);
     $day = phabricator_format_local_time($now, $user, 'j');
     $start_epoch = strtotime("{$year}-{$month}-01");
     $end_epoch = strtotime("{$year}-{$month}-01 next month");
     $statuses = id(new PhabricatorCalendarEventQuery())->setViewer($user)->withInvitedPHIDs(array($user->getPHID()))->withDateRange($start_epoch, $end_epoch)->execute();
     $start_range_value = AphrontFormDateControlValue::newFromEpoch($user, $start_epoch);
     $end_range_value = AphrontFormDateControlValue::newFromEpoch($user, $end_epoch);
     if ($month == $month_d && $year == $year_d) {
         $month_view = new PHUICalendarMonthView($start_range_value, $end_range_value, $month, $year, $day);
     } else {
         $month_view = new PHUICalendarMonthView($start_range_value, $end_range_value, $month, $year);
     }
     $month_view->setBrowseURI($request->getRequestURI());
     $month_view->setUser($user);
     $month_view->setImage($picture);
     $phids = mpull($statuses, 'getUserPHID');
     $handles = $this->loadViewerHandles($phids);
     foreach ($statuses as $status) {
         $event = new AphrontCalendarEventView();
         $event->setEpochRange($status->getDateFrom(), $status->getDateTo());
         $event->setUserPHID($status->getUserPHID());
         $event->setName($status->getName());
         $event->setDescription($status->getDescription());
         $event->setEventID($status->getID());
         $month_view->addEvent($event);
     }
     $nav = $this->getProfileMenu();
     $nav->selectFilter('calendar');
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb(pht('Calendar'));
     return $this->newPage()->setTitle(pht('Calendar'))->setNavigation($nav)->setCrumbs($crumbs)->appendChild($month_view);
 }
 public function processRequest()
 {
     $viewer = $this->getRequest()->getUser();
     $user = id(new PhabricatorPeopleQuery())->setViewer($viewer)->withUsernames(array($this->username))->needProfileImage(true)->executeOne();
     if (!$user) {
         return new Aphront404Response();
     }
     $picture = $user->loadProfileImageURI();
     $now = time();
     $request = $this->getRequest();
     $year_d = phabricator_format_local_time($now, $user, 'Y');
     $year = $request->getInt('year', $year_d);
     $month_d = phabricator_format_local_time($now, $user, 'm');
     $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");
     $statuses = id(new PhabricatorCalendarEventQuery())->setViewer($user)->withInvitedPHIDs(array($user->getPHID()))->withDateRange(strtotime("{$year}-{$month}-01"), strtotime("{$year}-{$month}-01 next month"))->execute();
     if ($month == $month_d && $year == $year_d) {
         $month_view = new PHUICalendarMonthView($month, $year, $day);
     } else {
         $month_view = new PHUICalendarMonthView($month, $year);
     }
     $month_view->setBrowseURI($request->getRequestURI());
     $month_view->setUser($user);
     $month_view->setHolidays($holidays);
     $month_view->setImage($picture);
     $phids = mpull($statuses, 'getUserPHID');
     $handles = $this->loadViewerHandles($phids);
     foreach ($statuses as $status) {
         $event = new AphrontCalendarEventView();
         $event->setEpochRange($status->getDateFrom(), $status->getDateTo());
         $event->setUserPHID($status->getUserPHID());
         $event->setName($status->getHumanStatus());
         $event->setDescription($status->getDescription());
         $event->setEventID($status->getID());
         $month_view->addEvent($event);
     }
     $date = new DateTime("{$year}-{$month}-01");
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb($user->getUsername(), '/p/' . $user->getUsername() . '/');
     $crumbs->addTextCrumb($date->format('F Y'));
     return $this->buildApplicationPage(array($crumbs, $month_view), array('title' => pht('Calendar')));
 }
 private function renderUserCalendar(PhabricatorUser $user)
 {
     $viewer = $this->getRequest()->getUser();
     $epochs = CalendarTimeUtil::getCalendarEventEpochs($viewer, 'today', 7);
     $start_epoch = $epochs['start_epoch'];
     $end_epoch = $epochs['end_epoch'];
     $statuses = id(new PhabricatorCalendarEventQuery())->setViewer($viewer)->withInvitedPHIDs(array($user->getPHID()))->withDateRange($start_epoch, $end_epoch)->execute();
     $timestamps = CalendarTimeUtil::getCalendarWeekTimestamps($viewer);
     $today = $timestamps['today'];
     $epoch_stamps = $timestamps['epoch_stamps'];
     $events = array();
     foreach ($epoch_stamps as $day) {
         $epoch_start = $day->format('U');
         $next_day = clone $day;
         $next_day->modify('+1 day');
         $epoch_end = $next_day->format('U');
         foreach ($statuses as $status) {
             if ($status->getDateTo() < $epoch_start) {
                 continue;
             }
             if ($status->getDateFrom() >= $epoch_end) {
                 continue;
             }
             $event = new AphrontCalendarEventView();
             $event->setEpochRange($status->getDateFrom(), $status->getDateTo());
             $status_text = $status->getHumanStatus();
             $event->setUserPHID($status->getUserPHID());
             $event->setName($status_text);
             $event->setDescription($status->getDescription());
             $event->setEventID($status->getID());
             $events[$epoch_start][] = $event;
         }
     }
     $week = array();
     foreach ($epoch_stamps as $day) {
         $epoch = $day->format('U');
         $headertext = phabricator_format_local_time($epoch, $user, 'l, M d');
         $list = new PHUICalendarListView();
         $list->setUser($viewer);
         $list->showBlankState(true);
         if (isset($events[$epoch])) {
             foreach ($events[$epoch] as $event) {
                 $list->addEvent($event);
             }
         }
         $header = phutil_tag('a', array('href' => $this->getRequest()->getRequestURI() . 'calendar/'), $headertext);
         $calendar = new PHUICalendarWidgetView();
         $calendar->setHeader($header);
         $calendar->setCalendarList($list);
         $week[] = $calendar;
     }
     return phutil_tag_div('profile-calendar', $week);
 }
 private function buildCalendarView(array $statuses, PhabricatorSavedQuery $query, array $handles)
 {
     $viewer = $this->requireViewer();
     $now = time();
     list($start_year, $start_month) = $this->getDisplayYearAndMonthAndDay($this->getQueryDateFrom($query)->getEpoch(), $this->getQueryDateTo($query)->getEpoch(), $query->getParameter('display'));
     $now_year = phabricator_format_local_time($now, $viewer, 'Y');
     $now_month = phabricator_format_local_time($now, $viewer, 'm');
     $now_day = phabricator_format_local_time($now, $viewer, 'j');
     if ($start_month == $now_month && $start_year == $now_year) {
         $month_view = new PHUICalendarMonthView($this->getQueryDateFrom($query), $this->getQueryDateTo($query), $start_month, $start_year, $now_day);
     } else {
         $month_view = new PHUICalendarMonthView($this->getQueryDateFrom($query), $this->getQueryDateTo($query), $start_month, $start_year);
     }
     $month_view->setUser($viewer);
     $phids = mpull($statuses, 'getUserPHID');
     foreach ($statuses as $status) {
         $viewer_is_invited = $status->getIsUserInvited($viewer->getPHID());
         $event = new AphrontCalendarEventView();
         $event->setEpochRange($status->getDateFrom(), $status->getDateTo());
         $event->setIsAllDay($status->getIsAllDay());
         $event->setIcon($status->getIcon());
         $name_text = $handles[$status->getUserPHID()]->getName();
         $status_text = $status->getName();
         $event->setUserPHID($status->getUserPHID());
         $event->setDescription(pht('%s (%s)', $name_text, $status_text));
         $event->setName($status_text);
         $event->setURI($status->getURI());
         $event->setViewerIsInvited($viewer_is_invited);
         $month_view->addEvent($event);
     }
     $month_view->setBrowseURI($this->getURI('query/' . $query->getQueryKey() . '/'));
     // TODO redesign-2015 : Move buttons out of PHUICalendarView?
     $result = new PhabricatorApplicationSearchResultView();
     $result->setContent($month_view);
     return $result;
 }