Refactored out of CRM_Core_PseudoConstant: IMProvider(), pcm(), preferredCommunicatio...
[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 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
229 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
230 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
231
232 $query = "
233 SELECT a.*, aa.contact_id as assigneeID, at.contact_id as targetID
234 {$selectCaseActivity}
235 FROM civicrm_activity a
236 {$joinCaseActivity}
237 LEFT JOIN civicrm_activity_contact at ON a.id = at.activity_id AND at.record_type_id = $targetID
238 LEFT JOIN civicrm_activity_contact aa ON a.id = aa.activity_id AND aa.record_type_id = $assigneeID
239 WHERE a.id = %1
240 ";
241 $params = array(1 => array($activityID, 'Integer'));
242 $dao = CRM_Core_DAO::executeQuery($query, $params);
243
244 if ($dao->fetch()) {
245 //if activity type is email get info of all activities.
246 if ($dao->activity_type_id == CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name')) {
247 $anyActivity = TRUE;
248 }
249 $activityTypes = $this->allActivityTypes(FALSE, $anyActivity);
250 $activityTypeInfo = NULL;
251
252 if (isset($activityTypes[$dao->activity_type_id])) {
253 $activityTypeInfo = $activityTypes[$dao->activity_type_id];
254 }
255 if ($activityTypeInfo) {
256 $activityInfos[$index] = $this->getActivity($clientID, $dao, $activityTypeInfo);
257 }
258 }
259 }
260
261 return $activityInfos[$index];
262 }
263
264 function &getActivity($clientID, $activityDAO, &$activityTypeInfo) {
265 if (empty($this->_redactionStringRules)) {
266 $this->_redactionStringRules = array();
267 }
268
269 $activity = array();
270 $activity['fields'] = array();
271 if ($clientID) {
272 $clientID = CRM_Utils_Type::escape($clientID, 'Integer');
273 if (!in_array($activityTypeInfo['name'], array(
274 'Email', 'Inbound Email'))) {
275 $activity['editURL'] = CRM_Utils_System::url('civicrm/case/activity',
276 "reset=1&cid={$clientID}&caseid={$activityDAO->caseID}&action=update&atype={$activityDAO->activity_type_id}&id={$activityDAO->id}"
277 );
278 }
279 else {
280 $activity['editURL'] = '';
281 }
282
283 $client = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'display_name');
284 // add Client SortName as well as Display to the strings to be redacted across the case session
285 // suffixed with a randomly generated 4-digit number
286 if (!array_key_exists($client, $this->_redactionStringRules)) {
287 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
288 array($client => 'name_' . rand(10000, 100000))
289 );
290
291 $clientSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'sort_name');
292 if (!array_key_exists($clientSortName, $this->_redactionStringRules)) {
293 $this->_redactionStringRules[$clientSortName] = $this->_redactionStringRules[$client];
294 }
295 }
296
297 $activity['fields'][] = array(
298 'label' => 'Client',
299 'value' => $this->redact($client),
300 'type' => 'String',
301 );
302 }
303
304 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
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 $label = ts('With Contact(s)');
313 if (in_array($activityTypeInfo['name'], array('Email', 'Inbound Email'))) {
314 $processTarget = TRUE;
315 $label = ts('Recipient');
316 }
317 if (!$processTarget) {
318 foreach ($targetNames as $targetID => $targetName) {
319 if ($targetID != $clientID) {
320 $processTarget = TRUE;
321 break;
322 }
323 }
324 }
325
326 if ($processTarget) {
327 $targetRedacted = array();
328 foreach ($targetNames as $targetID => $target) {
329 // add Recipient SortName as well as Display to the strings to be redacted across the case session
330 // suffixed with a randomly generated 4-digit number
331 if (!array_key_exists($target, $this->_redactionStringRules)) {
332 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
333 array($target => 'name_' . rand(10000, 100000))
334 );
335 $targetSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $targetID, 'sort_name');
336 if (!array_key_exists($targetSortName, $this->_redactionStringRules)) {
337 $this->_redactionStringRules[$targetSortName] = $this->_redactionStringRules[$target];
338 }
339 }
340 $targetRedacted[] = $this->redact($target);
341 }
342
343 $activity['fields'][] = array(
344 'label' => $label,
345 'value' => implode('; ', $targetRedacted),
346 'type' => 'String',
347 );
348 }
349 }
350
351 // Activity Type info is a special field
352 $activity['fields'][] = array(
353 'label' => ts('Activity Type'),
354 'value' => $activityTypeInfo['label'],
355 'type' => 'String',
356 );
357
358 $activity['fields'][] = array(
359 'label' => ts('Subject'),
360 'value' => htmlspecialchars($this->redact($activityDAO->subject)),
361 'type' => 'Memo',
362 );
363
364 $creator = $this->getCreatedBy($activityDAO->id);
365 // add Creator to the strings to be redacted across the case session
366 if (!array_key_exists($creator, $this->_redactionStringRules)) {
367 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
368 array($creator => 'name_' . rand(10000, 100000))
369 );
370 }
371 $activity['fields'][] = array(
372 'label' => ts('Created By'),
373 'value' => $this->redact($creator),
374 'type' => 'String',
375 );
376 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
377 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
378 $source_contact_id = CRM_Activity_BAO_Activity::getActivityContact($activityDAO->id, $sourceID);
379 $reporter = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
380 $source_contact_id,
381 'display_name'
382 );
383
384 // add Reporter SortName as well as Display to the strings to be redacted across the case session
385 // suffixed with a randomly generated 4-digit number
386 if (!array_key_exists($reporter, $this->_redactionStringRules)) {
387 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
388 array($reporter => 'name_' . rand(10000, 100000))
389 );
390
391 $reporterSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
392 $activityDAO->source_contact_id,
393 'sort_name'
394 );
395 if (!array_key_exists($reporterSortName, $this->_redactionStringRules)) {
396 $this->_redactionStringRules[$reporterSortName] = $this->_redactionStringRules[$reporter];
397 }
398 }
399
400 $activity['fields'][] = array(
401 'label' => ts('Reported By'),
402 'value' => $this->redact($reporter),
403 'type' => 'String',
404 );
405
406 if (!empty($activityDAO->assigneeID)) {
407 //allow multiple assignee contacts.CRM-4503.
408 $assignee_contact_names = CRM_Activity_BAO_ActivityContact::getNames($activityDAO->id, $assigneeID, TRUE);
409
410 foreach ($assignee_contact_names as & $assignee) {
411 // add Assignee to the strings to be redacted across the case session
412 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
413 array($assignee => 'name_' . rand(10000, 100000))
414 );
415 $assignee = $this->redact($assignee);
416 }
417 $assigneeContacts = implode(', ', $assignee_contact_names);
418 $activity['fields'][] = array(
419 'label' => ts('Assigned To'),
420 'value' => $assigneeContacts,
421 'type' => 'String',
422 );
423 }
424
425 if ($activityDAO->medium_id) {
426 $activity['fields'][] = array(
427 'label' => ts('Medium'),
428 'value' => CRM_Core_OptionGroup::getLabel('encounter_medium',
429 $activityDAO->medium_id, FALSE
430 ),
431 'type' => 'String',
432 );
433 }
434
435 $activity['fields'][] = array(
436 'label' => ts('Location'),
437 'value' => $activityDAO->location,
438 'type' => 'String',
439 );
440
441 $activity['fields'][] = array(
442 'label' => ts('Date and Time'),
443 'value' => $activityDAO->activity_date_time,
444 'type' => 'Date',
445 );
446
447 $activity['fields'][] = array(
448 'label' => ts('Details'),
449 'value' => $this->redact(CRM_Utils_String::stripAlternatives($activityDAO->details)),
450 'type' => 'Memo',
451 );
452
453 // Skip Duration field if empty (to avoid " minutes" output). Might want to do this for all fields at some point. dgg
454 if ($activityDAO->duration) {
455 $activity['fields'][] = array(
456 'label' => ts('Duration'),
457 'value' => $activityDAO->duration . ' ' . ts('minutes'),
458 'type' => 'Int',
459 );
460 }
461 $activity['fields'][] = array(
462 'label' => ts('Status'),
463 'value' => CRM_Core_OptionGroup::getLabel('activity_status',
464 $activityDAO->status_id
465 ),
466 'type' => 'String',
467 );
468
469 $activity['fields'][] = array(
470 'label' => ts('Priority'),
471 'value' => CRM_Core_OptionGroup::getLabel('priority',
472 $activityDAO->priority_id
473 ),
474 'type' => 'String',
475 );
476
477 //for now empty custom groups
478 $activity['customGroups'] = $this->getCustomData($clientID,
479 $activityDAO,
480 $activityTypeInfo
481 );
482
483 return $activity;
484 }
485
486 function getCustomData($clientID, $activityDAO, &$activityTypeInfo) {
487 list($typeValues, $options, $sql) = $this->getActivityTypeCustomSQL($activityTypeInfo['id'], '%Y-%m-%d');
488
489 $params = array(1 => array($activityDAO->id, 'Integer'));
490
491 $customGroups = array();
492 foreach ($sql as $tableName => $sqlClause) {
493 $dao = CRM_Core_DAO::executeQuery($sqlClause, $params);
494 if ($dao->fetch()) {
495 $customGroup = array();
496 foreach ($typeValues[$tableName] as $columnName => $typeValue) {
497 $value = CRM_Core_BAO_CustomField::getDisplayValue($dao->$columnName,
498 $typeValue['fieldID'],
499 $options
500 );
501
502 if (CRM_Utils_Array::value('type', $typeValue) == 'Date') {
503 $value = $dao->$columnName;
504 }
505
506 if ($value) {
507 // Note: this is already taken care in getDisplayValue above, but sometimes
508 // strings like '^A^A' creates problem. So to fix this special case -
509 if (strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) {
510 $value = trim($value, CRM_Core_DAO::VALUE_SEPARATOR);
511 }
512 if (CRM_Utils_Array::value('type', $typeValue) == 'String' ||
513 CRM_Utils_Array::value('type', $typeValue) == 'Memo'
514 ) {
515 $value = $this->redact($value);
516 }
517 elseif (CRM_Utils_Array::value('type', $typeValue) == 'File') {
518 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $typeValue, 'entity_table');
519 $value = CRM_Core_BAO_File::attachmentInfo($tableName, $activityDAO->id);
520 }
521 elseif (CRM_Utils_Array::value('type', $typeValue) == 'Link') {
522 $value = CRM_Utils_System::formatWikiURL($value);
523 }
524 }
525 //$typeValue
526 $customGroup[] = array(
527 'label' => $typeValue['label'],
528 'value' => $value,
529 'type' => $typeValue['type'],
530 );
531 }
532 $customGroups[$dao->groupTitle] = $customGroup;
533 }
534 }
535
536 return empty($customGroups) ? NULL : $customGroups;
537 }
538
539 function getActivityTypeCustomSQL($activityTypeID, $dateFormat = NULL) {
540 static $cache = array();
541
542 if (is_null($activityTypeID)) {
543 $activityTypeID = 0;
544 }
545
546 if (!isset($cache[$activityTypeID])) {
547 $query = "
548 SELECT cg.title as groupTitle,
549 cg.table_name as tableName ,
550 cf.column_name as columnName,
551 cf.label as label ,
552 cg.id as groupID ,
553 cf.id as fieldID ,
554 cf.data_type as dataType ,
555 cf.html_type as htmlType ,
556 cf.option_group_id as optionGroupID
557 FROM civicrm_custom_group cg,
558 civicrm_custom_field cf
559 WHERE cf.custom_group_id = cg.id
560 AND cg.extends = 'Activity'";
561
562 if ($activityTypeID) {
563 $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 . "%' )";
564 }
565 else {
566 $query .= "AND cg.extends_entity_column_value IS NULL";
567 }
568 $query .= "ORDER BY cf.weight";
569 $params = array(
570 1 => array($activityTypeID,
571 'Integer',
572 ));
573 $dao = CRM_Core_DAO::executeQuery($query, $params);
574
575 $result = $options = $sql = $groupTitle = array();
576 while ($dao->fetch()) {
577 if (!array_key_exists($dao->tableName, $result)) {
578 $result[$dao->tableName] = array();
579 $sql[$dao->tableName] = array();
580 }
581 $result[$dao->tableName][$dao->columnName] = array(
582 'label' => $dao->label,
583 'type' => $dao->dataType,
584 'fieldID' => $dao->fieldID,
585 );
586
587 $options[$dao->fieldID] = array();
588 $options[$dao->fieldID]['attributes'] = array(
589 'label' => $dao->label,
590 'data_type' => $dao->dataType,
591 'html_type' => $dao->htmlType,
592 );
593 // since we want to add ISO date format.
594 if ($dateFormat && $dao->htmlType == 'Select Date') {
595 $options[$dao->fieldID]['attributes']['format'] = $dateFormat;
596 }
597 if ($dao->optionGroupID) {
598 $query = "
599 SELECT label, value
600 FROM civicrm_option_value
601 WHERE option_group_id = {$dao->optionGroupID}
602 ";
603
604 $option = CRM_Core_DAO::executeQuery($query);
605 while ($option->fetch()) {
606 $dataType = $dao->dataType;
607 if ($dataType == 'Int' || $dataType == 'Float') {
608 $num = round($option->value, 2);
609 $options[$dao->fieldID]["$num"] = $option->label;
610 }
611 else {
612 $options[$dao->fieldID][$option->value] = $option->label;
613 }
614 }
615 }
616
617 $sql[$dao->tableName][] = $dao->columnName;
618 $groupTitle[$dao->tableName] = $dao->groupTitle;
619 }
620
621 foreach ($sql as $tableName => $values) {
622 $columnNames = implode(',', $values);
623 $sql[$tableName] = "
624 SELECT '{$groupTitle[$tableName]}' as groupTitle, $columnNames
625 FROM $tableName
626 WHERE entity_id = %1
627 ";
628 }
629
630 $cache[$activityTypeID] = array($result, $options, $sql);
631 }
632 return $cache[$activityTypeID];
633 }
634
635 function getCreatedBy($activityID) {
636 $query = "
637 SELECT c.display_name
638 FROM civicrm_contact c,
639 civicrm_log l
640 WHERE l.entity_table = 'civicrm_activity'
641 AND l.entity_id = %1
642 AND l.modified_id = c.id
643 LIMIT 1
644 ";
645
646 $params = array(1 => array($activityID, 'Integer'));
647 return CRM_Core_DAO::singleValueQuery($query, $params);
648 }
649
650 private function redact($string, $printReport = FALSE, $replaceString = array(
651 )) {
652 if ($printReport) {
653 return CRM_Utils_String::redaction($string, $replaceString);
654 }
655 elseif ($this->_isRedact) {
656 $regexToReplaceString = CRM_Utils_String::regex($string, $this->_redactionRegexRules);
657 return CRM_Utils_String::redaction($string, array_merge($this->_redactionStringRules, $regexToReplaceString));
658 }
659 return $string;
660 }
661
662 function getCaseReport($clientID, $caseID, $activitySetName, $params, $form) {
663
664 $template = CRM_Core_Smarty::singleton();
665
666 $template->assign('caseId', $caseID);
667 $template->assign('clientID', $clientID);
668 $template->assign('activitySetName', $activitySetName);
669
670 if (CRM_Utils_Array::value('is_redact', $params)) {
671 $form->_isRedact = TRUE;
672 $template->assign('_isRedact', 'true');
673 }
674 else {
675 $form->_isRedact = FALSE;
676 $template->assign('_isRedact', 'false');
677 }
678
679 // first get all case information
680 $case = $form->caseInfo($clientID, $caseID);
681 $template->assign_by_ref('case', $case);
682
683 if ($params['include_activities'] == 1) {
684 $template->assign('includeActivities', 'All');
685 }
686 else {
687 $template->assign('includeActivities', 'Missing activities only');
688 }
689
690 $xml = $form->retrieve($case['caseTypeName']);
691
692 $activitySetNames = CRM_Case_XMLProcessor_Process::activitySets($xml->ActivitySets);
693 $pageTitle = CRM_Utils_Array::value($activitySetName, $activitySetNames);
694 $template->assign('pageTitle', $pageTitle);
695
696 if ($activitySetName) {
697 $activityTypes = $form->getActivityTypes($xml, $activitySetName);
698 }
699 else {
700 $activityTypes = CRM_Case_XMLProcessor::allActivityTypes();
701 }
702
703 if (!$activityTypes) {
704 return FALSE;
705 }
706
707 // next get activity set Informtion
708 $activitySet = array('label' => $form->getActivitySetLabel($xml, $activitySetName),
709 'includeActivities' => 'All',
710 'redact' => 'false',
711 );
712 $template->assign_by_ref('activitySet', $activitySet);
713
714 //now collect all the information about activities
715 $activities = array();
716 $form->getActivities($clientID, $caseID, $activityTypes, $activities);
717 $template->assign_by_ref('activities', $activities);
718
719 // now run the template
720 $contents = $template->fetch('CRM/Case/XMLProcessor/Report.tpl');
721 return $contents;
722 }
723
724 function printCaseReport() {
725 $caseID = CRM_Utils_Request::retrieve('caseID', 'Positive', CRM_Core_DAO::$_nullObject);
726 $clientID = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject);
727 $activitySetName = CRM_Utils_Request::retrieve('asn', 'String', CRM_Core_DAO::$_nullObject);
728 $isRedact = CRM_Utils_Request::retrieve('redact', 'Boolean', CRM_Core_DAO::$_nullObject);
729 $includeActivities = CRM_Utils_Request::retrieve('all', 'Positive', CRM_Core_DAO::$_nullObject);
730 $params = $otherRelationships = $globalGroupInfo = array();
731 $report = new CRM_Case_XMLProcessor_Report($isRedact);
732 if ($includeActivities) {
733 $params['include_activities'] = 1;
734 }
735
736 if ($isRedact) {
737 $params['is_redact'] = 1;
738 $report->_redactionStringRules = array();
739 }
740 $template = CRM_Core_Smarty::singleton();
741
742 //get case related relationships (Case Role)
743 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($clientID, $caseID);
744 $caseType = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
745
746 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
747 $caseRoles = $xmlProcessor->get($caseType, 'CaseRoles');
748 foreach ($caseRelationships as $key => & $value) {
749 if (CRM_Utils_Array::value($value['relation_type'], $caseRoles)) {
750 unset($caseRoles[$value['relation_type']]);
751 }
752 if ($isRedact) {
753 if (!array_key_exists($value['name'], $report->_redactionStringRules)) {
754 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
755 array($value['name'] => 'name_' . rand(10000, 100000))
756 );
757 }
758 $value['name'] = self::redact($value['name'], TRUE, $report->_redactionStringRules);
759 if (CRM_Utils_Array::value('email', $value) &&
760 !array_key_exists($value['email'], $report->_redactionStringRules)
761 ) {
762 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
763 array($value['email'] => 'email_' . rand(10000, 100000))
764 );
765 }
766
767 $value['email'] = self::redact($value['email'], TRUE, $report->_redactionStringRules);
768
769 if (CRM_Utils_Array::value('phone', $value) &&
770 !array_key_exists($value['phone'], $report->_redactionStringRules)
771 ) {
772 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
773 array($value['phone'] => 'phone_' . rand(10000, 100000))
774 );
775 }
776 $value['phone'] = self::redact($value['phone'], TRUE, $report->_redactionStringRules);
777 }
778 }
779
780 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
781 if ($isRedact) {
782 if (!array_key_exists($caseRoles['client']['sort_name'], $report->_redactionStringRules)) {
783 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
784 array($caseRoles['client']['sort_name'] => 'name_' . rand(10000, 100000))
785 );
786 }
787 if (!array_key_exists($caseRoles['client']['display_name'], $report->_redactionStringRules)) {
788 $report->_redactionStringRules[$caseRoles['client']['display_name']] = $report->_redactionStringRules[$caseRoles['client']['sort_name']];
789 }
790 $caseRoles['client']['sort_name'] = self::redact($caseRoles['client']['sort_name'], TRUE, $report->_redactionStringRules);
791 if (CRM_Utils_Array::value('email', $caseRoles['client']) &&
792 !array_key_exists($caseRoles['client']['email'], $report->_redactionStringRules)
793 ) {
794 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
795 array($caseRoles['client']['email'] => 'email_' . rand(10000, 100000))
796 );
797 }
798 $caseRoles['client']['email'] = self::redact($caseRoles['client']['email'], TRUE, $report->_redactionStringRules);
799
800 if (CRM_Utils_Array::value('phone', $caseRoles['client']) &&
801 !array_key_exists($caseRoles['client']['phone'], $report->_redactionStringRules)
802 ) {
803 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
804 array($caseRoles['client']['phone'] => 'phone_' . rand(10000, 100000))
805 );
806 }
807 $caseRoles['client']['phone'] = self::redact($caseRoles['client']['phone'], TRUE, $report->_redactionStringRules);
808 }
809
810 // Retrieve ALL client relationships
811 $relClient = CRM_Contact_BAO_Relationship::getRelationship($clientID,
812 CRM_Contact_BAO_Relationship::CURRENT,
813 0, 0, 0, NULL, NULL, FALSE
814 );
815 foreach ($relClient as $r) {
816 if ($isRedact) {
817 if (!array_key_exists($r['name'], $report->_redactionStringRules)) {
818 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
819 array($r['name'] => 'name_' . rand(10000, 100000))
820 );
821 }
822 if (!array_key_exists($r['display_name'], $report->_redactionStringRules)) {
823 $report->_redactionStringRules[$r['display_name']] = $report->_redactionStringRules[$r['name']];
824 }
825 $r['name'] = self::redact($r['name'], TRUE, $report->_redactionStringRules);
826
827 if (CRM_Utils_Array::value('phone', $r) &&
828 !array_key_exists($r['phone'], $report->_redactionStringRules)
829 ) {
830 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
831 array($r['phone'] => 'phone_' . rand(10000, 100000))
832 );
833 }
834 $r['phone'] = self::redact($r['phone'], TRUE, $report->_redactionStringRules);
835
836 if (CRM_Utils_Array::value('email', $r) &&
837 !array_key_exists($r['email'], $report->_redactionStringRules)
838 ) {
839 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
840 array($r['email'] => 'email_' . rand(10000, 100000))
841 );
842 }
843 $r['email'] = self::redact($r['email'], TRUE, $report->_redactionStringRules);
844 }
845 if (!array_key_exists($r['id'], $caseRelationships)) {
846 $otherRelationships[] = $r;
847 }
848 }
849
850 // Now global contact list that appears on all cases.
851 $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo);
852 foreach ($relGlobal as & $r) {
853 if ($isRedact) {
854 if (!array_key_exists($r['sort_name'], $report->_redactionStringRules)) {
855 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
856 array($r['sort_name'] => 'name_' . rand(10000, 100000))
857 );
858 }
859 if (!array_key_exists($r['display_name'], $report->_redactionStringRules)) {
860 $report->_redactionStringRules[$r['display_name']] = $report->_redactionStringRules[$r['sort_name']];
861 }
862
863 $r['sort_name'] = self::redact($r['sort_name'], TRUE, $report->_redactionStringRules);
864
865 if (CRM_Utils_Array::value('phone', $r) &&
866 !array_key_exists($r['phone'], $report->_redactionStringRules)
867 ) {
868 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
869 array($r['phone'] => 'phone_' . rand(10000, 100000))
870 );
871 }
872 $r['phone'] = self::redact($r['phone'], TRUE, $report->_redactionStringRules);
873
874 if (CRM_Utils_Array::value('email', $r) &&
875 !array_key_exists($r['email'], $report->_redactionStringRules)
876 ) {
877 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
878 array($r['email'] => 'email_' . rand(10000, 100000))
879 );
880 }
881 $r['email'] = self::redact($r['email'], TRUE, $report->_redactionStringRules);
882 }
883 }
884
885 $template->assign('caseRelationships', $caseRelationships);
886 $template->assign('caseRoles', $caseRoles);
887 $template->assign('otherRelationships', $otherRelationships);
888 $template->assign('globalRelationships', $relGlobal);
889 $template->assign('globalGroupInfo', $globalGroupInfo);
890 $contents = self::getCaseReport($clientID,
891 $caseID,
892 $activitySetName,
893 $params,
894 $report
895 );
896 $printReport = CRM_Case_Audit_Audit::run($contents, $clientID, $caseID, TRUE);
897 echo $printReport;
898 CRM_Utils_System::civiExit();
899 }
900 }
901