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