Merge pull request #22966 from eileenmcnaughton/retrieve
[civicrm-core.git] / CRM / Case / XMLProcessor / 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 */
17 class CRM_Case_XMLProcessor_Report extends CRM_Case_XMLProcessor {
18
19 /**
20 * The default variable defined.
21 *
22 * @var bool
23 */
24 protected $_isRedact;
25
26 /**
27 */
28 public function __construct() {
29 }
30
31 public function getRedactionRules() {
32 foreach (array(
33 'redactionStringRules',
34 'redactionRegexRules',
35 ) as $key => $rule) {
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
56 /**
57 * @param int $clientID
58 * @param int $caseID
59 *
60 * @return array
61 */
62 public function &caseInfo(
63 $clientID,
64 $caseID
65 ) {
66 $case = $this->_redactionRegexRules = [];
67
68 if (empty($this->_redactionStringRules)) {
69 $this->_redactionStringRules = [];
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)) {
94 $case['subject'] = $dao->subject;
95 $case['start_date'] = $dao->start_date;
96 $case['end_date'] = $dao->end_date;
97 $case['caseType'] = CRM_Case_BAO_Case::getCaseType($caseID);
98 $case['caseTypeName'] = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
99 $case['status'] = CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'status_id', $dao->status_id);
100 }
101 return $case;
102 }
103
104 /**
105 * @param $xml
106 * @param string $activitySetName
107 *
108 * @return array|bool
109 */
110 public function getActivityTypes($xml, $activitySetName) {
111 foreach ($xml->ActivitySets as $activitySetsXML) {
112 foreach ($activitySetsXML->ActivitySet as $activitySetXML) {
113 if ((string ) $activitySetXML->name == $activitySetName) {
114 $activityTypes = [];
115 $allActivityTypes = CRM_Case_PseudoConstant::caseActivityType(TRUE, TRUE);
116 foreach ($activitySetXML->ActivityTypes as $activityTypesXML) {
117 foreach ($activityTypesXML as $activityTypeXML) {
118 $activityTypeName = (string ) $activityTypeXML->name;
119 $activityTypeInfo = $allActivityTypes[$activityTypeName] ?? NULL;
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
132 /**
133 * @param $xml
134 * @param string $activitySetName
135 *
136 * @return null|string
137 */
138 public function getActivitySetLabel($xml, $activitySetName) {
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
149 /**
150 * @param int $clientID
151 * @param int $caseID
152 * @param $activityTypes
153 * @param $activities
154 */
155 public function getActivities($clientID, $caseID, $activityTypes, &$activities) {
156 // get all activities for this case that in this activityTypes set
157 foreach ($activityTypes as $aType) {
158 $map[$aType['id']] = $aType;
159 }
160
161 $activityTypeIDs = implode(',', array_keys($map));
162 $query = "
163 SELECT a.*, c.id as caseID
164 FROM civicrm_activity a,
165 civicrm_case c,
166 civicrm_case_activity ac
167 WHERE a.is_current_revision = 1
168 AND a.is_deleted =0
169 AND a.activity_type_id IN ( $activityTypeIDs )
170 AND c.id = ac.case_id
171 AND a.id = ac.activity_id
172 AND 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
186 /**
187 * @param int $clientID
188 * @param int $activityID
189 * @param bool $anyActivity
190 * @param int $redact
191 *
192 * @return mixed
193 */
194 public function &getActivityInfo($clientID, $activityID, $anyActivity = FALSE, $redact = 0) {
195 static $activityInfos = [];
196 if ($redact) {
197 $this->_isRedact = 1;
198 $this->getRedactionRules();
199 }
200
201 $index = $activityID . '_' . (int) $anyActivity;
202
203 if ($clientID) {
204 $index = $index . '_' . $clientID;
205 }
206
207 if (!array_key_exists($index, $activityInfos)) {
208 $activityInfos[$index] = [];
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
217 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
218 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
219 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
220
221 $query = "
222 SELECT a.*, aa.contact_id as assigneeID, at.contact_id as targetID
223 {$selectCaseActivity}
224 FROM civicrm_activity a
225 {$joinCaseActivity}
226 LEFT JOIN civicrm_activity_contact at ON a.id = at.activity_id AND at.record_type_id = $targetID
227 LEFT JOIN civicrm_activity_contact aa ON a.id = aa.activity_id AND aa.record_type_id = $assigneeID
228 WHERE 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.
235 if ($dao->activity_type_id == CRM_Core_PseudoConstant::getKey('CRM_Activity_DAO_Activity', 'activity_type_id', 'Email')) {
236 $anyActivity = TRUE;
237 }
238 $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, $anyActivity);
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
253 /**
254 * @param int $clientID
255 * @param $activityDAO
256 * @param $activityTypeInfo
257 *
258 * @return array
259 */
260 public function &getActivity($clientID, $activityDAO, &$activityTypeInfo) {
261 if (empty($this->_redactionStringRules)) {
262 $this->_redactionStringRules = [];
263 }
264
265 $activity = [];
266 $activity['fields'] = [];
267 if ($clientID) {
268 $clientID = CRM_Utils_Type::escape($clientID, 'Integer');
269 if (!in_array($activityTypeInfo['name'], array(
270 'Email',
271 'Inbound Email',
272 ))
273 ) {
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(
297 'name' => 'Client',
298 'label' => ts('Client'),
299 'value' => $this->redact($client),
300 'type' => 'String',
301 );
302 }
303
304 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
305 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
306 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
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.
310 $targetNames = CRM_Activity_BAO_ActivityContact::getNames($activityDAO->id, $targetID);
311 $processTarget = FALSE;
312 $name = 'With Contact(s)';
313 $label = ts('With Contact(s)');
314 if (in_array($activityTypeInfo['name'], array('Email', 'Inbound Email'))) {
315 $processTarget = TRUE;
316 $name = 'Recipient';
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) {
329 $targetRedacted = [];
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(
346 'name' => $name,
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(
356 'name' => 'Activity Type',
357 'label' => ts('Activity Type'),
358 'value' => $activityTypeInfo['label'],
359 'type' => 'String',
360 );
361
362 $activity['fields'][] = array(
363 'name' => 'Subject',
364 'label' => ts('Subject'),
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?
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(
382 'name' => 'Created By',
383 'label' => ts('Created By'),
384 'value' => $this->redact($creator),
385 'type' => 'String',
386 );
387 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
388 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
389 $source_contact_id = CRM_Activity_BAO_Activity::getActivityContact($activityDAO->id, $sourceID);
390 $reporter = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
391 $source_contact_id,
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',
403 $source_contact_id,
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(
412 'name' => 'Reported By',
413 'label' => ts('Reported By'),
414 'value' => $this->redact($reporter),
415 'type' => 'String',
416 );
417
418 if (!empty($activityDAO->assigneeID)) {
419 //allow multiple assignee contacts.CRM-4503.
420 $assignee_contact_names = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames(array($activityDAO->id), TRUE);
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(
431 'name' => 'Assigned to',
432 'label' => ts('Assigned to'),
433 'value' => $assigneeContacts,
434 'type' => 'String',
435 );
436 }
437
438 if ($activityDAO->medium_id) {
439 $activity['fields'][] = array(
440 'name' => 'Medium',
441 'label' => ts('Medium'),
442 'value' => CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'medium_id', $activityDAO->medium_id),
443 'type' => 'String',
444 );
445 }
446
447 $activity['fields'][] = array(
448 'name' => 'Location',
449 'label' => ts('Location'),
450 'value' => $activityDAO->location,
451 'type' => 'String',
452 );
453
454 $activity['fields'][] = array(
455 'name' => 'Date and Time',
456 'label' => ts('Date and Time'),
457 'value' => $activityDAO->activity_date_time,
458 'type' => 'Date',
459 );
460
461 $activity['fields'][] = array(
462 'name' => 'Details',
463 'label' => ts('Details'),
464 'value' => $this->redact(CRM_Utils_String::purifyHTML(CRM_Utils_String::stripAlternatives($activityDAO->details))),
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(
471 'name' => 'Duration',
472 'label' => ts('Duration'),
473 'value' => $activityDAO->duration . ' ' . ts('minutes'),
474 'type' => 'Int',
475 );
476 }
477 $activity['fields'][] = array(
478 'name' => 'Status',
479 'label' => ts('Status'),
480 'value' => CRM_Core_PseudoConstant::getLabel('CRM_Activity_DAO_Activity', 'activity_status_id',
481 $activityDAO->status_id
482 ),
483 'type' => 'String',
484 );
485
486 $activity['fields'][] = array(
487 'name' => 'Priority',
488 'label' => ts('Priority'),
489 'value' => CRM_Core_PseudoConstant::getLabel('CRM_Activity_DAO_Activity', 'priority_id',
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
504 /**
505 * @param int $clientID
506 * @param $activityDAO
507 * @param $activityTypeInfo
508 *
509 * @return array|null
510 */
511 public function getCustomData($clientID, $activityDAO, &$activityTypeInfo) {
512 list($typeValues, $options, $sql) = $this->getActivityTypeCustomSQL($activityTypeInfo['id'], '%Y-%m-%d');
513
514 $params = array(1 => array($activityDAO->id, 'Integer'));
515
516 $customGroups = [];
517 foreach ($sql as $tableName => $sqlClause) {
518 $dao = CRM_Core_DAO::executeQuery($sqlClause, $params);
519 if ($dao->fetch()) {
520 $customGroup = [];
521 foreach ($typeValues[$tableName] as $columnName => $typeValue) {
522
523 if (CRM_Utils_Array::value('type', $typeValue) == 'Date') {
524 $value = $dao->$columnName;
525 }
526 else {
527 $value = CRM_Core_BAO_CustomField::displayValue($dao->$columnName, $typeValue['fieldID'], $activityDAO->id);
528 }
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 }
541 }
542 //$typeValue
543 $customGroup[] = array(
544 'label' => $typeValue['label'],
545 'value' => $value,
546 'type' => $typeValue['type'],
547 'fieldID' => $typeValue['fieldID'],
548 );
549 }
550 $customGroups[$dao->groupTitle] = $customGroup;
551 }
552 }
553
554 return empty($customGroups) ? NULL : $customGroups;
555 }
556
557 /**
558 * @param int $activityTypeID
559 * @param string|null $dateFormat
560 * @param bool $onlyActive
561 *
562 * @return mixed
563 */
564 public function getActivityTypeCustomSQL($activityTypeID, $dateFormat = NULL, $onlyActive = TRUE) {
565 static $cache = [];
566
567 if (is_null($activityTypeID)) {
568 $activityTypeID = 0;
569 }
570
571 if (!isset($cache[$activityTypeID])) {
572 $query = "
573 SELECT 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
582 FROM civicrm_custom_group cg,
583 civicrm_custom_field cf
584 WHERE cf.custom_group_id = cg.id
585 AND cg.extends = 'Activity'
586 AND " . CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, 'cg.');
587
588 if ($onlyActive) {
589 $query .= " AND cf.is_active = 1 ";
590 }
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 }
597 $query .= "ORDER BY cg.weight, cf.weight";
598 $params = array(
599 1 => array(
600 $activityTypeID,
601 'Integer',
602 ),
603 );
604 $dao = CRM_Core_DAO::executeQuery($query, $params);
605
606 $result = $options = $sql = $groupTitle = [];
607 while ($dao->fetch()) {
608 if (!array_key_exists($dao->tableName, $result)) {
609 $result[$dao->tableName] = [];
610 $sql[$dao->tableName] = [];
611 }
612 $result[$dao->tableName][$dao->columnName] = array(
613 'label' => $dao->label,
614 'type' => $dao->dataType,
615 'fieldID' => $dao->fieldID,
616 );
617
618 $options[$dao->fieldID] = [];
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 = "
630 SELECT label, value
631 FROM civicrm_option_value
632 WHERE option_group_id = %1
633 ";
634
635 $option = CRM_Core_DAO::executeQuery($query, array(1 => array($dao->optionGroupID, 'Positive')));
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);
654 $title = CRM_Core_DAO::escapeString($groupTitle[$tableName]);
655 $mysqlTableName = CRM_Utils_Type::escape($tableName, 'MysqlColumnNameOrAlias');
656 $sql[$tableName] = "
657 SELECT '" . $title . "' as groupTitle, $columnNames
658 FROM $mysqlTableName
659 WHERE entity_id = %1
660 ";
661 }
662
663 $cache[$activityTypeID] = array($result, $options, $sql);
664 }
665 return $cache[$activityTypeID];
666 }
667
668 /**
669 * @param int $activityID
670 *
671 * @return null|string
672 */
673 public function getCreatedBy($activityID) {
674 $query = "
675 SELECT c.display_name
676 FROM civicrm_contact c,
677 civicrm_log l
678 WHERE l.entity_table = 'civicrm_activity'
679 AND l.entity_id = %1
680 AND l.modified_id = c.id
681 LIMIT 1
682 ";
683
684 $params = array(1 => array($activityID, 'Integer'));
685 return CRM_Core_DAO::singleValueQuery($query, $params);
686 }
687
688 /**
689 * @param $string
690 * @param bool $printReport
691 * @param array $replaceString
692 *
693 * @return mixed
694 */
695 private function redact($string, $printReport = FALSE, $replaceString = []) {
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
706 /**
707 * @param int $clientID
708 * @param int $caseID
709 * @param string $activitySetName
710 * @param array $params
711 * @param CRM_Core_Form $form
712 *
713 * @return mixed
714 */
715 public static function getCaseReport($clientID, $caseID, $activitySetName, $params, $form) {
716
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
735 $template = CRM_Core_Smarty::singleton();
736
737 $template->assign('caseId', $caseID);
738 $template->assign('clientID', $clientID);
739 $template->assign('activitySetName', $activitySetName);
740
741 if (!empty($params['is_redact'])) {
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
754 if (CRM_Utils_Array::value('include_activities', $params) == 1) {
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);
764 $pageTitle = $activitySetNames[$activitySetName] ?? NULL;
765 $template->assign('pageTitle', $pageTitle);
766
767 if ($activitySetName) {
768 $activityTypes = $form->getActivityTypes($xml, $activitySetName);
769 }
770 else {
771 $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE);
772 }
773
774 if (!$activityTypes) {
775 return FALSE;
776 }
777
778 // next get activity set Information
779 $activitySet = array(
780 'label' => $form->getActivitySetLabel($xml, $activitySetName),
781 'includeActivities' => 'All',
782 'redact' => 'false',
783 );
784 $template->assign_by_ref('activitySet', $activitySet);
785
786 //now collect all the information about activities
787 $activities = [];
788 $form->getActivities($clientID, $caseID, $activityTypes, $activities);
789 $template->assign_by_ref('activities', $activities);
790
791 return $template;
792 }
793
794 public static function printCaseReport() {
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');
800 $params = $otherRelationships = $globalGroupInfo = [];
801 $report = new CRM_Case_XMLProcessor_Report($isRedact);
802 if ($includeActivities) {
803 $params['include_activities'] = 1;
804 }
805
806 if ($isRedact) {
807 $params['is_redact'] = 1;
808 $report->_redactionStringRules = [];
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) {
819 if (!empty($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']])) {
820 unset($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']]);
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 }
828 $value['name'] = $report->redact($value['name'], TRUE, $report->_redactionStringRules);
829 if (!empty($value['email']) &&
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
837 $value['email'] = $report->redact($value['email'], TRUE, $report->_redactionStringRules);
838
839 if (!empty($value['phone']) &&
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 }
846 $value['phone'] = $report->redact($value['phone'], TRUE, $report->_redactionStringRules);
847 }
848 }
849
850 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
851 if ($isRedact) {
852 foreach ($caseRoles['client'] as &$client) {
853 if (!array_key_exists(CRM_Utils_Array::value('sort_name', $client), $report->_redactionStringRules)) {
854
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);
863 if (!empty($client['email']) &&
864 !array_key_exists($client['email'], $report->_redactionStringRules)
865 ) {
866 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
867 array($client['email'] => 'email_' . rand(10000, 100000))
868 );
869 }
870 $client['email'] = $report->redact(CRM_Utils_Array::value('email', $client), TRUE, $report->_redactionStringRules);
871
872 if (!empty($client['phone']) &&
873 !array_key_exists($client['phone'], $report->_redactionStringRules)
874 ) {
875 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
876 array($client['phone'] => 'phone_' . rand(10000, 100000))
877 );
878 }
879 $client['phone'] = $report->redact(CRM_Utils_Array::value('phone', $client), TRUE, $report->_redactionStringRules);
880 }
881 }
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 }
897 $r['name'] = $report->redact($r['name'], TRUE, $report->_redactionStringRules);
898
899 if (!empty($r['phone']) &&
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 }
906 $r['phone'] = $report->redact($r['phone'], TRUE, $report->_redactionStringRules);
907
908 if (!empty($r['email']) &&
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 }
915 $r['email'] = $report->redact($r['email'], TRUE, $report->_redactionStringRules);
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
935 $r['sort_name'] = $report->redact($r['sort_name'], TRUE, $report->_redactionStringRules);
936 if (!empty($r['phone']) &&
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 }
943 $r['phone'] = $report->redact($r['phone'], TRUE, $report->_redactionStringRules);
944
945 if (!empty($r['email']) &&
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 }
952 $r['email'] = $report->redact($r['email'], TRUE, $report->_redactionStringRules);
953 }
954 }
955
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);
960 $caseCustomFields = [];
961 foreach ($groupTree as $gid => $group_values) {
962 foreach ($group_values['fields'] as $id => $field_values) {
963 if (array_key_exists($id, $customValues)) {
964 $caseCustomFields[$gid]['title'] = $group_values['title'];
965 $caseCustomFields[$gid]['values'][$id] = array(
966 'label' => $field_values['label'],
967 'value' => $customValues[$id],
968 );
969 }
970 }
971 }
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);
977 $template->assign('caseCustomFields', $caseCustomFields);
978 $contents = self::getCaseReport($clientID,
979 $caseID,
980 $activitySetName,
981 $params,
982 $report
983 );
984 $printReport = CRM_Case_Audit_Audit::run($contents, $clientID, $caseID);
985 echo $printReport;
986 CRM_Utils_System::civiExit();
987 }
988
989 }