@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhortuneCartQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
5
6 private $ids;
7 private $phids;
8 private $accountPHIDs;
9 private $merchantPHIDs;
10 private $subscriptionPHIDs;
11 private $statuses;
12 private $invoices;
13
14 private $needPurchases;
15
16 public function withIDs(array $ids) {
17 $this->ids = $ids;
18 return $this;
19 }
20
21 public function withPHIDs(array $phids) {
22 $this->phids = $phids;
23 return $this;
24 }
25
26 public function withAccountPHIDs(array $account_phids) {
27 $this->accountPHIDs = $account_phids;
28 return $this;
29 }
30
31 public function withMerchantPHIDs(array $merchant_phids) {
32 $this->merchantPHIDs = $merchant_phids;
33 return $this;
34 }
35
36 public function withSubscriptionPHIDs(array $subscription_phids) {
37 $this->subscriptionPHIDs = $subscription_phids;
38 return $this;
39 }
40
41 public function withStatuses(array $statuses) {
42 $this->statuses = $statuses;
43 return $this;
44 }
45
46
47 /**
48 * Include or exclude carts which represent invoices with payments due.
49 *
50 * @param bool `true` to select invoices; `false` to exclude invoices.
51 * @return $this
52 */
53 public function withInvoices($invoices) {
54 $this->invoices = $invoices;
55 return $this;
56 }
57
58 public function needPurchases($need_purchases) {
59 $this->needPurchases = $need_purchases;
60 return $this;
61 }
62
63 protected function loadPage() {
64 $table = new PhortuneCart();
65 $conn = $table->establishConnection('r');
66
67 $rows = queryfx_all(
68 $conn,
69 'SELECT cart.* FROM %T cart %Q %Q %Q',
70 $table->getTableName(),
71 $this->buildWhereClause($conn),
72 $this->buildOrderClause($conn),
73 $this->buildLimitClause($conn));
74
75 return $table->loadAllFromArray($rows);
76 }
77
78 protected function willFilterPage(array $carts) {
79 $accounts = id(new PhortuneAccountQuery())
80 ->setViewer($this->getViewer())
81 ->withPHIDs(mpull($carts, 'getAccountPHID'))
82 ->execute();
83 $accounts = mpull($accounts, null, 'getPHID');
84
85 foreach ($carts as $key => $cart) {
86 $account = idx($accounts, $cart->getAccountPHID());
87 if (!$account) {
88 unset($carts[$key]);
89 continue;
90 }
91 $cart->attachAccount($account);
92 }
93
94 if (!$carts) {
95 return array();
96 }
97
98 $merchants = id(new PhortuneMerchantQuery())
99 ->setViewer($this->getViewer())
100 ->withPHIDs(mpull($carts, 'getMerchantPHID'))
101 ->execute();
102 $merchants = mpull($merchants, null, 'getPHID');
103
104 foreach ($carts as $key => $cart) {
105 $merchant = idx($merchants, $cart->getMerchantPHID());
106 if (!$merchant) {
107 unset($carts[$key]);
108 continue;
109 }
110 $cart->attachMerchant($merchant);
111 }
112
113 if (!$carts) {
114 return array();
115 }
116
117 $implementations = array();
118
119 $cart_map = mgroup($carts, 'getCartClass');
120 foreach ($cart_map as $class => $class_carts) {
121 $implementations += newv($class, array())->loadImplementationsForCarts(
122 $this->getViewer(),
123 $class_carts);
124 }
125
126 foreach ($carts as $key => $cart) {
127 $implementation = idx($implementations, $key);
128 if (!$implementation) {
129 unset($carts[$key]);
130 continue;
131 }
132 $cart->attachImplementation($implementation);
133 }
134
135 return $carts;
136 }
137
138 protected function didFilterPage(array $carts) {
139 if ($this->needPurchases) {
140 $purchases = id(new PhortunePurchaseQuery())
141 ->setViewer($this->getViewer())
142 ->setParentQuery($this)
143 ->withCartPHIDs(mpull($carts, 'getPHID'))
144 ->execute();
145
146 $purchases = mgroup($purchases, 'getCartPHID');
147 foreach ($carts as $cart) {
148 $cart->attachPurchases(idx($purchases, $cart->getPHID(), array()));
149 }
150 }
151
152 return $carts;
153 }
154
155 protected function buildWhereClause(AphrontDatabaseConnection $conn) {
156 $where = array();
157
158 $where[] = $this->buildPagingClause($conn);
159
160 if ($this->ids !== null) {
161 $where[] = qsprintf(
162 $conn,
163 'cart.id IN (%Ld)',
164 $this->ids);
165 }
166
167 if ($this->phids !== null) {
168 $where[] = qsprintf(
169 $conn,
170 'cart.phid IN (%Ls)',
171 $this->phids);
172 }
173
174 if ($this->accountPHIDs !== null) {
175 $where[] = qsprintf(
176 $conn,
177 'cart.accountPHID IN (%Ls)',
178 $this->accountPHIDs);
179 }
180
181 if ($this->merchantPHIDs !== null) {
182 $where[] = qsprintf(
183 $conn,
184 'cart.merchantPHID IN (%Ls)',
185 $this->merchantPHIDs);
186 }
187
188 if ($this->subscriptionPHIDs !== null) {
189 $where[] = qsprintf(
190 $conn,
191 'cart.subscriptionPHID IN (%Ls)',
192 $this->subscriptionPHIDs);
193 }
194
195 if ($this->statuses !== null) {
196 $where[] = qsprintf(
197 $conn,
198 'cart.status IN (%Ls)',
199 $this->statuses);
200 }
201
202 if ($this->invoices !== null) {
203 if ($this->invoices) {
204 $where[] = qsprintf(
205 $conn,
206 'cart.status = %s AND cart.isInvoice = 1',
207 PhortuneCart::STATUS_READY);
208 } else {
209 $where[] = qsprintf(
210 $conn,
211 'cart.status != %s OR cart.isInvoice = 0',
212 PhortuneCart::STATUS_READY);
213 }
214 }
215
216 return $this->formatWhereClause($conn, $where);
217 }
218
219 public function getQueryApplicationClass() {
220 return PhabricatorPhortuneApplication::class;
221 }
222
223}