Remove product literal strings in "pht()", part 6
[phabricator.git] / src / applications / meta / controller / PhabricatorApplicationEditController.php
blobf7c2eac81e366e014aad75f2060263301ad1505c
1 <?php
3 final class PhabricatorApplicationEditController
4 extends PhabricatorApplicationsController {
6 public function handleRequest(AphrontRequest $request) {
7 $user = $request->getUser();
8 $application = $request->getURIData('application');
10 $application = id(new PhabricatorApplicationQuery())
11 ->setViewer($user)
12 ->withClasses(array($application))
13 ->requireCapabilities(
14 array(
15 PhabricatorPolicyCapability::CAN_VIEW,
16 PhabricatorPolicyCapability::CAN_EDIT,
18 ->executeOne();
19 if (!$application) {
20 return new Aphront404Response();
23 $title = $application->getName();
25 $view_uri = $this->getApplicationURI('view/'.get_class($application).'/');
27 $policies = id(new PhabricatorPolicyQuery())
28 ->setViewer($user)
29 ->setObject($application)
30 ->execute();
32 if ($request->isFormPost()) {
33 $xactions = array();
35 $template = $application->getApplicationTransactionTemplate();
36 foreach ($application->getCapabilities() as $capability) {
37 if (!$application->isCapabilityEditable($capability)) {
38 continue;
41 $old = $application->getPolicy($capability);
42 $new = $request->getStr('policy:'.$capability);
44 if ($old == $new) {
45 // No change to the setting.
46 continue;
49 $xactions[] = id(clone $template)
50 ->setTransactionType(
51 PhabricatorApplicationPolicyChangeTransaction::TRANSACTIONTYPE)
52 ->setMetadataValue(
53 PhabricatorApplicationPolicyChangeTransaction::METADATA_ATTRIBUTE,
54 $capability)
55 ->setNewValue($new);
58 $editor = id(new PhabricatorApplicationEditor())
59 ->setActor($user)
60 ->setContentSourceFromRequest($request)
61 ->setContinueOnNoEffect(true)
62 ->setContinueOnMissingFields(true);
64 try {
65 $editor->applyTransactions($application, $xactions);
66 return id(new AphrontRedirectResponse())->setURI($view_uri);
67 } catch (PhabricatorApplicationTransactionValidationException $ex) {
68 $validation_exception = $ex;
71 return $this->newDialog()
72 ->setTitle(pht('Validation Failed'))
73 ->setValidationException($validation_exception)
74 ->addCancelButton($view_uri);
77 $descriptions = PhabricatorPolicyQuery::renderPolicyDescriptions(
78 $user,
79 $application);
81 $form = id(new AphrontFormView())
82 ->setUser($user);
84 $locked_policies = PhabricatorEnv::getEnvConfig('policy.locked');
85 foreach ($application->getCapabilities() as $capability) {
86 $label = $application->getCapabilityLabel($capability);
87 $can_edit = $application->isCapabilityEditable($capability);
88 $locked = idx($locked_policies, $capability);
89 $caption = $application->getCapabilityCaption($capability);
91 if (!$can_edit || $locked) {
92 $form->appendChild(
93 id(new AphrontFormStaticControl())
94 ->setLabel($label)
95 ->setValue(idx($descriptions, $capability))
96 ->setCaption($caption));
97 } else {
98 $control = id(new AphrontFormPolicyControl())
99 ->setUser($user)
100 ->setDisabled($locked)
101 ->setCapability($capability)
102 ->setPolicyObject($application)
103 ->setPolicies($policies)
104 ->setLabel($label)
105 ->setName('policy:'.$capability)
106 ->setCaption($caption);
108 $template = $application->getCapabilityTemplatePHIDType($capability);
109 if ($template) {
110 $phid_types = PhabricatorPHIDType::getAllTypes();
111 $phid_type = idx($phid_types, $template);
112 if ($phid_type) {
113 $template_object = $phid_type->newObject();
114 if ($template_object) {
115 $template_policies = id(new PhabricatorPolicyQuery())
116 ->setViewer($user)
117 ->setObject($template_object)
118 ->execute();
120 // NOTE: We want to expose both any object template policies
121 // (like "Subscribers") and any custom policy.
122 $all_policies = $template_policies + $policies;
124 $control->setPolicies($all_policies);
125 $control->setTemplateObject($template_object);
129 $control->setTemplatePHIDType($template);
132 $form->appendControl($control);
137 $form->appendChild(
138 id(new AphrontFormSubmitControl())
139 ->setValue(pht('Save Policies'))
140 ->addCancelButton($view_uri));
142 $crumbs = $this->buildApplicationCrumbs();
143 $crumbs->addTextCrumb($application->getName(), $view_uri);
144 $crumbs->addTextCrumb(pht('Edit Policies'));
145 $crumbs->setBorder(true);
147 $header = id(new PHUIHeaderView())
148 ->setHeader(pht('Edit Policies: %s', $application->getName()))
149 ->setHeaderIcon('fa-pencil');
151 $object_box = id(new PHUIObjectBoxView())
152 ->setHeaderText(pht('Policies'))
153 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
154 ->setForm($form);
156 $view = id(new PHUITwoColumnView())
157 ->setHeader($header)
158 ->setFooter(array(
159 $object_box,
162 return $this->newPage()
163 ->setTitle($title)
164 ->setCrumbs($crumbs)
165 ->appendChild($view);