2011-12-16 17:08:18 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-01-17 16:29:35 -08:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-12-16 17:08:18 -08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
final class PhabricatorProjectQuery {
|
|
|
|
|
2012-01-17 16:29:35 -08:00
|
|
|
private $ids;
|
|
|
|
private $phids;
|
2012-08-07 11:54:24 -07:00
|
|
|
private $memberPHIDs;
|
2011-12-16 17:08:18 -08:00
|
|
|
|
2012-02-07 14:59:38 -08:00
|
|
|
private $status = 'status-any';
|
|
|
|
const STATUS_ANY = 'status-any';
|
|
|
|
const STATUS_OPEN = 'status-open';
|
|
|
|
const STATUS_CLOSED = 'status-closed';
|
|
|
|
const STATUS_ACTIVE = 'status-active';
|
|
|
|
const STATUS_ARCHIVED = 'status-archived';
|
|
|
|
|
2011-12-16 17:08:18 -08:00
|
|
|
private $limit;
|
|
|
|
private $offset;
|
|
|
|
|
2012-01-17 16:29:35 -08:00
|
|
|
private $needMembers;
|
|
|
|
|
|
|
|
public function withIDs(array $ids) {
|
|
|
|
$this->ids = $ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withPHIDs(array $phids) {
|
|
|
|
$this->phids = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-02-07 14:59:38 -08:00
|
|
|
public function withStatus($status) {
|
|
|
|
$this->status = $status;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-12-16 17:08:18 -08:00
|
|
|
public function setLimit($limit) {
|
|
|
|
$this->limit = $limit;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setOffset($offset) {
|
|
|
|
$this->offset = $offset;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-08-07 11:54:24 -07:00
|
|
|
public function withMemberPHIDs(array $member_phids) {
|
|
|
|
$this->memberPHIDs = $member_phids;
|
2011-12-16 17:08:18 -08:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-01-17 16:29:35 -08:00
|
|
|
public function needMembers($need_members) {
|
|
|
|
$this->needMembers = $need_members;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-12-16 17:08:18 -08:00
|
|
|
public function execute() {
|
|
|
|
$table = id(new PhabricatorProject());
|
|
|
|
$conn_r = $table->establishConnection('r');
|
|
|
|
|
2012-01-17 16:29:35 -08:00
|
|
|
$where = $this->buildWhereClause($conn_r);
|
2011-12-16 17:08:18 -08:00
|
|
|
$joins = $this->buildJoinsClause($conn_r);
|
|
|
|
|
2012-01-17 16:29:35 -08:00
|
|
|
$limit = '';
|
2011-12-16 17:08:18 -08:00
|
|
|
if ($this->limit) {
|
|
|
|
$limit = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'LIMIT %d, %d',
|
|
|
|
$this->offset,
|
|
|
|
$this->limit);
|
|
|
|
} else if ($this->offset) {
|
|
|
|
$limit = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'LIMIT %d, %d',
|
|
|
|
$this->offset,
|
|
|
|
PHP_INT_MAX);
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:56:26 -08:00
|
|
|
$order = 'ORDER BY name';
|
|
|
|
|
2011-12-16 17:08:18 -08:00
|
|
|
$data = queryfx_all(
|
|
|
|
$conn_r,
|
2012-01-17 16:29:35 -08:00
|
|
|
'SELECT p.* FROM %T p %Q %Q %Q %Q',
|
2011-12-16 17:08:18 -08:00
|
|
|
$table->getTableName(),
|
|
|
|
$joins,
|
2012-01-17 16:29:35 -08:00
|
|
|
$where,
|
2011-12-16 18:56:26 -08:00
|
|
|
$order,
|
2011-12-16 17:08:18 -08:00
|
|
|
$limit);
|
|
|
|
|
2012-01-17 16:29:35 -08:00
|
|
|
$projects = $table->loadAllFromArray($data);
|
|
|
|
|
|
|
|
if ($projects && $this->needMembers) {
|
|
|
|
$members = PhabricatorProjectAffiliation::loadAllForProjectPHIDs(
|
|
|
|
mpull($projects, 'getPHID'));
|
|
|
|
foreach ($projects as $project) {
|
|
|
|
$project->attachAffiliations(
|
|
|
|
array_values(idx($members, $project->getPHID(), array())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $projects;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildWhereClause($conn_r) {
|
|
|
|
$where = array();
|
|
|
|
|
2012-02-07 14:59:38 -08:00
|
|
|
if ($this->status != self::STATUS_ANY) {
|
|
|
|
switch ($this->status) {
|
|
|
|
case self::STATUS_OPEN:
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'status IN (%Ld)',
|
|
|
|
array(
|
|
|
|
PhabricatorProjectStatus::STATUS_ACTIVE,
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case self::STATUS_CLOSED:
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'status IN (%Ld)',
|
|
|
|
array(
|
|
|
|
PhabricatorProjectStatus::STATUS_ARCHIVED,
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case self::STATUS_ACTIVE:
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'status = %d',
|
|
|
|
PhabricatorProjectStatus::STATUS_ACTIVE);
|
|
|
|
break;
|
|
|
|
case self::STATUS_ARCHIVED:
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'status = %d',
|
|
|
|
PhabricatorProjectStatus::STATUS_ARCHIVED);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception(
|
|
|
|
"Unknown project status '{$this->status}'!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 16:29:35 -08:00
|
|
|
if ($this->ids) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'id IN (%Ld)',
|
|
|
|
$this->ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->phids) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'phid IN (%Ls)',
|
|
|
|
$this->phids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($where) {
|
|
|
|
$where = 'WHERE ('.implode(') AND (', $where).')';
|
|
|
|
} else {
|
|
|
|
$where = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $where;
|
2011-12-16 17:08:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private function buildJoinsClause($conn_r) {
|
|
|
|
$affil_table = new PhabricatorProjectAffiliation();
|
|
|
|
|
|
|
|
$joins = array();
|
2012-08-07 11:54:24 -07:00
|
|
|
if ($this->memberPHIDs) {
|
2011-12-16 17:08:18 -08:00
|
|
|
$joins[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'JOIN %T member ON member.projectPHID = p.phid
|
|
|
|
AND member.userPHID in (%Ls)',
|
|
|
|
$affil_table->getTableName(),
|
2012-08-07 11:54:24 -07:00
|
|
|
$this->memberPHIDs);
|
2011-12-16 17:08:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return implode(' ', $joins);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|