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