Merge pull request #15829 from eileenmcnaughton/dedupe3
[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');
8f077bdf
SL
215 // Output UTF BOM so that MS Excel copes with diacritics. This is recommended as
216 // the Windows variant but is tested with MS Excel for Mac (Office 365 v 16.31)
217 // and it continues to work on Libre Office, Numbers, Notes etc.
218 echo "\xEF\xBB\xBF";
6a488035
TO
219 echo self::makeCsv($form, $rows);
220 CRM_Utils_System::civiExit();
221 }
222
223 /**
224 * Utility function for export2csv and CRM_Report_Form::endPostProcess
225 * - make CSV file content and return as string.
ea3ddccf 226 *
227 * @param CRM_Core_Form $form
228 * @param array $rows
229 *
230 * @return string
6a488035 231 */
00be9182 232 public static function makeCsv(&$form, &$rows) {
6a488035
TO
233 $config = CRM_Core_Config::singleton();
234 $csv = '';
235
236 // Add headers if this is the first row.
237 $columnHeaders = array_keys($form->_columnHeaders);
238
239 // Replace internal header names with friendly ones, where available.
240 foreach ($columnHeaders as $header) {
241 if (isset($form->_columnHeaders[$header])) {
242 $headers[] = '"' . html_entity_decode(strip_tags($form->_columnHeaders[$header]['title'])) . '"';
243 }
244 }
245 // Add the headers.
246 $csv .= implode($config->fieldSeparator,
353ffa53
TO
247 $headers
248 ) . "\r\n";
6a488035 249
be2fb01f 250 $displayRows = [];
6a488035
TO
251 $value = NULL;
252 foreach ($rows as $row) {
253 foreach ($columnHeaders as $k => $v) {
254 $value = CRM_Utils_Array::value($v, $row);
255 if (isset($value)) {
256 // Remove HTML, unencode entities, and escape quotation marks.
257 $value = str_replace('"', '""', html_entity_decode(strip_tags($value)));
258
259 if (CRM_Utils_Array::value('type', $form->_columnHeaders[$v]) & 4) {
260 if (CRM_Utils_Array::value('group_by', $form->_columnHeaders[$v]) == 'MONTH' ||
261 CRM_Utils_Array::value('group_by', $form->_columnHeaders[$v]) == 'QUARTER'
262 ) {
263 $value = CRM_Utils_Date::customFormat($value, $config->dateformatPartial);
264 }
265 elseif (CRM_Utils_Array::value('group_by', $form->_columnHeaders[$v]) == 'YEAR') {
266 $value = CRM_Utils_Date::customFormat($value, $config->dateformatYear);
267 }
908a11e7
EM
268 elseif ($form->_columnHeaders[$v]['type'] == 12) {
269 // This is a datetime format
270 $value = CRM_Utils_Date::customFormat($value, '%Y-%m-%d %H:%i');
271 }
6a488035
TO
272 else {
273 $value = CRM_Utils_Date::customFormat($value, '%Y-%m-%d');
274 }
275 }
182f5081 276 // Note the reference to a specific field does not belong in this generic class & does not work on all reports.
277 // @todo - fix this properly rather than just supressing the en-otice. Repeat transaction report is a good example.
278 elseif (CRM_Utils_Array::value('type', $form->_columnHeaders[$v]) == 1024 && !empty($row['civicrm_contribution_currency'])) {
39d06e38 279 $value = CRM_Utils_Money::format($value, $row['civicrm_contribution_currency']);
6a488035
TO
280 }
281 $displayRows[$v] = '"' . $value . '"';
282 }
283 else {
b5be5afe 284 $displayRows[$v] = "";
6a488035
TO
285 }
286 }
287 // Add the data row.
288 $csv .= implode($config->fieldSeparator,
353ffa53
TO
289 $displayRows
290 ) . "\r\n";
6a488035
TO
291 }
292
293 return $csv;
294 }
295
74cf4551
EM
296 /**
297 * @return mixed
298 */
00be9182 299 public static function getInstanceID() {
6a488035
TO
300
301 $config = CRM_Core_Config::singleton();
302 $arg = explode('/', $_GET[$config->userFrameworkURLVar]);
303
304 if ($arg[1] == 'report' &&
305 CRM_Utils_Array::value(2, $arg) == 'instance'
306 ) {
307 if (CRM_Utils_Rule::positiveInteger($arg[3])) {
308 return $arg[3];
309 }
310 }
311 }
312
74cf4551
EM
313 /**
314 * @return string
315 */
00be9182 316 public static function getInstancePath() {
6a488035
TO
317 $config = CRM_Core_Config::singleton();
318 $arg = explode('/', $_GET[$config->userFrameworkURLVar]);
319
320 if ($arg[1] == 'report' &&
321 CRM_Utils_Array::value(2, $arg) == 'instance'
322 ) {
323 unset($arg[0], $arg[1], $arg[2]);
324 $path = trim(CRM_Utils_Type::escape(implode('/', $arg), 'String'), '/');
325 return $path;
326 }
327 }
328
74cf4551 329 /**
100fef9d 330 * @param int $instanceId
74cf4551
EM
331 *
332 * @return bool
333 */
00be9182 334 public static function isInstancePermissioned($instanceId) {
6a488035
TO
335 if (!$instanceId) {
336 return TRUE;
337 }
338
be2fb01f
CW
339 $instanceValues = [];
340 $params = ['id' => $instanceId];
0b25329b 341 CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
6a488035
TO
342 $params,
343 $instanceValues
344 );
345
346 if (!empty($instanceValues['permission']) &&
347 (!(CRM_Core_Permission::check($instanceValues['permission']) ||
353ffa53
TO
348 CRM_Core_Permission::check('administer Reports')
349 ))
6a488035
TO
350 ) {
351 return FALSE;
352 }
353
354 return TRUE;
355 }
356
357 /**
358 * Check if the user can view a report instance based on their role(s)
359 *
360 * @instanceId string $str the report instance to check
361 *
100fef9d 362 * @param int $instanceId
fd31fa4c 363 *
acb1052e 364 * @return bool
a6c01b45 365 * true if yes, else false
6a488035 366 */
00be9182 367 public static function isInstanceGroupRoleAllowed($instanceId) {
6a488035
TO
368 if (!$instanceId) {
369 return TRUE;
370 }
371
be2fb01f
CW
372 $instanceValues = [];
373 $params = ['id' => $instanceId];
0b25329b 374 CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
6a488035
TO
375 $params,
376 $instanceValues
377 );
378 //transform grouprole to array
379 if (!empty($instanceValues['grouprole'])) {
380 $grouprole_array = explode(CRM_Core_DAO::VALUE_SEPARATOR,
381 $instanceValues['grouprole']
382 );
383 if (!CRM_Core_Permission::checkGroupRole($grouprole_array) &&
384 !CRM_Core_Permission::check('administer Reports')
385 ) {
386 return FALSE;
387 }
388 }
389 return TRUE;
390 }
391
74cf4551 392 /**
c490a46a 393 * @param array $params
74cf4551
EM
394 *
395 * @return array
396 */
00be9182 397 public static function processReport($params) {
6a488035
TO
398 $instanceId = CRM_Utils_Array::value('instanceId', $params);
399
400 // hack for now, CRM-8358
401 $_REQUEST['instanceId'] = $instanceId;
402 $_REQUEST['sendmail'] = CRM_Utils_Array::value('sendmail', $params, 1);
884605ca 403
6a488035
TO
404 // if cron is run from terminal --output is reserved, and therefore we would provide another name 'format'
405 $_REQUEST['output'] = CRM_Utils_Array::value('format', $params, CRM_Utils_Array::value('output', $params, 'pdf'));
406 $_REQUEST['reset'] = CRM_Utils_Array::value('reset', $params, 1);
407
408 $optionVal = self::getValueFromUrl($instanceId);
be2fb01f 409 $messages = ["Report Mail Triggered..."];
6a488035
TO
410
411 $templateInfo = CRM_Core_OptionGroup::getRowValues('report_template', $optionVal, 'value');
353ffa53
TO
412 $obj = new CRM_Report_Page_Instance();
413 $is_error = 0;
6a488035 414 if (strstr(CRM_Utils_Array::value('name', $templateInfo), '_Form')) {
be2fb01f
CW
415 $instanceInfo = [];
416 CRM_Report_BAO_ReportInstance::retrieve(['id' => $instanceId], $instanceInfo);
6a488035
TO
417
418 if (!empty($instanceInfo['title'])) {
419 $obj->assign('reportTitle', $instanceInfo['title']);
420 }
421 else {
422 $obj->assign('reportTitle', $templateInfo['label']);
423 }
424
425 $wrapper = new CRM_Utils_Wrapper();
be2fb01f
CW
426 $arguments = [
427 'urlToSession' => [
428 [
884605ca
DL
429 'urlVar' => 'instanceId',
430 'type' => 'Positive',
431 'sessionVar' => 'instanceId',
432 'default' => 'null',
be2fb01f
CW
433 ],
434 ],
21dfd5f5 435 'ignoreKey' => TRUE,
be2fb01f 436 ];
6a488035
TO
437 $messages[] = $wrapper->run($templateInfo['name'], NULL, $arguments);
438 }
439 else {
440 $is_error = 1;
441 if (!$instanceId) {
442 $messages[] = 'Required parameter missing: instanceId';
443 }
444 else {
445 $messages[] = 'Did not find valid instance to execute';
446 }
447 }
448
be2fb01f 449 $result = [
6a488035
TO
450 'is_error' => $is_error,
451 'messages' => implode("\n", $messages),
be2fb01f 452 ];
6a488035
TO
453 return $result;
454 }
455
456 /**
457 * Build a URL query string containing all report filter criteria that are
458 * stipulated in $_GET or in a report Preview, but which haven't yet been
459 * saved in the report instance.
460 *
7e06c9f5
TO
461 * @param array $defaults
462 * The report criteria that aren't coming in as submitted form values, as in CRM_Report_Form::_defaults.
463 * @param array $params
464 * All effective report criteria, as in CRM_Report_Form::_params.
6a488035 465 *
a6c01b45
CW
466 * @return string
467 * URL query string
6a488035 468 */
be2fb01f 469 public static function getPreviewCriteriaQueryParams($defaults = [], $params = []) {
6a488035
TO
470 static $query_string;
471 if (!isset($query_string)) {
472 if (!empty($params)) {
be2fb01f 473 $url_params = $op_values = $string_values = $process_params = [];
6a488035
TO
474
475 // We'll only use $params that are different from what's in $default.
476 foreach ($params as $field_name => $field_value) {
477 if (!array_key_exists($field_name, $defaults) || $defaults[$field_name] != $field_value) {
478 $process_params[$field_name] = $field_value;
479 }
480 }
481 // Criteria stipulated in $_GET will be in $defaults even if they're not
482 // saved, so we can't easily tell if they're saved or not. So just include them.
483 $process_params += $_GET;
484
485 // All $process_params should be passed on if they have an effective value
486 // (in other words, there's no point in propagating blank filters).
487 foreach ($process_params as $field_name => $field_value) {
488 $suffix_position = strrpos($field_name, '_');
353ffa53
TO
489 $suffix = substr($field_name, $suffix_position);
490 $basename = substr($field_name, 0, $suffix_position);
6a488035
TO
491 if ($suffix == '_min' || $suffix == '_max' ||
492 $suffix == '_from' || $suffix == '_to' ||
493 $suffix == '_relative'
494 ) {
495 // For these types, we only keep them if they have a value.
496 if (!empty($field_value)) {
497 $url_params[$field_name] = $field_value;
498 }
499 }
500 elseif ($suffix == '_value') {
501 // These filters can have an effect even without a value
502 // (e.g., values for 'nll' and 'nnll' ops are blank),
503 // so store them temporarily and examine below.
504 $string_values[$basename] = $field_value;
505 $op_values[$basename] = CRM_Utils_Array::value("{$basename}_op", $params);
506 }
507 elseif ($suffix == '_op') {
508 // These filters can have an effect even without a value
509 // (e.g., values for 'nll' and 'nnll' ops are blank),
510 // so store them temporarily and examine below.
511 $op_values[$basename] = $field_value;
512 $string_values[$basename] = $params["{$basename}_value"];
513 }
514 }
515
516 // Check the *_value and *_op criteria and include them if
517 // they'll have an effective value.
518 foreach ($op_values as $basename => $field_value) {
519 if ($field_value == 'nll' || $field_value == 'nnll') {
520 // 'nll' and 'nnll' filters should be included even with empty values.
521 $url_params["{$basename}_op"] = $field_value;
522 }
523 elseif ($string_values[$basename]) {
524 // Other filters are only included if they have a value.
525 $url_params["{$basename}_op"] = $field_value;
526 $url_params["{$basename}_value"] = (is_array($string_values[$basename]) ? implode(',', $string_values[$basename]) : $string_values[$basename]);
527 }
528 }
529 $query_string = http_build_query($url_params);
530 }
531 else {
532 $query_string = '';
533 }
534 }
535 return $query_string;
536 }
537
74cf4551
EM
538 /**
539 * @param $reportUrl
540 *
541 * @return mixed
542 */
00be9182 543 public static function getInstanceList($reportUrl) {
be2fb01f 544 static $instanceDetails = [];
6001af1f 545
481a74f4 546 if (!array_key_exists($reportUrl, $instanceDetails)) {
be2fb01f 547 $instanceDetails[$reportUrl] = [];
6a488035
TO
548
549 $sql = "
550SELECT id, title FROM civicrm_report_instance
551WHERE report_id = %1";
be2fb01f 552 $params = [1 => [$reportUrl, 'String']];
6a488035 553 $result = CRM_Core_DAO::executeQuery($sql, $params);
22e263ad 554 while ($result->fetch()) {
6a488035
TO
555 $instanceDetails[$reportUrl][$result->id] = $result->title . " (ID: {$result->id})";
556 }
557 }
558 return $instanceDetails[$reportUrl];
559 }
96025800 560
6a488035 561}