@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 180 lines 5.6 kB view raw
1<?php 2 3final class PhluxEditController extends PhluxController { 4 5 public function handleRequest(AphrontRequest $request) { 6 $viewer = $this->getViewer(); 7 $key = $request->getURIData('key'); 8 9 $is_new = ($key === null); 10 if ($is_new) { 11 $var = new PhluxVariable(); 12 $var->setViewPolicy(PhabricatorPolicies::POLICY_USER); 13 $var->setEditPolicy(PhabricatorPolicies::POLICY_USER); 14 } else { 15 $var = id(new PhluxVariableQuery()) 16 ->setViewer($viewer) 17 ->requireCapabilities( 18 array( 19 PhabricatorPolicyCapability::CAN_VIEW, 20 PhabricatorPolicyCapability::CAN_EDIT, 21 )) 22 ->withKeys(array($key)) 23 ->executeOne(); 24 if (!$var) { 25 return new Aphront404Response(); 26 } 27 $view_uri = $this->getApplicationURI('/view/'.$key.'/'); 28 } 29 30 $e_key = ($is_new ? true : null); 31 $e_value = true; 32 $errors = array(); 33 34 $key = $var->getVariableKey(); 35 36 $display_value = null; 37 $value = $var->getVariableValue(); 38 39 if ($request->isFormPost()) { 40 if ($is_new) { 41 $key = $request->getStr('key'); 42 if (!strlen($key)) { 43 $errors[] = pht('Variable key is required.'); 44 $e_key = pht('Required'); 45 } else if (!preg_match('/^[a-z0-9.-]+\z/', $key)) { 46 $errors[] = pht( 47 'Variable key "%s" must contain only lowercase letters, digits, '. 48 'period, and hyphen.', 49 $key); 50 $e_key = pht('Invalid'); 51 } 52 } 53 54 $raw_value = $request->getStr('value'); 55 $value = json_decode($raw_value, true); 56 if ($value === null && strtolower($raw_value) !== 'null') { 57 $e_value = pht('Invalid'); 58 $errors[] = pht('Variable value must be valid JSON.'); 59 $display_value = $raw_value; 60 } 61 62 if (!$errors) { 63 $editor = id(new PhluxVariableEditor()) 64 ->setActor($viewer) 65 ->setContinueOnNoEffect(true) 66 ->setContentSourceFromRequest($request); 67 68 $xactions = array(); 69 $xactions[] = id(new PhluxTransaction()) 70 ->setTransactionType(PhluxTransaction::TYPE_EDIT_KEY) 71 ->setNewValue($key); 72 73 $xactions[] = id(new PhluxTransaction()) 74 ->setTransactionType(PhluxTransaction::TYPE_EDIT_VALUE) 75 ->setNewValue($value); 76 77 $xactions[] = id(new PhluxTransaction()) 78 ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY) 79 ->setNewValue($request->getStr('viewPolicy')); 80 81 $xactions[] = id(new PhluxTransaction()) 82 ->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY) 83 ->setNewValue($request->getStr('editPolicy')); 84 85 try { 86 $editor->applyTransactions($var, $xactions); 87 $view_uri = $this->getApplicationURI('/view/'.$key.'/'); 88 return id(new AphrontRedirectResponse())->setURI($view_uri); 89 } catch (AphrontDuplicateKeyQueryException $ex) { 90 $e_key = pht('Not Unique'); 91 $errors[] = pht('Variable key must be unique.'); 92 } 93 } 94 } 95 96 if ($display_value === null) { 97 if (is_array($value) && 98 (array_keys($value) !== array_keys(array_values($value)))) { 99 $json = new PhutilJSON(); 100 $display_value = $json->encodeFormatted($value); 101 } else { 102 $display_value = json_encode($value); 103 } 104 } 105 106 $policies = id(new PhabricatorPolicyQuery()) 107 ->setViewer($viewer) 108 ->setObject($var) 109 ->execute(); 110 111 $form = id(new AphrontFormView()) 112 ->setUser($viewer) 113 ->appendChild( 114 id(new AphrontFormTextControl()) 115 ->setValue($var->getVariableKey()) 116 ->setLabel(pht('Key')) 117 ->setName('key') 118 ->setError($e_key) 119 ->setCaption(pht('Lowercase letters, digits, dot and hyphen only.')) 120 ->setDisabled(!$is_new)) 121 ->appendChild( 122 id(new AphrontFormTextAreaControl()) 123 ->setValue($display_value) 124 ->setLabel(pht('Value')) 125 ->setName('value') 126 ->setCaption(pht('Enter value as JSON.')) 127 ->setError($e_value)) 128 ->appendChild( 129 id(new AphrontFormPolicyControl()) 130 ->setName('viewPolicy') 131 ->setPolicyObject($var) 132 ->setCapability(PhabricatorPolicyCapability::CAN_VIEW) 133 ->setPolicies($policies)) 134 ->appendChild( 135 id(new AphrontFormPolicyControl()) 136 ->setName('editPolicy') 137 ->setPolicyObject($var) 138 ->setCapability(PhabricatorPolicyCapability::CAN_EDIT) 139 ->setPolicies($policies)); 140 141 if ($is_new) { 142 $form->appendChild( 143 id(new AphrontFormSubmitControl()) 144 ->setValue(pht('Create Variable'))); 145 } else { 146 $form->appendChild( 147 id(new AphrontFormSubmitControl()) 148 ->setValue(pht('Update Variable')) 149 ->addCancelButton($view_uri)); 150 } 151 152 $crumbs = $this->buildApplicationCrumbs(); 153 154 if ($is_new) { 155 $title = pht('Create Variable'); 156 $crumbs->addTextCrumb($title, $request->getRequestURI()); 157 } else { 158 $title = pht('Edit Variable: %s', $key); 159 $crumbs->addTextCrumb($title, $request->getRequestURI()); 160 } 161 $crumbs->setBorder(true); 162 163 $box = id(new PHUIObjectBoxView()) 164 ->setHeaderText($title) 165 ->setFormErrors($errors) 166 ->setBackground(PHUIObjectBoxView::WHITE_CONFIG) 167 ->setForm($form); 168 169 $view = id(new PHUITwoColumnView()) 170 ->setFooter(array( 171 $box, 172 )); 173 174 return $this->newPage() 175 ->setTitle($title) 176 ->setCrumbs($crumbs) 177 ->appendChild($view); 178 } 179 180}