Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / almanac / controller / AlmanacBindingEditController.php
blobf33bf73ccb3cb41a703e2518d15a597b49d45441
1 <?php
3 final class AlmanacBindingEditController
4 extends AlmanacServiceController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $request->getViewer();
9 $id = $request->getURIData('id');
10 if ($id) {
11 $binding = id(new AlmanacBindingQuery())
12 ->setViewer($viewer)
13 ->withIDs(array($id))
14 ->requireCapabilities(
15 array(
16 PhabricatorPolicyCapability::CAN_VIEW,
17 PhabricatorPolicyCapability::CAN_EDIT,
19 ->executeOne();
20 if (!$binding) {
21 return new Aphront404Response();
24 $service = $binding->getService();
25 $is_new = false;
27 $service_uri = $service->getURI();
28 $cancel_uri = $binding->getURI();
29 $title = pht('Edit Binding');
30 $save_button = pht('Save Changes');
31 } else {
32 $service = id(new AlmanacServiceQuery())
33 ->setViewer($viewer)
34 ->withIDs(array($request->getStr('serviceID')))
35 ->requireCapabilities(
36 array(
37 PhabricatorPolicyCapability::CAN_VIEW,
38 PhabricatorPolicyCapability::CAN_EDIT,
40 ->executeOne();
42 $binding = AlmanacBinding::initializeNewBinding($service);
43 $is_new = true;
45 $service_uri = $service->getURI();
46 $cancel_uri = $service_uri;
47 $title = pht('Create Binding');
48 $save_button = pht('Create Binding');
51 $v_interface = array();
52 if ($binding->getInterfacePHID()) {
53 $v_interface = array($binding->getInterfacePHID());
55 $e_interface = true;
57 $validation_exception = null;
58 if ($request->isFormPost()) {
59 $v_interface = $request->getArr('interfacePHIDs');
61 $type_interface = AlmanacBindingInterfaceTransaction::TRANSACTIONTYPE;
63 $xactions = array();
65 $xactions[] = id(new AlmanacBindingTransaction())
66 ->setTransactionType($type_interface)
67 ->setNewValue(head($v_interface));
69 $editor = id(new AlmanacBindingEditor())
70 ->setActor($viewer)
71 ->setContentSourceFromRequest($request)
72 ->setContinueOnNoEffect(true);
74 try {
75 $editor->applyTransactions($binding, $xactions);
77 $binding_uri = $binding->getURI();
78 return id(new AphrontRedirectResponse())->setURI($binding_uri);
79 } catch (PhabricatorApplicationTransactionValidationException $ex) {
80 $validation_exception = $ex;
81 $e_interface = $ex->getShortMessage($type_interface);
85 $form = id(new AphrontFormView())
86 ->setUser($viewer)
87 ->appendControl(
88 id(new AphrontFormTokenizerControl())
89 ->setName('interfacePHIDs')
90 ->setLabel(pht('Interface'))
91 ->setLimit(1)
92 ->setDatasource(new AlmanacInterfaceDatasource())
93 ->setValue($v_interface)
94 ->setError($e_interface))
95 ->appendChild(
96 id(new AphrontFormSubmitControl())
97 ->addCancelButton($cancel_uri)
98 ->setValue($save_button));
100 $box = id(new PHUIObjectBoxView())
101 ->setValidationException($validation_exception)
102 ->setHeaderText(pht('Binding'))
103 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
104 ->appendChild($form);
106 $crumbs = $this->buildApplicationCrumbs();
107 $crumbs->addTextCrumb($service->getName(), $service_uri);
108 if ($is_new) {
109 $crumbs->addTextCrumb(pht('Create Binding'));
110 $header = id(new PHUIHeaderView())
111 ->setHeader(pht('Create Binding'))
112 ->setHeaderIcon('fa-plus-square');
113 } else {
114 $crumbs->addTextCrumb(pht('Edit Binding'));
115 $header = id(new PHUIHeaderView())
116 ->setHeader(pht('Create Binding'))
117 ->setHeaderIcon('fa-pencil');
119 $crumbs->setBorder(true);
121 $view = id(new PHUITwoColumnView())
122 ->setHeader($header)
123 ->setFooter(array(
124 $box,
127 return $this->newPage()
128 ->setTitle($title)
129 ->setCrumbs($crumbs)
130 ->appendChild($view);