protected function loadPage()
 {
     $task_dao = new ManiphestTask();
     $conn = $task_dao->establishConnection('r');
     $where = $this->buildWhereClause($conn);
     $group_column = '';
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $group_column = qsprintf($conn, ', projectGroupName.indexedObjectPHID projectGroupPHID');
             break;
     }
     $rows = queryfx_all($conn, '%Q %Q FROM %T task %Q %Q %Q %Q %Q %Q', $this->buildSelectClause($conn), $group_column, $task_dao->getTableName(), $this->buildJoinClause($conn), $where, $this->buildGroupClause($conn), $this->buildHavingClause($conn), $this->buildOrderClause($conn), $this->buildLimitClause($conn));
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $data = ipull($rows, null, 'id');
             break;
         default:
             $data = $rows;
             break;
     }
     $tasks = $task_dao->loadAllFromArray($data);
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $results = array();
             foreach ($rows as $row) {
                 $task = clone $tasks[$row['id']];
                 $task->attachGroupByProjectPHID($row['projectGroupPHID']);
                 $results[] = $task;
             }
             $tasks = $results;
             break;
     }
     return $tasks;
 }
 public function execute()
 {
     $task_dao = new ManiphestTask();
     $conn = $task_dao->establishConnection('r');
     if ($this->calculateRows) {
         $calc = 'SQL_CALC_FOUND_ROWS';
     } else {
         $calc = '';
     }
     $where = array();
     $where[] = $this->buildTaskIDsWhereClause($conn);
     $where[] = $this->buildStatusWhereClause($conn);
     $where[] = $this->buildPriorityWhereClause($conn);
     $where[] = $this->buildAuthorWhereClause($conn);
     $where[] = $this->buildOwnerWhereClause($conn);
     $where[] = $this->buildSubscriberWhereClause($conn);
     $where[] = $this->buildProjectWhereClause($conn);
     $where[] = $this->buildXProjectWhereClause($conn);
     $where[] = $this->buildFullTextWhereClause($conn);
     $where = array_filter($where);
     if ($where) {
         $where = 'WHERE (' . implode(') AND (', $where) . ')';
     } else {
         $where = '';
     }
     $join = array();
     $join[] = $this->buildProjectJoinClause($conn);
     $join[] = $this->buildXProjectJoinClause($conn);
     $join[] = $this->buildSubscriberJoinClause($conn);
     $join = array_filter($join);
     if ($join) {
         $join = implode(' ', $join);
     } else {
         $join = '';
     }
     $having = '';
     $count = '';
     $group = '';
     if (count($this->projectPHIDs) > 1) {
         // If we're searching for more than one project:
         //  - We'll get multiple rows for tasks when they join the project table
         //    multiple times. We use GROUP BY to make them distinct again.
         //  - We want to treat the query as an intersection query, not a union
         //    query. We sum the project count and require it be the same as the
         //    number of projects we're searching for. (If 'anyProject' is set,
         //    we do union instead.)
         $group = 'GROUP BY task.id';
         if (!$this->anyProject) {
             $count = ', COUNT(project.projectPHID) projectCount';
             $having = qsprintf($conn, 'HAVING projectCount = %d', count($this->projectPHIDs));
         }
     }
     $order = $this->buildOrderClause($conn);
     $offset = (int) nonempty($this->offset, 0);
     $limit = (int) nonempty($this->limit, self::DEFAULT_PAGE_SIZE);
     if ($this->groupBy == self::GROUP_PROJECT) {
         $limit = PHP_INT_MAX;
         $offset = 0;
     }
     $data = queryfx_all($conn, 'SELECT %Q * %Q FROM %T task %Q %Q %Q %Q %Q LIMIT %d, %d', $calc, $count, $task_dao->getTableName(), $join, $where, $group, $having, $order, $offset, $limit);
     if ($this->calculateRows) {
         $count = queryfx_one($conn, 'SELECT FOUND_ROWS() N');
         $this->rowCount = $count['N'];
     } else {
         $this->rowCount = null;
     }
     $tasks = $task_dao->loadAllFromArray($data);
     if ($this->groupBy == self::GROUP_PROJECT) {
         $tasks = $this->applyGroupByProject($tasks);
     }
     return $tasks;
 }
 public function loadPage()
 {
     // TODO: (T603) It is possible for a user to find the PHID of a project
     // they can't see, then query for tasks in that project and deduce the
     // identity of unknown/invisible projects. Before we allow the user to
     // execute a project-based PHID query, we should verify that they
     // can see the project.
     $task_dao = new ManiphestTask();
     $conn = $task_dao->establishConnection('r');
     $where = array();
     $where[] = $this->buildTaskIDsWhereClause($conn);
     $where[] = $this->buildTaskPHIDsWhereClause($conn);
     $where[] = $this->buildStatusWhereClause($conn);
     $where[] = $this->buildStatusesWhereClause($conn);
     $where[] = $this->buildPrioritiesWhereClause($conn);
     $where[] = $this->buildAuthorWhereClause($conn);
     $where[] = $this->buildOwnerWhereClause($conn);
     $where[] = $this->buildSubscriberWhereClause($conn);
     $where[] = $this->buildProjectWhereClause($conn);
     $where[] = $this->buildAnyProjectWhereClause($conn);
     $where[] = $this->buildAnyUserProjectWhereClause($conn);
     $where[] = $this->buildXProjectWhereClause($conn);
     $where[] = $this->buildFullTextWhereClause($conn);
     if ($this->dateCreatedAfter) {
         $where[] = qsprintf($conn, 'task.dateCreated >= %d', $this->dateCreatedAfter);
     }
     if ($this->dateCreatedBefore) {
         $where[] = qsprintf($conn, 'task.dateCreated <= %d', $this->dateCreatedBefore);
     }
     if ($this->dateModifiedAfter) {
         $where[] = qsprintf($conn, 'task.dateModified >= %d', $this->dateModifiedAfter);
     }
     if ($this->dateModifiedBefore) {
         $where[] = qsprintf($conn, 'task.dateModified <= %d', $this->dateModifiedBefore);
     }
     $where[] = $this->buildPagingClause($conn);
     $where = $this->formatWhereClause($where);
     $having = '';
     $count = '';
     if (count($this->projectPHIDs) > 1) {
         // We want to treat the query as an intersection query, not a union
         // query. We sum the project count and require it be the same as the
         // number of projects we're searching for.
         $count = ', COUNT(project.dst) projectCount';
         $having = qsprintf($conn, 'HAVING projectCount = %d', count($this->projectPHIDs));
     }
     $order = $this->buildCustomOrderClause($conn);
     // TODO: Clean up this nonstandardness.
     if (!$this->getLimit()) {
         $this->setLimit(self::DEFAULT_PAGE_SIZE);
     }
     $group_column = '';
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $group_column = qsprintf($conn, ', projectGroupName.indexedObjectPHID projectGroupPHID');
             break;
     }
     $rows = queryfx_all($conn, 'SELECT task.* %Q %Q FROM %T task %Q %Q %Q %Q %Q %Q', $count, $group_column, $task_dao->getTableName(), $this->buildJoinsClause($conn), $where, $this->buildGroupClause($conn), $having, $order, $this->buildLimitClause($conn));
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $data = ipull($rows, null, 'id');
             break;
         default:
             $data = $rows;
             break;
     }
     $tasks = $task_dao->loadAllFromArray($data);
     switch ($this->groupBy) {
         case self::GROUP_PROJECT:
             $results = array();
             foreach ($rows as $row) {
                 $task = clone $tasks[$row['id']];
                 $task->attachGroupByProjectPHID($row['projectGroupPHID']);
                 $results[] = $task;
             }
             $tasks = $results;
             break;
     }
     return $tasks;
 }