Merge pull request #17588 from artfulrobot/artfulrobot-property-bag-support-empty
[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 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'],
550 'fieldID' => $typeValue['fieldID'],
551 );
552 }
553 $customGroups[$dao->groupTitle] = $customGroup;
554 }
555 }
556
557 return empty($customGroups) ? NULL : $customGroups;
558 }
559
560 /**
561 * @param int $activityTypeID
562 * @param null $dateFormat
563 * @param bool $onlyActive
564 *
565 * @return mixed
566 */
567 public function getActivityTypeCustomSQL($activityTypeID, $dateFormat = NULL, $onlyActive = TRUE) {
568 static $cache = [];
569
570 if (is_null($activityTypeID)) {
571 $activityTypeID = 0;
572 }
573
574 if (!isset($cache[$activityTypeID])) {
575 $query = "
576 SELECT 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
585 FROM civicrm_custom_group cg,
586 civicrm_custom_field cf
587 WHERE cf.custom_group_id = cg.id
588 AND cg.extends = 'Activity'
589 AND " . CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, 'cg.');
590
591 if ($onlyActive) {
592 $query .= " AND cf.is_active = 1 ";
593 }
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 }
600 $query .= "ORDER BY cg.weight, cf.weight";
601 $params = array(
602 1 => array(
603 $activityTypeID,
604 'Integer',
605 ),
606 );
607 $dao = CRM_Core_DAO::executeQuery($query, $params);
608
609 $result = $options = $sql = $groupTitle = [];
610 while ($dao->fetch()) {
611 if (!array_key_exists($dao->tableName, $result)) {
612 $result[$dao->tableName] = [];
613 $sql[$dao->tableName] = [];
614 }
615 $result[$dao->tableName][$dao->columnName] = array(
616 'label' => $dao->label,
617 'type' => $dao->dataType,
618 'fieldID' => $dao->fieldID,
619 );
620
621 $options[$dao->fieldID] = [];
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 = "
633 SELECT label, value
634 FROM civicrm_option_value
635 WHERE option_group_id = %1
636 ";
637
638 $option = CRM_Core_DAO::executeQuery($query, array(1 => array($dao->optionGroupID, 'Positive')));
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);
657 $title = CRM_Core_DAO::escapeString($groupTitle[$tableName]);
658 $mysqlTableName = CRM_Utils_Type::escape($tableName, 'MysqlColumnNameOrAlias');
659 $sql[$tableName] = "
660 SELECT '" . $title . "' as groupTitle, $columnNames
661 FROM $mysqlTableName
662 WHERE entity_id = %1
663 ";
664 }
665
666 $cache[$activityTypeID] = array($result, $options, $sql);
667 }
668 return $cache[$activityTypeID];
669 }
670
671 /**
672 * @param int $activityID
673 *
674 * @return null|string
675 */
676 public function getCreatedBy($activityID) {
677 $query = "
678 SELECT c.display_name
679 FROM civicrm_contact c,
680 civicrm_log l
681 WHERE l.entity_table = 'civicrm_activity'
682 AND l.entity_id = %1
683 AND l.modified_id = c.id
684 LIMIT 1
685 ";
686
687 $params = array(1 => array($activityID, 'Integer'));
688 return CRM_Core_DAO::singleValueQuery($query, $params);
689 }
690
691 /**
692 * @param $string
693 * @param bool $printReport
694 * @param array $replaceString
695 *
696 * @return mixed
697 */
698 private function redact($string, $printReport = FALSE, $replaceString = []) {
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
709 /**
710 * @param int $clientID
711 * @param int $caseID
712 * @param string $activitySetName
713 * @param array $params
714 * @param CRM_Core_Form $form
715 *
716 * @return mixed
717 */
718 public static function getCaseReport($clientID, $caseID, $activitySetName, $params, $form) {
719
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
738 $template = CRM_Core_Smarty::singleton();
739
740 $template->assign('caseId', $caseID);
741 $template->assign('clientID', $clientID);
742 $template->assign('activitySetName', $activitySetName);
743
744 if (!empty($params['is_redact'])) {
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
757 if (CRM_Utils_Array::value('include_activities', $params) == 1) {
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);
767 $pageTitle = $activitySetNames[$activitySetName] ?? NULL;
768 $template->assign('pageTitle', $pageTitle);
769
770 if ($activitySetName) {
771 $activityTypes = $form->getActivityTypes($xml, $activitySetName);
772 }
773 else {
774 $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE);
775 }
776
777 if (!$activityTypes) {
778 return FALSE;
779 }
780
781 // next get activity set Information
782 $activitySet = array(
783 'label' => $form->getActivitySetLabel($xml, $activitySetName),
784 'includeActivities' => 'All',
785 'redact' => 'false',
786 );
787 $template->assign_by_ref('activitySet', $activitySet);
788
789 //now collect all the information about activities
790 $activities = [];
791 $form->getActivities($clientID, $caseID, $activityTypes, $activities);
792 $template->assign_by_ref('activities', $activities);
793
794 return $template;
795 }
796
797 public static function printCaseReport() {
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');
803 $params = $otherRelationships = $globalGroupInfo = [];
804 $report = new CRM_Case_XMLProcessor_Report($isRedact);
805 if ($includeActivities) {
806 $params['include_activities'] = 1;
807 }
808
809 if ($isRedact) {
810 $params['is_redact'] = 1;
811 $report->_redactionStringRules = [];
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) {
822 if (!empty($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']])) {
823 unset($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']]);
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 }
831 $value['name'] = $report->redact($value['name'], TRUE, $report->_redactionStringRules);
832 if (!empty($value['email']) &&
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
840 $value['email'] = $report->redact($value['email'], TRUE, $report->_redactionStringRules);
841
842 if (!empty($value['phone']) &&
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 }
849 $value['phone'] = $report->redact($value['phone'], TRUE, $report->_redactionStringRules);
850 }
851 }
852
853 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
854 if ($isRedact) {
855 foreach ($caseRoles['client'] as &$client) {
856 if (!array_key_exists(CRM_Utils_Array::value('sort_name', $client), $report->_redactionStringRules)) {
857
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);
866 if (!empty($client['email']) &&
867 !array_key_exists($client['email'], $report->_redactionStringRules)
868 ) {
869 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
870 array($client['email'] => 'email_' . rand(10000, 100000))
871 );
872 }
873 $client['email'] = $report->redact(CRM_Utils_Array::value('email', $client), TRUE, $report->_redactionStringRules);
874
875 if (!empty($client['phone']) &&
876 !array_key_exists($client['phone'], $report->_redactionStringRules)
877 ) {
878 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
879 array($client['phone'] => 'phone_' . rand(10000, 100000))
880 );
881 }
882 $client['phone'] = $report->redact(CRM_Utils_Array::value('phone', $client), TRUE, $report->_redactionStringRules);
883 }
884 }
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 }
900 $r['name'] = $report->redact($r['name'], TRUE, $report->_redactionStringRules);
901
902 if (!empty($r['phone']) &&
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 }
909 $r['phone'] = $report->redact($r['phone'], TRUE, $report->_redactionStringRules);
910
911 if (!empty($r['email']) &&
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 }
918 $r['email'] = $report->redact($r['email'], TRUE, $report->_redactionStringRules);
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
938 $r['sort_name'] = $report->redact($r['sort_name'], TRUE, $report->_redactionStringRules);
939 if (!empty($r['phone']) &&
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 }
946 $r['phone'] = $report->redact($r['phone'], TRUE, $report->_redactionStringRules);
947
948 if (!empty($r['email']) &&
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 }
955 $r['email'] = $report->redact($r['email'], TRUE, $report->_redactionStringRules);
956 }
957 }
958
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);
963 $caseCustomFields = [];
964 foreach ($groupTree as $gid => $group_values) {
965 foreach ($group_values['fields'] as $id => $field_values) {
966 if (array_key_exists($id, $customValues)) {
967 $caseCustomFields[$gid]['title'] = $group_values['title'];
968 $caseCustomFields[$gid]['values'][$id] = array(
969 'label' => $field_values['label'],
970 'value' => $customValues[$id],
971 );
972 }
973 }
974 }
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);
980 $template->assign('caseCustomFields', $caseCustomFields);
981 $contents = self::getCaseReport($clientID,
982 $caseID,
983 $activitySetName,
984 $params,
985 $report
986 );
987 $printReport = CRM_Case_Audit_Audit::run($contents, $clientID, $caseID);
988 echo $printReport;
989 CRM_Utils_System::civiExit();
990 }
991
992 }