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