[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/fund/controller/ -> FundInitiativeBackController.php (source)

   1  <?php
   2  
   3  final class FundInitiativeBackController
   4    extends FundController {
   5  
   6    private $id;
   7  
   8    public function willProcessRequest(array $data) {
   9      $this->id = $data['id'];
  10    }
  11  
  12    public function processRequest() {
  13      $request = $this->getRequest();
  14      $viewer = $request->getUser();
  15  
  16      $initiative = id(new FundInitiativeQuery())
  17        ->setViewer($viewer)
  18        ->withIDs(array($this->id))
  19        ->executeOne();
  20      if (!$initiative) {
  21        return new Aphront404Response();
  22      }
  23  
  24      $merchant = id(new PhortuneMerchantQuery())
  25        ->setViewer($viewer)
  26        ->withPHIDs(array($initiative->getMerchantPHID()))
  27        ->executeOne();
  28      if (!$merchant) {
  29        return new Aphront404Response();
  30      }
  31  
  32      $initiative_uri = '/'.$initiative->getMonogram();
  33  
  34      if ($initiative->isClosed()) {
  35        return $this->newDialog()
  36          ->setTitle(pht('Initiative Closed'))
  37          ->appendParagraph(
  38            pht('You can not back a closed initiative.'))
  39          ->addCancelButton($initiative_uri);
  40      }
  41  
  42      $accounts = PhortuneAccountQuery::loadAccountsForUser(
  43        $viewer,
  44        PhabricatorContentSource::newFromRequest($request));
  45  
  46      $v_amount = null;
  47      $e_amount = true;
  48  
  49      $v_account = head($accounts)->getPHID();
  50  
  51      $errors = array();
  52      if ($request->isFormPost()) {
  53        $v_amount = $request->getStr('amount');
  54        $v_account = $request->getStr('accountPHID');
  55  
  56        if (empty($accounts[$v_account])) {
  57          $errors[] = pht('You must specify an account.');
  58        } else {
  59          $account = $accounts[$v_account];
  60        }
  61  
  62        if (!strlen($v_amount)) {
  63          $errors[] = pht(
  64            'You must specify how much money you want to contribute to the '.
  65            'initiative.');
  66          $e_amount = pht('Required');
  67        } else {
  68          try {
  69            $currency = PhortuneCurrency::newFromUserInput(
  70              $viewer,
  71              $v_amount);
  72            $currency->assertInRange('1.00 USD', null);
  73          } catch (Exception $ex) {
  74            $errors[] = $ex->getMessage();
  75            $e_amount = pht('Invalid');
  76          }
  77        }
  78  
  79        if (!$errors) {
  80          $backer = FundBacker::initializeNewBacker($viewer)
  81            ->setInitiativePHID($initiative->getPHID())
  82            ->attachInitiative($initiative)
  83            ->setAmountAsCurrency($currency)
  84            ->save();
  85  
  86          $product = id(new PhortuneProductQuery())
  87            ->setViewer($viewer)
  88            ->withClassAndRef('FundBackerProduct', $initiative->getPHID())
  89            ->executeOne();
  90  
  91          $cart_implementation = id(new FundBackerCart())
  92            ->setInitiative($initiative);
  93  
  94          $cart = $account->newCart($viewer, $cart_implementation, $merchant);
  95  
  96          $purchase = $cart->newPurchase($viewer, $product);
  97          $purchase
  98            ->setBasePriceAsCurrency($currency)
  99            ->setMetadataValue('backerPHID', $backer->getPHID())
 100            ->save();
 101  
 102          $xactions = array();
 103  
 104          $xactions[] = id(new FundBackerTransaction())
 105            ->setTransactionType(FundBackerTransaction::TYPE_STATUS)
 106            ->setNewValue(FundBacker::STATUS_IN_CART);
 107  
 108          $editor = id(new FundBackerEditor())
 109            ->setActor($viewer)
 110            ->setContentSourceFromRequest($request);
 111  
 112          $editor->applyTransactions($backer, $xactions);
 113  
 114          $cart->activateCart();
 115  
 116          return id(new AphrontRedirectResponse())
 117            ->setURI($cart->getCheckoutURI());
 118        }
 119      }
 120  
 121      $form = id(new AphrontFormView())
 122        ->setUser($viewer)
 123        ->appendChild(
 124          id(new AphrontFormSelectControl())
 125            ->setName('accountPHID')
 126            ->setLabel(pht('Account'))
 127            ->setValue($v_account)
 128            ->setOptions(mpull($accounts, 'getName', 'getPHID')))
 129        ->appendChild(
 130          id(new AphrontFormTextControl())
 131            ->setName('amount')
 132            ->setLabel(pht('Amount'))
 133            ->setValue($v_amount)
 134            ->setError($e_amount));
 135  
 136      return $this->newDialog()
 137        ->setTitle(
 138          pht('Back %s %s', $initiative->getMonogram(), $initiative->getName()))
 139        ->setErrors($errors)
 140        ->appendChild($form->buildLayoutView())
 141        ->addCancelButton($initiative_uri)
 142        ->addSubmitButton(pht('Continue'));
 143    }
 144  
 145  }


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