Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / transactions / controller / PhabricatorEditEngineConfigurationSortController.php
blob5e8680b651488ce835c340c4deaa1ab97b1dd1bc
1 <?php
3 final class PhabricatorEditEngineConfigurationSortController
4 extends PhabricatorEditEngineController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $this->getViewer();
8 $engine_key = $request->getURIData('engineKey');
9 $this->setEngineKey($engine_key);
11 $type = $request->getURIData('type');
12 $is_create = ($type == 'create');
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,
22 ->executeOne();
23 if (!$engine) {
24 return id(new Aphront404Response());
27 $cancel_uri = "/transactions/editengine/{$engine_key}/";
28 $reorder_uri = "/transactions/editengine/{$engine_key}/sort/{$type}/";
30 $query = id(new PhabricatorEditEngineConfigurationQuery())
31 ->setViewer($viewer)
32 ->withEngineKeys(array($engine->getEngineKey()));
34 if ($is_create) {
35 $query->withIsDefault(true);
36 } else {
37 $query->withIsEdit(true);
40 $configs = $query->execute();
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);
51 if ($is_create) {
52 $configs = msort($configs, 'getCreateSortKey');
53 } else {
54 $configs = msort($configs, 'getEditSortKey');
57 if ($request->isFormPost()) {
58 $form_order = $request->getStrList('formOrder');
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.
64 $configs = mpull($configs, null, 'getIdentifier');
65 $configs = array_select_keys($configs, $form_order) + $configs;
67 $order = 1;
68 foreach ($configs as $config) {
69 $xactions = array();
71 if ($is_create) {
72 $xaction_type =
73 PhabricatorEditEngineCreateOrderTransaction::TRANSACTIONTYPE;
74 } else {
75 $xaction_type =
76 PhabricatorEditEngineEditOrderTransaction::TRANSACTIONTYPE;
79 $xactions[] = id(new PhabricatorEditEngineConfigurationTransaction())
80 ->setTransactionType($xaction_type)
81 ->setNewValue($order);
83 $editor = id(new PhabricatorEditEngineConfigurationEditor())
84 ->setActor($viewer)
85 ->setContentSourceFromRequest($request)
86 ->setContinueOnNoEffect(true);
88 $editor->applyTransactions($config, $xactions);
90 $order++;
93 return id(new AphrontRedirectResponse())
94 ->setURI($cancel_uri);
97 $list_id = celerity_generate_unique_node_id();
98 $input_id = celerity_generate_unique_node_id();
100 $list = id(new PHUIObjectItemListView())
101 ->setUser($viewer)
102 ->setID($list_id)
103 ->setFlush(true);
105 $form_order = array();
106 foreach ($configs as $config) {
107 $name = $config->getName();
108 $identifier = $config->getIdentifier();
110 $item = id(new PHUIObjectItemView())
111 ->setHeader($name)
112 ->setGrippable(true)
113 ->addSigil('editengine-form-config')
114 ->setMetadata(
115 array(
116 'formIdentifier' => $identifier,
119 $list->addItem($item);
121 $form_order[] = $identifier;
124 Javelin::initBehavior(
125 'editengine-reorder-configs',
126 array(
127 'listID' => $list_id,
128 'inputID' => $input_id,
129 'reorderURI' => $reorder_uri,
132 if ($is_create) {
133 $title = pht('Reorder Create Forms');
134 $button = pht('Save Create Order');
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');
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.');
149 $note = id(new PHUIInfoView())
150 ->appendChild($note_text)
151 ->setSeverity(PHUIInfoView::SEVERITY_NOTICE);
153 $input = phutil_tag(
154 'input',
155 array(
156 'type' => 'hidden',
157 'name' => 'formOrder',
158 'value' => implode(', ', $form_order),
159 'id' => $input_id,
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);