Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / spaces / controller / PhabricatorSpacesEditController.php
blobcf3b6578b6ea9b5abf9230536343c8da9de24eb0
1 <?php
3 final class PhabricatorSpacesEditController
4 extends PhabricatorSpacesController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $request->getUser();
9 $make_default = false;
11 $id = $request->getURIData('id');
12 if ($id) {
13 $space = id(new PhabricatorSpacesNamespaceQuery())
14 ->setViewer($viewer)
15 ->withIDs(array($id))
16 ->requireCapabilities(
17 array(
18 PhabricatorPolicyCapability::CAN_VIEW,
19 PhabricatorPolicyCapability::CAN_EDIT,
21 ->executeOne();
22 if (!$space) {
23 return new Aphront404Response();
26 $is_new = false;
27 $cancel_uri = '/'.$space->getMonogram();
29 $header_text = pht('Edit %s', $space->getNamespaceName());
30 $title = pht('Edit Space');
31 $button_text = pht('Save Changes');
32 } else {
33 $this->requireApplicationCapability(
34 PhabricatorSpacesCapabilityCreateSpaces::CAPABILITY);
36 $space = PhabricatorSpacesNamespace::initializeNewNamespace($viewer);
38 $is_new = true;
39 $cancel_uri = $this->getApplicationURI();
41 $header_text = pht('Create Space');
42 $title = pht('Create Space');
43 $button_text = pht('Create Space');
45 $default = id(new PhabricatorSpacesNamespaceQuery())
46 ->setViewer(PhabricatorUser::getOmnipotentUser())
47 ->withIsDefaultNamespace(true)
48 ->execute();
49 if (!$default) {
50 $make_default = true;
54 $validation_exception = null;
55 $e_name = true;
56 $v_name = $space->getNamespaceName();
57 $v_desc = $space->getDescription();
58 $v_view = $space->getViewPolicy();
59 $v_edit = $space->getEditPolicy();
61 if ($request->isFormPost()) {
62 $xactions = array();
63 $e_name = null;
65 $v_name = $request->getStr('name');
66 $v_desc = $request->getStr('description');
67 $v_view = $request->getStr('viewPolicy');
68 $v_edit = $request->getStr('editPolicy');
70 $type_name =
71 PhabricatorSpacesNamespaceNameTransaction::TRANSACTIONTYPE;
72 $type_desc =
73 PhabricatorSpacesNamespaceDescriptionTransaction::TRANSACTIONTYPE;
74 $type_default =
75 PhabricatorSpacesNamespaceDefaultTransaction::TRANSACTIONTYPE;
76 $type_view = PhabricatorTransactions::TYPE_VIEW_POLICY;
77 $type_edit = PhabricatorTransactions::TYPE_EDIT_POLICY;
79 $xactions[] = id(new PhabricatorSpacesNamespaceTransaction())
80 ->setTransactionType($type_name)
81 ->setNewValue($v_name);
83 $xactions[] = id(new PhabricatorSpacesNamespaceTransaction())
84 ->setTransactionType($type_desc)
85 ->setNewValue($v_desc);
87 $xactions[] = id(new PhabricatorSpacesNamespaceTransaction())
88 ->setTransactionType($type_view)
89 ->setNewValue($v_view);
91 $xactions[] = id(new PhabricatorSpacesNamespaceTransaction())
92 ->setTransactionType($type_edit)
93 ->setNewValue($v_edit);
95 if ($make_default) {
96 $xactions[] = id(new PhabricatorSpacesNamespaceTransaction())
97 ->setTransactionType($type_default)
98 ->setNewValue(1);
101 $editor = id(new PhabricatorSpacesNamespaceEditor())
102 ->setActor($viewer)
103 ->setContinueOnNoEffect(true)
104 ->setContentSourceFromRequest($request);
106 try {
107 $editor->applyTransactions($space, $xactions);
109 return id(new AphrontRedirectResponse())
110 ->setURI('/'.$space->getMonogram());
111 } catch (PhabricatorApplicationTransactionValidationException $ex) {
112 $validation_exception = $ex;
114 $e_name = $ex->getShortMessage($type_name);
118 $policies = id(new PhabricatorPolicyQuery())
119 ->setViewer($viewer)
120 ->setObject($space)
121 ->execute();
123 $form = id(new AphrontFormView())
124 ->setUser($viewer);
126 if ($make_default) {
127 $form->appendRemarkupInstructions(
128 pht(
129 'NOTE: You are creating the **default space**. All existing '.
130 'objects will be put into this space. You must create a default '.
131 'space before you can create other spaces.'));
134 $form
135 ->appendChild(
136 id(new AphrontFormTextControl())
137 ->setLabel(pht('Name'))
138 ->setName('name')
139 ->setValue($v_name)
140 ->setError($e_name))
141 ->appendControl(
142 id(new PhabricatorRemarkupControl())
143 ->setLabel(pht('Description'))
144 ->setName('description')
145 ->setValue($v_desc))
146 ->appendChild(
147 id(new AphrontFormPolicyControl())
148 ->setUser($viewer)
149 ->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
150 ->setPolicyObject($space)
151 ->setPolicies($policies)
152 ->setValue($v_view)
153 ->setName('viewPolicy'))
154 ->appendChild(
155 id(new AphrontFormPolicyControl())
156 ->setUser($viewer)
157 ->setCapability(PhabricatorPolicyCapability::CAN_EDIT)
158 ->setPolicyObject($space)
159 ->setPolicies($policies)
160 ->setValue($v_edit)
161 ->setName('editPolicy'))
162 ->appendChild(
163 id(new AphrontFormSubmitControl())
164 ->setValue($button_text)
165 ->addCancelButton($cancel_uri));
167 $box = id(new PHUIObjectBoxView())
168 ->setHeaderText($title)
169 ->setBackground(PHUIObjectBoxView::WHITE_CONFIG)
170 ->setValidationException($validation_exception)
171 ->appendChild($form);
173 $crumbs = $this->buildApplicationCrumbs();
174 if (!$is_new) {
175 $crumbs->addTextCrumb(
176 $space->getMonogram(),
177 $cancel_uri);
179 $crumbs->addTextCrumb($title);
180 $crumbs->setBorder(true);
182 $view = id(new PHUITwoColumnView())
183 ->setFooter(array(
184 $box,
187 return $this->newPage()
188 ->setTitle($title)
189 ->setCrumbs($crumbs)
190 ->appendChild($view);