Merge pull request #4893 from colemanw/INFRA-132
[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', '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(
98 $clientID,
99 $caseID
100 ) {
101 $case = $this->_redactionRegexRules = array();
102
103 if (empty($this->_redactionStringRules)) {
104 $this->_redactionStringRules = array();
105 }
106
107 if ($this->_isRedact == 1) {
108 $this->getRedactionRules();
109 }
110
111 $client = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'display_name');
112
113 // add Client to the strings to be redacted across the case session
114 if (!array_key_exists($client, $this->_redactionStringRules)) {
115 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
116 array($client => 'name_' . rand(10000, 100000))
117 );
118 $clientSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'sort_name');
119 if (!array_key_exists($clientSortName, $this->_redactionStringRules)) {
120 $this->_redactionStringRules[$clientSortName] = $this->_redactionStringRules[$client];
121 }
122 }
123
124 $case['clientName'] = $this->redact($client);
125
126 $dao = new CRM_Case_DAO_Case();
127 $dao->id = $caseID;
128 if ($dao->find(TRUE)) {
129 $case['subject'] = $dao->subject;
130 $case['start_date'] = $dao->start_date;
131 $case['end_date'] = $dao->end_date;
132 // FIXME: when we resolve if case_type_is single or multi-select
133 if (strpos($dao->case_type_id, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) {
134 $caseTypeID = substr($dao->case_type_id, 1, -1);
135 }
136 else {
137 $caseTypeID = $dao->case_type_id;
138 }
139 $caseTypeIDs = explode(CRM_Core_DAO::VALUE_SEPARATOR,
140 $dao->case_type_id
141 );
142
143 $case['caseType'] = CRM_Case_BAO_Case::getCaseType($caseID);
144 $case['caseTypeName'] = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
145 $case['status'] = CRM_Core_OptionGroup::getLabel('case_status', $dao->status_id, FALSE);
146 }
147 return $case;
148 }
149
150 /**
151 * @param $xml
152 * @param string $activitySetName
153 *
154 * @return array|bool
155 */
156 public function getActivityTypes($xml, $activitySetName) {
157 foreach ($xml->ActivitySets as $activitySetsXML) {
158 foreach ($activitySetsXML->ActivitySet as $activitySetXML) {
159 if ((string ) $activitySetXML->name == $activitySetName) {
160 $activityTypes = array();
161 $allActivityTypes = &$this->allActivityTypes();
162 foreach ($activitySetXML->ActivityTypes as $activityTypesXML) {
163 foreach ($activityTypesXML as $activityTypeXML) {
164 $activityTypeName = (string ) $activityTypeXML->name;
165 $activityTypeInfo = CRM_Utils_Array::value($activityTypeName, $allActivityTypes);
166 if ($activityTypeInfo) {
167 $activityTypes[$activityTypeInfo['id']] = $activityTypeInfo;
168 }
169 }
170 }
171 return empty($activityTypes) ? FALSE : $activityTypes;
172 }
173 }
174 }
175 return FALSE;
176 }
177
178 /**
179 * @param $xml
180 * @param string $activitySetName
181 *
182 * @return null|string
183 */
184 public function getActivitySetLabel($xml, $activitySetName) {
185 foreach ($xml->ActivitySets as $activitySetsXML) {
186 foreach ($activitySetsXML->ActivitySet as $activitySetXML) {
187 if ((string ) $activitySetXML->name == $activitySetName) {
188 return (string ) $activitySetXML->label;
189 }
190 }
191 }
192 return NULL;
193 }
194
195 /**
196 * @param int $clientID
197 * @param int $caseID
198 * @param $activityTypes
199 * @param $activities
200 */
201 public function getActivities($clientID, $caseID, $activityTypes, &$activities) {
202 // get all activities for this case that in this activityTypes set
203 foreach ($activityTypes as $aType) {
204 $map[$aType['id']] = $aType;
205 }
206
207 // get all core activities
208 $coreActivityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE);
209
210 foreach ($coreActivityTypes as $aType) {
211 $map[$aType['id']] = $aType;
212 }
213
214 $activityTypeIDs = implode(',', array_keys($map));
215 $query = "
216 SELECT a.*, c.id as caseID
217 FROM civicrm_activity a,
218 civicrm_case c,
219 civicrm_case_activity ac
220 WHERE a.is_current_revision = 1
221 AND a.is_deleted =0
222 AND a.activity_type_id IN ( $activityTypeIDs )
223 AND c.id = ac.case_id
224 AND a.id = ac.activity_id
225 AND ac.case_id = %1
226 ";
227
228 $params = array(1 => array($caseID, 'Integer'));
229 $dao = CRM_Core_DAO::executeQuery($query, $params);
230 while ($dao->fetch()) {
231 $activityTypeInfo = $map[$dao->activity_type_id];
232 $activities[] = $this->getActivity($clientID,
233 $dao,
234 $activityTypeInfo
235 );
236 }
237 }
238
239 /**
240 * @param int $clientID
241 * @param int $activityID
242 * @param bool $anyActivity
243 * @param int $redact
244 *
245 * @return mixed
246 */
247 public function &getActivityInfo($clientID, $activityID, $anyActivity = FALSE, $redact = 0) {
248 static $activityInfos = array();
249 if ($redact) {
250 $this->_isRedact = 1;
251 $this->getRedactionRules();
252 }
253
254 $index = $activityID . '_' . (int) $anyActivity;
255
256 if ($clientID) {
257 $index = $index . '_' . $clientID;
258 }
259
260 if (!array_key_exists($index, $activityInfos)) {
261 $activityInfos[$index] = array();
262 $selectCaseActivity = "";
263 $joinCaseActivity = "";
264
265 if ($clientID) {
266 $selectCaseActivity = ", ca.case_id as caseID ";
267 $joinCaseActivity = " INNER JOIN civicrm_case_activity ca ON a.id = ca.activity_id ";
268 }
269
270 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
271 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
272 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
273
274 $query = "
275 SELECT a.*, aa.contact_id as assigneeID, at.contact_id as targetID
276 {$selectCaseActivity}
277 FROM civicrm_activity a
278 {$joinCaseActivity}
279 LEFT JOIN civicrm_activity_contact at ON a.id = at.activity_id AND at.record_type_id = $targetID
280 LEFT JOIN civicrm_activity_contact aa ON a.id = aa.activity_id AND aa.record_type_id = $assigneeID
281 WHERE a.id = %1
282 ";
283 $params = array(1 => array($activityID, 'Integer'));
284 $dao = CRM_Core_DAO::executeQuery($query, $params);
285
286 if ($dao->fetch()) {
287 //if activity type is email get info of all activities.
288 if ($dao->activity_type_id == CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name')) {
289 $anyActivity = TRUE;
290 }
291 $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, $anyActivity);
292 $activityTypeInfo = NULL;
293
294 if (isset($activityTypes[$dao->activity_type_id])) {
295 $activityTypeInfo = $activityTypes[$dao->activity_type_id];
296 }
297 if ($activityTypeInfo) {
298 $activityInfos[$index] = $this->getActivity($clientID, $dao, $activityTypeInfo);
299 }
300 }
301 }
302
303 return $activityInfos[$index];
304 }
305
306 /**
307 * @param int $clientID
308 * @param $activityDAO
309 * @param $activityTypeInfo
310 *
311 * @return array
312 */
313 public function &getActivity($clientID, $activityDAO, &$activityTypeInfo) {
314 if (empty($this->_redactionStringRules)) {
315 $this->_redactionStringRules = array();
316 }
317
318 $activity = array();
319 $activity['fields'] = array();
320 if ($clientID) {
321 $clientID = CRM_Utils_Type::escape($clientID, 'Integer');
322 if (!in_array($activityTypeInfo['name'], array(
323 'Email', 'Inbound Email'))) {
324 $activity['editURL'] = CRM_Utils_System::url('civicrm/case/activity',
325 "reset=1&cid={$clientID}&caseid={$activityDAO->caseID}&action=update&atype={$activityDAO->activity_type_id}&id={$activityDAO->id}"
326 );
327 }
328 else {
329 $activity['editURL'] = '';
330 }
331
332 $client = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'display_name');
333 // add Client SortName as well as Display to the strings to be redacted across the case session
334 // suffixed with a randomly generated 4-digit number
335 if (!array_key_exists($client, $this->_redactionStringRules)) {
336 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
337 array($client => 'name_' . rand(10000, 100000))
338 );
339
340 $clientSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $clientID, 'sort_name');
341 if (!array_key_exists($clientSortName, $this->_redactionStringRules)) {
342 $this->_redactionStringRules[$clientSortName] = $this->_redactionStringRules[$client];
343 }
344 }
345
346 $activity['fields'][] = array(
347 'label' => 'Client',
348 'value' => $this->redact($client),
349 'type' => 'String',
350 );
351 }
352
353 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
354 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
355 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
356 if (!empty($activityDAO->targetID)) {
357 // Re-lookup the target ID since the DAO only has the first recipient if there are multiple.
358 // Maybe not the best solution.
359 $targetNames = CRM_Activity_BAO_ActivityContact::getNames($activityDAO->id, $targetID);
360 $processTarget = FALSE;
361 $label = ts('With Contact(s)');
362 if (in_array($activityTypeInfo['name'], array('Email', 'Inbound Email'))) {
363 $processTarget = TRUE;
364 $label = ts('Recipient');
365 }
366 if (!$processTarget) {
367 foreach ($targetNames as $targetID => $targetName) {
368 if ($targetID != $clientID) {
369 $processTarget = TRUE;
370 break;
371 }
372 }
373 }
374
375 if ($processTarget) {
376 $targetRedacted = array();
377 foreach ($targetNames as $targetID => $target) {
378 // add Recipient SortName as well as Display to the strings to be redacted across the case session
379 // suffixed with a randomly generated 4-digit number
380 if (!array_key_exists($target, $this->_redactionStringRules)) {
381 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
382 array($target => 'name_' . rand(10000, 100000))
383 );
384 $targetSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $targetID, 'sort_name');
385 if (!array_key_exists($targetSortName, $this->_redactionStringRules)) {
386 $this->_redactionStringRules[$targetSortName] = $this->_redactionStringRules[$target];
387 }
388 }
389 $targetRedacted[] = $this->redact($target);
390 }
391
392 $activity['fields'][] = array(
393 'label' => $label,
394 'value' => implode('; ', $targetRedacted),
395 'type' => 'String',
396 );
397 }
398 }
399
400 // Activity Type info is a special field
401 $activity['fields'][] = array(
402 'label' => ts('Activity Type'),
403 'value' => $activityTypeInfo['label'],
404 'type' => 'String',
405 );
406
407 $activity['fields'][] = array(
408 'label' => ts('Subject'),
409 'value' => htmlspecialchars($this->redact($activityDAO->subject)),
410 'type' => 'Memo',
411 );
412
413 $creator = $this->getCreatedBy($activityDAO->id);
414 // add Creator to the strings to be redacted across the case session
415 if (!array_key_exists($creator, $this->_redactionStringRules)) {
416 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
417 array($creator => 'name_' . rand(10000, 100000))
418 );
419 }
420 $activity['fields'][] = array(
421 'label' => ts('Created By'),
422 'value' => $this->redact($creator),
423 'type' => 'String',
424 );
425 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
426 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
427 $source_contact_id = CRM_Activity_BAO_Activity::getActivityContact($activityDAO->id, $sourceID);
428 $reporter = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
429 $source_contact_id,
430 'display_name'
431 );
432
433 // add Reporter SortName as well as Display to the strings to be redacted across the case session
434 // suffixed with a randomly generated 4-digit number
435 if (!array_key_exists($reporter, $this->_redactionStringRules)) {
436 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
437 array($reporter => 'name_' . rand(10000, 100000))
438 );
439
440 $reporterSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
441 $source_contact_id,
442 'sort_name'
443 );
444 if (!array_key_exists($reporterSortName, $this->_redactionStringRules)) {
445 $this->_redactionStringRules[$reporterSortName] = $this->_redactionStringRules[$reporter];
446 }
447 }
448
449 $activity['fields'][] = array(
450 'label' => ts('Reported By'),
451 'value' => $this->redact($reporter),
452 'type' => 'String',
453 );
454
455 if (!empty($activityDAO->assigneeID)) {
456 //allow multiple assignee contacts.CRM-4503.
457 $assignee_contact_names = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames(array($activityDAO->id), TRUE);
458
459 foreach ($assignee_contact_names as & $assignee) {
460 // add Assignee to the strings to be redacted across the case session
461 $this->_redactionStringRules = CRM_Utils_Array::crmArrayMerge($this->_redactionStringRules,
462 array($assignee => 'name_' . rand(10000, 100000))
463 );
464 $assignee = $this->redact($assignee);
465 }
466 $assigneeContacts = implode(', ', $assignee_contact_names);
467 $activity['fields'][] = array(
468 'label' => ts('Assigned to'),
469 'value' => $assigneeContacts,
470 'type' => 'String',
471 );
472 }
473
474 if ($activityDAO->medium_id) {
475 $activity['fields'][] = array(
476 'label' => ts('Medium'),
477 'value' => CRM_Core_OptionGroup::getLabel('encounter_medium',
478 $activityDAO->medium_id, FALSE
479 ),
480 'type' => 'String',
481 );
482 }
483
484 $activity['fields'][] = array(
485 'label' => ts('Location'),
486 'value' => $activityDAO->location,
487 'type' => 'String',
488 );
489
490 $activity['fields'][] = array(
491 'label' => ts('Date and Time'),
492 'value' => $activityDAO->activity_date_time,
493 'type' => 'Date',
494 );
495
496 $activity['fields'][] = array(
497 'label' => ts('Details'),
498 'value' => $this->redact(CRM_Utils_String::stripAlternatives($activityDAO->details)),
499 'type' => 'Memo',
500 );
501
502 // Skip Duration field if empty (to avoid " minutes" output). Might want to do this for all fields at some point. dgg
503 if ($activityDAO->duration) {
504 $activity['fields'][] = array(
505 'label' => ts('Duration'),
506 'value' => $activityDAO->duration . ' ' . ts('minutes'),
507 'type' => 'Int',
508 );
509 }
510 $activity['fields'][] = array(
511 'label' => ts('Status'),
512 'value' => CRM_Core_OptionGroup::getLabel('activity_status',
513 $activityDAO->status_id
514 ),
515 'type' => 'String',
516 );
517
518 $activity['fields'][] = array(
519 'label' => ts('Priority'),
520 'value' => CRM_Core_OptionGroup::getLabel('priority',
521 $activityDAO->priority_id
522 ),
523 'type' => 'String',
524 );
525
526 //for now empty custom groups
527 $activity['customGroups'] = $this->getCustomData($clientID,
528 $activityDAO,
529 $activityTypeInfo
530 );
531
532 return $activity;
533 }
534
535 /**
536 * @param int $clientID
537 * @param $activityDAO
538 * @param $activityTypeInfo
539 *
540 * @return array|null
541 */
542 public function getCustomData($clientID, $activityDAO, &$activityTypeInfo) {
543 list($typeValues, $options, $sql) = $this->getActivityTypeCustomSQL($activityTypeInfo['id'], '%Y-%m-%d');
544
545 $params = array(1 => array($activityDAO->id, 'Integer'));
546
547 $customGroups = array();
548 foreach ($sql as $tableName => $sqlClause) {
549 $dao = CRM_Core_DAO::executeQuery($sqlClause, $params);
550 if ($dao->fetch()) {
551 $customGroup = array();
552 foreach ($typeValues[$tableName] as $columnName => $typeValue) {
553 $value = CRM_Core_BAO_CustomField::getDisplayValue($dao->$columnName,
554 $typeValue['fieldID'],
555 $options
556 );
557
558 if (CRM_Utils_Array::value('type', $typeValue) == 'Date') {
559 $value = $dao->$columnName;
560 }
561
562 if ($value) {
563 // Note: this is already taken care in getDisplayValue above, but sometimes
564 // strings like '^A^A' creates problem. So to fix this special case -
565 if (strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) {
566 $value = trim($value, CRM_Core_DAO::VALUE_SEPARATOR);
567 }
568 if (CRM_Utils_Array::value('type', $typeValue) == 'String' ||
569 CRM_Utils_Array::value('type', $typeValue) == 'Memo'
570 ) {
571 $value = $this->redact($value);
572 }
573 elseif (CRM_Utils_Array::value('type', $typeValue) == 'File') {
574 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $typeValue, 'entity_table');
575 $value = CRM_Core_BAO_File::attachmentInfo($tableName, $activityDAO->id);
576 }
577 elseif (CRM_Utils_Array::value('type', $typeValue) == 'Link') {
578 $value = CRM_Utils_System::formatWikiURL($value);
579 }
580 }
581 //$typeValue
582 $customGroup[] = array(
583 'label' => $typeValue['label'],
584 'value' => $value,
585 'type' => $typeValue['type'],
586 );
587 }
588 $customGroups[$dao->groupTitle] = $customGroup;
589 }
590 }
591
592 return empty($customGroups) ? NULL : $customGroups;
593 }
594
595 /**
596 * @param int $activityTypeID
597 * @param null $dateFormat
598 *
599 * @return mixed
600 */
601 public function getActivityTypeCustomSQL($activityTypeID, $dateFormat = NULL) {
602 static $cache = array();
603
604 if (is_null($activityTypeID)) {
605 $activityTypeID = 0;
606 }
607
608 if (!isset($cache[$activityTypeID])) {
609 $query = "
610 SELECT cg.title as groupTitle,
611 cg.table_name as tableName ,
612 cf.column_name as columnName,
613 cf.label as label ,
614 cg.id as groupID ,
615 cf.id as fieldID ,
616 cf.data_type as dataType ,
617 cf.html_type as htmlType ,
618 cf.option_group_id as optionGroupID
619 FROM civicrm_custom_group cg,
620 civicrm_custom_field cf
621 WHERE cf.custom_group_id = cg.id
622 AND cg.extends = 'Activity'";
623
624 if ($activityTypeID) {
625 $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 . "%' )";
626 }
627 else {
628 $query .= "AND cg.extends_entity_column_value IS NULL";
629 }
630 $query .= "ORDER BY cg.weight, cf.weight";
631 $params = array(
632 1 => array($activityTypeID,
633 'Integer',
634 ));
635 $dao = CRM_Core_DAO::executeQuery($query, $params);
636
637 $result = $options = $sql = $groupTitle = array();
638 while ($dao->fetch()) {
639 if (!array_key_exists($dao->tableName, $result)) {
640 $result[$dao->tableName] = array();
641 $sql[$dao->tableName] = array();
642 }
643 $result[$dao->tableName][$dao->columnName] = array(
644 'label' => $dao->label,
645 'type' => $dao->dataType,
646 'fieldID' => $dao->fieldID,
647 );
648
649 $options[$dao->fieldID] = array();
650 $options[$dao->fieldID]['attributes'] = array(
651 'label' => $dao->label,
652 'data_type' => $dao->dataType,
653 'html_type' => $dao->htmlType,
654 );
655 // since we want to add ISO date format.
656 if ($dateFormat && $dao->htmlType == 'Select Date') {
657 $options[$dao->fieldID]['attributes']['format'] = $dateFormat;
658 }
659 if ($dao->optionGroupID) {
660 $query = "
661 SELECT label, value
662 FROM civicrm_option_value
663 WHERE option_group_id = {$dao->optionGroupID}
664 ";
665
666 $option = CRM_Core_DAO::executeQuery($query);
667 while ($option->fetch()) {
668 $dataType = $dao->dataType;
669 if ($dataType == 'Int' || $dataType == 'Float') {
670 $num = round($option->value, 2);
671 $options[$dao->fieldID]["$num"] = $option->label;
672 }
673 else {
674 $options[$dao->fieldID][$option->value] = $option->label;
675 }
676 }
677 }
678
679 $sql[$dao->tableName][] = $dao->columnName;
680 $groupTitle[$dao->tableName] = $dao->groupTitle;
681 }
682
683 foreach ($sql as $tableName => $values) {
684 $columnNames = implode(',', $values);
685 $sql[$tableName] = "
686 SELECT '{$groupTitle[$tableName]}' as groupTitle, $columnNames
687 FROM $tableName
688 WHERE entity_id = %1
689 ";
690 }
691
692 $cache[$activityTypeID] = array($result, $options, $sql);
693 }
694 return $cache[$activityTypeID];
695 }
696
697 /**
698 * @param int $activityID
699 *
700 * @return null|string
701 */
702 public function getCreatedBy($activityID) {
703 $query = "
704 SELECT c.display_name
705 FROM civicrm_contact c,
706 civicrm_log l
707 WHERE l.entity_table = 'civicrm_activity'
708 AND l.entity_id = %1
709 AND l.modified_id = c.id
710 LIMIT 1
711 ";
712
713 $params = array(1 => array($activityID, 'Integer'));
714 return CRM_Core_DAO::singleValueQuery($query, $params);
715 }
716
717 /**
718 * @param $string
719 * @param bool $printReport
720 * @param array $replaceString
721 *
722 * @return mixed
723 */
724 private function redact($string, $printReport = FALSE, $replaceString = array()) {
725 if ($printReport) {
726 return CRM_Utils_String::redaction($string, $replaceString);
727 }
728 elseif ($this->_isRedact) {
729 $regexToReplaceString = CRM_Utils_String::regex($string, $this->_redactionRegexRules);
730 return CRM_Utils_String::redaction($string, array_merge($this->_redactionStringRules, $regexToReplaceString));
731 }
732 return $string;
733 }
734
735 /**
736 * @param int $clientID
737 * @param int $caseID
738 * @param string $activitySetName
739 * @param array $params
740 * @param CRM_Core_Form $form
741 *
742 * @return mixed
743 */
744 public static function getCaseReport($clientID, $caseID, $activitySetName, $params, $form) {
745
746 $template = CRM_Core_Smarty::singleton();
747
748 $template->assign('caseId', $caseID);
749 $template->assign('clientID', $clientID);
750 $template->assign('activitySetName', $activitySetName);
751
752 if (!empty($params['is_redact'])) {
753 $form->_isRedact = TRUE;
754 $template->assign('_isRedact', 'true');
755 }
756 else {
757 $form->_isRedact = FALSE;
758 $template->assign('_isRedact', 'false');
759 }
760
761 // first get all case information
762 $case = $form->caseInfo($clientID, $caseID);
763 $template->assign_by_ref('case', $case);
764
765 if ($params['include_activities'] == 1) {
766 $template->assign('includeActivities', 'All');
767 }
768 else {
769 $template->assign('includeActivities', 'Missing activities only');
770 }
771
772 $xml = $form->retrieve($case['caseTypeName']);
773
774 $activitySetNames = CRM_Case_XMLProcessor_Process::activitySets($xml->ActivitySets);
775 $pageTitle = CRM_Utils_Array::value($activitySetName, $activitySetNames);
776 $template->assign('pageTitle', $pageTitle);
777
778 if ($activitySetName) {
779 $activityTypes = $form->getActivityTypes($xml, $activitySetName);
780 }
781 else {
782 $activityTypes = CRM_Case_XMLProcessor::allActivityTypes();
783 }
784
785 if (!$activityTypes) {
786 return FALSE;
787 }
788
789 // next get activity set Informtion
790 $activitySet = array(
791 '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 }