2014-07-23 19:36:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhortuneChargeQuery
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
|
|
|
|
|
|
|
private $ids;
|
|
|
|
private $phids;
|
|
|
|
private $accountPHIDs;
|
2014-07-23 19:36:25 +02:00
|
|
|
private $cartPHIDs;
|
2014-10-06 23:20:40 +02:00
|
|
|
private $statuses;
|
2014-07-23 19:36:12 +02:00
|
|
|
|
2014-08-18 22:15:21 +02:00
|
|
|
private $needCarts;
|
|
|
|
|
2014-07-23 19:36:12 +02:00
|
|
|
public function withIDs(array $ids) {
|
|
|
|
$this->ids = $ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withPHIDs(array $phids) {
|
|
|
|
$this->phids = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withAccountPHIDs(array $account_phids) {
|
|
|
|
$this->accountPHIDs = $account_phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-07-23 19:36:25 +02:00
|
|
|
public function withCartPHIDs(array $cart_phids) {
|
|
|
|
$this->cartPHIDs = $cart_phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-10-06 23:20:40 +02:00
|
|
|
public function withStatuses(array $statuses) {
|
|
|
|
$this->statuses = $statuses;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:15:21 +02:00
|
|
|
public function needCarts($need_carts) {
|
|
|
|
$this->needCarts = $need_carts;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-07-23 19:36:12 +02:00
|
|
|
protected function loadPage() {
|
|
|
|
$table = new PhortuneCharge();
|
|
|
|
$conn = $table->establishConnection('r');
|
|
|
|
|
|
|
|
$rows = queryfx_all(
|
|
|
|
$conn,
|
|
|
|
'SELECT charge.* FROM %T charge %Q %Q %Q',
|
|
|
|
$table->getTableName(),
|
|
|
|
$this->buildWhereClause($conn),
|
|
|
|
$this->buildOrderClause($conn),
|
|
|
|
$this->buildLimitClause($conn));
|
|
|
|
|
|
|
|
return $table->loadAllFromArray($rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function willFilterPage(array $charges) {
|
|
|
|
$accounts = id(new PhortuneAccountQuery())
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->setParentQuery($this)
|
|
|
|
->withPHIDs(mpull($charges, 'getAccountPHID'))
|
|
|
|
->execute();
|
|
|
|
$accounts = mpull($accounts, null, 'getPHID');
|
|
|
|
|
|
|
|
foreach ($charges as $key => $charge) {
|
|
|
|
$account = idx($accounts, $charge->getAccountPHID());
|
|
|
|
if (!$account) {
|
|
|
|
unset($charges[$key]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$charge->attachAccount($account);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $charges;
|
|
|
|
}
|
|
|
|
|
2014-08-18 22:15:21 +02:00
|
|
|
protected function didFilterPage(array $charges) {
|
|
|
|
if ($this->needCarts) {
|
|
|
|
$carts = id(new PhortuneCartQuery())
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->setParentQuery($this)
|
|
|
|
->withPHIDs(mpull($charges, 'getCartPHID'))
|
|
|
|
->execute();
|
|
|
|
$carts = mpull($carts, null, 'getPHID');
|
|
|
|
|
|
|
|
foreach ($charges as $charge) {
|
|
|
|
$cart = idx($carts, $charge->getCartPHID());
|
|
|
|
$charge->attachCart($cart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $charges;
|
|
|
|
}
|
|
|
|
|
Make buildWhereClause() a method of AphrontCursorPagedPolicyAwareQuery
Summary:
Ref T4100. Ref T5595.
To support a unified "Projects:" query across all applications, a future diff is going to add a set of "Edge Logic" capabilities to `PolicyAwareQuery` which write the required SELECT, JOIN, WHERE, HAVING and GROUP clauses for you.
With the addition of "Edge Logic", we'll have three systems which may need to build components of query claues: ordering/paging, customfields/applicationsearch, and edge logic.
For most clauses, queries don't currently call into the parent explicitly to get default components. I want to move more query construction logic up the class tree so it can be shared.
For most methods, this isn't a problem, but many subclasses define a `buildWhereClause()`. Make all such definitions protected and consistent.
This causes no behavioral changes.
Test Plan: Ran `arc unit --everything`, which does a pretty through job of verifying this statically.
Reviewers: btrahan
Reviewed By: btrahan
Subscribers: yelirekim, hach-que, epriestley
Maniphest Tasks: T4100, T5595
Differential Revision: https://secure.phabricator.com/D12453
2015-04-18 16:08:30 +02:00
|
|
|
protected function buildWhereClause(AphrontDatabaseConnection $conn) {
|
2014-07-23 19:36:12 +02:00
|
|
|
$where = array();
|
|
|
|
|
|
|
|
$where[] = $this->buildPagingClause($conn);
|
|
|
|
|
|
|
|
if ($this->ids !== null) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'charge.id IN (%Ld)',
|
|
|
|
$this->ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->phids !== null) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'charge.phid IN (%Ls)',
|
|
|
|
$this->phids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->accountPHIDs !== null) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'charge.accountPHID IN (%Ls)',
|
|
|
|
$this->accountPHIDs);
|
|
|
|
}
|
|
|
|
|
2014-07-23 19:36:25 +02:00
|
|
|
if ($this->cartPHIDs !== null) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'charge.cartPHID IN (%Ls)',
|
|
|
|
$this->cartPHIDs);
|
|
|
|
}
|
|
|
|
|
2014-10-06 23:20:40 +02:00
|
|
|
if ($this->statuses !== null) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'charge.status IN (%Ls)',
|
|
|
|
$this->statuses);
|
|
|
|
}
|
|
|
|
|
2014-07-23 19:36:12 +02:00
|
|
|
return $this->formatWhereClause($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getQueryApplicationClass() {
|
2014-07-24 00:14:45 +02:00
|
|
|
return 'PhabricatorPhortuneApplication';
|
2014-07-23 19:36:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|