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