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