[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phortune/query/ -> PhortuneCartSearchEngine.php (source)

   1  <?php
   2  
   3  final class PhortuneCartSearchEngine
   4    extends PhabricatorApplicationSearchEngine {
   5  
   6    private $merchant;
   7    private $account;
   8  
   9    public function setAccount(PhortuneAccount $account) {
  10      $this->account = $account;
  11      return $this;
  12    }
  13  
  14    public function getAccount() {
  15      return $this->account;
  16    }
  17  
  18    public function setMerchant(PhortuneMerchant $merchant) {
  19      $this->merchant = $merchant;
  20      return $this;
  21    }
  22  
  23    public function getMerchant() {
  24      return $this->merchant;
  25    }
  26  
  27    public function getResultTypeDescription() {
  28      return pht('Phortune Orders');
  29    }
  30  
  31    public function buildSavedQueryFromRequest(AphrontRequest $request) {
  32      $saved = new PhabricatorSavedQuery();
  33  
  34      return $saved;
  35    }
  36  
  37    public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) {
  38      $query = id(new PhortuneCartQuery())
  39        ->needPurchases(true)
  40        ->withStatuses(
  41          array(
  42            PhortuneCart::STATUS_PURCHASING,
  43            PhortuneCart::STATUS_CHARGED,
  44            PhortuneCart::STATUS_HOLD,
  45            PhortuneCart::STATUS_REVIEW,
  46            PhortuneCart::STATUS_PURCHASED,
  47          ));
  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('You can not query orders for a merchant you do not control.'));
  61        }
  62        $query->withMerchantPHIDs(array($merchant->getPHID()));
  63      } else if ($account) {
  64        $can_edit = PhabricatorPolicyFilter::hasCapability(
  65          $viewer,
  66          $account,
  67          PhabricatorPolicyCapability::CAN_EDIT);
  68        if (!$can_edit) {
  69          throw new Exception(
  70            pht(
  71              'You can not query orders for an account you are not '.
  72              'a member of.'));
  73        }
  74        $query->withAccountPHIDs(array($account->getPHID()));
  75      } else {
  76        $accounts = id(new PhortuneAccountQuery())
  77          ->withMemberPHIDs(array($viewer->getPHID()))
  78          ->execute();
  79        if ($accounts) {
  80          $query->withAccountPHIDs(mpull($accounts, 'getPHID'));
  81        } else {
  82          throw new Exception(pht('You have no accounts!'));
  83        }
  84      }
  85  
  86      return $query;
  87    }
  88  
  89    public function buildSearchForm(
  90      AphrontFormView $form,
  91      PhabricatorSavedQuery $saved_query) {}
  92  
  93    protected function getURI($path) {
  94      $merchant = $this->getMerchant();
  95      $account = $this->getAccount();
  96      if ($merchant) {
  97        return '/phortune/merchant/'.$merchant->getID().'/order/'.$path;
  98      } else if ($account) {
  99        return '/phortune/'.$account->getID().'/order/';
 100      } else {
 101        return '/phortune/order/'.$path;
 102      }
 103    }
 104  
 105    public function getBuiltinQueryNames() {
 106      $names = array(
 107        'all' => pht('All Orders'),
 108      );
 109  
 110      return $names;
 111    }
 112  
 113    public function buildSavedQueryFromBuiltin($query_key) {
 114  
 115      $query = $this->newSavedQuery();
 116      $query->setQueryKey($query_key);
 117  
 118      switch ($query_key) {
 119        case 'all':
 120          return $query;
 121      }
 122  
 123      return parent::buildSavedQueryFromBuiltin($query_key);
 124    }
 125  
 126    protected function getRequiredHandlePHIDsForResultList(
 127      array $carts,
 128      PhabricatorSavedQuery $query) {
 129      $phids = array();
 130      foreach ($carts as $cart) {
 131        $phids[] = $cart->getPHID();
 132        $phids[] = $cart->getMerchantPHID();
 133        $phids[] = $cart->getAuthorPHID();
 134      }
 135      return $phids;
 136    }
 137  
 138    protected function renderResultList(
 139      array $carts,
 140      PhabricatorSavedQuery $query,
 141      array $handles) {
 142      assert_instances_of($carts, 'PhortuneCart');
 143  
 144      $viewer = $this->requireViewer();
 145  
 146      $rows = array();
 147      foreach ($carts as $cart) {
 148        $merchant = $cart->getMerchant();
 149  
 150        $rows[] = array(
 151          $cart->getID(),
 152          $handles[$cart->getPHID()]->renderLink(),
 153          $handles[$merchant->getPHID()]->renderLink(),
 154          $handles[$cart->getAuthorPHID()]->renderLink(),
 155          $cart->getTotalPriceAsCurrency()->formatForDisplay(),
 156          PhortuneCart::getNameForStatus($cart->getStatus()),
 157          phabricator_datetime($cart->getDateModified(), $viewer),
 158        );
 159      }
 160  
 161      $table = id(new AphrontTableView($rows))
 162        ->setNoDataString(pht('No orders match the query.'))
 163        ->setHeaders(
 164          array(
 165            pht('ID'),
 166            pht('Order'),
 167            pht('Merchant'),
 168            pht('Authorized By'),
 169            pht('Amount'),
 170            pht('Status'),
 171            pht('Updated'),
 172          ))
 173        ->setColumnClasses(
 174          array(
 175            '',
 176            'pri',
 177            '',
 178            '',
 179            'wide right',
 180            '',
 181            'right',
 182          ));
 183  
 184      $merchant = $this->getMerchant();
 185      if ($merchant) {
 186        $header = pht('Orders for %s', $merchant->getName());
 187      } else {
 188        $header = pht('Your Orders');
 189      }
 190  
 191      return id(new PHUIObjectBoxView())
 192        ->setHeaderText($header)
 193        ->appendChild($table);
 194    }
 195  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1