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