Merge pull request #24117 from civicrm/5.52
[civicrm-core.git] / CRM / Case / XMLProcessor / Report.php
CommitLineData
6a488035
TO
1<?php
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_Case_XMLProcessor_Report extends CRM_Case_XMLProcessor {
18
19 /**
fe482240 20 * The default variable defined.
6a488035 21 *
b67daa72 22 * @var bool
6a488035
TO
23 */
24 protected $_isRedact;
25
4c6ce474 26 /**
4c6ce474 27 */
e547f744
TO
28 public function __construct() {
29 }
6a488035 30
a7e52def 31 public function getRedactionRules() {
6a488035 32 foreach (array(
f157740d
SL
33 'redactionStringRules',
34 'redactionRegexRules',
35 ) as $key => $rule) {
6a488035
TO
36 $$rule = CRM_Case_PseudoConstant::redactionRule($key);
37
38 if (!empty($$rule)) {
39 foreach ($$rule as & $val) {
40 //suffixed with a randomly generated 4-digit number
41 if ($key == 'redactionStringRules') {
42 $val .= rand(10000, 100000);
43 }
44 }
45
46 if (!empty($this->{'_' . $rule})) {
47 $this->{'_' . $rule} = CRM_Utils_Array::crmArrayMerge($this->{'_' . $rule}, $$rule);
48 }
49 else {
50 $this->{'_' . $rule} = $$rule;
51 }
52 }
53 }
54 }
55
4c6ce474 56 /**
100fef9d
CW
57 * @param int $clientID
58 * @param int $caseID
4c6ce474
EM
59 *
60 * @return array
61 */
acb1052e 62 public function &caseInfo(
28d4d481 63 $clientID,
6a488035
TO
64 $caseID
65 ) {
affcc9d2 66 $case = $this->_redactionRegexRules = [];
6a488035
TO
67
68 if (empty($this->_redactionStringRules)) {
affcc9d2 69 $this->_redactionStringRules = [];
6a488035
TO
70 }
71
72 if ($this->_isRedact == 1) {
73 $this->getRedactionRules();
74 }
75
76 $client = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'display_name');
77
78 // add Client to the strings to be redacted across the case session
79 if (!array_key_exists($client, $this->_redactionStringRules)) {
80 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
81 array($client => 'name_' . rand(10000, 100000))
82 );
83 $clientSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'sort_name');
84 if (!array_key_exists($clientSortName, $this->_redactionStringRules)) {
85 $this->_redactionStringRules[$clientSortName] = $this->_redactionStringRules[$client];
86 }
87 }
88
89 $case['clientName'] = $this->redact($client);
90
91 $dao = new CRM_Case_DAO_Case();
92 $dao->id = $caseID;
93 if ($dao->find(TRUE)) {
353ffa53 94 $case['subject'] = $dao->subject;
6a488035 95 $case['start_date'] = $dao->start_date;
353ffa53 96 $case['end_date'] = $dao->end_date;
6a488035
TO
97 $case['caseType'] = CRM_Case_BAO_Case::getCaseType($caseID);
98 $case['caseTypeName'] = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
cedb74cd 99 $case['status'] = CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'status_id', $dao->status_id);
6a488035
TO
100 }
101 return $case;
102 }
103
4c6ce474
EM
104 /**
105 * @param $xml
100fef9d 106 * @param string $activitySetName
4c6ce474
EM
107 *
108 * @return array|bool
109 */
00be9182 110 public function getActivityTypes($xml, $activitySetName) {
6a488035
TO
111 foreach ($xml->ActivitySets as $activitySetsXML) {
112 foreach ($activitySetsXML->ActivitySet as $activitySetXML) {
113 if ((string ) $activitySetXML->name == $activitySetName) {
affcc9d2 114 $activityTypes = [];
a9410a63 115 $allActivityTypes = CRM_Case_PseudoConstant::caseActivityType(TRUE, TRUE);
6a488035
TO
116 foreach ($activitySetXML->ActivityTypes as $activityTypesXML) {
117 foreach ($activityTypesXML as $activityTypeXML) {
118 $activityTypeName = (string ) $activityTypeXML->name;
9c1bc317 119 $activityTypeInfo = $allActivityTypes[$activityTypeName] ?? NULL;
6a488035
TO
120 if ($activityTypeInfo) {
121 $activityTypes[$activityTypeInfo['id']] = $activityTypeInfo;
122 }
123 }
124 }
125 return empty($activityTypes) ? FALSE : $activityTypes;
126 }
127 }
128 }
129 return FALSE;
130 }
131
4c6ce474
EM
132 /**
133 * @param $xml
100fef9d 134 * @param string $activitySetName
4c6ce474
EM
135 *
136 * @return null|string
137 */
00be9182 138 public function getActivitySetLabel($xml, $activitySetName) {
6a488035
TO
139 foreach ($xml->ActivitySets as $activitySetsXML) {
140 foreach ($activitySetsXML->ActivitySet as $activitySetXML) {
141 if ((string ) $activitySetXML->name == $activitySetName) {
142 return (string ) $activitySetXML->label;
143 }
144 }
145 }
146 return NULL;
147 }
148
4c6ce474 149 /**
100fef9d
CW
150 * @param int $clientID
151 * @param int $caseID
4c6ce474
EM
152 * @param $activityTypes
153 * @param $activities
154 */
00be9182 155 public function getActivities($clientID, $caseID, $activityTypes, &$activities) {
6a488035
TO
156 // get all activities for this case that in this activityTypes set
157 foreach ($activityTypes as $aType) {
158 $map[$aType['id']] = $aType;
159 }
160
6a488035
TO
161 $activityTypeIDs = implode(',', array_keys($map));
162 $query = "
163SELECT a.*, c.id as caseID
164FROM civicrm_activity a,
165 civicrm_case c,
166 civicrm_case_activity ac
167WHERE a.is_current_revision = 1
168AND a.is_deleted =0
169AND a.activity_type_id IN ( $activityTypeIDs )
170AND c.id = ac.case_id
171AND a.id = ac.activity_id
172AND ac.case_id = %1
173";
174
175 $params = array(1 => array($caseID, 'Integer'));
176 $dao = CRM_Core_DAO::executeQuery($query, $params);
177 while ($dao->fetch()) {
178 $activityTypeInfo = $map[$dao->activity_type_id];
179 $activities[] = $this->getActivity($clientID,
180 $dao,
181 $activityTypeInfo
182 );
183 }
184 }
185
4c6ce474 186 /**
100fef9d
CW
187 * @param int $clientID
188 * @param int $activityID
4c6ce474
EM
189 * @param bool $anyActivity
190 * @param int $redact
191 *
192 * @return mixed
193 */
00be9182 194 public function &getActivityInfo($clientID, $activityID, $anyActivity = FALSE, $redact = 0) {
affcc9d2 195 static $activityInfos = [];
6a488035
TO
196 if ($redact) {
197 $this->_isRedact = 1;
198 $this->getRedactionRules();
199 }
200
6a488035
TO
201 $index = $activityID . '_' . (int) $anyActivity;
202
203 if ($clientID) {
204 $index = $index . '_' . $clientID;
205 }
206
6a488035 207 if (!array_key_exists($index, $activityInfos)) {
affcc9d2 208 $activityInfos[$index] = [];
6a488035
TO
209 $selectCaseActivity = "";
210 $joinCaseActivity = "";
211
212 if ($clientID) {
213 $selectCaseActivity = ", ca.case_id as caseID ";
214 $joinCaseActivity = " INNER JOIN civicrm_case_activity ca ON a.id = ca.activity_id ";
215 }
216
44f817d4 217 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
9e74e3ce 218 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
219 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
8ef12e64 220
6a488035 221 $query = "
5a1bd70b 222SELECT a.*, aa.contact_id as assigneeID, at.contact_id as targetID
6a488035
TO
223{$selectCaseActivity}
224FROM civicrm_activity a
225{$joinCaseActivity}
9e74e3ce 226LEFT JOIN civicrm_activity_contact at ON a.id = at.activity_id AND at.record_type_id = $targetID
227LEFT JOIN civicrm_activity_contact aa ON a.id = aa.activity_id AND aa.record_type_id = $assigneeID
6a488035
TO
228WHERE a.id = %1
229 ";
230 $params = array(1 => array($activityID, 'Integer'));
231 $dao = CRM_Core_DAO::executeQuery($query, $params);
232
233 if ($dao->fetch()) {
234 //if activity type is email get info of all activities.
95a718e1 235 if ($dao->activity_type_id == CRM_Core_PseudoConstant::getKey('CRM_Activity_DAO_Activity', 'activity_type_id', 'Email')) {
6a488035
TO
236 $anyActivity = TRUE;
237 }
a953bf20 238 $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, $anyActivity);
6a488035
TO
239 $activityTypeInfo = NULL;
240
241 if (isset($activityTypes[$dao->activity_type_id])) {
242 $activityTypeInfo = $activityTypes[$dao->activity_type_id];
243 }
244 if ($activityTypeInfo) {
245 $activityInfos[$index] = $this->getActivity($clientID, $dao, $activityTypeInfo);
246 }
247 }
248 }
249
250 return $activityInfos[$index];
251 }
252
4c6ce474 253 /**
100fef9d 254 * @param int $clientID
4c6ce474
EM
255 * @param $activityDAO
256 * @param $activityTypeInfo
257 *
258 * @return array
259 */
00be9182 260 public function &getActivity($clientID, $activityDAO, &$activityTypeInfo) {
6a488035 261 if (empty($this->_redactionStringRules)) {
affcc9d2 262 $this->_redactionStringRules = [];
6a488035
TO
263 }
264
affcc9d2
CW
265 $activity = [];
266 $activity['fields'] = [];
6a488035
TO
267 if ($clientID) {
268 $clientID = CRM_Utils_Type::escape($clientID, 'Integer');
269 if (!in_array($activityTypeInfo['name'], array(
353ffa53 270 'Email',
acb1052e 271 'Inbound Email',
353ffa53
TO
272 ))
273 ) {
6a488035
TO
274 $activity['editURL'] = CRM_Utils_System::url('civicrm/case/activity',
275 "reset=1&cid={$clientID}&caseid={$activityDAO->caseID}&action=update&atype={$activityDAO->activity_type_id}&id={$activityDAO->id}"
276 );
277 }
278 else {
279 $activity['editURL'] = '';
280 }
281
282 $client = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'display_name');
283 // add Client SortName as well as Display to the strings to be redacted across the case session
284 // suffixed with a randomly generated 4-digit number
285 if (!array_key_exists($client, $this->_redactionStringRules)) {
286 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
287 array($client => 'name_' . rand(10000, 100000))
288 );
289
290 $clientSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'sort_name');
291 if (!array_key_exists($clientSortName, $this->_redactionStringRules)) {
292 $this->_redactionStringRules[$clientSortName] = $this->_redactionStringRules[$client];
293 }
294 }
295
296 $activity['fields'][] = array(
1474f129 297 'name' => 'Client',
298 'label' => ts('Client'),
6a488035
TO
299 'value' => $this->redact($client),
300 'type' => 'String',
301 );
302 }
303
44f817d4 304 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
034500d4 305 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
306 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
6a488035
TO
307 if (!empty($activityDAO->targetID)) {
308 // Re-lookup the target ID since the DAO only has the first recipient if there are multiple.
309 // Maybe not the best solution.
353ffa53 310 $targetNames = CRM_Activity_BAO_ActivityContact::getNames($activityDAO->id, $targetID);
6a488035 311 $processTarget = FALSE;
1474f129 312 $name = 'With Contact(s)';
353ffa53 313 $label = ts('With Contact(s)');
22d5fc65 314 if (in_array($activityTypeInfo['name'], array('Email', 'Inbound Email'))) {
6a488035 315 $processTarget = TRUE;
1474f129 316 $name = 'Recipient';
6a488035
TO
317 $label = ts('Recipient');
318 }
319 if (!$processTarget) {
320 foreach ($targetNames as $targetID => $targetName) {
321 if ($targetID != $clientID) {
322 $processTarget = TRUE;
323 break;
324 }
325 }
326 }
327
328 if ($processTarget) {
affcc9d2 329 $targetRedacted = [];
6a488035
TO
330 foreach ($targetNames as $targetID => $target) {
331 // add Recipient SortName as well as Display to the strings to be redacted across the case session
332 // suffixed with a randomly generated 4-digit number
333 if (!array_key_exists($target, $this->_redactionStringRules)) {
334 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
335 array($target => 'name_' . rand(10000, 100000))
336 );
337 $targetSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $targetID, 'sort_name');
338 if (!array_key_exists($targetSortName, $this->_redactionStringRules)) {
339 $this->_redactionStringRules[$targetSortName] = $this->_redactionStringRules[$target];
340 }
341 }
342 $targetRedacted[] = $this->redact($target);
343 }
344
345 $activity['fields'][] = array(
1474f129 346 'name' => $name,
6a488035
TO
347 'label' => $label,
348 'value' => implode('; ', $targetRedacted),
349 'type' => 'String',
350 );
351 }
352 }
353
354 // Activity Type info is a special field
355 $activity['fields'][] = array(
1474f129 356 'name' => 'Activity Type',
22d5fc65 357 'label' => ts('Activity Type'),
6a488035
TO
358 'value' => $activityTypeInfo['label'],
359 'type' => 'String',
360 );
361
362 $activity['fields'][] = array(
1474f129 363 'name' => 'Subject',
22d5fc65 364 'label' => ts('Subject'),
1474f129 365 // TODO: Why is this being escaped at this point in the flow? Should
366 // this just be done in the tpl where all the other fields get escaped?
367 // Is anything depending on this currently or is it just a result of
368 // the see-sawing and some double-escaping that went back and forth
369 // for a few years?
f21e34df 370 'value' => htmlspecialchars($this->redact($activityDAO->subject) ?? ''),
6a488035
TO
371 'type' => 'Memo',
372 );
373
374 $creator = $this->getCreatedBy($activityDAO->id);
375 // add Creator to the strings to be redacted across the case session
376 if (!array_key_exists($creator, $this->_redactionStringRules)) {
377 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
378 array($creator => 'name_' . rand(10000, 100000))
379 );
380 }
381 $activity['fields'][] = array(
1474f129 382 'name' => 'Created By',
22d5fc65 383 'label' => ts('Created By'),
6a488035
TO
384 'value' => $this->redact($creator),
385 'type' => 'String',
386 );
44f817d4 387 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
034500d4 388 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
389 $source_contact_id = CRM_Activity_BAO_Activity::getActivityContact($activityDAO->id, $sourceID);
6a488035 390 $reporter = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
034500d4 391 $source_contact_id,
6a488035
TO
392 'display_name'
393 );
394
395 // add Reporter SortName as well as Display to the strings to be redacted across the case session
396 // suffixed with a randomly generated 4-digit number
397 if (!array_key_exists($reporter, $this->_redactionStringRules)) {
398 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
399 array($reporter => 'name_' . rand(10000, 100000))
400 );
401
402 $reporterSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
353ffa53 403 $source_contact_id,
6a488035
TO
404 'sort_name'
405 );
406 if (!array_key_exists($reporterSortName, $this->_redactionStringRules)) {
407 $this->_redactionStringRules[$reporterSortName] = $this->_redactionStringRules[$reporter];
408 }
409 }
410
411 $activity['fields'][] = array(
1474f129 412 'name' => 'Reported By',
22d5fc65 413 'label' => ts('Reported By'),
6a488035
TO
414 'value' => $this->redact($reporter),
415 'type' => 'String',
416 );
417
418 if (!empty($activityDAO->assigneeID)) {
419 //allow multiple assignee contacts.CRM-4503.
90b05581 420 $assignee_contact_names = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames(array($activityDAO->id), TRUE);
6a488035
TO
421
422 foreach ($assignee_contact_names as & $assignee) {
423 // add Assignee to the strings to be redacted across the case session
424 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
425 array($assignee => 'name_' . rand(10000, 100000))
426 );
427 $assignee = $this->redact($assignee);
428 }
429 $assigneeContacts = implode(', ', $assignee_contact_names);
430 $activity['fields'][] = array(
1474f129 431 'name' => 'Assigned to',
3bd48a28 432 'label' => ts('Assigned to'),
6a488035
TO
433 'value' => $assigneeContacts,
434 'type' => 'String',
435 );
436 }
437
438 if ($activityDAO->medium_id) {
439 $activity['fields'][] = array(
1474f129 440 'name' => 'Medium',
22d5fc65 441 'label' => ts('Medium'),
cedb74cd 442 'value' => CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'medium_id', $activityDAO->medium_id),
6a488035
TO
443 'type' => 'String',
444 );
445 }
446
447 $activity['fields'][] = array(
1474f129 448 'name' => 'Location',
22d5fc65 449 'label' => ts('Location'),
6a488035
TO
450 'value' => $activityDAO->location,
451 'type' => 'String',
452 );
453
454 $activity['fields'][] = array(
1474f129 455 'name' => 'Date and Time',
22d5fc65 456 'label' => ts('Date and Time'),
6a488035
TO
457 'value' => $activityDAO->activity_date_time,
458 'type' => 'Date',
459 );
460
461 $activity['fields'][] = array(
1474f129 462 'name' => 'Details',
22d5fc65 463 'label' => ts('Details'),
504ab71e 464 'value' => $this->redact(CRM_Utils_String::purifyHTML(CRM_Utils_String::stripAlternatives($activityDAO->details))),
6a488035
TO
465 'type' => 'Memo',
466 );
467
468 // Skip Duration field if empty (to avoid " minutes" output). Might want to do this for all fields at some point. dgg
469 if ($activityDAO->duration) {
470 $activity['fields'][] = array(
1474f129 471 'name' => 'Duration',
22d5fc65 472 'label' => ts('Duration'),
6a488035
TO
473 'value' => $activityDAO->duration . ' ' . ts('minutes'),
474 'type' => 'Int',
475 );
476 }
477 $activity['fields'][] = array(
1474f129 478 'name' => 'Status',
22d5fc65 479 'label' => ts('Status'),
95a718e1 480 'value' => CRM_Core_PseudoConstant::getLabel('CRM_Activity_DAO_Activity', 'activity_status_id',
6a488035
TO
481 $activityDAO->status_id
482 ),
483 'type' => 'String',
484 );
485
486 $activity['fields'][] = array(
1474f129 487 'name' => 'Priority',
22d5fc65 488 'label' => ts('Priority'),
8d04ae48 489 'value' => CRM_Core_PseudoConstant::getLabel('CRM_Activity_DAO_Activity', 'priority_id',
6a488035
TO
490 $activityDAO->priority_id
491 ),
492 'type' => 'String',
493 );
494
495 //for now empty custom groups
496 $activity['customGroups'] = $this->getCustomData($clientID,
497 $activityDAO,
498 $activityTypeInfo
499 );
500
501 return $activity;
502 }
503
4c6ce474 504 /**
100fef9d 505 * @param int $clientID
4c6ce474
EM
506 * @param $activityDAO
507 * @param $activityTypeInfo
508 *
509 * @return array|null
510 */
00be9182 511 public function getCustomData($clientID, $activityDAO, &$activityTypeInfo) {
6a488035
TO
512 list($typeValues, $options, $sql) = $this->getActivityTypeCustomSQL($activityTypeInfo['id'], '%Y-%m-%d');
513
514 $params = array(1 => array($activityDAO->id, 'Integer'));
515
affcc9d2 516 $customGroups = [];
6a488035
TO
517 foreach ($sql as $tableName => $sqlClause) {
518 $dao = CRM_Core_DAO::executeQuery($sqlClause, $params);
519 if ($dao->fetch()) {
affcc9d2 520 $customGroup = [];
6a488035 521 foreach ($typeValues[$tableName] as $columnName => $typeValue) {
6a488035
TO
522
523 if (CRM_Utils_Array::value('type', $typeValue) == 'Date') {
524 $value = $dao->$columnName;
525 }
2e894314 526 else {
f3ceb112 527 $value = CRM_Core_BAO_CustomField::displayValue($dao->$columnName, $typeValue['fieldID'], $activityDAO->id);
2e894314 528 }
6a488035
TO
529
530 if ($value) {
531 // Note: this is already taken care in getDisplayValue above, but sometimes
532 // strings like '^A^A' creates problem. So to fix this special case -
533 if (strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) {
534 $value = trim($value, CRM_Core_DAO::VALUE_SEPARATOR);
535 }
536 if (CRM_Utils_Array::value('type', $typeValue) == 'String' ||
537 CRM_Utils_Array::value('type', $typeValue) == 'Memo'
538 ) {
539 $value = $this->redact($value);
540 }
6a488035
TO
541 }
542 //$typeValue
543 $customGroup[] = array(
544 'label' => $typeValue['label'],
545 'value' => $value,
546 'type' => $typeValue['type'],
dc7c0d62 547 'fieldID' => $typeValue['fieldID'],
6a488035
TO
548 );
549 }
550 $customGroups[$dao->groupTitle] = $customGroup;
551 }
552 }
553
554 return empty($customGroups) ? NULL : $customGroups;
555 }
556
4c6ce474 557 /**
100fef9d 558 * @param int $activityTypeID
3fd42bb5 559 * @param string|null $dateFormat
54f54538 560 * @param bool $onlyActive
4c6ce474
EM
561 *
562 * @return mixed
563 */
54f54538 564 public function getActivityTypeCustomSQL($activityTypeID, $dateFormat = NULL, $onlyActive = TRUE) {
affcc9d2 565 static $cache = [];
6a488035
TO
566
567 if (is_null($activityTypeID)) {
568 $activityTypeID = 0;
569 }
570
571 if (!isset($cache[$activityTypeID])) {
572 $query = "
573SELECT cg.title as groupTitle,
574 cg.table_name as tableName ,
575 cf.column_name as columnName,
576 cf.label as label ,
577 cg.id as groupID ,
578 cf.id as fieldID ,
579 cf.data_type as dataType ,
580 cf.html_type as htmlType ,
581 cf.option_group_id as optionGroupID
582FROM civicrm_custom_group cg,
583 civicrm_custom_field cf
584WHERE cf.custom_group_id = cg.id
fb6aa4b9 585AND cg.extends = 'Activity'
586AND " . CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, 'cg.');
6a488035 587
54f54538
JP
588 if ($onlyActive) {
589 $query .= " AND cf.is_active = 1 ";
590 }
6a488035
TO
591 if ($activityTypeID) {
592 $query .= "AND ( cg.extends_entity_column_value IS NULL OR cg.extends_entity_column_value LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . "%1" . CRM_Core_DAO::VALUE_SEPARATOR . "%' )";
593 }
594 else {
595 $query .= "AND cg.extends_entity_column_value IS NULL";
596 }
a5c0c7f2 597 $query .= "ORDER BY cg.weight, cf.weight";
6a488035 598 $params = array(
353ffa53
TO
599 1 => array(
600 $activityTypeID,
6a488035 601 'Integer',
acb1052e 602 ),
353ffa53 603 );
6a488035
TO
604 $dao = CRM_Core_DAO::executeQuery($query, $params);
605
affcc9d2 606 $result = $options = $sql = $groupTitle = [];
6a488035
TO
607 while ($dao->fetch()) {
608 if (!array_key_exists($dao->tableName, $result)) {
affcc9d2
CW
609 $result[$dao->tableName] = [];
610 $sql[$dao->tableName] = [];
6a488035
TO
611 }
612 $result[$dao->tableName][$dao->columnName] = array(
613 'label' => $dao->label,
614 'type' => $dao->dataType,
615 'fieldID' => $dao->fieldID,
616 );
617
affcc9d2 618 $options[$dao->fieldID] = [];
6a488035
TO
619 $options[$dao->fieldID]['attributes'] = array(
620 'label' => $dao->label,
621 'data_type' => $dao->dataType,
622 'html_type' => $dao->htmlType,
623 );
624 // since we want to add ISO date format.
625 if ($dateFormat && $dao->htmlType == 'Select Date') {
626 $options[$dao->fieldID]['attributes']['format'] = $dateFormat;
627 }
628 if ($dao->optionGroupID) {
629 $query = "
630SELECT label, value
631 FROM civicrm_option_value
78c187fb 632 WHERE option_group_id = %1
6a488035
TO
633";
634
78c187fb 635 $option = CRM_Core_DAO::executeQuery($query, array(1 => array($dao->optionGroupID, 'Positive')));
6a488035
TO
636 while ($option->fetch()) {
637 $dataType = $dao->dataType;
638 if ($dataType == 'Int' || $dataType == 'Float') {
639 $num = round($option->value, 2);
640 $options[$dao->fieldID]["$num"] = $option->label;
641 }
642 else {
643 $options[$dao->fieldID][$option->value] = $option->label;
644 }
645 }
646 }
647
648 $sql[$dao->tableName][] = $dao->columnName;
649 $groupTitle[$dao->tableName] = $dao->groupTitle;
650 }
651
652 foreach ($sql as $tableName => $values) {
653 $columnNames = implode(',', $values);
79acbc27
SL
654 $title = CRM_Core_DAO::escapeString($groupTitle[$tableName]);
655 $mysqlTableName = CRM_Utils_Type::escape($tableName, 'MysqlColumnNameOrAlias');
6a488035 656 $sql[$tableName] = "
79acbc27
SL
657SELECT '" . $title . "' as groupTitle, $columnNames
658FROM $mysqlTableName
6a488035
TO
659WHERE entity_id = %1
660";
661 }
662
663 $cache[$activityTypeID] = array($result, $options, $sql);
664 }
665 return $cache[$activityTypeID];
666 }
667
4c6ce474 668 /**
100fef9d 669 * @param int $activityID
4c6ce474
EM
670 *
671 * @return null|string
672 */
00be9182 673 public function getCreatedBy($activityID) {
6a488035
TO
674 $query = "
675SELECT c.display_name
676FROM civicrm_contact c,
677 civicrm_log l
678WHERE l.entity_table = 'civicrm_activity'
679AND l.entity_id = %1
680AND l.modified_id = c.id
681LIMIT 1
682";
683
684 $params = array(1 => array($activityID, 'Integer'));
685 return CRM_Core_DAO::singleValueQuery($query, $params);
686 }
687
4c6ce474
EM
688 /**
689 * @param $string
690 * @param bool $printReport
691 * @param array $replaceString
692 *
693 * @return mixed
694 */
affcc9d2 695 private function redact($string, $printReport = FALSE, $replaceString = []) {
6a488035
TO
696 if ($printReport) {
697 return CRM_Utils_String::redaction($string, $replaceString);
698 }
699 elseif ($this->_isRedact) {
700 $regexToReplaceString = CRM_Utils_String::regex($string, $this->_redactionRegexRules);
701 return CRM_Utils_String::redaction($string, array_merge($this->_redactionStringRules, $regexToReplaceString));
702 }
703 return $string;
704 }
705
4c6ce474 706 /**
100fef9d
CW
707 * @param int $clientID
708 * @param int $caseID
709 * @param string $activitySetName
c490a46a
CW
710 * @param array $params
711 * @param CRM_Core_Form $form
4c6ce474
EM
712 *
713 * @return mixed
714 */
00be9182 715 public static function getCaseReport($clientID, $caseID, $activitySetName, $params, $form) {
6a488035 716
8ed46a8d
D
717 $template = self::populateCaseReportTemplate($clientID, $caseID, $activitySetName, $params, $form);
718
719 // now run the template
720 $contents = $template->fetch('CRM/Case/XMLProcessor/Report.tpl');
721 return $contents;
722 }
723
724 /**
725 * @param int $clientID
726 * @param int $caseID
727 * @param string $activitySetName
728 * @param array $params
729 * @param CRM_Core_Form $form
730 *
731 * @return CRM_Core_Smarty
732 */
733 public static function populateCaseReportTemplate($clientID, $caseID, $activitySetName, $params, $form) {
734
6a488035
TO
735 $template = CRM_Core_Smarty::singleton();
736
737 $template->assign('caseId', $caseID);
738 $template->assign('clientID', $clientID);
739 $template->assign('activitySetName', $activitySetName);
740
a7488080 741 if (!empty($params['is_redact'])) {
6a488035
TO
742 $form->_isRedact = TRUE;
743 $template->assign('_isRedact', 'true');
744 }
745 else {
746 $form->_isRedact = FALSE;
747 $template->assign('_isRedact', 'false');
748 }
749
750 // first get all case information
751 $case = $form->caseInfo($clientID, $caseID);
752 $template->assign_by_ref('case', $case);
753
a9410a63 754 if (CRM_Utils_Array::value('include_activities', $params) == 1) {
6a488035
TO
755 $template->assign('includeActivities', 'All');
756 }
757 else {
758 $template->assign('includeActivities', 'Missing activities only');
759 }
760
761 $xml = $form->retrieve($case['caseTypeName']);
762
763 $activitySetNames = CRM_Case_XMLProcessor_Process::activitySets($xml->ActivitySets);
9c1bc317 764 $pageTitle = $activitySetNames[$activitySetName] ?? NULL;
6a488035
TO
765 $template->assign('pageTitle', $pageTitle);
766
767 if ($activitySetName) {
768 $activityTypes = $form->getActivityTypes($xml, $activitySetName);
769 }
770 else {
69805643 771 $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE);
6a488035
TO
772 }
773
774 if (!$activityTypes) {
775 return FALSE;
776 }
777
b44e3f84 778 // next get activity set Information
e547f744 779 $activitySet = array(
353ffa53 780 'label' => $form->getActivitySetLabel($xml, $activitySetName),
6a488035
TO
781 'includeActivities' => 'All',
782 'redact' => 'false',
783 );
784 $template->assign_by_ref('activitySet', $activitySet);
785
786 //now collect all the information about activities
affcc9d2 787 $activities = [];
6a488035
TO
788 $form->getActivities($clientID, $caseID, $activityTypes, $activities);
789 $template->assign_by_ref('activities', $activities);
790
8ed46a8d 791 return $template;
6a488035
TO
792 }
793
00be9182 794 public static function printCaseReport() {
a3d827a7
CW
795 $caseID = CRM_Utils_Request::retrieve('caseID', 'Positive');
796 $clientID = CRM_Utils_Request::retrieve('cid', 'Positive');
797 $activitySetName = CRM_Utils_Request::retrieve('asn', 'String');
798 $isRedact = CRM_Utils_Request::retrieve('redact', 'Boolean');
799 $includeActivities = CRM_Utils_Request::retrieve('all', 'Positive');
affcc9d2 800 $params = $otherRelationships = $globalGroupInfo = [];
353ffa53 801 $report = new CRM_Case_XMLProcessor_Report($isRedact);
6a488035
TO
802 if ($includeActivities) {
803 $params['include_activities'] = 1;
804 }
805
806 if ($isRedact) {
807 $params['is_redact'] = 1;
affcc9d2 808 $report->_redactionStringRules = [];
6a488035
TO
809 }
810 $template = CRM_Core_Smarty::singleton();
811
812 //get case related relationships (Case Role)
813 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($clientID, $caseID);
814 $caseType = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
815
816 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
817 $caseRoles = $xmlProcessor->get($caseType, 'CaseRoles');
818 foreach ($caseRelationships as $key => & $value) {
5655a8d4
AF
819 if (!empty($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']])) {
820 unset($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']]);
6a488035
TO
821 }
822 if ($isRedact) {
823 if (!array_key_exists($value['name'], $report->_redactionStringRules)) {
824 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
825 array($value['name'] => 'name_' . rand(10000, 100000))
826 );
827 }
a7e52def 828 $value['name'] = $report->redact($value['name'], TRUE, $report->_redactionStringRules);
a7488080 829 if (!empty($value['email']) &&
6a488035
TO
830 !array_key_exists($value['email'], $report->_redactionStringRules)
831 ) {
832 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
833 array($value['email'] => 'email_' . rand(10000, 100000))
834 );
835 }
836
a7e52def 837 $value['email'] = $report->redact($value['email'], TRUE, $report->_redactionStringRules);
6a488035 838
a7488080 839 if (!empty($value['phone']) &&
6a488035
TO
840 !array_key_exists($value['phone'], $report->_redactionStringRules)
841 ) {
842 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
843 array($value['phone'] => 'phone_' . rand(10000, 100000))
844 );
845 }
a7e52def 846 $value['phone'] = $report->redact($value['phone'], TRUE, $report->_redactionStringRules);
6a488035
TO
847 }
848 }
849
850 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
851 if ($isRedact) {
a7e52def 852 foreach ($caseRoles['client'] as &$client) {
853 if (!array_key_exists(CRM_Utils_Array::value('sort_name', $client), $report->_redactionStringRules)) {
6a488035 854
a7e52def 855 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
856 array(CRM_Utils_Array::value('sort_name', $client) => 'name_' . rand(10000, 100000))
857 );
858 }
859 if (!array_key_exists(CRM_Utils_Array::value('display_name', $client), $report->_redactionStringRules)) {
860 $report->_redactionStringRules[CRM_Utils_Array::value('display_name', $client)] = $report->_redactionStringRules[CRM_Utils_Array::value('sort_name', $client)];
861 }
862 $client['sort_name'] = $report->redact(CRM_Utils_Array::value('sort_name', $client), TRUE, $report->_redactionStringRules);
01217591 863 if (!empty($client['email']) &&
864 !array_key_exists($client['email'], $report->_redactionStringRules)
a7e52def 865 ) {
866 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
01217591 867 array($client['email'] => 'email_' . rand(10000, 100000))
a7e52def 868 );
869 }
870 $client['email'] = $report->redact(CRM_Utils_Array::value('email', $client), TRUE, $report->_redactionStringRules);
871
01217591 872 if (!empty($client['phone']) &&
873 !array_key_exists($client['phone'], $report->_redactionStringRules)
a7e52def 874 ) {
875 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
01217591 876 array($client['phone'] => 'phone_' . rand(10000, 100000))
a7e52def 877 );
878 }
879 $client['phone'] = $report->redact(CRM_Utils_Array::value('phone', $client), TRUE, $report->_redactionStringRules);
6a488035 880 }
6a488035 881 }
6a488035
TO
882 // Retrieve ALL client relationships
883 $relClient = CRM_Contact_BAO_Relationship::getRelationship($clientID,
884 CRM_Contact_BAO_Relationship::CURRENT,
885 0, 0, 0, NULL, NULL, FALSE
886 );
887 foreach ($relClient as $r) {
888 if ($isRedact) {
889 if (!array_key_exists($r['name'], $report->_redactionStringRules)) {
890 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
891 array($r['name'] => 'name_' . rand(10000, 100000))
892 );
893 }
894 if (!array_key_exists($r['display_name'], $report->_redactionStringRules)) {
895 $report->_redactionStringRules[$r['display_name']] = $report->_redactionStringRules[$r['name']];
896 }
a7e52def 897 $r['name'] = $report->redact($r['name'], TRUE, $report->_redactionStringRules);
6a488035 898
a7488080 899 if (!empty($r['phone']) &&
6a488035
TO
900 !array_key_exists($r['phone'], $report->_redactionStringRules)
901 ) {
902 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
903 array($r['phone'] => 'phone_' . rand(10000, 100000))
904 );
905 }
a7e52def 906 $r['phone'] = $report->redact($r['phone'], TRUE, $report->_redactionStringRules);
6a488035 907
a7488080 908 if (!empty($r['email']) &&
6a488035
TO
909 !array_key_exists($r['email'], $report->_redactionStringRules)
910 ) {
911 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
912 array($r['email'] => 'email_' . rand(10000, 100000))
913 );
914 }
a7e52def 915 $r['email'] = $report->redact($r['email'], TRUE, $report->_redactionStringRules);
6a488035
TO
916 }
917 if (!array_key_exists($r['id'], $caseRelationships)) {
918 $otherRelationships[] = $r;
919 }
920 }
921
922 // Now global contact list that appears on all cases.
923 $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo);
924 foreach ($relGlobal as & $r) {
925 if ($isRedact) {
926 if (!array_key_exists($r['sort_name'], $report->_redactionStringRules)) {
927 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
928 array($r['sort_name'] => 'name_' . rand(10000, 100000))
929 );
930 }
931 if (!array_key_exists($r['display_name'], $report->_redactionStringRules)) {
932 $report->_redactionStringRules[$r['display_name']] = $report->_redactionStringRules[$r['sort_name']];
933 }
934
a7e52def 935 $r['sort_name'] = $report->redact($r['sort_name'], TRUE, $report->_redactionStringRules);
a7488080 936 if (!empty($r['phone']) &&
6a488035
TO
937 !array_key_exists($r['phone'], $report->_redactionStringRules)
938 ) {
939 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
940 array($r['phone'] => 'phone_' . rand(10000, 100000))
941 );
942 }
a7e52def 943 $r['phone'] = $report->redact($r['phone'], TRUE, $report->_redactionStringRules);
6a488035 944
a7488080 945 if (!empty($r['email']) &&
6a488035
TO
946 !array_key_exists($r['email'], $report->_redactionStringRules)
947 ) {
948 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
949 array($r['email'] => 'email_' . rand(10000, 100000))
950 );
951 }
a7e52def 952 $r['email'] = $report->redact($r['email'], TRUE, $report->_redactionStringRules);
6a488035
TO
953 }
954 }
955
b077cff9
JM
956 // Retrieve custom values for cases.
957 $customValues = CRM_Core_BAO_CustomValueTable::getEntityValues($caseID, 'Case');
958 $extends = array('case');
959 $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends);
affcc9d2 960 $caseCustomFields = [];
762891db
SL
961 foreach ($groupTree as $gid => $group_values) {
962 foreach ($group_values['fields'] as $id => $field_values) {
219af4d3
CW
963 if (array_key_exists($id, $customValues)) {
964 $caseCustomFields[$gid]['title'] = $group_values['title'];
965 $caseCustomFields[$gid]['values'][$id] = array(
b077cff9
JM
966 'label' => $field_values['label'],
967 'value' => $customValues[$id],
968 );
969 }
970 }
971 }
6a488035
TO
972 $template->assign('caseRelationships', $caseRelationships);
973 $template->assign('caseRoles', $caseRoles);
974 $template->assign('otherRelationships', $otherRelationships);
975 $template->assign('globalRelationships', $relGlobal);
976 $template->assign('globalGroupInfo', $globalGroupInfo);
b077cff9 977 $template->assign('caseCustomFields', $caseCustomFields);
6a488035
TO
978 $contents = self::getCaseReport($clientID,
979 $caseID,
980 $activitySetName,
981 $params,
982 $report
983 );
67f8a3ed 984 $printReport = CRM_Case_Audit_Audit::run($contents, $clientID, $caseID);
6a488035
TO
985 echo $printReport;
986 CRM_Utils_System::civiExit();
987 }
96025800 988
6a488035 989}