Path: blob/master/src/applications/fund/controller/FundInitiativeBackController.php
12262 views
<?php12final class FundInitiativeBackController3extends FundController {45public function handleRequest(AphrontRequest $request) {6$viewer = $request->getViewer();7$id = $request->getURIData('id');89$initiative = id(new FundInitiativeQuery())10->setViewer($viewer)11->withIDs(array($id))12->executeOne();13if (!$initiative) {14return new Aphront404Response();15}1617$merchant = id(new PhortuneMerchantQuery())18->setViewer($viewer)19->withPHIDs(array($initiative->getMerchantPHID()))20->executeOne();21if (!$merchant) {22return new Aphront404Response();23}2425$initiative_uri = '/'.$initiative->getMonogram();2627if ($initiative->isClosed()) {28return $this->newDialog()29->setTitle(pht('Initiative Closed'))30->appendParagraph(31pht('You can not back a closed initiative.'))32->addCancelButton($initiative_uri);33}3435$accounts = PhortuneAccountQuery::loadAccountsForUser(36$viewer,37PhabricatorContentSource::newFromRequest($request));3839$v_amount = null;40$e_amount = true;4142$v_account = head($accounts)->getPHID();4344$errors = array();45if ($request->isFormPost()) {46$v_amount = $request->getStr('amount');47$v_account = $request->getStr('accountPHID');4849if (empty($accounts[$v_account])) {50$errors[] = pht('You must specify an account.');51} else {52$account = $accounts[$v_account];53}5455if (!strlen($v_amount)) {56$errors[] = pht(57'You must specify how much money you want to contribute to the '.58'initiative.');59$e_amount = pht('Required');60} else {61try {62$currency = PhortuneCurrency::newFromUserInput(63$viewer,64$v_amount);65$currency->assertInRange('1.00 USD', null);66} catch (Exception $ex) {67$errors[] = $ex->getMessage();68$e_amount = pht('Invalid');69}70}7172if (!$errors) {73$backer = FundBacker::initializeNewBacker($viewer)74->setInitiativePHID($initiative->getPHID())75->attachInitiative($initiative)76->setAmountAsCurrency($currency)77->save();7879$product = id(new PhortuneProductQuery())80->setViewer($viewer)81->withClassAndRef('FundBackerProduct', $initiative->getPHID())82->executeOne();8384$cart_implementation = id(new FundBackerCart())85->setInitiative($initiative);8687$cart = $account->newCart($viewer, $cart_implementation, $merchant);8889$purchase = $cart->newPurchase($viewer, $product);90$purchase91->setBasePriceAsCurrency($currency)92->setMetadataValue('backerPHID', $backer->getPHID())93->save();9495$xactions = array();9697$xactions[] = id(new FundBackerTransaction())98->setTransactionType(FundBackerStatusTransaction::TRANSACTIONTYPE)99->setNewValue(FundBacker::STATUS_IN_CART);100101$editor = id(new FundBackerEditor())102->setActor($viewer)103->setContentSourceFromRequest($request);104105$editor->applyTransactions($backer, $xactions);106107$cart->activateCart();108109return id(new AphrontRedirectResponse())110->setURI($cart->getCheckoutURI());111}112}113114$form = id(new AphrontFormView())115->setUser($viewer)116->appendChild(117id(new AphrontFormSelectControl())118->setName('accountPHID')119->setLabel(pht('Account'))120->setValue($v_account)121->setOptions(mpull($accounts, 'getName', 'getPHID')))122->appendChild(123id(new AphrontFormTextControl())124->setName('amount')125->setLabel(pht('Amount'))126->setValue($v_amount)127->setError($e_amount));128129return $this->newDialog()130->setTitle(131pht('Back %s %s', $initiative->getMonogram(), $initiative->getName()))132->setErrors($errors)133->appendChild($form->buildLayoutView())134->addCancelButton($initiative_uri)135->addSubmitButton(pht('Continue'));136}137138}139140141