|
| 1 | +<?php |
| 2 | + |
| 3 | +final class AlmanacBindingEditController |
| 4 | + extends AlmanacServiceController { |
| 5 | + |
| 6 | + public function handleRequest(AphrontRequest $request) { |
| 7 | + $viewer = $request->getViewer(); |
| 8 | + |
| 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, |
| 18 | + )) |
| 19 | + ->executeOne(); |
| 20 | + if (!$binding) { |
| 21 | + return new Aphront404Response(); |
| 22 | + } |
| 23 | + |
| 24 | + $service = $binding->getService(); |
| 25 | + $is_new = false; |
| 26 | + |
| 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, |
| 39 | + )) |
| 40 | + ->executeOne(); |
| 41 | + |
| 42 | + $binding = AlmanacBinding::initializeNewBinding($service); |
| 43 | + $is_new = true; |
| 44 | + |
| 45 | + $service_uri = $service->getURI(); |
| 46 | + $cancel_uri = $service_uri; |
| 47 | + $title = pht('Create Binding'); |
| 48 | + $save_button = pht('Create Binding'); |
| 49 | + } |
| 50 | + |
| 51 | + $v_interface = array(); |
| 52 | + if ($binding->getInterfacePHID()) { |
| 53 | + $v_interface = array($binding->getInterfacePHID()); |
| 54 | + } |
| 55 | + $e_interface = true; |
| 56 | + |
| 57 | + $validation_exception = null; |
| 58 | + if ($request->isFormPost()) { |
| 59 | + $v_interface = $request->getArr('interfacePHIDs'); |
| 60 | + |
| 61 | + $type_interface = AlmanacBindingTransaction::TYPE_INTERFACE; |
| 62 | + |
| 63 | + $xactions = array(); |
| 64 | + |
| 65 | + $xactions[] = id(new AlmanacBindingTransaction()) |
| 66 | + ->setTransactionType($type_interface) |
| 67 | + ->setNewValue(head($v_interface)); |
| 68 | + |
| 69 | + $editor = id(new AlmanacBindingEditor()) |
| 70 | + ->setActor($viewer) |
| 71 | + ->setContentSourceFromRequest($request) |
| 72 | + ->setContinueOnNoEffect(true); |
| 73 | + |
| 74 | + try { |
| 75 | + $editor->applyTransactions($binding, $xactions); |
| 76 | + |
| 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); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + $interface_handles = array(); |
| 86 | + if ($v_interface) { |
| 87 | + $interface_handles = $this->loadViewerHandles($v_interface); |
| 88 | + } |
| 89 | + |
| 90 | + $form = id(new AphrontFormView()) |
| 91 | + ->setUser($viewer) |
| 92 | + ->appendChild( |
| 93 | + id(new AphrontFormTokenizerControl()) |
| 94 | + ->setName('interfacePHIDs') |
| 95 | + ->setLabel('Interface') |
| 96 | + ->setLimit(1) |
| 97 | + ->setDatasource(new AlmanacInterfaceDatasource()) |
| 98 | + ->setValue($interface_handles) |
| 99 | + ->setError($e_interface)) |
| 100 | + ->appendChild( |
| 101 | + id(new AphrontFormSubmitControl()) |
| 102 | + ->addCancelButton($cancel_uri) |
| 103 | + ->setValue($save_button)); |
| 104 | + |
| 105 | + $box = id(new PHUIObjectBoxView()) |
| 106 | + ->setValidationException($validation_exception) |
| 107 | + ->setHeaderText($title) |
| 108 | + ->appendChild($form); |
| 109 | + |
| 110 | + $crumbs = $this->buildApplicationCrumbs(); |
| 111 | + $crumbs->addTextCrumb($service->getName(), $service_uri); |
| 112 | + if ($is_new) { |
| 113 | + $crumbs->addTextCrumb(pht('Create Binding')); |
| 114 | + } else { |
| 115 | + $crumbs->addTextCrumb(pht('Edit Binding')); |
| 116 | + } |
| 117 | + |
| 118 | + return $this->buildApplicationPage( |
| 119 | + array( |
| 120 | + $crumbs, |
| 121 | + $box, |
| 122 | + ), |
| 123 | + array( |
| 124 | + 'title' => $title, |
| 125 | + )); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments