Remove product literal strings in "pht()", part 25
[phabricator.git] / src / applications / releeph / query / ReleephBranchQuery.php
blob97e47bdcafc8e09a8d1d88b6b07de18285255ef0
1 <?php
3 final class ReleephBranchQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $ids;
7 private $phids;
8 private $productPHIDs;
9 private $productIDs;
11 const STATUS_ALL = 'status-all';
12 const STATUS_OPEN = 'status-open';
13 private $status = self::STATUS_ALL;
15 private $needCutPointCommits;
17 public function withIDs(array $ids) {
18 $this->ids = $ids;
19 return $this;
22 public function withPHIDs(array $phids) {
23 $this->phids = $phids;
24 return $this;
27 public function needCutPointCommits($need_commits) {
28 $this->needCutPointCommits = $need_commits;
29 return $this;
32 public function withStatus($status) {
33 $this->status = $status;
34 return $this;
37 public function withProductPHIDs($product_phids) {
38 $this->productPHIDs = $product_phids;
39 return $this;
42 protected function loadPage() {
43 $table = new ReleephBranch();
44 $conn_r = $table->establishConnection('r');
46 $data = queryfx_all(
47 $conn_r,
48 'SELECT * FROM %T %Q %Q %Q',
49 $table->getTableName(),
50 $this->buildWhereClause($conn_r),
51 $this->buildOrderClause($conn_r),
52 $this->buildLimitClause($conn_r));
54 return $table->loadAllFromArray($data);
57 protected function willExecute() {
58 if ($this->productPHIDs !== null) {
59 $products = id(new ReleephProductQuery())
60 ->setViewer($this->getViewer())
61 ->withPHIDs($this->productPHIDs)
62 ->execute();
64 if (!$products) {
65 throw new PhabricatorEmptyQueryException();
68 $this->productIDs = mpull($products, 'getID');
72 protected function willFilterPage(array $branches) {
73 $project_ids = mpull($branches, 'getReleephProjectID');
75 $projects = id(new ReleephProductQuery())
76 ->withIDs($project_ids)
77 ->setViewer($this->getViewer())
78 ->execute();
80 foreach ($branches as $key => $branch) {
81 $project_id = $project_ids[$key];
82 if (isset($projects[$project_id])) {
83 $branch->attachProject($projects[$project_id]);
84 } else {
85 unset($branches[$key]);
89 if ($this->needCutPointCommits) {
90 $commit_phids = mpull($branches, 'getCutPointCommitPHID');
91 $commits = id(new DiffusionCommitQuery())
92 ->setViewer($this->getViewer())
93 ->withPHIDs($commit_phids)
94 ->execute();
95 $commits = mpull($commits, null, 'getPHID');
97 foreach ($branches as $branch) {
98 $commit = idx($commits, $branch->getCutPointCommitPHID());
99 $branch->attachCutPointCommit($commit);
103 return $branches;
106 protected function buildWhereClause(AphrontDatabaseConnection $conn) {
107 $where = array();
109 if ($this->ids !== null) {
110 $where[] = qsprintf(
111 $conn,
112 'id IN (%Ld)',
113 $this->ids);
116 if ($this->phids !== null) {
117 $where[] = qsprintf(
118 $conn,
119 'phid IN (%Ls)',
120 $this->phids);
123 if ($this->productIDs !== null) {
124 $where[] = qsprintf(
125 $conn,
126 'releephProjectID IN (%Ld)',
127 $this->productIDs);
130 $status = $this->status;
131 switch ($status) {
132 case self::STATUS_ALL:
133 break;
134 case self::STATUS_OPEN:
135 $where[] = qsprintf(
136 $conn,
137 'isActive = 1');
138 break;
139 default:
140 throw new Exception(pht("Unknown status constant '%s'!", $status));
143 $where[] = $this->buildPagingClause($conn);
145 return $this->formatWhereClause($conn, $where);
148 public function getQueryApplicationClass() {
149 return 'PhabricatorReleephApplication';