Merge pull request #17640 from samuelsov/bugreportcivigrant
[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?
6a488035
TO
370 'value' => htmlspecialchars($this->redact($activityDAO->subject)),
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 elseif (CRM_Utils_Array::value('type', $typeValue) == 'Link') {
542 $value = CRM_Utils_System::formatWikiURL($value);
543 }
544 }
545 //$typeValue
546 $customGroup[] = array(
547 'label' => $typeValue['label'],
548 'value' => $value,
549 'type' => $typeValue['type'],
dc7c0d62 550 'fieldID' => $typeValue['fieldID'],
6a488035
TO
551 );
552 }
553 $customGroups[$dao->groupTitle] = $customGroup;
554 }
555 }
556
557 return empty($customGroups) ? NULL : $customGroups;
558 }
559
4c6ce474 560 /**
100fef9d 561 * @param int $activityTypeID
4c6ce474 562 * @param null $dateFormat
54f54538 563 * @param bool $onlyActive
4c6ce474
EM
564 *
565 * @return mixed
566 */
54f54538 567 public function getActivityTypeCustomSQL($activityTypeID, $dateFormat = NULL, $onlyActive = TRUE) {
affcc9d2 568 static $cache = [];
6a488035
TO
569
570 if (is_null($activityTypeID)) {
571 $activityTypeID = 0;
572 }
573
574 if (!isset($cache[$activityTypeID])) {
575 $query = "
576SELECT cg.title as groupTitle,
577 cg.table_name as tableName ,
578 cf.column_name as columnName,
579 cf.label as label ,
580 cg.id as groupID ,
581 cf.id as fieldID ,
582 cf.data_type as dataType ,
583 cf.html_type as htmlType ,
584 cf.option_group_id as optionGroupID
585FROM civicrm_custom_group cg,
586 civicrm_custom_field cf
587WHERE cf.custom_group_id = cg.id
fb6aa4b9 588AND cg.extends = 'Activity'
589AND " . CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, 'cg.');
6a488035 590
54f54538
JP
591 if ($onlyActive) {
592 $query .= " AND cf.is_active = 1 ";
593 }
6a488035
TO
594 if ($activityTypeID) {
595 $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 . "%' )";
596 }
597 else {
598 $query .= "AND cg.extends_entity_column_value IS NULL";
599 }
a5c0c7f2 600 $query .= "ORDER BY cg.weight, cf.weight";
6a488035 601 $params = array(
353ffa53
TO
602 1 => array(
603 $activityTypeID,
6a488035 604 'Integer',
acb1052e 605 ),
353ffa53 606 );
6a488035
TO
607 $dao = CRM_Core_DAO::executeQuery($query, $params);
608
affcc9d2 609 $result = $options = $sql = $groupTitle = [];
6a488035
TO
610 while ($dao->fetch()) {
611 if (!array_key_exists($dao->tableName, $result)) {
affcc9d2
CW
612 $result[$dao->tableName] = [];
613 $sql[$dao->tableName] = [];
6a488035
TO
614 }
615 $result[$dao->tableName][$dao->columnName] = array(
616 'label' => $dao->label,
617 'type' => $dao->dataType,
618 'fieldID' => $dao->fieldID,
619 );
620
affcc9d2 621 $options[$dao->fieldID] = [];
6a488035
TO
622 $options[$dao->fieldID]['attributes'] = array(
623 'label' => $dao->label,
624 'data_type' => $dao->dataType,
625 'html_type' => $dao->htmlType,
626 );
627 // since we want to add ISO date format.
628 if ($dateFormat && $dao->htmlType == 'Select Date') {
629 $options[$dao->fieldID]['attributes']['format'] = $dateFormat;
630 }
631 if ($dao->optionGroupID) {
632 $query = "
633SELECT label, value
634 FROM civicrm_option_value
78c187fb 635 WHERE option_group_id = %1
6a488035
TO
636";
637
78c187fb 638 $option = CRM_Core_DAO::executeQuery($query, array(1 => array($dao->optionGroupID, 'Positive')));
6a488035
TO
639 while ($option->fetch()) {
640 $dataType = $dao->dataType;
641 if ($dataType == 'Int' || $dataType == 'Float') {
642 $num = round($option->value, 2);
643 $options[$dao->fieldID]["$num"] = $option->label;
644 }
645 else {
646 $options[$dao->fieldID][$option->value] = $option->label;
647 }
648 }
649 }
650
651 $sql[$dao->tableName][] = $dao->columnName;
652 $groupTitle[$dao->tableName] = $dao->groupTitle;
653 }
654
655 foreach ($sql as $tableName => $values) {
656 $columnNames = implode(',', $values);
79acbc27
SL
657 $title = CRM_Core_DAO::escapeString($groupTitle[$tableName]);
658 $mysqlTableName = CRM_Utils_Type::escape($tableName, 'MysqlColumnNameOrAlias');
6a488035 659 $sql[$tableName] = "
79acbc27
SL
660SELECT '" . $title . "' as groupTitle, $columnNames
661FROM $mysqlTableName
6a488035
TO
662WHERE entity_id = %1
663";
664 }
665
666 $cache[$activityTypeID] = array($result, $options, $sql);
667 }
668 return $cache[$activityTypeID];
669 }
670
4c6ce474 671 /**
100fef9d 672 * @param int $activityID
4c6ce474
EM
673 *
674 * @return null|string
675 */
00be9182 676 public function getCreatedBy($activityID) {
6a488035
TO
677 $query = "
678SELECT c.display_name
679FROM civicrm_contact c,
680 civicrm_log l
681WHERE l.entity_table = 'civicrm_activity'
682AND l.entity_id = %1
683AND l.modified_id = c.id
684LIMIT 1
685";
686
687 $params = array(1 => array($activityID, 'Integer'));
688 return CRM_Core_DAO::singleValueQuery($query, $params);
689 }
690
4c6ce474
EM
691 /**
692 * @param $string
693 * @param bool $printReport
694 * @param array $replaceString
695 *
696 * @return mixed
697 */
affcc9d2 698 private function redact($string, $printReport = FALSE, $replaceString = []) {
6a488035
TO
699 if ($printReport) {
700 return CRM_Utils_String::redaction($string, $replaceString);
701 }
702 elseif ($this->_isRedact) {
703 $regexToReplaceString = CRM_Utils_String::regex($string, $this->_redactionRegexRules);
704 return CRM_Utils_String::redaction($string, array_merge($this->_redactionStringRules, $regexToReplaceString));
705 }
706 return $string;
707 }
708
4c6ce474 709 /**
100fef9d
CW
710 * @param int $clientID
711 * @param int $caseID
712 * @param string $activitySetName
c490a46a
CW
713 * @param array $params
714 * @param CRM_Core_Form $form
4c6ce474
EM
715 *
716 * @return mixed
717 */
00be9182 718 public static function getCaseReport($clientID, $caseID, $activitySetName, $params, $form) {
6a488035 719
8ed46a8d
D
720 $template = self::populateCaseReportTemplate($clientID, $caseID, $activitySetName, $params, $form);
721
722 // now run the template
723 $contents = $template->fetch('CRM/Case/XMLProcessor/Report.tpl');
724 return $contents;
725 }
726
727 /**
728 * @param int $clientID
729 * @param int $caseID
730 * @param string $activitySetName
731 * @param array $params
732 * @param CRM_Core_Form $form
733 *
734 * @return CRM_Core_Smarty
735 */
736 public static function populateCaseReportTemplate($clientID, $caseID, $activitySetName, $params, $form) {
737
6a488035
TO
738 $template = CRM_Core_Smarty::singleton();
739
740 $template->assign('caseId', $caseID);
741 $template->assign('clientID', $clientID);
742 $template->assign('activitySetName', $activitySetName);
743
a7488080 744 if (!empty($params['is_redact'])) {
6a488035
TO
745 $form->_isRedact = TRUE;
746 $template->assign('_isRedact', 'true');
747 }
748 else {
749 $form->_isRedact = FALSE;
750 $template->assign('_isRedact', 'false');
751 }
752
753 // first get all case information
754 $case = $form->caseInfo($clientID, $caseID);
755 $template->assign_by_ref('case', $case);
756
a9410a63 757 if (CRM_Utils_Array::value('include_activities', $params) == 1) {
6a488035
TO
758 $template->assign('includeActivities', 'All');
759 }
760 else {
761 $template->assign('includeActivities', 'Missing activities only');
762 }
763
764 $xml = $form->retrieve($case['caseTypeName']);
765
766 $activitySetNames = CRM_Case_XMLProcessor_Process::activitySets($xml->ActivitySets);
9c1bc317 767 $pageTitle = $activitySetNames[$activitySetName] ?? NULL;
6a488035
TO
768 $template->assign('pageTitle', $pageTitle);
769
770 if ($activitySetName) {
771 $activityTypes = $form->getActivityTypes($xml, $activitySetName);
772 }
773 else {
69805643 774 $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE);
6a488035
TO
775 }
776
777 if (!$activityTypes) {
778 return FALSE;
779 }
780
b44e3f84 781 // next get activity set Information
e547f744 782 $activitySet = array(
353ffa53 783 'label' => $form->getActivitySetLabel($xml, $activitySetName),
6a488035
TO
784 'includeActivities' => 'All',
785 'redact' => 'false',
786 );
787 $template->assign_by_ref('activitySet', $activitySet);
788
789 //now collect all the information about activities
affcc9d2 790 $activities = [];
6a488035
TO
791 $form->getActivities($clientID, $caseID, $activityTypes, $activities);
792 $template->assign_by_ref('activities', $activities);
793
8ed46a8d 794 return $template;
6a488035
TO
795 }
796
00be9182 797 public static function printCaseReport() {
a3d827a7
CW
798 $caseID = CRM_Utils_Request::retrieve('caseID', 'Positive');
799 $clientID = CRM_Utils_Request::retrieve('cid', 'Positive');
800 $activitySetName = CRM_Utils_Request::retrieve('asn', 'String');
801 $isRedact = CRM_Utils_Request::retrieve('redact', 'Boolean');
802 $includeActivities = CRM_Utils_Request::retrieve('all', 'Positive');
affcc9d2 803 $params = $otherRelationships = $globalGroupInfo = [];
353ffa53 804 $report = new CRM_Case_XMLProcessor_Report($isRedact);
6a488035
TO
805 if ($includeActivities) {
806 $params['include_activities'] = 1;
807 }
808
809 if ($isRedact) {
810 $params['is_redact'] = 1;
affcc9d2 811 $report->_redactionStringRules = [];
6a488035
TO
812 }
813 $template = CRM_Core_Smarty::singleton();
814
815 //get case related relationships (Case Role)
816 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($clientID, $caseID);
817 $caseType = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
818
819 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
820 $caseRoles = $xmlProcessor->get($caseType, 'CaseRoles');
821 foreach ($caseRelationships as $key => & $value) {
5655a8d4
AF
822 if (!empty($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']])) {
823 unset($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']]);
6a488035
TO
824 }
825 if ($isRedact) {
826 if (!array_key_exists($value['name'], $report->_redactionStringRules)) {
827 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
828 array($value['name'] => 'name_' . rand(10000, 100000))
829 );
830 }
a7e52def 831 $value['name'] = $report->redact($value['name'], TRUE, $report->_redactionStringRules);
a7488080 832 if (!empty($value['email']) &&
6a488035
TO
833 !array_key_exists($value['email'], $report->_redactionStringRules)
834 ) {
835 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
836 array($value['email'] => 'email_' . rand(10000, 100000))
837 );
838 }
839
a7e52def 840 $value['email'] = $report->redact($value['email'], TRUE, $report->_redactionStringRules);
6a488035 841
a7488080 842 if (!empty($value['phone']) &&
6a488035
TO
843 !array_key_exists($value['phone'], $report->_redactionStringRules)
844 ) {
845 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
846 array($value['phone'] => 'phone_' . rand(10000, 100000))
847 );
848 }
a7e52def 849 $value['phone'] = $report->redact($value['phone'], TRUE, $report->_redactionStringRules);
6a488035
TO
850 }
851 }
852
853 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
854 if ($isRedact) {
a7e52def 855 foreach ($caseRoles['client'] as &$client) {
856 if (!array_key_exists(CRM_Utils_Array::value('sort_name', $client), $report->_redactionStringRules)) {
6a488035 857
a7e52def 858 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
859 array(CRM_Utils_Array::value('sort_name', $client) => 'name_' . rand(10000, 100000))
860 );
861 }
862 if (!array_key_exists(CRM_Utils_Array::value('display_name', $client), $report->_redactionStringRules)) {
863 $report->_redactionStringRules[CRM_Utils_Array::value('display_name', $client)] = $report->_redactionStringRules[CRM_Utils_Array::value('sort_name', $client)];
864 }
865 $client['sort_name'] = $report->redact(CRM_Utils_Array::value('sort_name', $client), TRUE, $report->_redactionStringRules);
01217591 866 if (!empty($client['email']) &&
867 !array_key_exists($client['email'], $report->_redactionStringRules)
a7e52def 868 ) {
869 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
01217591 870 array($client['email'] => 'email_' . rand(10000, 100000))
a7e52def 871 );
872 }
873 $client['email'] = $report->redact(CRM_Utils_Array::value('email', $client), TRUE, $report->_redactionStringRules);
874
01217591 875 if (!empty($client['phone']) &&
876 !array_key_exists($client['phone'], $report->_redactionStringRules)
a7e52def 877 ) {
878 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
01217591 879 array($client['phone'] => 'phone_' . rand(10000, 100000))
a7e52def 880 );
881 }
882 $client['phone'] = $report->redact(CRM_Utils_Array::value('phone', $client), TRUE, $report->_redactionStringRules);
6a488035 883 }
6a488035 884 }
6a488035
TO
885 // Retrieve ALL client relationships
886 $relClient = CRM_Contact_BAO_Relationship::getRelationship($clientID,
887 CRM_Contact_BAO_Relationship::CURRENT,
888 0, 0, 0, NULL, NULL, FALSE
889 );
890 foreach ($relClient as $r) {
891 if ($isRedact) {
892 if (!array_key_exists($r['name'], $report->_redactionStringRules)) {
893 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
894 array($r['name'] => 'name_' . rand(10000, 100000))
895 );
896 }
897 if (!array_key_exists($r['display_name'], $report->_redactionStringRules)) {
898 $report->_redactionStringRules[$r['display_name']] = $report->_redactionStringRules[$r['name']];
899 }
a7e52def 900 $r['name'] = $report->redact($r['name'], TRUE, $report->_redactionStringRules);
6a488035 901
a7488080 902 if (!empty($r['phone']) &&
6a488035
TO
903 !array_key_exists($r['phone'], $report->_redactionStringRules)
904 ) {
905 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
906 array($r['phone'] => 'phone_' . rand(10000, 100000))
907 );
908 }
a7e52def 909 $r['phone'] = $report->redact($r['phone'], TRUE, $report->_redactionStringRules);
6a488035 910
a7488080 911 if (!empty($r['email']) &&
6a488035
TO
912 !array_key_exists($r['email'], $report->_redactionStringRules)
913 ) {
914 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
915 array($r['email'] => 'email_' . rand(10000, 100000))
916 );
917 }
a7e52def 918 $r['email'] = $report->redact($r['email'], TRUE, $report->_redactionStringRules);
6a488035
TO
919 }
920 if (!array_key_exists($r['id'], $caseRelationships)) {
921 $otherRelationships[] = $r;
922 }
923 }
924
925 // Now global contact list that appears on all cases.
926 $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo);
927 foreach ($relGlobal as & $r) {
928 if ($isRedact) {
929 if (!array_key_exists($r['sort_name'], $report->_redactionStringRules)) {
930 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
931 array($r['sort_name'] => 'name_' . rand(10000, 100000))
932 );
933 }
934 if (!array_key_exists($r['display_name'], $report->_redactionStringRules)) {
935 $report->_redactionStringRules[$r['display_name']] = $report->_redactionStringRules[$r['sort_name']];
936 }
937
a7e52def 938 $r['sort_name'] = $report->redact($r['sort_name'], TRUE, $report->_redactionStringRules);
a7488080 939 if (!empty($r['phone']) &&
6a488035
TO
940 !array_key_exists($r['phone'], $report->_redactionStringRules)
941 ) {
942 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
943 array($r['phone'] => 'phone_' . rand(10000, 100000))
944 );
945 }
a7e52def 946 $r['phone'] = $report->redact($r['phone'], TRUE, $report->_redactionStringRules);
6a488035 947
a7488080 948 if (!empty($r['email']) &&
6a488035
TO
949 !array_key_exists($r['email'], $report->_redactionStringRules)
950 ) {
951 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
952 array($r['email'] => 'email_' . rand(10000, 100000))
953 );
954 }
a7e52def 955 $r['email'] = $report->redact($r['email'], TRUE, $report->_redactionStringRules);
6a488035
TO
956 }
957 }
958
b077cff9
JM
959 // Retrieve custom values for cases.
960 $customValues = CRM_Core_BAO_CustomValueTable::getEntityValues($caseID, 'Case');
961 $extends = array('case');
962 $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends);
affcc9d2 963 $caseCustomFields = [];
762891db
SL
964 foreach ($groupTree as $gid => $group_values) {
965 foreach ($group_values['fields'] as $id => $field_values) {
219af4d3
CW
966 if (array_key_exists($id, $customValues)) {
967 $caseCustomFields[$gid]['title'] = $group_values['title'];
968 $caseCustomFields[$gid]['values'][$id] = array(
b077cff9
JM
969 'label' => $field_values['label'],
970 'value' => $customValues[$id],
971 );
972 }
973 }
974 }
6a488035
TO
975 $template->assign('caseRelationships', $caseRelationships);
976 $template->assign('caseRoles', $caseRoles);
977 $template->assign('otherRelationships', $otherRelationships);
978 $template->assign('globalRelationships', $relGlobal);
979 $template->assign('globalGroupInfo', $globalGroupInfo);
b077cff9 980 $template->assign('caseCustomFields', $caseCustomFields);
6a488035
TO
981 $contents = self::getCaseReport($clientID,
982 $caseID,
983 $activitySetName,
984 $params,
985 $report
986 );
67f8a3ed 987 $printReport = CRM_Case_Audit_Audit::run($contents, $clientID, $caseID);
6a488035
TO
988 echo $printReport;
989 CRM_Utils_System::civiExit();
990 }
96025800 991
6a488035 992}