-
-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathInstanceList.php
297 lines (268 loc) · 9.62 KB
/
InstanceList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
/**
* Page for invoking report instances.
*/
class CRM_Report_Page_InstanceList extends CRM_Core_Page {
public static $_links = NULL;
public static $_exceptions = ['logging/contact/detail'];
/**
* Name of component if report list is filtered.
*
* @var string
*/
protected $_compName = NULL;
/**
* ID of component if report list is filtered.
*
* @var int
*/
protected $compID = NULL;
/**
* ID of grouping if report list is filtered.
*
* @var int
*/
protected $grouping = NULL;
/**
* Possibly always null.... maybe $_title is used...
*
* The relationship between this & $_title is ambigous & seemingly not worked through.
*
* @var string
*/
protected $title;
/**
* ID of parent report template if list is filtered by template.
*
* @var int
*/
protected $ovID = NULL;
/**
* Title of parent report template if list is filtered by template.
*
* @var string
*/
protected $_title = NULL;
/**
* @var string
*/
protected $myReports;
/**
* Retrieves report instances, optionally filtered.
*
* Filtering available by parent report template ($ovID) or by component ($compID).
*
* @return array
*/
public function info() {
$report = '';
$queryParams = [];
if ($this->ovID) {
$report .= " AND v.id = %1 ";
$queryParams[1] = [$this->ovID, 'Integer'];
}
if ($this->compID) {
if ($this->compID == 99) {
$report .= " AND v.component_id IS NULL ";
$this->_compName = 'Contact';
}
else {
$report .= " AND v.component_id = %2 ";
$queryParams[2] = [$this->compID, 'Integer'];
$cmpName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component', $this->compID,
'name', 'id'
);
$this->_compName = substr($cmpName, 4);
if ($this->_compName == 'Contribute') {
$this->_compName = 'Contribution';
}
}
}
elseif ($this->grouping) {
$report .= " AND v.grouping = %3 ";
$queryParams[3] = [$this->grouping, 'String'];
}
elseif ($this->myReports) {
$report .= " AND inst.owner_id = %4 ";
$queryParams[4] = [CRM_Core_Session::getLoggedInContactID(), 'Integer'];
}
$sql = "
SELECT inst.id, inst.title, inst.report_id, inst.description, inst.owner_id, v.label, v.grouping, v.name as class_name,
CASE
WHEN comp.name IS NOT NULL THEN SUBSTRING(comp.name, 5)
WHEN v.grouping IS NOT NULL THEN v.grouping
ELSE 'Contact'
END as compName
FROM civicrm_option_group g
LEFT JOIN civicrm_option_value v
ON v.option_group_id = g.id AND
g.name = 'report_template'
LEFT JOIN civicrm_report_instance inst
ON v.value = inst.report_id
LEFT JOIN civicrm_component comp
ON v.component_id = comp.id
WHERE v.is_active = 1 {$report}
AND inst.domain_id = %9
ORDER BY v.weight ASC, inst.title ASC";
$queryParams[9] = [CRM_Core_Config::domainID(), 'Integer'];
$dao = CRM_Core_DAO::executeQuery($sql, $queryParams);
$rows = [];
$url = 'civicrm/report/instance';
$my_reports_grouping = 'My';
while ($dao->fetch()) {
if (in_array($dao->report_id, self::$_exceptions)) {
continue;
}
$enabled = CRM_Core_Component::isEnabled("Civi{$dao->compName}");
if ($dao->compName == 'Contact' || $dao->compName == $dao->grouping) {
$enabled = TRUE;
}
// filter report listings for private reports
if (!empty($dao->owner_id) && CRM_Core_Session::getLoggedInContactID() != $dao->owner_id) {
continue;
}
//filter report listings by permissions
if (!($enabled && CRM_Report_Utils_Report::isInstancePermissioned($dao->id))) {
continue;
}
//filter report listing by group/role
if (!($enabled && CRM_Report_Utils_Report::isInstanceGroupRoleAllowed($dao->id))) {
continue;
}
if (trim($dao->title ?? '')) {
if ($this->ovID) {
$this->title = ts("Report(s) created from the template: %1", [1 => $dao->label]);
}
$report_grouping = $dao->compName;
if ($dao->owner_id != NULL) {
$report_grouping = $my_reports_grouping;
}
$rows[$report_grouping][$dao->id]['title'] = $dao->title;
$rows[$report_grouping][$dao->id]['label'] = $dao->label;
$rows[$report_grouping][$dao->id]['description'] = $dao->description;
$rows[$report_grouping][$dao->id]['url'] = CRM_Utils_System::url("{$url}/{$dao->id}", "reset=1&output=criteria");
$rows[$report_grouping][$dao->id]['viewUrl'] = CRM_Utils_System::url("{$url}/{$dao->id}", 'force=1&reset=1');
$rows[$report_grouping][$dao->id]['actions'] = $this->getActionLinks($dao->id, $dao->class_name);
}
}
// Move My Reports to the beginning of the reports list
if (isset($rows[$my_reports_grouping])) {
$my_reports = $rows[$my_reports_grouping];
unset($rows[$my_reports_grouping]);
$rows = [$my_reports_grouping => $my_reports] + $rows;
}
return $rows;
}
/**
* Run this page (figure out the action needed and perform it).
*/
public function run() {
//Filters by source report template or by component
$this->ovID = CRM_Utils_Request::retrieve('ovid', 'Positive', $this);
$this->myReports = CRM_Utils_Request::retrieve('myreports', 'String', $this);
$this->compID = CRM_Utils_Request::retrieve('compid', 'Positive', $this);
$this->grouping = CRM_Utils_Request::retrieve('grp', 'String', $this);
$rows = $this->info();
$this->assign('title', $this->title);
$this->assign('list', $rows);
if ($this->ovID or $this->compID) {
// link to view all reports
$reportUrl = CRM_Utils_System::url('civicrm/report/list', "reset=1");
if (!$this->ovID) {
CRM_Utils_System::setTitle(ts('%1 Reports', [1 => $this->_compName]));
}
}
$this->assign('reportUrl', $reportUrl ?? FALSE);
// assign link to template list for users with appropriate permissions
if (CRM_Core_Permission::check('administer Reports')) {
if ($this->compID) {
$newButton = ts('New %1 Report', [1 => $this->_compName]);
$templateUrl = CRM_Utils_System::url('civicrm/report/template/list', "reset=1&compid={$this->compID}");
}
else {
$newButton = ts('New Report');
$templateUrl = CRM_Utils_System::url('civicrm/report/template/list', "reset=1");
}
$this->assign('newButton', $newButton);
$this->assign('compName', $this->_compName);
}
$this->assign('myReports', $this->myReports);
$this->assign('templateUrl', $templateUrl ?? NULL);
return parent::run();
}
/**
* Get action links.
*
* @param int $instanceID
* @param string $className
*
* @return array
*/
protected function getActionLinks($instanceID, $className) {
$urlCommon = 'civicrm/report/instance/' . $instanceID;
$actions = [
'copy' => [
'url' => CRM_Utils_System::url($urlCommon, 'reset=1&output=copy'),
'label' => ts('Save a Copy'),
'confirm_message' => NULL,
'weight' => CRM_Core_Action::getWeight(\CRM_Core_Action::COPY),
],
'pdf' => [
'url' => CRM_Utils_System::url($urlCommon, 'reset=1&force=1&output=pdf'),
'label' => ts('View as pdf'),
'confirm_message' => NULL,
'weight' => CRM_Core_Action::getWeight(\CRM_Core_Action::EXPORT),
],
'print' => [
'url' => CRM_Utils_System::url($urlCommon, 'reset=1&force=1&output=print'),
'label' => ts('Print report'),
'confirm_message' => NULL,
'weight' => CRM_Core_Action::getWeight(\CRM_Core_Action::EXPORT),
],
];
// Hackery, Hackera, Hacker ahahahahahaha a super nasty hack.
// Almost all report classes support csv & loading each class to call the method seems too
// expensive. We also have on our later list 'do they support charts' which is instance specific
// e.g use of group by might affect it. So, lets just skip for the few that don't for now.
$csvBlackList = [
'CRM_Report_Form_Contact_Detail',
'CRM_Report_Form_Event_Income',
];
if (!in_array($className, $csvBlackList)) {
$actions['csv'] = [
'url' => CRM_Utils_System::url($urlCommon, 'reset=1&force=1&output=csv'),
'label' => ts('Export to csv'),
'confirm_message' => NULL,
'weight' => CRM_Core_Action::getWeight(\CRM_Core_Action::EXPORT),
];
}
if (CRM_Core_Permission::check('administer Reports')) {
$actions['delete'] = [
'url' => CRM_Utils_System::url($urlCommon, 'reset=1&action=delete'),
'label' => ts('Delete report'),
'confirm_message' => ts('Are you sure you want delete this report? This action cannot be undone.'),
'weight' => CRM_Core_Action::getWeight(\CRM_Core_Action::DELETE),
];
}
CRM_Utils_Hook::links('view.report.links',
$className,
$instanceID,
$actions
);
return $actions;
}
}