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