@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 PhortunePurchaseQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
5
6 private $ids;
7 private $phids;
8 private $cartPHIDs;
9
10 public function withIDs(array $ids) {
11 $this->ids = $ids;
12 return $this;
13 }
14
15 public function withPHIDs(array $phids) {
16 $this->phids = $phids;
17 return $this;
18 }
19
20 public function withCartPHIDs(array $cart_phids) {
21 $this->cartPHIDs = $cart_phids;
22 return $this;
23 }
24
25 protected function loadPage() {
26 $table = new PhortunePurchase();
27 $conn = $table->establishConnection('r');
28
29 $rows = queryfx_all(
30 $conn,
31 'SELECT purchase.* FROM %T purchase %Q %Q %Q',
32 $table->getTableName(),
33 $this->buildWhereClause($conn),
34 $this->buildOrderClause($conn),
35 $this->buildLimitClause($conn));
36
37 return $table->loadAllFromArray($rows);
38 }
39
40 protected function willFilterPage(array $purchases) {
41 $carts = id(new PhortuneCartQuery())
42 ->setViewer($this->getViewer())
43 ->setParentQuery($this)
44 ->withPHIDs(mpull($purchases, 'getCartPHID'))
45 ->execute();
46 $carts = mpull($carts, null, 'getPHID');
47
48 foreach ($purchases as $key => $purchase) {
49 $cart = idx($carts, $purchase->getCartPHID());
50 if (!$cart) {
51 unset($purchases[$key]);
52 continue;
53 }
54 $purchase->attachCart($cart);
55 }
56
57 $products = id(new PhortuneProductQuery())
58 ->setViewer($this->getViewer())
59 ->setParentQuery($this)
60 ->withPHIDs(mpull($purchases, 'getProductPHID'))
61 ->execute();
62 $products = mpull($products, null, 'getPHID');
63
64 foreach ($purchases as $key => $purchase) {
65 $product = idx($products, $purchase->getProductPHID());
66 if (!$product) {
67 unset($purchases[$key]);
68 continue;
69 }
70 $purchase->attachProduct($product);
71 }
72
73
74 return $purchases;
75 }
76
77 protected function buildWhereClause(AphrontDatabaseConnection $conn) {
78 $where = array();
79
80 $where[] = $this->buildPagingClause($conn);
81
82 if ($this->ids !== null) {
83 $where[] = qsprintf(
84 $conn,
85 'purchase.id IN (%Ld)',
86 $this->ids);
87 }
88
89 if ($this->phids !== null) {
90 $where[] = qsprintf(
91 $conn,
92 'purchase.phid IN (%Ls)',
93 $this->phids);
94 }
95
96 if ($this->cartPHIDs !== null) {
97 $where[] = qsprintf(
98 $conn,
99 'purchase.cartPHID IN (%Ls)',
100 $this->cartPHIDs);
101 }
102
103 return $this->formatWhereClause($conn, $where);
104 }
105
106 public function getQueryApplicationClass() {
107 return PhabricatorPhortuneApplication::class;
108 }
109
110}