Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / dashboard / controller / PhabricatorDashboardQueryPanelInstallController.php
bloba229fcb41c3c0568b95e15d422ee4955babfc2ab
1 <?php
3 final class PhabricatorDashboardQueryPanelInstallController
4 extends PhabricatorDashboardController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $request->getViewer();
9 $v_dashboard = null;
10 $e_dashboard = null;
12 $v_name = null;
13 $e_name = true;
15 $v_engine = $request->getStr('engine');
16 if (!strlen($v_engine)) {
17 $v_engine = $request->getURIData('engineKey');
20 $v_query = $request->getStr('query');
21 if (!strlen($v_query)) {
22 $v_query = $request->getURIData('queryKey');
25 $engines = PhabricatorApplicationSearchEngine::getAllEngines();
26 $engine = idx($engines, $v_engine);
27 if ($engine) {
28 $engine = id(clone $engine)
29 ->setViewer($viewer);
31 $redirect_uri = $engine->getQueryResultsPageURI($v_query);
33 $named_query = idx($engine->loadEnabledNamedQueries(), $v_query);
34 if ($named_query) {
35 $v_name = $named_query->getQueryName();
37 } else {
38 $redirect_uri = '/';
41 $errors = array();
43 $xaction_name = PhabricatorDashboardPanelNameTransaction::TRANSACTIONTYPE;
44 $xaction_engine =
45 PhabricatorDashboardQueryPanelApplicationTransaction::TRANSACTIONTYPE;
46 $xaction_query =
47 PhabricatorDashboardQueryPanelQueryTransaction::TRANSACTIONTYPE;
49 if ($request->isFormPost()) {
50 $v_name = $request->getStr('name');
51 if (!$v_name) {
52 $errors[] = pht('You must provide a name for this panel.');
53 $e_name = pht('Required');
56 $v_dashboard = head($request->getArr('dashboardPHIDs'));
57 if (!$v_dashboard) {
58 $errors[] = pht('You must select a dashboard.');
59 $e_dashboard = pht('Required');
60 } else {
61 $dashboard = id(new PhabricatorDashboardQuery())
62 ->setViewer($viewer)
63 ->withPHIDs(array($v_dashboard))
64 ->executeOne();
65 if (!$dashboard) {
66 $errors[] = pht('You must select a valid dashboard.');
67 $e_dashboard = pht('Invalid');
70 $can_edit = PhabricatorPolicyFilter::hasCapability(
71 $viewer,
72 $dashboard,
73 PhabricatorPolicyCapability::CAN_EDIT);
74 if (!$can_edit) {
75 $errors[] = pht(
76 'You must select a dashboard you have permission to edit.');
80 if (!$errors) {
81 $done_uri = $dashboard->getURI();
83 // First, create a new panel.
85 $panel_type = id(new PhabricatorDashboardQueryPanelType())
86 ->getPanelTypeKey();
88 $panel = PhabricatorDashboardPanel::initializeNewPanel($viewer)
89 ->setPanelType($panel_type);
91 $xactions = array();
93 $xactions[] = $panel->getApplicationTransactionTemplate()
94 ->setTransactionType($xaction_engine)
95 ->setNewValue($v_engine);
97 $xactions[] = $panel->getApplicationTransactionTemplate()
98 ->setTransactionType($xaction_query)
99 ->setNewValue($v_query);
101 $xactions[] = $panel->getApplicationTransactionTemplate()
102 ->setTransactionType($xaction_name)
103 ->setNewValue($v_name);
105 $editor = $panel->getApplicationTransactionEditor()
106 ->setActor($viewer)
107 ->setContentSourceFromRequest($request)
108 ->applyTransactions($panel, $xactions);
110 // Now that we've created a panel, add it to the dashboard.
112 $xactions = array();
114 $ref_list = clone $dashboard->getPanelRefList();
115 $ref_list->newPanelRef($panel);
116 $new_panels = $ref_list->toDictionary();
118 $xactions[] = $dashboard->getApplicationTransactionTemplate()
119 ->setTransactionType(
120 PhabricatorDashboardPanelsTransaction::TRANSACTIONTYPE)
121 ->setNewValue($new_panels);
123 $editor = $dashboard->getApplicationTransactionEditor()
124 ->setActor($viewer)
125 ->setContentSourceFromRequest($request)
126 ->setContinueOnNoEffect(true)
127 ->setContinueOnMissingFields(true)
128 ->applyTransactions($dashboard, $xactions);
130 return id(new AphrontRedirectResponse())->setURI($done_uri);
134 if ($v_dashboard) {
135 $dashboard_phids = array($v_dashboard);
136 } else {
137 $dashboard_phids = array();
140 $form = id(new AphrontFormView())
141 ->setViewer($viewer)
142 ->appendControl(
143 id(new AphrontFormTextControl())
144 ->setLabel(pht('Name'))
145 ->setName('name')
146 ->setValue($v_name)
147 ->setError($e_name))
148 ->appendControl(
149 id(new AphrontFormTokenizerControl())
150 ->setValue($dashboard_phids)
151 ->setError($e_dashboard)
152 ->setName('dashboardPHIDs')
153 ->setLimit(1)
154 ->setDatasource(new PhabricatorDashboardDatasource())
155 ->setLabel(pht('Dashboard')));
157 return $this->newDialog()
158 ->setTitle(pht('Add Panel to Dashboard'))
159 ->setErrors($errors)
160 ->setWidth(AphrontDialogView::WIDTH_FORM)
161 ->addHiddenInput('engine', $v_engine)
162 ->addHiddenInput('query', $v_query)
163 ->appendForm($form)
164 ->addCancelButton($redirect_uri)
165 ->addSubmitButton(pht('Add Panel'));