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