@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
at recaptime-dev/main 134 lines 3.1 kB view raw
1<?php 2 3final class PhortuneAccountQuery 4 extends PhabricatorCursorPagedPolicyAwareQuery { 5 6 private $ids; 7 private $phids; 8 private $memberPHIDs; 9 10 public static function loadAccountsForUser( 11 PhabricatorUser $user, 12 PhabricatorContentSource $content_source) { 13 14 $accounts = id(new PhortuneAccountQuery()) 15 ->setViewer($user) 16 ->withMemberPHIDs(array($user->getPHID())) 17 ->execute(); 18 19 if (!$accounts) { 20 $accounts = array( 21 PhortuneAccount::createNewAccount($user, $content_source), 22 ); 23 } 24 25 $accounts = mpull($accounts, null, 'getPHID'); 26 27 return $accounts; 28 } 29 30 public function withIDs(array $ids) { 31 $this->ids = $ids; 32 return $this; 33 } 34 35 public function withPHIDs(array $phids) { 36 $this->phids = $phids; 37 return $this; 38 } 39 40 public function withMemberPHIDs(array $phids) { 41 $this->memberPHIDs = $phids; 42 return $this; 43 } 44 45 public function newResultObject() { 46 return new PhortuneAccount(); 47 } 48 49 protected function willFilterPage(array $accounts) { 50 $query = id(new PhabricatorEdgeQuery()) 51 ->withSourcePHIDs(mpull($accounts, 'getPHID')) 52 ->withEdgeTypes( 53 array( 54 PhortuneAccountHasMemberEdgeType::EDGECONST, 55 PhortuneAccountHasMerchantEdgeType::EDGECONST, 56 )); 57 58 $query->execute(); 59 60 foreach ($accounts as $account) { 61 $member_phids = $query->getDestinationPHIDs( 62 array( 63 $account->getPHID(), 64 ), 65 array( 66 PhortuneAccountHasMemberEdgeType::EDGECONST, 67 )); 68 $member_phids = array_reverse($member_phids); 69 $account->attachMemberPHIDs($member_phids); 70 71 $merchant_phids = $query->getDestinationPHIDs( 72 array( 73 $account->getPHID(), 74 ), 75 array( 76 PhortuneAccountHasMerchantEdgeType::EDGECONST, 77 )); 78 $merchant_phids = array_reverse($merchant_phids); 79 $account->attachMerchantPHIDs($merchant_phids); 80 } 81 82 return $accounts; 83 } 84 85 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { 86 $where = parent::buildWhereClauseParts($conn); 87 88 if ($this->ids !== null) { 89 $where[] = qsprintf( 90 $conn, 91 'a.id IN (%Ld)', 92 $this->ids); 93 } 94 95 if ($this->phids !== null) { 96 $where[] = qsprintf( 97 $conn, 98 'a.phid IN (%Ls)', 99 $this->phids); 100 } 101 102 if ($this->memberPHIDs !== null) { 103 $where[] = qsprintf( 104 $conn, 105 'm.dst IN (%Ls)', 106 $this->memberPHIDs); 107 } 108 109 return $where; 110 } 111 112 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { 113 $joins = parent::buildJoinClauseParts($conn); 114 115 if ($this->memberPHIDs !== null) { 116 $joins[] = qsprintf( 117 $conn, 118 'LEFT JOIN %T m ON a.phid = m.src AND m.type = %d', 119 PhabricatorEdgeConfig::TABLE_NAME_EDGE, 120 PhortuneAccountHasMemberEdgeType::EDGECONST); 121 } 122 123 return $joins; 124 } 125 126 public function getQueryApplicationClass() { 127 return PhabricatorPhortuneApplication::class; 128 } 129 130 protected function getPrimaryTableAlias() { 131 return 'a'; 132 } 133 134}