Merge pull request #5278 from kurund/CRM-15756
[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 'fieldID' => $typeValue['fieldID'],
592 );
593 }
594 $customGroups[$dao->groupTitle] = $customGroup;
595 }
596 }
597
598 return empty($customGroups) ? NULL : $customGroups;
599 }
600
601 /**
602 * @param int $activityTypeID
603 * @param null $dateFormat
604 *
605 * @return mixed
606 */
607 public function getActivityTypeCustomSQL($activityTypeID, $dateFormat = NULL) {
608 static $cache = array();
609
610 if (is_null($activityTypeID)) {
611 $activityTypeID = 0;
612 }
613
614 if (!isset($cache[$activityTypeID])) {
615 $query = "
616 SELECT cg.title as groupTitle,
617 cg.table_name as tableName ,
618 cf.column_name as columnName,
619 cf.label as label ,
620 cg.id as groupID ,
621 cf.id as fieldID ,
622 cf.data_type as dataType ,
623 cf.html_type as htmlType ,
624 cf.option_group_id as optionGroupID
625 FROM civicrm_custom_group cg,
626 civicrm_custom_field cf
627 WHERE cf.custom_group_id = cg.id
628 AND cg.extends = 'Activity'";
629
630 if ($activityTypeID) {
631 $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 . "%' )";
632 }
633 else {
634 $query .= "AND cg.extends_entity_column_value IS NULL";
635 }
636 $query .= "ORDER BY cg.weight, cf.weight";
637 $params = array(
638 1 => array(
639 $activityTypeID,
640 'Integer',
641 ),
642 );
643 $dao = CRM_Core_DAO::executeQuery($query, $params);
644
645 $result = $options = $sql = $groupTitle = array();
646 while ($dao->fetch()) {
647 if (!array_key_exists($dao->tableName, $result)) {
648 $result[$dao->tableName] = array();
649 $sql[$dao->tableName] = array();
650 }
651 $result[$dao->tableName][$dao->columnName] = array(
652 'label' => $dao->label,
653 'type' => $dao->dataType,
654 'fieldID' => $dao->fieldID,
655 );
656
657 $options[$dao->fieldID] = array();
658 $options[$dao->fieldID]['attributes'] = array(
659 'label' => $dao->label,
660 'data_type' => $dao->dataType,
661 'html_type' => $dao->htmlType,
662 );
663 // since we want to add ISO date format.
664 if ($dateFormat && $dao->htmlType == 'Select Date') {
665 $options[$dao->fieldID]['attributes']['format'] = $dateFormat;
666 }
667 if ($dao->optionGroupID) {
668 $query = "
669 SELECT label, value
670 FROM civicrm_option_value
671 WHERE option_group_id = {$dao->optionGroupID}
672 ";
673
674 $option = CRM_Core_DAO::executeQuery($query);
675 while ($option->fetch()) {
676 $dataType = $dao->dataType;
677 if ($dataType == 'Int' || $dataType == 'Float') {
678 $num = round($option->value, 2);
679 $options[$dao->fieldID]["$num"] = $option->label;
680 }
681 else {
682 $options[$dao->fieldID][$option->value] = $option->label;
683 }
684 }
685 }
686
687 $sql[$dao->tableName][] = $dao->columnName;
688 $groupTitle[$dao->tableName] = $dao->groupTitle;
689 }
690
691 foreach ($sql as $tableName => $values) {
692 $columnNames = implode(',', $values);
693 $sql[$tableName] = "
694 SELECT '{$groupTitle[$tableName]}' as groupTitle, $columnNames
695 FROM $tableName
696 WHERE entity_id = %1
697 ";
698 }
699
700 $cache[$activityTypeID] = array($result, $options, $sql);
701 }
702 return $cache[$activityTypeID];
703 }
704
705 /**
706 * @param int $activityID
707 *
708 * @return null|string
709 */
710 public function getCreatedBy($activityID) {
711 $query = "
712 SELECT c.display_name
713 FROM civicrm_contact c,
714 civicrm_log l
715 WHERE l.entity_table = 'civicrm_activity'
716 AND l.entity_id = %1
717 AND l.modified_id = c.id
718 LIMIT 1
719 ";
720
721 $params = array(1 => array($activityID, 'Integer'));
722 return CRM_Core_DAO::singleValueQuery($query, $params);
723 }
724
725 /**
726 * @param $string
727 * @param bool $printReport
728 * @param array $replaceString
729 *
730 * @return mixed
731 */
732 private function redact($string, $printReport = FALSE, $replaceString = array()) {
733 if ($printReport) {
734 return CRM_Utils_String::redaction($string, $replaceString);
735 }
736 elseif ($this->_isRedact) {
737 $regexToReplaceString = CRM_Utils_String::regex($string, $this->_redactionRegexRules);
738 return CRM_Utils_String::redaction($string, array_merge($this->_redactionStringRules, $regexToReplaceString));
739 }
740 return $string;
741 }
742
743 /**
744 * @param int $clientID
745 * @param int $caseID
746 * @param string $activitySetName
747 * @param array $params
748 * @param CRM_Core_Form $form
749 *
750 * @return mixed
751 */
752 public static function getCaseReport($clientID, $caseID, $activitySetName, $params, $form) {
753
754 $template = CRM_Core_Smarty::singleton();
755
756 $template->assign('caseId', $caseID);
757 $template->assign('clientID', $clientID);
758 $template->assign('activitySetName', $activitySetName);
759
760 if (!empty($params['is_redact'])) {
761 $form->_isRedact = TRUE;
762 $template->assign('_isRedact', 'true');
763 }
764 else {
765 $form->_isRedact = FALSE;
766 $template->assign('_isRedact', 'false');
767 }
768
769 // first get all case information
770 $case = $form->caseInfo($clientID, $caseID);
771 $template->assign_by_ref('case', $case);
772
773 if ($params['include_activities'] == 1) {
774 $template->assign('includeActivities', 'All');
775 }
776 else {
777 $template->assign('includeActivities', 'Missing activities only');
778 }
779
780 $xml = $form->retrieve($case['caseTypeName']);
781
782 $activitySetNames = CRM_Case_XMLProcessor_Process::activitySets($xml->ActivitySets);
783 $pageTitle = CRM_Utils_Array::value($activitySetName, $activitySetNames);
784 $template->assign('pageTitle', $pageTitle);
785
786 if ($activitySetName) {
787 $activityTypes = $form->getActivityTypes($xml, $activitySetName);
788 }
789 else {
790 $activityTypes = CRM_Case_XMLProcessor::allActivityTypes();
791 }
792
793 if (!$activityTypes) {
794 return FALSE;
795 }
796
797 // next get activity set Informtion
798 $activitySet = array(
799 'label' => $form->getActivitySetLabel($xml, $activitySetName),
800 'includeActivities' => 'All',
801 'redact' => 'false',
802 );
803 $template->assign_by_ref('activitySet', $activitySet);
804
805 //now collect all the information about activities
806 $activities = array();
807 $form->getActivities($clientID, $caseID, $activityTypes, $activities);
808 $template->assign_by_ref('activities', $activities);
809
810 // now run the template
811 $contents = $template->fetch('CRM/Case/XMLProcessor/Report.tpl');
812 return $contents;
813 }
814
815 public static function printCaseReport() {
816 $caseID = CRM_Utils_Request::retrieve('caseID', 'Positive', CRM_Core_DAO::$_nullObject);
817 $clientID = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject);
818 $activitySetName = CRM_Utils_Request::retrieve('asn', 'String', CRM_Core_DAO::$_nullObject);
819 $isRedact = CRM_Utils_Request::retrieve('redact', 'Boolean', CRM_Core_DAO::$_nullObject);
820 $includeActivities = CRM_Utils_Request::retrieve('all', 'Positive', CRM_Core_DAO::$_nullObject);
821 $params = $otherRelationships = $globalGroupInfo = array();
822 $report = new CRM_Case_XMLProcessor_Report($isRedact);
823 if ($includeActivities) {
824 $params['include_activities'] = 1;
825 }
826
827 if ($isRedact) {
828 $params['is_redact'] = 1;
829 $report->_redactionStringRules = array();
830 }
831 $template = CRM_Core_Smarty::singleton();
832
833 //get case related relationships (Case Role)
834 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($clientID, $caseID);
835 $caseType = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
836
837 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
838 $caseRoles = $xmlProcessor->get($caseType, 'CaseRoles');
839 foreach ($caseRelationships as $key => & $value) {
840 if (!empty($caseRoles[$value['relation_type']])) {
841 unset($caseRoles[$value['relation_type']]);
842 }
843 if ($isRedact) {
844 if (!array_key_exists($value['name'], $report->_redactionStringRules)) {
845 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
846 array($value['name'] => 'name_' . rand(10000, 100000))
847 );
848 }
849 $value['name'] = self::redact($value['name'], TRUE, $report->_redactionStringRules);
850 if (!empty($value['email']) &&
851 !array_key_exists($value['email'], $report->_redactionStringRules)
852 ) {
853 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
854 array($value['email'] => 'email_' . rand(10000, 100000))
855 );
856 }
857
858 $value['email'] = self::redact($value['email'], TRUE, $report->_redactionStringRules);
859
860 if (!empty($value['phone']) &&
861 !array_key_exists($value['phone'], $report->_redactionStringRules)
862 ) {
863 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
864 array($value['phone'] => 'phone_' . rand(10000, 100000))
865 );
866 }
867 $value['phone'] = self::redact($value['phone'], TRUE, $report->_redactionStringRules);
868 }
869 }
870
871 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
872 if ($isRedact) {
873 if (!array_key_exists($caseRoles['client']['sort_name'], $report->_redactionStringRules)) {
874 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
875 array($caseRoles['client']['sort_name'] => 'name_' . rand(10000, 100000))
876 );
877 }
878 if (!array_key_exists($caseRoles['client']['display_name'], $report->_redactionStringRules)) {
879 $report->_redactionStringRules[$caseRoles['client']['display_name']] = $report->_redactionStringRules[$caseRoles['client']['sort_name']];
880 }
881 $caseRoles['client']['sort_name'] = self::redact($caseRoles['client']['sort_name'], TRUE, $report->_redactionStringRules);
882 if (!empty($caseRoles['client']['email']) &&
883 !array_key_exists($caseRoles['client']['email'], $report->_redactionStringRules)
884 ) {
885 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
886 array($caseRoles['client']['email'] => 'email_' . rand(10000, 100000))
887 );
888 }
889 $caseRoles['client']['email'] = self::redact($caseRoles['client']['email'], TRUE, $report->_redactionStringRules);
890
891 if (!empty($caseRoles['client']['phone']) &&
892 !array_key_exists($caseRoles['client']['phone'], $report->_redactionStringRules)
893 ) {
894 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
895 array($caseRoles['client']['phone'] => 'phone_' . rand(10000, 100000))
896 );
897 }
898 $caseRoles['client']['phone'] = self::redact($caseRoles['client']['phone'], TRUE, $report->_redactionStringRules);
899 }
900
901 // Retrieve ALL client relationships
902 $relClient = CRM_Contact_BAO_Relationship::getRelationship($clientID,
903 CRM_Contact_BAO_Relationship::CURRENT,
904 0, 0, 0, NULL, NULL, FALSE
905 );
906 foreach ($relClient as $r) {
907 if ($isRedact) {
908 if (!array_key_exists($r['name'], $report->_redactionStringRules)) {
909 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
910 array($r['name'] => 'name_' . rand(10000, 100000))
911 );
912 }
913 if (!array_key_exists($r['display_name'], $report->_redactionStringRules)) {
914 $report->_redactionStringRules[$r['display_name']] = $report->_redactionStringRules[$r['name']];
915 }
916 $r['name'] = self::redact($r['name'], TRUE, $report->_redactionStringRules);
917
918 if (!empty($r['phone']) &&
919 !array_key_exists($r['phone'], $report->_redactionStringRules)
920 ) {
921 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
922 array($r['phone'] => 'phone_' . rand(10000, 100000))
923 );
924 }
925 $r['phone'] = self::redact($r['phone'], TRUE, $report->_redactionStringRules);
926
927 if (!empty($r['email']) &&
928 !array_key_exists($r['email'], $report->_redactionStringRules)
929 ) {
930 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
931 array($r['email'] => 'email_' . rand(10000, 100000))
932 );
933 }
934 $r['email'] = self::redact($r['email'], TRUE, $report->_redactionStringRules);
935 }
936 if (!array_key_exists($r['id'], $caseRelationships)) {
937 $otherRelationships[] = $r;
938 }
939 }
940
941 // Now global contact list that appears on all cases.
942 $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo);
943 foreach ($relGlobal as & $r) {
944 if ($isRedact) {
945 if (!array_key_exists($r['sort_name'], $report->_redactionStringRules)) {
946 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
947 array($r['sort_name'] => 'name_' . rand(10000, 100000))
948 );
949 }
950 if (!array_key_exists($r['display_name'], $report->_redactionStringRules)) {
951 $report->_redactionStringRules[$r['display_name']] = $report->_redactionStringRules[$r['sort_name']];
952 }
953
954 $r['sort_name'] = self::redact($r['sort_name'], TRUE, $report->_redactionStringRules);
955
956 if (!empty($r['phone']) &&
957 !array_key_exists($r['phone'], $report->_redactionStringRules)
958 ) {
959 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
960 array($r['phone'] => 'phone_' . rand(10000, 100000))
961 );
962 }
963 $r['phone'] = self::redact($r['phone'], TRUE, $report->_redactionStringRules);
964
965 if (!empty($r['email']) &&
966 !array_key_exists($r['email'], $report->_redactionStringRules)
967 ) {
968 $report->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($report->_redactionStringRules,
969 array($r['email'] => 'email_' . rand(10000, 100000))
970 );
971 }
972 $r['email'] = self::redact($r['email'], TRUE, $report->_redactionStringRules);
973 }
974 }
975
976 $template->assign('caseRelationships', $caseRelationships);
977 $template->assign('caseRoles', $caseRoles);
978 $template->assign('otherRelationships', $otherRelationships);
979 $template->assign('globalRelationships', $relGlobal);
980 $template->assign('globalGroupInfo', $globalGroupInfo);
981 $contents = self::getCaseReport($clientID,
982 $caseID,
983 $activitySetName,
984 $params,
985 $report
986 );
987 $printReport = CRM_Case_Audit_Audit::run($contents, $clientID, $caseID, TRUE);
988 echo $printReport;
989 CRM_Utils_System::civiExit();
990 }
991
992 }