Merge pull request #22586 from braders/checkTemplateFileExists-consistency-phpdoc
[civicrm-core.git] / CRM / Report / Utils / Report.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Report_Utils_Report {
18
74cf4551 19 /**
100fef9d 20 * @param int $instanceID
74cf4551
EM
21 *
22 * @return null|string
23 */
00be9182 24 public static function getValueFromUrl($instanceID = NULL) {
6a488035 25 if ($instanceID) {
0b25329b 26 $optionVal = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance',
6a488035
TO
27 $instanceID,
28 'report_id'
29 );
30 }
31 else {
13594d55 32 $args = explode('/', CRM_Utils_System::currentPath());
6a488035
TO
33
34 // remove 'civicrm/report' from args
35 array_shift($args);
36 array_shift($args);
37
b44e3f84 38 // put rest of argument back in the form of url, which is how value
6a488035
TO
39 // is stored in option value table
40 $optionVal = implode('/', $args);
41 }
42 return $optionVal;
43 }
44
74cf4551 45 /**
100fef9d 46 * @param int $instanceID
74cf4551
EM
47 *
48 * @return array|bool
49 */
00be9182 50 public static function getValueIDFromUrl($instanceID = NULL) {
6a488035
TO
51 $optionVal = self::getValueFromUrl($instanceID);
52
53 if ($optionVal) {
54 $templateInfo = CRM_Core_OptionGroup::getRowValues('report_template', "{$optionVal}", 'value');
be2fb01f 55 return [CRM_Utils_Array::value('id', $templateInfo), $optionVal];
6a488035
TO
56 }
57
58 return FALSE;
59 }
60
74cf4551
EM
61 /**
62 * @param $optionVal
63 *
64 * @return mixed
65 */
00be9182 66 public static function getInstanceIDForValue($optionVal) {
be2fb01f 67 static $valId = [];
6a488035
TO
68
69 if (!array_key_exists($optionVal, $valId)) {
70 $sql = "
71SELECT MIN(id) FROM civicrm_report_instance
72WHERE report_id = %1";
73
be2fb01f 74 $params = [1 => [$optionVal, 'String']];
6a488035
TO
75 $valId[$optionVal] = CRM_Core_DAO::singleValueQuery($sql, $params);
76 }
77 return $valId[$optionVal];
78 }
79
74cf4551
EM
80 /**
81 * @param null $path
82 *
83 * @return mixed
84 */
00be9182 85 public static function getInstanceIDForPath($path = NULL) {
be2fb01f 86 static $valId = [];
6a488035
TO
87
88 // if $path is null, try to get it from url
89 $path = self::getInstancePath();
90
91 if ($path && !array_key_exists($path, $valId)) {
92 $sql = "
93SELECT MIN(id) FROM civicrm_report_instance
94WHERE TRIM(BOTH '/' FROM CONCAT(report_id, '/', name)) = %1";
95
be2fb01f 96 $params = [1 => [$path, 'String']];
6a488035
TO
97 $valId[$path] = CRM_Core_DAO::singleValueQuery($sql, $params);
98 }
914d3734 99 return $valId[$path] ?? NULL;
6a488035
TO
100 }
101
74cf4551
EM
102 /**
103 * @param $urlValue
104 * @param string $query
105 * @param bool $absolute
100fef9d 106 * @param int $instanceID
74cf4551
EM
107 * @param array $drilldownReport
108 *
109 * @return bool|string
110 */
be2fb01f 111 public static function getNextUrl($urlValue, $query = 'reset=1', $absolute = FALSE, $instanceID = NULL, $drilldownReport = []) {
6a488035 112 if ($instanceID) {
84178120
TO
113 $drilldownInstanceID = FALSE;
114 if (array_key_exists($urlValue, $drilldownReport)) {
0b25329b 115 $drilldownInstanceID = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'drilldown_id', 'id');
84178120 116 }
6a488035 117
84178120 118 if (!$drilldownInstanceID) {
6a488035 119 $drilldownInstanceID = self::getInstanceIDForValue($urlValue);
84178120 120 }
6a488035
TO
121
122 if ($drilldownInstanceID) {
123 return CRM_Utils_System::url("civicrm/report/instance/{$drilldownInstanceID}",
124 "{$query}", $absolute
125 );
126 }
127 else {
128 return FALSE;
129 }
130 }
131 else {
132 return CRM_Utils_System::url("civicrm/report/" . trim($urlValue, '/'),
133 $query, $absolute
134 );
135 }
136 }
137
74cf4551 138 /**
fe482240 139 * get instance count for a template.
74cf4551
EM
140 * @param $optionVal
141 *
142 * @return int|null|string
143 */
00be9182 144 public static function getInstanceCount($optionVal) {
4f99ca55
TO
145 if (empty($optionVal)) {
146 return 0;
84178120 147 }
ae555e90 148
6a488035
TO
149 $sql = "
150SELECT count(inst.id)
151FROM civicrm_report_instance inst
152WHERE inst.report_id = %1";
153
be2fb01f 154 $params = [1 => [$optionVal, 'String']];
6a488035
TO
155 $count = CRM_Core_DAO::singleValueQuery($sql, $params);
156 return $count;
157 }
158
74cf4551
EM
159 /**
160 * @param $fileContent
100fef9d 161 * @param int $instanceID
74cf4551
EM
162 * @param string $outputMode
163 * @param array $attachments
164 *
165 * @return bool
166 */
be2fb01f 167 public static function mailReport($fileContent, $instanceID = NULL, $outputMode = 'html', $attachments = []) {
6a488035
TO
168 if (!$instanceID) {
169 return FALSE;
170 }
171
172 list($domainEmailName,
173 $domainEmailAddress
353ffa53 174 ) = CRM_Core_BAO_Domain::getNameAndEmail();
6a488035 175
be2fb01f
CW
176 $params = ['id' => $instanceID];
177 $instanceInfo = [];
0b25329b 178 CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
6a488035
TO
179 $params,
180 $instanceInfo
181 );
182
be2fb01f 183 $params = [];
6a488035 184 $params['groupName'] = 'Report Email Sender';
353ffa53 185 $params['from'] = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
6a488035 186 //$domainEmailName;
353ffa53 187 $params['toName'] = "";
9c1bc317
CW
188 $params['toEmail'] = $instanceInfo['email_to'] ?? NULL;
189 $params['cc'] = $instanceInfo['email_cc'] ?? NULL;
190 $params['subject'] = $instanceInfo['email_subject'] ?? NULL;
a7488080 191 if (empty($instanceInfo['attachments'])) {
be2fb01f 192 $instanceInfo['attachments'] = [];
6a488035
TO
193 }
194 $params['attachments'] = array_merge(CRM_Utils_Array::value('attachments', $instanceInfo), $attachments);
195 $params['text'] = '';
196 $params['html'] = $fileContent;
197
198 return CRM_Utils_Mail::send($params);
199 }
200
74cf4551 201 /**
c490a46a 202 * @param CRM_Core_Form $form
74cf4551
EM
203 * @param $rows
204 */
00be9182 205 public static function export2csv(&$form, &$rows) {
6a488035 206 //Mark as a CSV file.
d42a224c 207 CRM_Utils_System::setHttpHeader('Content-Type', 'text/csv');
6a488035
TO
208
209 //Force a download and name the file using the current timestamp.
210 $datetime = date('Ymd-Gi', $_SERVER['REQUEST_TIME']);
d42a224c 211 CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename=Report_' . $datetime . '.csv');
6a488035
TO
212 echo self::makeCsv($form, $rows);
213 CRM_Utils_System::civiExit();
214 }
215
216 /**
217 * Utility function for export2csv and CRM_Report_Form::endPostProcess
218 * - make CSV file content and return as string.
ea3ddccf 219 *
220 * @param CRM_Core_Form $form
221 * @param array $rows
222 *
223 * @return string
6a488035 224 */
00be9182 225 public static function makeCsv(&$form, &$rows) {
6a488035 226 $config = CRM_Core_Config::singleton();
d0d1c38b 227
228 // Output UTF BOM so that MS Excel copes with diacritics. This is recommended as
229 // the Windows variant but is tested with MS Excel for Mac (Office 365 v 16.31)
230 // and it continues to work on Libre Office, Numbers, Notes etc.
231 $csv = "\xEF\xBB\xBF";
6a488035
TO
232
233 // Add headers if this is the first row.
234 $columnHeaders = array_keys($form->_columnHeaders);
235
236 // Replace internal header names with friendly ones, where available.
237 foreach ($columnHeaders as $header) {
238 if (isset($form->_columnHeaders[$header])) {
239 $headers[] = '"' . html_entity_decode(strip_tags($form->_columnHeaders[$header]['title'])) . '"';
240 }
241 }
242 // Add the headers.
243 $csv .= implode($config->fieldSeparator,
353ffa53
TO
244 $headers
245 ) . "\r\n";
6a488035 246
be2fb01f 247 $displayRows = [];
6a488035
TO
248 $value = NULL;
249 foreach ($rows as $row) {
250 foreach ($columnHeaders as $k => $v) {
9c1bc317 251 $value = $row[$v] ?? NULL;
6a488035
TO
252 if (isset($value)) {
253 // Remove HTML, unencode entities, and escape quotation marks.
4e017cbe 254 $value = str_replace('"', '""', html_entity_decode(strip_tags($value), ENT_QUOTES | ENT_HTML401));
6a488035
TO
255
256 if (CRM_Utils_Array::value('type', $form->_columnHeaders[$v]) & 4) {
257 if (CRM_Utils_Array::value('group_by', $form->_columnHeaders[$v]) == 'MONTH' ||
258 CRM_Utils_Array::value('group_by', $form->_columnHeaders[$v]) == 'QUARTER'
259 ) {
260 $value = CRM_Utils_Date::customFormat($value, $config->dateformatPartial);
261 }
262 elseif (CRM_Utils_Array::value('group_by', $form->_columnHeaders[$v]) == 'YEAR') {
263 $value = CRM_Utils_Date::customFormat($value, $config->dateformatYear);
264 }
908a11e7
EM
265 elseif ($form->_columnHeaders[$v]['type'] == 12) {
266 // This is a datetime format
267 $value = CRM_Utils_Date::customFormat($value, '%Y-%m-%d %H:%i');
268 }
6a488035
TO
269 else {
270 $value = CRM_Utils_Date::customFormat($value, '%Y-%m-%d');
271 }
272 }
182f5081 273 // Note the reference to a specific field does not belong in this generic class & does not work on all reports.
274 // @todo - fix this properly rather than just supressing the en-otice. Repeat transaction report is a good example.
275 elseif (CRM_Utils_Array::value('type', $form->_columnHeaders[$v]) == 1024 && !empty($row['civicrm_contribution_currency'])) {
39d06e38 276 $value = CRM_Utils_Money::format($value, $row['civicrm_contribution_currency']);
6a488035
TO
277 }
278 $displayRows[$v] = '"' . $value . '"';
279 }
280 else {
b5be5afe 281 $displayRows[$v] = "";
6a488035
TO
282 }
283 }
284 // Add the data row.
285 $csv .= implode($config->fieldSeparator,
353ffa53
TO
286 $displayRows
287 ) . "\r\n";
6a488035
TO
288 }
289
290 return $csv;
291 }
292
74cf4551
EM
293 /**
294 * @return mixed
295 */
00be9182 296 public static function getInstanceID() {
6a488035 297
13594d55 298 $arg = explode('/', CRM_Utils_System::currentPath());
6a488035 299
13594d55
CW
300 if (isset($arg[3]) && $arg[1] == 'report' && $arg[2] == 'instance' && CRM_Utils_Rule::positiveInteger($arg[3])) {
301 return $arg[3];
6a488035
TO
302 }
303 }
304
74cf4551
EM
305 /**
306 * @return string
307 */
00be9182 308 public static function getInstancePath() {
13594d55 309 $arg = explode('/', CRM_Utils_System::currentPath());
6a488035 310
13594d55 311 if (isset($arg[3]) && $arg[1] == 'report' && $arg[2] == 'instance') {
6a488035 312 unset($arg[0], $arg[1], $arg[2]);
13594d55 313 return trim(CRM_Utils_Type::escape(implode('/', $arg), 'String'), '/');
6a488035
TO
314 }
315 }
316
74cf4551 317 /**
100fef9d 318 * @param int $instanceId
74cf4551
EM
319 *
320 * @return bool
321 */
00be9182 322 public static function isInstancePermissioned($instanceId) {
6a488035
TO
323 if (!$instanceId) {
324 return TRUE;
325 }
326
be2fb01f
CW
327 $instanceValues = [];
328 $params = ['id' => $instanceId];
0b25329b 329 CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
6a488035
TO
330 $params,
331 $instanceValues
332 );
333
334 if (!empty($instanceValues['permission']) &&
335 (!(CRM_Core_Permission::check($instanceValues['permission']) ||
353ffa53
TO
336 CRM_Core_Permission::check('administer Reports')
337 ))
6a488035
TO
338 ) {
339 return FALSE;
340 }
341
342 return TRUE;
343 }
344
345 /**
346 * Check if the user can view a report instance based on their role(s)
347 *
348 * @instanceId string $str the report instance to check
349 *
100fef9d 350 * @param int $instanceId
fd31fa4c 351 *
acb1052e 352 * @return bool
a6c01b45 353 * true if yes, else false
6a488035 354 */
00be9182 355 public static function isInstanceGroupRoleAllowed($instanceId) {
6a488035
TO
356 if (!$instanceId) {
357 return TRUE;
358 }
359
be2fb01f
CW
360 $instanceValues = [];
361 $params = ['id' => $instanceId];
0b25329b 362 CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
6a488035
TO
363 $params,
364 $instanceValues
365 );
366 //transform grouprole to array
367 if (!empty($instanceValues['grouprole'])) {
368 $grouprole_array = explode(CRM_Core_DAO::VALUE_SEPARATOR,
369 $instanceValues['grouprole']
370 );
371 if (!CRM_Core_Permission::checkGroupRole($grouprole_array) &&
372 !CRM_Core_Permission::check('administer Reports')
373 ) {
374 return FALSE;
375 }
376 }
377 return TRUE;
378 }
379
74cf4551 380 /**
c490a46a 381 * @param array $params
74cf4551
EM
382 *
383 * @return array
384 */
00be9182 385 public static function processReport($params) {
9c1bc317 386 $instanceId = $params['instanceId'] ?? NULL;
6a488035
TO
387
388 // hack for now, CRM-8358
389 $_REQUEST['instanceId'] = $instanceId;
390 $_REQUEST['sendmail'] = CRM_Utils_Array::value('sendmail', $params, 1);
884605ca 391
6a488035
TO
392 // if cron is run from terminal --output is reserved, and therefore we would provide another name 'format'
393 $_REQUEST['output'] = CRM_Utils_Array::value('format', $params, CRM_Utils_Array::value('output', $params, 'pdf'));
394 $_REQUEST['reset'] = CRM_Utils_Array::value('reset', $params, 1);
395
396 $optionVal = self::getValueFromUrl($instanceId);
6e1fbb97 397 $messages = ['Report Mail Triggered...'];
6a488035
TO
398
399 $templateInfo = CRM_Core_OptionGroup::getRowValues('report_template', $optionVal, 'value');
353ffa53
TO
400 $obj = new CRM_Report_Page_Instance();
401 $is_error = 0;
6a488035 402 if (strstr(CRM_Utils_Array::value('name', $templateInfo), '_Form')) {
be2fb01f
CW
403 $instanceInfo = [];
404 CRM_Report_BAO_ReportInstance::retrieve(['id' => $instanceId], $instanceInfo);
6a488035
TO
405
406 if (!empty($instanceInfo['title'])) {
407 $obj->assign('reportTitle', $instanceInfo['title']);
408 }
409 else {
410 $obj->assign('reportTitle', $templateInfo['label']);
411 }
412
413 $wrapper = new CRM_Utils_Wrapper();
be2fb01f
CW
414 $arguments = [
415 'urlToSession' => [
416 [
884605ca
DL
417 'urlVar' => 'instanceId',
418 'type' => 'Positive',
419 'sessionVar' => 'instanceId',
420 'default' => 'null',
be2fb01f
CW
421 ],
422 ],
21dfd5f5 423 'ignoreKey' => TRUE,
be2fb01f 424 ];
6a488035
TO
425 $messages[] = $wrapper->run($templateInfo['name'], NULL, $arguments);
426 }
427 else {
428 $is_error = 1;
429 if (!$instanceId) {
430 $messages[] = 'Required parameter missing: instanceId';
431 }
432 else {
433 $messages[] = 'Did not find valid instance to execute';
434 }
435 }
436
be2fb01f 437 $result = [
6a488035
TO
438 'is_error' => $is_error,
439 'messages' => implode("\n", $messages),
be2fb01f 440 ];
6a488035
TO
441 return $result;
442 }
443
444 /**
445 * Build a URL query string containing all report filter criteria that are
446 * stipulated in $_GET or in a report Preview, but which haven't yet been
447 * saved in the report instance.
448 *
7e06c9f5
TO
449 * @param array $defaults
450 * The report criteria that aren't coming in as submitted form values, as in CRM_Report_Form::_defaults.
451 * @param array $params
452 * All effective report criteria, as in CRM_Report_Form::_params.
6a488035 453 *
a6c01b45
CW
454 * @return string
455 * URL query string
6a488035 456 */
be2fb01f 457 public static function getPreviewCriteriaQueryParams($defaults = [], $params = []) {
6a488035
TO
458 static $query_string;
459 if (!isset($query_string)) {
460 if (!empty($params)) {
be2fb01f 461 $url_params = $op_values = $string_values = $process_params = [];
6a488035
TO
462
463 // We'll only use $params that are different from what's in $default.
464 foreach ($params as $field_name => $field_value) {
465 if (!array_key_exists($field_name, $defaults) || $defaults[$field_name] != $field_value) {
466 $process_params[$field_name] = $field_value;
467 }
468 }
469 // Criteria stipulated in $_GET will be in $defaults even if they're not
470 // saved, so we can't easily tell if they're saved or not. So just include them.
471 $process_params += $_GET;
472
473 // All $process_params should be passed on if they have an effective value
474 // (in other words, there's no point in propagating blank filters).
475 foreach ($process_params as $field_name => $field_value) {
476 $suffix_position = strrpos($field_name, '_');
353ffa53
TO
477 $suffix = substr($field_name, $suffix_position);
478 $basename = substr($field_name, 0, $suffix_position);
6a488035
TO
479 if ($suffix == '_min' || $suffix == '_max' ||
480 $suffix == '_from' || $suffix == '_to' ||
481 $suffix == '_relative'
482 ) {
483 // For these types, we only keep them if they have a value.
484 if (!empty($field_value)) {
485 $url_params[$field_name] = $field_value;
486 }
487 }
488 elseif ($suffix == '_value') {
489 // These filters can have an effect even without a value
490 // (e.g., values for 'nll' and 'nnll' ops are blank),
491 // so store them temporarily and examine below.
492 $string_values[$basename] = $field_value;
9c1bc317 493 $op_values[$basename] = $params["{$basename}_op"] ?? NULL;
6a488035
TO
494 }
495 elseif ($suffix == '_op') {
496 // These filters can have an effect even without a value
497 // (e.g., values for 'nll' and 'nnll' ops are blank),
498 // so store them temporarily and examine below.
499 $op_values[$basename] = $field_value;
500 $string_values[$basename] = $params["{$basename}_value"];
501 }
502 }
503
504 // Check the *_value and *_op criteria and include them if
505 // they'll have an effective value.
506 foreach ($op_values as $basename => $field_value) {
507 if ($field_value == 'nll' || $field_value == 'nnll') {
508 // 'nll' and 'nnll' filters should be included even with empty values.
509 $url_params["{$basename}_op"] = $field_value;
510 }
511 elseif ($string_values[$basename]) {
512 // Other filters are only included if they have a value.
513 $url_params["{$basename}_op"] = $field_value;
514 $url_params["{$basename}_value"] = (is_array($string_values[$basename]) ? implode(',', $string_values[$basename]) : $string_values[$basename]);
515 }
516 }
517 $query_string = http_build_query($url_params);
518 }
519 else {
520 $query_string = '';
521 }
522 }
523 return $query_string;
524 }
525
74cf4551
EM
526 /**
527 * @param $reportUrl
528 *
529 * @return mixed
530 */
00be9182 531 public static function getInstanceList($reportUrl) {
be2fb01f 532 static $instanceDetails = [];
6001af1f 533
481a74f4 534 if (!array_key_exists($reportUrl, $instanceDetails)) {
be2fb01f 535 $instanceDetails[$reportUrl] = [];
6a488035
TO
536
537 $sql = "
538SELECT id, title FROM civicrm_report_instance
539WHERE report_id = %1";
be2fb01f 540 $params = [1 => [$reportUrl, 'String']];
6a488035 541 $result = CRM_Core_DAO::executeQuery($sql, $params);
22e263ad 542 while ($result->fetch()) {
6a488035
TO
543 $instanceDetails[$reportUrl][$result->id] = $result->title . " (ID: {$result->id})";
544 }
545 }
546 return $instanceDetails[$reportUrl];
547 }
96025800 548
6a488035 549}