Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/dashboard/controller/PhabricatorDashboardQueryPanelInstallController.php
12242 views
1
<?php
2
3
final class PhabricatorDashboardQueryPanelInstallController
4
extends PhabricatorDashboardController {
5
6
public function handleRequest(AphrontRequest $request) {
7
$viewer = $request->getViewer();
8
9
$v_dashboard = null;
10
$e_dashboard = null;
11
12
$v_name = null;
13
$e_name = true;
14
15
$v_engine = $request->getStr('engine');
16
if (!strlen($v_engine)) {
17
$v_engine = $request->getURIData('engineKey');
18
}
19
20
$v_query = $request->getStr('query');
21
if (!strlen($v_query)) {
22
$v_query = $request->getURIData('queryKey');
23
}
24
25
$engines = PhabricatorApplicationSearchEngine::getAllEngines();
26
$engine = idx($engines, $v_engine);
27
if ($engine) {
28
$engine = id(clone $engine)
29
->setViewer($viewer);
30
31
$redirect_uri = $engine->getQueryResultsPageURI($v_query);
32
33
$named_query = idx($engine->loadEnabledNamedQueries(), $v_query);
34
if ($named_query) {
35
$v_name = $named_query->getQueryName();
36
}
37
} else {
38
$redirect_uri = '/';
39
}
40
41
$errors = array();
42
43
$xaction_name = PhabricatorDashboardPanelNameTransaction::TRANSACTIONTYPE;
44
$xaction_engine =
45
PhabricatorDashboardQueryPanelApplicationTransaction::TRANSACTIONTYPE;
46
$xaction_query =
47
PhabricatorDashboardQueryPanelQueryTransaction::TRANSACTIONTYPE;
48
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');
54
}
55
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');
68
}
69
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.');
77
}
78
}
79
80
if (!$errors) {
81
$done_uri = $dashboard->getURI();
82
83
// First, create a new panel.
84
85
$panel_type = id(new PhabricatorDashboardQueryPanelType())
86
->getPanelTypeKey();
87
88
$panel = PhabricatorDashboardPanel::initializeNewPanel($viewer)
89
->setPanelType($panel_type);
90
91
$xactions = array();
92
93
$xactions[] = $panel->getApplicationTransactionTemplate()
94
->setTransactionType($xaction_engine)
95
->setNewValue($v_engine);
96
97
$xactions[] = $panel->getApplicationTransactionTemplate()
98
->setTransactionType($xaction_query)
99
->setNewValue($v_query);
100
101
$xactions[] = $panel->getApplicationTransactionTemplate()
102
->setTransactionType($xaction_name)
103
->setNewValue($v_name);
104
105
$editor = $panel->getApplicationTransactionEditor()
106
->setActor($viewer)
107
->setContentSourceFromRequest($request)
108
->applyTransactions($panel, $xactions);
109
110
// Now that we've created a panel, add it to the dashboard.
111
112
$xactions = array();
113
114
$ref_list = clone $dashboard->getPanelRefList();
115
$ref_list->newPanelRef($panel);
116
$new_panels = $ref_list->toDictionary();
117
118
$xactions[] = $dashboard->getApplicationTransactionTemplate()
119
->setTransactionType(
120
PhabricatorDashboardPanelsTransaction::TRANSACTIONTYPE)
121
->setNewValue($new_panels);
122
123
$editor = $dashboard->getApplicationTransactionEditor()
124
->setActor($viewer)
125
->setContentSourceFromRequest($request)
126
->setContinueOnNoEffect(true)
127
->setContinueOnMissingFields(true)
128
->applyTransactions($dashboard, $xactions);
129
130
return id(new AphrontRedirectResponse())->setURI($done_uri);
131
}
132
}
133
134
if ($v_dashboard) {
135
$dashboard_phids = array($v_dashboard);
136
} else {
137
$dashboard_phids = array();
138
}
139
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')));
156
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'));
166
167
}
168
169
}
170
171