@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 upstream/main 155 lines 3.9 kB view raw
1<?php 2 3final class PhortuneSubscriptionSearchEngine 4 extends PhabricatorApplicationSearchEngine { 5 6 private $merchant; 7 private $account; 8 9 public function canUseInPanelContext() { 10 // These only make sense in an account or merchant context. 11 return false; 12 } 13 14 public function setAccount(PhortuneAccount $account) { 15 $this->account = $account; 16 return $this; 17 } 18 19 public function getAccount() { 20 return $this->account; 21 } 22 23 public function setMerchant(PhortuneMerchant $merchant) { 24 $this->merchant = $merchant; 25 return $this; 26 } 27 28 public function getMerchant() { 29 return $this->merchant; 30 } 31 32 public function getResultTypeDescription() { 33 return pht('Phortune Subscriptions'); 34 } 35 36 public function getApplicationClassName() { 37 return PhabricatorPhortuneApplication::class; 38 } 39 40 public function buildSavedQueryFromRequest(AphrontRequest $request) { 41 $saved = new PhabricatorSavedQuery(); 42 43 return $saved; 44 } 45 46 public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { 47 $query = new PhortuneSubscriptionQuery(); 48 49 $viewer = $this->requireViewer(); 50 51 $merchant = $this->getMerchant(); 52 $account = $this->getAccount(); 53 if ($merchant) { 54 $can_edit = PhabricatorPolicyFilter::hasCapability( 55 $viewer, 56 $merchant, 57 PhabricatorPolicyCapability::CAN_EDIT); 58 if (!$can_edit) { 59 throw new Exception( 60 pht( 61 'You can not query subscriptions for a merchant you do not '. 62 'control.')); 63 } 64 $query->withMerchantPHIDs(array($merchant->getPHID())); 65 } else if ($account) { 66 $can_edit = PhabricatorPolicyFilter::hasCapability( 67 $viewer, 68 $account, 69 PhabricatorPolicyCapability::CAN_EDIT); 70 if (!$can_edit) { 71 throw new Exception( 72 pht( 73 'You can not query subscriptions for an account you are not '. 74 'a member of.')); 75 } 76 $query->withAccountPHIDs(array($account->getPHID())); 77 } else { 78 $accounts = id(new PhortuneAccountQuery()) 79 ->withMemberPHIDs(array($viewer->getPHID())) 80 ->execute(); 81 if ($accounts) { 82 $query->withAccountPHIDs(mpull($accounts, 'getPHID')); 83 } else { 84 throw new Exception(pht('You have no accounts!')); 85 } 86 } 87 88 return $query; 89 } 90 91 public function buildSearchForm( 92 AphrontFormView $form, 93 PhabricatorSavedQuery $saved_query) {} 94 95 protected function getURI($path) { 96 $merchant = $this->getMerchant(); 97 $account = $this->getAccount(); 98 if ($merchant) { 99 return $merchant->getSubscriptionListURI($path); 100 } else if ($account) { 101 return $account->getSubscriptionListURI($path); 102 } else { 103 return '/phortune/subscription/'.$path; 104 } 105 } 106 107 protected function getBuiltinQueryNames() { 108 $names = array( 109 'all' => pht('All Subscriptions'), 110 ); 111 112 return $names; 113 } 114 115 public function buildSavedQueryFromBuiltin($query_key) { 116 117 $query = $this->newSavedQuery(); 118 $query->setQueryKey($query_key); 119 120 switch ($query_key) { 121 case 'all': 122 return $query; 123 } 124 125 return parent::buildSavedQueryFromBuiltin($query_key); 126 } 127 128 protected function renderResultList( 129 array $subscriptions, 130 PhabricatorSavedQuery $query, 131 array $handles) { 132 assert_instances_of($subscriptions, 'PhortuneSubscription'); 133 134 $viewer = $this->requireViewer(); 135 136 $table = id(new PhortuneSubscriptionTableView()) 137 ->setUser($viewer) 138 ->setSubscriptions($subscriptions); 139 140 $merchant = $this->getMerchant(); 141 if ($merchant) { 142 $header = pht('Subscriptions for %s', $merchant->getName()); 143 $table->setIsMerchantView(true); 144 } else { 145 $header = pht('Your Subscriptions'); 146 } 147 148 $table->setNotice($header); 149 150 $result = new PhabricatorApplicationSearchResultView(); 151 $result->setTable($table); 152 153 return $result; 154 } 155}