[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhortuneCartQuery 4 extends PhabricatorCursorPagedPolicyAwareQuery { 5 6 private $ids; 7 private $phids; 8 private $accountPHIDs; 9 private $merchantPHIDs; 10 private $statuses; 11 12 private $needPurchases; 13 14 public function withIDs(array $ids) { 15 $this->ids = $ids; 16 return $this; 17 } 18 19 public function withPHIDs(array $phids) { 20 $this->phids = $phids; 21 return $this; 22 } 23 24 public function withAccountPHIDs(array $account_phids) { 25 $this->accountPHIDs = $account_phids; 26 return $this; 27 } 28 29 public function withMerchantPHIDs(array $merchant_phids) { 30 $this->merchantPHIDs = $merchant_phids; 31 return $this; 32 } 33 34 public function withStatuses(array $statuses) { 35 $this->statuses = $statuses; 36 return $this; 37 } 38 39 public function needPurchases($need_purchases) { 40 $this->needPurchases = $need_purchases; 41 return $this; 42 } 43 44 protected function loadPage() { 45 $table = new PhortuneCart(); 46 $conn = $table->establishConnection('r'); 47 48 $rows = queryfx_all( 49 $conn, 50 'SELECT cart.* FROM %T cart %Q %Q %Q', 51 $table->getTableName(), 52 $this->buildWhereClause($conn), 53 $this->buildOrderClause($conn), 54 $this->buildLimitClause($conn)); 55 56 return $table->loadAllFromArray($rows); 57 } 58 59 protected function willFilterPage(array $carts) { 60 $accounts = id(new PhortuneAccountQuery()) 61 ->setViewer($this->getViewer()) 62 ->withPHIDs(mpull($carts, 'getAccountPHID')) 63 ->execute(); 64 $accounts = mpull($accounts, null, 'getPHID'); 65 66 foreach ($carts as $key => $cart) { 67 $account = idx($accounts, $cart->getAccountPHID()); 68 if (!$account) { 69 unset($carts[$key]); 70 continue; 71 } 72 $cart->attachAccount($account); 73 } 74 75 $merchants = id(new PhortuneMerchantQuery()) 76 ->setViewer($this->getViewer()) 77 ->withPHIDs(mpull($carts, 'getMerchantPHID')) 78 ->execute(); 79 $merchants = mpull($merchants, null, 'getPHID'); 80 81 foreach ($carts as $key => $cart) { 82 $merchant = idx($merchants, $cart->getMerchantPHID()); 83 if (!$merchant) { 84 unset($carts[$key]); 85 continue; 86 } 87 $cart->attachMerchant($merchant); 88 } 89 90 $implementations = array(); 91 92 $cart_map = mgroup($carts, 'getCartClass'); 93 foreach ($cart_map as $class => $class_carts) { 94 $implementations += newv($class, array())->loadImplementationsForCarts( 95 $this->getViewer(), 96 $class_carts); 97 } 98 99 foreach ($carts as $key => $cart) { 100 $implementation = idx($implementations, $key); 101 if (!$implementation) { 102 unset($carts[$key]); 103 continue; 104 } 105 $cart->attachImplementation($implementation); 106 } 107 108 return $carts; 109 } 110 111 protected function didFilterPage(array $carts) { 112 if ($this->needPurchases) { 113 $purchases = id(new PhortunePurchaseQuery()) 114 ->setViewer($this->getViewer()) 115 ->setParentQuery($this) 116 ->withCartPHIDs(mpull($carts, 'getPHID')) 117 ->execute(); 118 119 $purchases = mgroup($purchases, 'getCartPHID'); 120 foreach ($carts as $cart) { 121 $cart->attachPurchases(idx($purchases, $cart->getPHID(), array())); 122 } 123 } 124 125 return $carts; 126 } 127 128 private function buildWhereClause(AphrontDatabaseConnection $conn) { 129 $where = array(); 130 131 $where[] = $this->buildPagingClause($conn); 132 133 if ($this->ids !== null) { 134 $where[] = qsprintf( 135 $conn, 136 'cart.id IN (%Ld)', 137 $this->ids); 138 } 139 140 if ($this->phids !== null) { 141 $where[] = qsprintf( 142 $conn, 143 'cart.phid IN (%Ls)', 144 $this->phids); 145 } 146 147 if ($this->accountPHIDs !== null) { 148 $where[] = qsprintf( 149 $conn, 150 'cart.accountPHID IN (%Ls)', 151 $this->accountPHIDs); 152 } 153 154 if ($this->merchantPHIDs !== null) { 155 $where[] = qsprintf( 156 $conn, 157 'cart.merchantPHID IN (%Ls)', 158 $this->merchantPHIDs); 159 } 160 161 if ($this->statuses !== null) { 162 $where[] = qsprintf( 163 $conn, 164 'cart.status IN (%Ls)', 165 $this->statuses); 166 } 167 168 return $this->formatWhereClause($where); 169 } 170 171 public function getQueryApplicationClass() { 172 return 'PhabricatorPhortuneApplication'; 173 } 174 175 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |