[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phortune/controller/ -> PhortuneAccountViewController.php (source)

   1  <?php
   2  
   3  final class PhortuneAccountViewController extends PhortuneController {
   4  
   5    private $accountID;
   6  
   7    public function willProcessRequest(array $data) {
   8      $this->accountID = $data['accountID'];
   9    }
  10  
  11    public function processRequest() {
  12      $request = $this->getRequest();
  13      $user = $request->getUser();
  14  
  15      // TODO: Currently, you must be able to edit an account to view the detail
  16      // page, because the account must be broadly visible so merchants can
  17      // process orders but merchants should not be able to see all the details
  18      // of an account. Ideally this page should be visible to merchants, too,
  19      // just with less information.
  20      $can_edit = true;
  21  
  22      $account = id(new PhortuneAccountQuery())
  23        ->setViewer($user)
  24        ->withIDs(array($this->accountID))
  25        ->requireCapabilities(
  26          array(
  27            PhabricatorPolicyCapability::CAN_VIEW,
  28            PhabricatorPolicyCapability::CAN_EDIT,
  29          ))
  30        ->executeOne();
  31      if (!$account) {
  32        return new Aphront404Response();
  33      }
  34  
  35      $title = $account->getName();
  36  
  37      $crumbs = $this->buildApplicationCrumbs();
  38      $crumbs->addTextCrumb(
  39        $account->getName(),
  40        $request->getRequestURI());
  41  
  42      $header = id(new PHUIHeaderView())
  43        ->setHeader($title);
  44  
  45      $edit_uri = $this->getApplicationURI('account/edit/'.$account->getID().'/');
  46  
  47      $actions = id(new PhabricatorActionListView())
  48        ->setUser($user)
  49        ->setObjectURI($request->getRequestURI())
  50        ->addAction(
  51          id(new PhabricatorActionView())
  52            ->setName(pht('Edit Account'))
  53            ->setIcon('fa-pencil')
  54            ->setHref($edit_uri)
  55            ->setDisabled(!$can_edit)
  56            ->setWorkflow(!$can_edit));
  57  
  58      $crumbs->setActionList($actions);
  59  
  60      $properties = id(new PHUIPropertyListView())
  61        ->setObject($account)
  62        ->setUser($user);
  63  
  64      $this->loadHandles($account->getMemberPHIDs());
  65  
  66      $properties->addProperty(
  67        pht('Members'),
  68        $this->renderHandlesForPHIDs($account->getMemberPHIDs()));
  69  
  70      $properties->setActionList($actions);
  71  
  72      $payment_methods = $this->buildPaymentMethodsSection($account);
  73      $purchase_history = $this->buildPurchaseHistorySection($account);
  74      $charge_history = $this->buildChargeHistorySection($account);
  75      $account_history = $this->buildAccountHistorySection($account);
  76  
  77      $object_box = id(new PHUIObjectBoxView())
  78        ->setHeader($header)
  79        ->addPropertyList($properties);
  80  
  81      return $this->buildApplicationPage(
  82        array(
  83          $crumbs,
  84          $object_box,
  85          $payment_methods,
  86          $purchase_history,
  87          $charge_history,
  88          $account_history,
  89        ),
  90        array(
  91          'title' => $title,
  92        ));
  93    }
  94  
  95    private function buildPaymentMethodsSection(PhortuneAccount $account) {
  96      $request = $this->getRequest();
  97      $viewer = $request->getUser();
  98  
  99      $can_edit = PhabricatorPolicyFilter::hasCapability(
 100        $viewer,
 101        $account,
 102        PhabricatorPolicyCapability::CAN_EDIT);
 103  
 104      $id = $account->getID();
 105  
 106      $header = id(new PHUIHeaderView())
 107        ->setHeader(pht('Payment Methods'));
 108  
 109      $list = id(new PHUIObjectItemListView())
 110        ->setUser($viewer)
 111        ->setNoDataString(
 112          pht('No payment methods associated with this account.'));
 113  
 114      $methods = id(new PhortunePaymentMethodQuery())
 115        ->setViewer($viewer)
 116        ->withAccountPHIDs(array($account->getPHID()))
 117        ->execute();
 118  
 119      if ($methods) {
 120        $this->loadHandles(mpull($methods, 'getAuthorPHID'));
 121      }
 122  
 123      foreach ($methods as $method) {
 124        $id = $method->getID();
 125  
 126        $item = new PHUIObjectItemView();
 127        $item->setHeader($method->getFullDisplayName());
 128  
 129        switch ($method->getStatus()) {
 130          case PhortunePaymentMethod::STATUS_ACTIVE:
 131            $item->setBarColor('green');
 132  
 133            $disable_uri = $this->getApplicationURI('card/'.$id.'/disable/');
 134            $item->addAction(
 135              id(new PHUIListItemView())
 136                ->setIcon('fa-times')
 137                ->setHref($disable_uri)
 138                ->setDisabled(!$can_edit)
 139                ->setWorkflow(true));
 140            break;
 141          case PhortunePaymentMethod::STATUS_DISABLED:
 142            $item->setDisabled(true);
 143            break;
 144        }
 145  
 146        $provider = $method->buildPaymentProvider();
 147        $item->addAttribute($provider->getPaymentMethodProviderDescription());
 148  
 149        $edit_uri = $this->getApplicationURI('card/'.$id.'/edit/');
 150  
 151        $item->addAction(
 152          id(new PHUIListItemView())
 153            ->setIcon('fa-pencil')
 154            ->setHref($edit_uri)
 155            ->setDisabled(!$can_edit)
 156            ->setWorkflow(!$can_edit));
 157  
 158        $list->addItem($item);
 159      }
 160  
 161      return id(new PHUIObjectBoxView())
 162        ->setHeader($header)
 163        ->appendChild($list);
 164    }
 165  
 166    private function buildPurchaseHistorySection(PhortuneAccount $account) {
 167      $request = $this->getRequest();
 168      $viewer = $request->getUser();
 169  
 170      $carts = id(new PhortuneCartQuery())
 171        ->setViewer($viewer)
 172        ->withAccountPHIDs(array($account->getPHID()))
 173        ->needPurchases(true)
 174        ->withStatuses(
 175          array(
 176            PhortuneCart::STATUS_PURCHASING,
 177            PhortuneCart::STATUS_CHARGED,
 178            PhortuneCart::STATUS_HOLD,
 179            PhortuneCart::STATUS_REVIEW,
 180            PhortuneCart::STATUS_PURCHASED,
 181          ))
 182        ->setLimit(10)
 183        ->execute();
 184  
 185      $phids = array();
 186      foreach ($carts as $cart) {
 187        $phids[] = $cart->getPHID();
 188        foreach ($cart->getPurchases() as $purchase) {
 189          $phids[] = $purchase->getPHID();
 190        }
 191      }
 192      $handles = $this->loadViewerHandles($phids);
 193  
 194      $orders_uri = $this->getApplicationURI($account->getID().'/order/');
 195  
 196      $table = id(new PhortuneOrderTableView())
 197        ->setUser($viewer)
 198        ->setCarts($carts)
 199        ->setHandles($handles);
 200  
 201      $header = id(new PHUIHeaderView())
 202        ->setHeader(pht('Recent Orders'))
 203        ->addActionLink(
 204          id(new PHUIButtonView())
 205            ->setTag('a')
 206            ->setIcon(
 207              id(new PHUIIconView())
 208                ->setIconFont('fa-list'))
 209            ->setHref($orders_uri)
 210            ->setText(pht('View All Orders')));
 211  
 212      return id(new PHUIObjectBoxView())
 213        ->setHeader($header)
 214        ->appendChild($table);
 215    }
 216  
 217    private function buildChargeHistorySection(PhortuneAccount $account) {
 218      $request = $this->getRequest();
 219      $viewer = $request->getUser();
 220  
 221      $charges = id(new PhortuneChargeQuery())
 222        ->setViewer($viewer)
 223        ->withAccountPHIDs(array($account->getPHID()))
 224        ->needCarts(true)
 225        ->setLimit(10)
 226        ->execute();
 227  
 228      $phids = array();
 229      foreach ($charges as $charge) {
 230        $phids[] = $charge->getProviderPHID();
 231        $phids[] = $charge->getCartPHID();
 232        $phids[] = $charge->getMerchantPHID();
 233        $phids[] = $charge->getPaymentMethodPHID();
 234      }
 235  
 236      $handles = $this->loadViewerHandles($phids);
 237  
 238      $charges_uri = $this->getApplicationURI($account->getID().'/charge/');
 239  
 240      $table = id(new PhortuneChargeTableView())
 241        ->setUser($viewer)
 242        ->setCharges($charges)
 243        ->setHandles($handles);
 244  
 245      $header = id(new PHUIHeaderView())
 246        ->setHeader(pht('Recent Charges'))
 247        ->addActionLink(
 248          id(new PHUIButtonView())
 249            ->setTag('a')
 250            ->setIcon(
 251              id(new PHUIIconView())
 252                ->setIconFont('fa-list'))
 253            ->setHref($charges_uri)
 254            ->setText(pht('View All Charges')));
 255  
 256      return id(new PHUIObjectBoxView())
 257        ->setHeader($header)
 258        ->appendChild($table);
 259    }
 260  
 261    private function buildAccountHistorySection(PhortuneAccount $account) {
 262      $request = $this->getRequest();
 263      $user = $request->getUser();
 264  
 265      $xactions = id(new PhortuneAccountTransactionQuery())
 266        ->setViewer($user)
 267        ->withObjectPHIDs(array($account->getPHID()))
 268        ->execute();
 269  
 270      $xaction_view = id(new PhabricatorApplicationTransactionView())
 271        ->setUser($user)
 272        ->setObjectPHID($account->getPHID())
 273        ->setTransactions($xactions);
 274  
 275      return $xaction_view;
 276    }
 277  
 278    protected function buildApplicationCrumbs() {
 279      $crumbs = parent::buildApplicationCrumbs();
 280  
 281      $crumbs->addAction(
 282        id(new PHUIListItemView())
 283          ->setIcon('fa-exchange')
 284          ->setHref($this->getApplicationURI('account/'))
 285          ->setName(pht('Switch Accounts')));
 286  
 287      return $crumbs;
 288    }
 289  
 290  }


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