Merge pull request #15841 from mattwire/participant_cleanup_removeparticipantfrominput
[civicrm-core.git] / CRM / Report / Utils / Report.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19 class CRM_Report_Utils_Report {
20
21 /**
22 * @param int $instanceID
23 *
24 * @return null|string
25 */
26 public static function getValueFromUrl($instanceID = NULL) {
27 if ($instanceID) {
28 $optionVal = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance',
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
41 // put rest of argument back in the form of url, which is how value
42 // is stored in option value table
43 $optionVal = implode('/', $args);
44 }
45 return $optionVal;
46 }
47
48 /**
49 * @param int $instanceID
50 *
51 * @return array|bool
52 */
53 public static function getValueIDFromUrl($instanceID = NULL) {
54 $optionVal = self::getValueFromUrl($instanceID);
55
56 if ($optionVal) {
57 $templateInfo = CRM_Core_OptionGroup::getRowValues('report_template', "{$optionVal}", 'value');
58 return [CRM_Utils_Array::value('id', $templateInfo), $optionVal];
59 }
60
61 return FALSE;
62 }
63
64 /**
65 * @param $optionVal
66 *
67 * @return mixed
68 */
69 public static function getInstanceIDForValue($optionVal) {
70 static $valId = [];
71
72 if (!array_key_exists($optionVal, $valId)) {
73 $sql = "
74 SELECT MIN(id) FROM civicrm_report_instance
75 WHERE report_id = %1";
76
77 $params = [1 => [$optionVal, 'String']];
78 $valId[$optionVal] = CRM_Core_DAO::singleValueQuery($sql, $params);
79 }
80 return $valId[$optionVal];
81 }
82
83 /**
84 * @param null $path
85 *
86 * @return mixed
87 */
88 public static function getInstanceIDForPath($path = NULL) {
89 static $valId = [];
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 = "
96 SELECT MIN(id) FROM civicrm_report_instance
97 WHERE TRIM(BOTH '/' FROM CONCAT(report_id, '/', name)) = %1";
98
99 $params = [1 => [$path, 'String']];
100 $valId[$path] = CRM_Core_DAO::singleValueQuery($sql, $params);
101 }
102 return CRM_Utils_Array::value($path, $valId);
103 }
104
105 /**
106 * @param $urlValue
107 * @param string $query
108 * @param bool $absolute
109 * @param int $instanceID
110 * @param array $drilldownReport
111 *
112 * @return bool|string
113 */
114 public static function getNextUrl($urlValue, $query = 'reset=1', $absolute = FALSE, $instanceID = NULL, $drilldownReport = []) {
115 if ($instanceID) {
116 $drilldownInstanceID = FALSE;
117 if (array_key_exists($urlValue, $drilldownReport)) {
118 $drilldownInstanceID = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'drilldown_id', 'id');
119 }
120
121 if (!$drilldownInstanceID) {
122 $drilldownInstanceID = self::getInstanceIDForValue($urlValue);
123 }
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
141 /**
142 * get instance count for a template.
143 * @param $optionVal
144 *
145 * @return int|null|string
146 */
147 public static function getInstanceCount($optionVal) {
148 if (empty($optionVal)) {
149 return 0;
150 }
151
152 $sql = "
153 SELECT count(inst.id)
154 FROM civicrm_report_instance inst
155 WHERE inst.report_id = %1";
156
157 $params = [1 => [$optionVal, 'String']];
158 $count = CRM_Core_DAO::singleValueQuery($sql, $params);
159 return $count;
160 }
161
162 /**
163 * @param $fileContent
164 * @param int $instanceID
165 * @param string $outputMode
166 * @param array $attachments
167 *
168 * @return bool
169 */
170 public static function mailReport($fileContent, $instanceID = NULL, $outputMode = 'html', $attachments = []) {
171 if (!$instanceID) {
172 return FALSE;
173 }
174
175 list($domainEmailName,
176 $domainEmailAddress
177 ) = CRM_Core_BAO_Domain::getNameAndEmail();
178
179 $params = ['id' => $instanceID];
180 $instanceInfo = [];
181 CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
182 $params,
183 $instanceInfo
184 );
185
186 $params = [];
187 $params['groupName'] = 'Report Email Sender';
188 $params['from'] = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
189 //$domainEmailName;
190 $params['toName'] = "";
191 $params['toEmail'] = CRM_Utils_Array::value('email_to', $instanceInfo);
192 $params['cc'] = CRM_Utils_Array::value('email_cc', $instanceInfo);
193 $params['subject'] = CRM_Utils_Array::value('email_subject', $instanceInfo);
194 if (empty($instanceInfo['attachments'])) {
195 $instanceInfo['attachments'] = [];
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
204 /**
205 * @param CRM_Core_Form $form
206 * @param $rows
207 */
208 public static function export2csv(&$form, &$rows) {
209 //Mark as a CSV file.
210 CRM_Utils_System::setHttpHeader('Content-Type', 'text/csv');
211
212 //Force a download and name the file using the current timestamp.
213 $datetime = date('Ymd-Gi', $_SERVER['REQUEST_TIME']);
214 CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename=Report_' . $datetime . '.csv');
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.
222 *
223 * @param CRM_Core_Form $form
224 * @param array $rows
225 *
226 * @return string
227 */
228 public static function makeCsv(&$form, &$rows) {
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,
243 $headers
244 ) . "\r\n";
245
246 $displayRows = [];
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 }
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 }
268 else {
269 $value = CRM_Utils_Date::customFormat($value, '%Y-%m-%d');
270 }
271 }
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'])) {
275 $value = CRM_Utils_Money::format($value, $row['civicrm_contribution_currency']);
276 }
277 $displayRows[$v] = '"' . $value . '"';
278 }
279 else {
280 $displayRows[$v] = "";
281 }
282 }
283 // Add the data row.
284 $csv .= implode($config->fieldSeparator,
285 $displayRows
286 ) . "\r\n";
287 }
288
289 return $csv;
290 }
291
292 /**
293 * @return mixed
294 */
295 public static function getInstanceID() {
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
309 /**
310 * @return string
311 */
312 public static function getInstancePath() {
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
325 /**
326 * @param int $instanceId
327 *
328 * @return bool
329 */
330 public static function isInstancePermissioned($instanceId) {
331 if (!$instanceId) {
332 return TRUE;
333 }
334
335 $instanceValues = [];
336 $params = ['id' => $instanceId];
337 CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
338 $params,
339 $instanceValues
340 );
341
342 if (!empty($instanceValues['permission']) &&
343 (!(CRM_Core_Permission::check($instanceValues['permission']) ||
344 CRM_Core_Permission::check('administer Reports')
345 ))
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 *
358 * @param int $instanceId
359 *
360 * @return bool
361 * true if yes, else false
362 */
363 public static function isInstanceGroupRoleAllowed($instanceId) {
364 if (!$instanceId) {
365 return TRUE;
366 }
367
368 $instanceValues = [];
369 $params = ['id' => $instanceId];
370 CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
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
388 /**
389 * @param array $params
390 *
391 * @return array
392 */
393 public static function processReport($params) {
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);
399
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);
405 $messages = ["Report Mail Triggered..."];
406
407 $templateInfo = CRM_Core_OptionGroup::getRowValues('report_template', $optionVal, 'value');
408 $obj = new CRM_Report_Page_Instance();
409 $is_error = 0;
410 if (strstr(CRM_Utils_Array::value('name', $templateInfo), '_Form')) {
411 $instanceInfo = [];
412 CRM_Report_BAO_ReportInstance::retrieve(['id' => $instanceId], $instanceInfo);
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();
422 $arguments = [
423 'urlToSession' => [
424 [
425 'urlVar' => 'instanceId',
426 'type' => 'Positive',
427 'sessionVar' => 'instanceId',
428 'default' => 'null',
429 ],
430 ],
431 'ignoreKey' => TRUE,
432 ];
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
445 $result = [
446 'is_error' => $is_error,
447 'messages' => implode("\n", $messages),
448 ];
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 *
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.
461 *
462 * @return string
463 * URL query string
464 */
465 public static function getPreviewCriteriaQueryParams($defaults = [], $params = []) {
466 static $query_string;
467 if (!isset($query_string)) {
468 if (!empty($params)) {
469 $url_params = $op_values = $string_values = $process_params = [];
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, '_');
485 $suffix = substr($field_name, $suffix_position);
486 $basename = substr($field_name, 0, $suffix_position);
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
534 /**
535 * @param $reportUrl
536 *
537 * @return mixed
538 */
539 public static function getInstanceList($reportUrl) {
540 static $instanceDetails = [];
541
542 if (!array_key_exists($reportUrl, $instanceDetails)) {
543 $instanceDetails[$reportUrl] = [];
544
545 $sql = "
546 SELECT id, title FROM civicrm_report_instance
547 WHERE report_id = %1";
548 $params = [1 => [$reportUrl, 'String']];
549 $result = CRM_Core_DAO::executeQuery($sql, $params);
550 while ($result->fetch()) {
551 $instanceDetails[$reportUrl][$result->id] = $result->title . " (ID: {$result->id})";
552 }
553 }
554 return $instanceDetails[$reportUrl];
555 }
556
557 }