2014-10-10 01:59:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhortuneAccountEditController extends PhortuneController {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = idx($data, 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$viewer = $request->getUser();
|
|
|
|
|
|
|
|
if ($this->id) {
|
|
|
|
$account = id(new PhortuneAccountQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withIDs(array($this->id))
|
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
|
|
|
->executeOne();
|
|
|
|
if (!$account) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
$is_new = false;
|
|
|
|
} else {
|
|
|
|
$account = PhortuneAccount::initializeNewAccount($viewer);
|
2014-10-11 00:00:06 +02:00
|
|
|
$account->attachMemberPHIDs(array($viewer->getPHID()));
|
2014-10-10 01:59:03 +02:00
|
|
|
$is_new = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$v_name = $account->getName();
|
|
|
|
$e_name = true;
|
2014-10-11 00:00:06 +02:00
|
|
|
|
|
|
|
$v_members = $account->getMemberPHIDs();
|
|
|
|
$e_members = null;
|
|
|
|
|
2014-10-10 01:59:03 +02:00
|
|
|
$validation_exception = null;
|
|
|
|
|
|
|
|
if ($request->isFormPost()) {
|
|
|
|
$v_name = $request->getStr('name');
|
2014-10-11 00:00:06 +02:00
|
|
|
$v_members = $request->getArr('memberPHIDs');
|
2014-10-10 01:59:03 +02:00
|
|
|
|
|
|
|
$type_name = PhortuneAccountTransaction::TYPE_NAME;
|
2014-10-11 00:00:06 +02:00
|
|
|
$type_edge = PhabricatorTransactions::TYPE_EDGE;
|
2014-10-10 01:59:03 +02:00
|
|
|
|
|
|
|
$xactions = array();
|
|
|
|
$xactions[] = id(new PhortuneAccountTransaction())
|
|
|
|
->setTransactionType($type_name)
|
|
|
|
->setNewValue($v_name);
|
|
|
|
|
2014-10-11 00:00:06 +02:00
|
|
|
$xactions[] = id(new PhortuneAccountTransaction())
|
|
|
|
->setTransactionType($type_edge)
|
|
|
|
->setMetadataValue(
|
|
|
|
'edge:type',
|
|
|
|
PhortuneAccountHasMemberEdgeType::EDGECONST)
|
|
|
|
->setNewValue(
|
|
|
|
array(
|
|
|
|
'=' => array_fuse($v_members),
|
|
|
|
));
|
2014-10-10 01:59:03 +02:00
|
|
|
|
|
|
|
$editor = id(new PhortuneAccountEditor())
|
|
|
|
->setActor($viewer)
|
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
->setContinueOnNoEffect(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$editor->applyTransactions($account, $xactions);
|
|
|
|
|
|
|
|
$account_uri = $this->getApplicationURI($account->getID().'/');
|
|
|
|
return id(new AphrontRedirectResponse())->setURI($account_uri);
|
|
|
|
} catch (PhabricatorApplicationTransactionValidationException $ex) {
|
|
|
|
$validation_exception = $ex;
|
|
|
|
$e_name = $ex->getShortMessage($type_name);
|
2014-10-11 00:00:06 +02:00
|
|
|
$e_members = $ex->getShortMessage($type_edge);
|
2014-10-10 01:59:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
|
|
|
|
|
|
|
if ($is_new) {
|
|
|
|
$cancel_uri = $this->getApplicationURI('account/');
|
|
|
|
$crumbs->addTextCrumb(pht('Accounts'), $cancel_uri);
|
|
|
|
$crumbs->addTextCrumb(pht('Create Account'));
|
|
|
|
|
|
|
|
$title = pht('Create Payment Account');
|
|
|
|
$submit_button = pht('Create Account');
|
|
|
|
} else {
|
|
|
|
$cancel_uri = $this->getApplicationURI($account->getID().'/');
|
|
|
|
$crumbs->addTextCrumb($account->getName(), $cancel_uri);
|
|
|
|
$crumbs->addTextCrumb(pht('Edit'));
|
|
|
|
|
|
|
|
$title = pht('Edit %s', $account->getName());
|
|
|
|
$submit_button = pht('Save Changes');
|
|
|
|
}
|
|
|
|
|
2014-10-11 00:00:06 +02:00
|
|
|
$member_handles = $this->loadViewerHandles($v_members);
|
|
|
|
|
2014-10-10 01:59:03 +02:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($viewer)
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setName('name')
|
|
|
|
->setLabel(pht('Name'))
|
|
|
|
->setValue($v_name)
|
|
|
|
->setError($e_name))
|
2014-10-11 00:00:06 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTokenizerControl())
|
|
|
|
->setDatasource(new PhabricatorPeopleDatasource())
|
|
|
|
->setLabel(pht('Members'))
|
|
|
|
->setName('memberPHIDs')
|
|
|
|
->setValue($member_handles)
|
|
|
|
->setError($e_members))
|
2014-10-10 01:59:03 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
|
|
|
->setValue($submit_button)
|
|
|
|
->addCancelButton($cancel_uri));
|
|
|
|
|
|
|
|
$box = id(new PHUIObjectBoxView())
|
|
|
|
->setHeaderText($title)
|
|
|
|
->setValidationException($validation_exception)
|
|
|
|
->appendChild($form);
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
array(
|
|
|
|
$crumbs,
|
|
|
|
$box,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => $title,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|