public static function loadTasks(PhabricatorSearchQuery $search_query)
 {
     $any_project = false;
     $search_text = $search_query->getParameter('fullTextSearch');
     $user_phids = $search_query->getParameter('userPHIDs', array());
     $project_phids = $search_query->getParameter('projectPHIDs', array());
     $task_ids = $search_query->getParameter('taskIDs', array());
     $xproject_phids = $search_query->getParameter('excludeProjectPHIDs', array());
     $owner_phids = $search_query->getParameter('ownerPHIDs', array());
     $author_phids = $search_query->getParameter('authorPHIDs', array());
     $low_priority = $search_query->getParameter('lowPriority');
     $low_priority = nonempty($low_priority, ManiphestTaskPriority::getLowestPriority());
     $high_priority = $search_query->getParameter('highPriority');
     $high_priority = nonempty($high_priority, ManiphestTaskPriority::getHighestPriority());
     $query = new ManiphestTaskQuery();
     $query->withProjects($project_phids);
     $query->withTaskIDs($task_ids);
     if ($xproject_phids) {
         $query->withoutProjects($xproject_phids);
     }
     if ($owner_phids) {
         $query->withOwners($owner_phids);
     }
     if ($author_phids) {
         $query->withAuthors($author_phids);
     }
     $status = $search_query->getParameter('status', 'all');
     if (!empty($status['open']) && !empty($status['closed'])) {
         $query->withStatus(ManiphestTaskQuery::STATUS_ANY);
     } else {
         if (!empty($status['open'])) {
             $query->withStatus(ManiphestTaskQuery::STATUS_OPEN);
         } else {
             $query->withStatus(ManiphestTaskQuery::STATUS_CLOSED);
         }
     }
     switch ($search_query->getParameter('view')) {
         case 'action':
             $query->withOwners($user_phids);
             break;
         case 'created':
             $query->withAuthors($user_phids);
             break;
         case 'subscribed':
             $query->withSubscribers($user_phids);
             break;
         case 'triage':
             $query->withOwners($user_phids);
             $query->withPriority(ManiphestTaskPriority::PRIORITY_TRIAGE);
             break;
         case 'alltriage':
             $query->withPriority(ManiphestTaskPriority::PRIORITY_TRIAGE);
             break;
         case 'all':
             break;
         case 'projecttriage':
             $query->withPriority(ManiphestTaskPriority::PRIORITY_TRIAGE);
             $any_project = true;
             break;
         case 'projectall':
             $any_project = true;
             break;
         case 'custom':
             $query->withPrioritiesBetween($low_priority, $high_priority);
             break;
     }
     $query->withAnyProject($any_project);
     $query->withFullTextSearch($search_text);
     $order_map = array('priority' => ManiphestTaskQuery::ORDER_PRIORITY, 'created' => ManiphestTaskQuery::ORDER_CREATED, 'title' => ManiphestTaskQuery::ORDER_TITLE);
     $query->setOrderBy(idx($order_map, $search_query->getParameter('order'), ManiphestTaskQuery::ORDER_MODIFIED));
     $group_map = array('priority' => ManiphestTaskQuery::GROUP_PRIORITY, 'owner' => ManiphestTaskQuery::GROUP_OWNER, 'status' => ManiphestTaskQuery::GROUP_STATUS, 'project' => ManiphestTaskQuery::GROUP_PROJECT);
     $query->setGroupBy(idx($group_map, $search_query->getParameter('group'), ManiphestTaskQuery::GROUP_NONE));
     $query->setCalculateRows(true);
     $query->setLimit($search_query->getParameter('limit'));
     $query->setOffset($search_query->getParameter('offset'));
     $data = $query->execute();
     $total_row_count = $query->getRowCount();
     $project_group_phids = array();
     if ($search_query->getParameter('group') == 'project') {
         foreach ($data as $task) {
             foreach ($task->getProjectPHIDs() as $phid) {
                 $project_group_phids[] = $phid;
             }
         }
     }
     $handle_phids = mpull($data, 'getOwnerPHID');
     $handle_phids = array_merge($handle_phids, $project_phids, $user_phids, $xproject_phids, $owner_phids, $author_phids, $project_group_phids, array_mergev(mpull($data, 'getProjectPHIDs')));
     $handles = id(new PhabricatorObjectHandleData($handle_phids))->loadHandles();
     switch ($search_query->getParameter('group')) {
         case 'priority':
             $data = mgroup($data, 'getPriority');
             // If we have invalid priorities, they'll all map to "???". Merge
             // arrays to prevent them from overwriting each other.
             $out = array();
             foreach ($data as $pri => $tasks) {
                 $out[ManiphestTaskPriority::getTaskPriorityName($pri)][] = $tasks;
             }
             foreach ($out as $pri => $tasks) {
                 $out[$pri] = array_mergev($tasks);
             }
             $data = $out;
             break;
         case 'status':
             $data = mgroup($data, 'getStatus');
             $out = array();
             foreach ($data as $status => $tasks) {
                 $out[ManiphestTaskStatus::getTaskStatusFullName($status)] = $tasks;
             }
             $data = $out;
             break;
         case 'owner':
             $data = mgroup($data, 'getOwnerPHID');
             $out = array();
             foreach ($data as $phid => $tasks) {
                 if ($phid) {
                     $out[$handles[$phid]->getFullName()] = $tasks;
                 } else {
                     $out['Unassigned'] = $tasks;
                 }
             }
             $data = $out;
             ksort($data);
             // Move "Unassigned" to the top of the list.
             if (isset($data['Unassigned'])) {
                 $data = array('Unassigned' => $out['Unassigned']) + $out;
             }
             break;
         case 'project':
             $grouped = array();
             foreach ($query->getGroupByProjectResults() as $project => $tasks) {
                 foreach ($tasks as $task) {
                     $group = $project ? $handles[$project]->getName() : 'No Project';
                     $grouped[$group][$task->getID()] = $task;
                 }
             }
             $data = $grouped;
             ksort($data);
             // Move "No Project" to the end of the list.
             if (isset($data['No Project'])) {
                 $noproject = $data['No Project'];
                 unset($data['No Project']);
                 $data += array('No Project' => $noproject);
             }
             break;
         default:
             $data = array('Tasks' => $data);
             break;
     }
     return array($data, $handles, $total_row_count);
 }
 private function loadTasks(array $user_phids, array $project_phids, array $task_ids, array $dict)
 {
     $query = new ManiphestTaskQuery();
     $query->withProjects($project_phids);
     $query->withTaskIDs($task_ids);
     $status = $dict['status'];
     if (!empty($status['open']) && !empty($status['closed'])) {
         $query->withStatus(ManiphestTaskQuery::STATUS_ANY);
     } else {
         if (!empty($status['open'])) {
             $query->withStatus(ManiphestTaskQuery::STATUS_OPEN);
         } else {
             $query->withStatus(ManiphestTaskQuery::STATUS_CLOSED);
         }
     }
     switch ($this->view) {
         case 'action':
             $query->withOwners($user_phids);
             break;
         case 'created':
             $query->withAuthors($user_phids);
             break;
         case 'subscribed':
             $query->withSubscribers($user_phids);
             break;
         case 'triage':
             $query->withOwners($user_phids);
             $query->withPriority(ManiphestTaskPriority::PRIORITY_TRIAGE);
             break;
         case 'alltriage':
             $query->withPriority(ManiphestTaskPriority::PRIORITY_TRIAGE);
             break;
         case 'all':
             break;
     }
     $order_map = array('priority' => ManiphestTaskQuery::ORDER_PRIORITY, 'created' => ManiphestTaskQuery::ORDER_CREATED);
     $query->setOrderBy(idx($order_map, $dict['order'], ManiphestTaskQuery::ORDER_MODIFIED));
     $group_map = array('priority' => ManiphestTaskQuery::GROUP_PRIORITY, 'owner' => ManiphestTaskQuery::GROUP_OWNER, 'status' => ManiphestTaskQuery::GROUP_STATUS);
     $query->setGroupBy(idx($group_map, $dict['group'], ManiphestTaskQuery::GROUP_NONE));
     $query->setCalculateRows(true);
     $query->setLimit($dict['limit']);
     $query->setOffset($dict['offset']);
     $data = $query->execute();
     $total_row_count = $query->getRowCount();
     $handle_phids = mpull($data, 'getOwnerPHID');
     $handle_phids = array_merge($handle_phids, $project_phids, $user_phids);
     $handles = id(new PhabricatorObjectHandleData($handle_phids))->loadHandles();
     switch ($dict['group']) {
         case 'priority':
             $data = mgroup($data, 'getPriority');
             krsort($data);
             // If we have invalid priorities, they'll all map to "???". Merge
             // arrays to prevent them from overwriting each other.
             $out = array();
             foreach ($data as $pri => $tasks) {
                 $out[ManiphestTaskPriority::getTaskPriorityName($pri)][] = $tasks;
             }
             foreach ($out as $pri => $tasks) {
                 $out[$pri] = array_mergev($tasks);
             }
             $data = $out;
             break;
         case 'status':
             $data = mgroup($data, 'getStatus');
             ksort($data);
             $out = array();
             foreach ($data as $status => $tasks) {
                 $out[ManiphestTaskStatus::getTaskStatusFullName($status)] = $tasks;
             }
             $data = $out;
             break;
         case 'owner':
             $data = mgroup($data, 'getOwnerPHID');
             $out = array();
             foreach ($data as $phid => $tasks) {
                 if ($phid) {
                     $out[$handles[$phid]->getFullName()] = $tasks;
                 } else {
                     $out['Unassigned'] = $tasks;
                 }
             }
             if (isset($out['Unassigned'])) {
                 // If any tasks are unassigned, move them to the front of the list.
                 $data = array('Unassigned' => $out['Unassigned']) + $out;
             } else {
                 $data = $out;
             }
             ksort($data);
             break;
         default:
             $data = array('Tasks' => $data);
             break;
     }
     return array($data, $handles, $total_row_count);
 }
 private function buildTasksPanel()
 {
     $user = $this->getRequest()->getUser();
     $user_phid = $user->getPHID();
     $task_query = new ManiphestTaskQuery();
     $task_query->withStatus(ManiphestTaskQuery::STATUS_OPEN);
     $task_query->setGroupBy(ManiphestTaskQuery::GROUP_PRIORITY);
     $task_query->withOwners(array($user_phid));
     $task_query->setLimit(10);
     $tasks = $task_query->execute();
     if (!$tasks) {
         return $this->renderMiniPanel('No Assigned Tasks', 'You have no assigned tasks.');
     }
     $panel = new AphrontPanelView();
     $panel->setHeader('Assigned Tasks');
     $panel->setCaption('Tasks assigned to you.');
     $panel->addButton(phutil_render_tag('a', array('href' => '/maniphest/', 'class' => 'button grey'), "View Active Tasks »"));
     $panel->appendChild($this->buildTaskListView($tasks));
     return $panel;
 }