@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 recaptime-dev/main 172 lines 5.0 kB view raw
1<?php 2 3final class PhabricatorEditEngineConfigurationSortController 4 extends PhabricatorEditEngineController { 5 6 public function handleRequest(AphrontRequest $request) { 7 $viewer = $this->getViewer(); 8 $engine_key = $request->getURIData('engineKey'); 9 $this->setEngineKey($engine_key); 10 11 $type = $request->getURIData('type'); 12 $is_create = ($type == 'create'); 13 14 $engine = id(new PhabricatorEditEngineQuery()) 15 ->setViewer($viewer) 16 ->withEngineKeys(array($engine_key)) 17 ->requireCapabilities( 18 array( 19 PhabricatorPolicyCapability::CAN_VIEW, 20 PhabricatorPolicyCapability::CAN_EDIT, 21 )) 22 ->executeOne(); 23 if (!$engine) { 24 return id(new Aphront404Response()); 25 } 26 27 $cancel_uri = "/transactions/editengine/{$engine_key}/"; 28 $reorder_uri = "/transactions/editengine/{$engine_key}/sort/{$type}/"; 29 30 $query = id(new PhabricatorEditEngineConfigurationQuery()) 31 ->setViewer($viewer) 32 ->withEngineKeys(array($engine->getEngineKey())); 33 34 if ($is_create) { 35 $query->withIsDefault(true); 36 } else { 37 $query->withIsEdit(true); 38 } 39 40 $configs = $query->execute(); 41 42 // Do this check here (instead of in the Query above) to get a proper 43 // policy exception if the user doesn't satisfy 44 foreach ($configs as $config) { 45 PhabricatorPolicyFilter::requireCapability( 46 $viewer, 47 $config, 48 PhabricatorPolicyCapability::CAN_EDIT); 49 } 50 51 if ($is_create) { 52 $configs = msort($configs, 'getCreateSortKey'); 53 } else { 54 $configs = msort($configs, 'getEditSortKey'); 55 } 56 57 if ($request->isFormPost()) { 58 $form_order = $request->getStrList('formOrder'); 59 60 // NOTE: This has a side-effect of saving any factory-default forms 61 // to the database. We might want to warn the user better, but this 62 // shouldn't generally be very important or confusing. 63 64 $configs = mpull($configs, null, 'getIdentifier'); 65 $configs = array_select_keys($configs, $form_order) + $configs; 66 67 $order = 1; 68 foreach ($configs as $config) { 69 $xactions = array(); 70 71 if ($is_create) { 72 $xaction_type = 73 PhabricatorEditEngineCreateOrderTransaction::TRANSACTIONTYPE; 74 } else { 75 $xaction_type = 76 PhabricatorEditEngineEditOrderTransaction::TRANSACTIONTYPE; 77 } 78 79 $xactions[] = id(new PhabricatorEditEngineConfigurationTransaction()) 80 ->setTransactionType($xaction_type) 81 ->setNewValue($order); 82 83 $editor = id(new PhabricatorEditEngineConfigurationEditor()) 84 ->setActor($viewer) 85 ->setContentSourceFromRequest($request) 86 ->setContinueOnNoEffect(true); 87 88 $editor->applyTransactions($config, $xactions); 89 90 $order++; 91 } 92 93 return id(new AphrontRedirectResponse()) 94 ->setURI($cancel_uri); 95 } 96 97 $list_id = celerity_generate_unique_node_id(); 98 $input_id = celerity_generate_unique_node_id(); 99 100 $list = id(new PHUIObjectItemListView()) 101 ->setUser($viewer) 102 ->setID($list_id) 103 ->setFlush(true); 104 105 $form_order = array(); 106 foreach ($configs as $config) { 107 $name = $config->getName(); 108 $identifier = $config->getIdentifier(); 109 110 $item = id(new PHUIObjectItemView()) 111 ->setHeader($name) 112 ->setGrippable(true) 113 ->addSigil('editengine-form-config') 114 ->setMetadata( 115 array( 116 'formIdentifier' => $identifier, 117 )); 118 119 $list->addItem($item); 120 121 $form_order[] = $identifier; 122 } 123 124 Javelin::initBehavior( 125 'editengine-reorder-configs', 126 array( 127 'listID' => $list_id, 128 'inputID' => $input_id, 129 'reorderURI' => $reorder_uri, 130 )); 131 132 if ($is_create) { 133 $title = pht('Reorder Create Forms'); 134 $button = pht('Save Create Order'); 135 136 $note_text = pht( 137 'Drag and drop fields to change the order in which they appear in '. 138 'the application "Create" menu.'); 139 } else { 140 $title = pht('Reorder Edit Forms'); 141 $button = pht('Save Edit Order'); 142 143 $note_text = pht( 144 'Drag and drop fields to change their priority for edits. When a '. 145 'user edits an object, they will be shown the first form in this '. 146 'list that they have permission to see.'); 147 } 148 149 $note = id(new PHUIInfoView()) 150 ->appendChild($note_text) 151 ->setSeverity(PHUIInfoView::SEVERITY_NOTICE); 152 153 $input = phutil_tag( 154 'input', 155 array( 156 'type' => 'hidden', 157 'name' => 'formOrder', 158 'value' => implode(', ', $form_order), 159 'id' => $input_id, 160 )); 161 162 return $this->newDialog() 163 ->setTitle($title) 164 ->setWidth(AphrontDialogView::WIDTH_FORM) 165 ->appendChild($note) 166 ->appendChild($list) 167 ->appendChild($input) 168 ->addSubmitButton(pht('Save Changes')) 169 ->addCancelButton($cancel_uri); 170 } 171 172}