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