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 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;
 }
 private function buildCalendarMonthView(array $events, PhabricatorSavedQuery $query)
 {
     assert_instances_of($events, 'PhabricatorCalendarEvent');
     $viewer = $this->requireViewer();
     $now = PhabricatorTime::getNow();
     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);
     $viewer_phid = $viewer->getPHID();
     foreach ($events as $event) {
         $epoch_min = $event->getStartDateTimeEpoch();
         $epoch_max = $event->getEndDateTimeEpoch();
         $is_invited = $event->isRSVPInvited($viewer_phid);
         $is_attending = $event->getIsUserAttending($viewer_phid);
         $event_view = id(new AphrontCalendarEventView())->setHostPHID($event->getHostPHID())->setEpochRange($epoch_min, $epoch_max)->setIsCancelled($event->getIsCancelled())->setName($event->getName())->setURI($event->getURI())->setIsAllDay($event->getIsAllDay())->setIcon($event->getDisplayIcon($viewer))->setViewerIsInvited($is_invited || $is_attending)->setDatetimeSummary($event->renderEventDate($viewer, true))->setIconColor($event->getDisplayIconColor($viewer));
         $month_view->addEvent($event_view);
     }
     $month_view->setBrowseURI($this->getURI('query/' . $query->getQueryKey() . '/'));
     $from = $this->getQueryDateFrom($query)->getDateTime();
     $crumbs = array();
     $crumbs[] = id(new PHUICrumbView())->setName($from->format('F Y'));
     $header = id(new PHUIHeaderView())->setProfileHeader(true)->setHeader($from->format('F Y'));
     return $this->newResultView($month_view)->setCrumbs($crumbs)->setHeader($header);
 }