[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhortuneMerchantEditController 4 extends PhortuneMerchantController { 5 6 private $id; 7 8 public function willProcessRequest(array $data) { 9 $this->id = idx($data, 'id'); 10 } 11 12 public function processRequest() { 13 $request = $this->getRequest(); 14 $viewer = $request->getUser(); 15 16 if ($this->id) { 17 $merchant = id(new PhortuneMerchantQuery()) 18 ->setViewer($viewer) 19 ->withIDs(array($this->id)) 20 ->requireCapabilities( 21 array( 22 PhabricatorPolicyCapability::CAN_VIEW, 23 PhabricatorPolicyCapability::CAN_EDIT, 24 )) 25 ->executeOne(); 26 if (!$merchant) { 27 return new Aphront404Response(); 28 } 29 $is_new = false; 30 } else { 31 $this->requireApplicationCapability( 32 PhortuneMerchantCapability::CAPABILITY); 33 34 $merchant = PhortuneMerchant::initializeNewMerchant($viewer); 35 $merchant->attachMemberPHIDs(array($viewer->getPHID())); 36 $is_new = true; 37 } 38 39 if ($is_new) { 40 $title = pht('Create Merchant'); 41 $button_text = pht('Create Merchant'); 42 $cancel_uri = $this->getApplicationURI('merchant/'); 43 } else { 44 $title = pht( 45 'Edit Merchant %d %s', 46 $merchant->getID(), 47 $merchant->getName()); 48 $button_text = pht('Save Changes'); 49 $cancel_uri = $this->getApplicationURI( 50 '/merchant/'.$merchant->getID().'/'); 51 } 52 53 $e_name = true; 54 $v_name = $merchant->getName(); 55 $v_desc = $merchant->getDescription(); 56 $v_members = $merchant->getMemberPHIDs(); 57 $e_members = null; 58 59 $validation_exception = null; 60 if ($request->isFormPost()) { 61 $v_name = $request->getStr('name'); 62 $v_desc = $request->getStr('desc'); 63 $v_view = $request->getStr('viewPolicy'); 64 $v_edit = $request->getStr('editPolicy'); 65 $v_members = $request->getArr('memberPHIDs'); 66 67 $type_name = PhortuneMerchantTransaction::TYPE_NAME; 68 $type_desc = PhortuneMerchantTransaction::TYPE_DESCRIPTION; 69 $type_edge = PhabricatorTransactions::TYPE_EDGE; 70 $type_view = PhabricatorTransactions::TYPE_VIEW_POLICY; 71 72 $edge_members = PhortuneMerchantHasMemberEdgeType::EDGECONST; 73 74 $xactions = array(); 75 76 $xactions[] = id(new PhortuneMerchantTransaction()) 77 ->setTransactionType($type_name) 78 ->setNewValue($v_name); 79 80 $xactions[] = id(new PhortuneMerchantTransaction()) 81 ->setTransactionType($type_desc) 82 ->setNewValue($v_desc); 83 84 $xactions[] = id(new PhortuneMerchantTransaction()) 85 ->setTransactionType($type_view) 86 ->setNewValue($v_view); 87 88 $xactions[] = id(new PhortuneMerchantTransaction()) 89 ->setTransactionType($type_edge) 90 ->setMetadataValue('edge:type', $edge_members) 91 ->setNewValue( 92 array( 93 '=' => array_fuse($v_members), 94 )); 95 96 $editor = id(new PhortuneMerchantEditor()) 97 ->setActor($viewer) 98 ->setContentSourceFromRequest($request) 99 ->setContinueOnNoEffect(true); 100 101 try { 102 $editor->applyTransactions($merchant, $xactions); 103 104 $id = $merchant->getID(); 105 $merchant_uri = $this->getApplicationURI("merchant/{$id}/"); 106 return id(new AphrontRedirectResponse())->setURI($merchant_uri); 107 } catch (PhabricatorApplicationTransactionValidationException $ex) { 108 $validation_exception = $ex; 109 110 $e_name = $ex->getShortMessage($type_name); 111 $e_mbmers = $ex->getShortMessage($type_edge); 112 113 $merchant->setViewPolicy($v_view); 114 } 115 } 116 117 $policies = id(new PhabricatorPolicyQuery()) 118 ->setViewer($viewer) 119 ->setObject($merchant) 120 ->execute(); 121 122 $member_handles = $this->loadViewerHandles($v_members); 123 124 $form = id(new AphrontFormView()) 125 ->setUser($viewer) 126 ->appendChild( 127 id(new AphrontFormTextControl()) 128 ->setName('name') 129 ->setLabel(pht('Name')) 130 ->setValue($v_name) 131 ->setError($e_name)) 132 ->appendChild( 133 id(new PhabricatorRemarkupControl()) 134 ->setUser($viewer) 135 ->setName('desc') 136 ->setLabel(pht('Description')) 137 ->setValue($v_desc)) 138 ->appendChild( 139 id(new AphrontFormTokenizerControl()) 140 ->setDatasource(new PhabricatorPeopleDatasource()) 141 ->setLabel(pht('Members')) 142 ->setName('memberPHIDs') 143 ->setValue($member_handles) 144 ->setError($e_members)) 145 ->appendChild( 146 id(new AphrontFormPolicyControl()) 147 ->setName('viewPolicy') 148 ->setPolicyObject($merchant) 149 ->setCapability(PhabricatorPolicyCapability::CAN_VIEW) 150 ->setPolicies($policies)) 151 ->appendChild( 152 id(new AphrontFormSubmitControl()) 153 ->setValue($button_text) 154 ->addCancelButton($cancel_uri)); 155 156 $crumbs = $this->buildApplicationCrumbs(); 157 if ($is_new) { 158 $crumbs->addTextCrumb(pht('Create Merchant')); 159 } else { 160 $crumbs->addTextCrumb( 161 pht('Merchant %d', $merchant->getID()), 162 $this->getApplicationURI('/merchant/'.$merchant->getID().'/')); 163 $crumbs->addTextCrumb(pht('Edit')); 164 } 165 166 $box = id(new PHUIObjectBoxView()) 167 ->setValidationException($validation_exception) 168 ->setHeaderText($title) 169 ->appendChild($form); 170 171 return $this->buildApplicationPage( 172 array( 173 $crumbs, 174 $box, 175 ), 176 array( 177 'title' => $title, 178 )); 179 } 180 181 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |