Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | ||
36 | /** | |
d17cd024 | 37 | * This class contains the functions for Case Management |
6a488035 TO |
38 | * |
39 | */ | |
40 | class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { | |
41 | ||
42 | /** | |
100fef9d | 43 | * Static field for all the case information that we can potentially export |
6a488035 TO |
44 | * |
45 | * @var array | |
46 | * @static | |
47 | */ | |
48 | static $_exportableFields = NULL; | |
e96f025a | 49 | |
4c6ce474 EM |
50 | /** |
51 | * | |
52 | */ | |
00be9182 | 53 | public function __construct() { |
6a488035 TO |
54 | parent::__construct(); |
55 | } | |
56 | ||
57 | /** | |
077dbf5e CW |
58 | * Is CiviCase enabled? |
59 | * | |
60 | * @return bool | |
61 | */ | |
00be9182 | 62 | public static function enabled() { |
077dbf5e CW |
63 | $config = CRM_Core_Config::singleton(); |
64 | return in_array('CiviCase', $config->enableComponents); | |
65 | } | |
66 | ||
6a488035 | 67 | /** |
100fef9d | 68 | * Takes an associative array and creates a case object |
6a488035 TO |
69 | * |
70 | * the function extract all the params it needs to initialize the create a | |
71 | * case object. the params array could contain additional unused name/value | |
72 | * pairs | |
73 | * | |
64bd5a0e TO |
74 | * @param array $params |
75 | * (reference ) an assoc array of name/value pairs. | |
77b97be7 | 76 | * |
c490a46a | 77 | * @return CRM_Case_BAO_Case object |
6a488035 TO |
78 | * @static |
79 | */ | |
00be9182 | 80 | public static function add(&$params) { |
6a488035 TO |
81 | $caseDAO = new CRM_Case_DAO_Case(); |
82 | $caseDAO->copyValues($params); | |
83 | return $caseDAO->save(); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Given the list of params in the params array, fetch the object | |
88 | * and store the values in the values array | |
89 | * | |
64bd5a0e TO |
90 | * @param array $params |
91 | * Input parameters to find object. | |
92 | * @param array $values | |
93 | * Output values of the object. | |
94 | * @param array $ids | |
95 | * The array that holds all the db ids. | |
6a488035 TO |
96 | * |
97 | * @return CRM_Case_BAO_Case|null the found object or null | |
6a488035 TO |
98 | * @static |
99 | */ | |
00be9182 | 100 | public static function &getValues(&$params, &$values, &$ids) { |
6a488035 TO |
101 | $case = new CRM_Case_BAO_Case(); |
102 | ||
103 | $case->copyValues($params); | |
104 | ||
105 | if ($case->find(TRUE)) { | |
106 | $ids['case'] = $case->id; | |
107 | CRM_Core_DAO::storeValues($case, $values); | |
108 | return $case; | |
109 | } | |
110 | return NULL; | |
111 | } | |
112 | ||
113 | /** | |
100fef9d | 114 | * Takes an associative array and creates a case object |
6a488035 | 115 | * |
64bd5a0e TO |
116 | * @param array $params |
117 | * (reference ) an assoc array of name/value pairs. | |
77b97be7 | 118 | * |
c490a46a | 119 | * @return CRM_Case_BAO_Case object |
6a488035 TO |
120 | * @static |
121 | */ | |
00be9182 | 122 | public static function &create(&$params) { |
6a488035 TO |
123 | $transaction = new CRM_Core_Transaction(); |
124 | ||
a7488080 | 125 | if (!empty($params['id'])) { |
6a488035 TO |
126 | CRM_Utils_Hook::pre('edit', 'Case', $params['id'], $params); |
127 | } | |
128 | else { | |
129 | CRM_Utils_Hook::pre('create', 'Case', NULL, $params); | |
130 | } | |
131 | ||
132 | $case = self::add($params); | |
133 | ||
a7488080 | 134 | if (!empty($params['custom']) && |
6a488035 TO |
135 | is_array($params['custom']) |
136 | ) { | |
137 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_case', $case->id); | |
138 | } | |
139 | ||
140 | if (is_a($case, 'CRM_Core_Error')) { | |
141 | $transaction->rollback(); | |
142 | return $case; | |
143 | } | |
144 | ||
a7488080 | 145 | if (!empty($params['id'])) { |
6a488035 TO |
146 | CRM_Utils_Hook::post('edit', 'Case', $case->id, $case); |
147 | } | |
148 | else { | |
149 | CRM_Utils_Hook::post('create', 'Case', $case->id, $case); | |
150 | } | |
151 | $transaction->commit(); | |
152 | ||
153 | //we are not creating log for case | |
154 | //since case log can be tracked using log for activity. | |
155 | return $case; | |
156 | } | |
157 | ||
158 | /** | |
159 | * Create case contact record | |
160 | * | |
64bd5a0e | 161 | * @param array case_id, contact_id |
6a488035 TO |
162 | * |
163 | * @return object | |
6a488035 | 164 | */ |
00be9182 | 165 | public static function addCaseToContact($params) { |
6a488035 TO |
166 | $caseContact = new CRM_Case_DAO_CaseContact(); |
167 | $caseContact->case_id = $params['case_id']; | |
168 | $caseContact->contact_id = $params['contact_id']; | |
169 | $caseContact->find(TRUE); | |
170 | $caseContact->save(); | |
171 | ||
172 | // add to recently viewed | |
8ffdec17 | 173 | $caseType = CRM_Case_BAO_Case::getCaseType($caseContact->case_id); |
6a488035 TO |
174 | $url = CRM_Utils_System::url('civicrm/contact/view/case', |
175 | "action=view&reset=1&id={$caseContact->case_id}&cid={$caseContact->contact_id}&context=home" | |
176 | ); | |
177 | ||
8ffdec17 | 178 | $title = CRM_Contact_BAO_Contact::displayName($caseContact->contact_id) . ' - ' . $caseType; |
6a488035 TO |
179 | |
180 | $recentOther = array(); | |
181 | if (CRM_Core_Permission::checkActionPermission('CiviCase', CRM_Core_Action::DELETE)) { | |
182 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/case', | |
183 | "action=delete&reset=1&id={$caseContact->case_id}&cid={$caseContact->contact_id}&context=home" | |
184 | ); | |
185 | } | |
186 | ||
187 | // add the recently created case | |
188 | CRM_Utils_Recent::add($title, | |
189 | $url, | |
190 | $caseContact->case_id, | |
191 | 'Case', | |
192 | $params['contact_id'], | |
193 | NULL, | |
194 | $recentOther | |
195 | ); | |
196 | ||
197 | return $caseContact; | |
198 | } | |
199 | ||
200 | /** | |
201 | * Delet case contact record | |
202 | * | |
64bd5a0e | 203 | * @param int case_id |
6a488035 TO |
204 | * |
205 | * @return Void | |
6a488035 | 206 | */ |
00be9182 | 207 | public static function deleteCaseContact($caseID) { |
6a488035 TO |
208 | $caseContact = new CRM_Case_DAO_CaseContact(); |
209 | $caseContact->case_id = $caseID; | |
210 | $caseContact->delete(); | |
211 | ||
212 | // delete the recently created Case | |
213 | $caseRecent = array( | |
214 | 'id' => $caseID, | |
215 | 'type' => 'Case', | |
216 | ); | |
217 | CRM_Utils_Recent::del($caseRecent); | |
218 | } | |
219 | ||
220 | /** | |
221 | * This function is used to convert associative array names to values | |
222 | * and vice-versa. | |
223 | * | |
224 | * This function is used by both the web form layer and the api. Note that | |
225 | * the api needs the name => value conversion, also the view layer typically | |
226 | * requires value => name conversion | |
227 | */ | |
00be9182 | 228 | public static function lookupValue(&$defaults, $property, &$lookup, $reverse) { |
6a488035 TO |
229 | $id = $property . '_id'; |
230 | ||
231 | $src = $reverse ? $property : $id; | |
232 | $dst = $reverse ? $id : $property; | |
233 | ||
234 | if (!array_key_exists($src, $defaults)) { | |
235 | return FALSE; | |
236 | } | |
237 | ||
238 | $look = $reverse ? array_flip($lookup) : $lookup; | |
239 | ||
240 | if (is_array($look)) { | |
241 | if (!array_key_exists($defaults[$src], $look)) { | |
242 | return FALSE; | |
243 | } | |
244 | } | |
245 | $defaults[$dst] = $look[$defaults[$src]]; | |
246 | return TRUE; | |
247 | } | |
248 | ||
249 | /** | |
250 | * Takes a bunch of params that are needed to match certain criteria and | |
251 | * retrieves the relevant objects. We'll tweak this function to be more | |
252 | * full featured over a period of time. This is the inverse function of | |
253 | * create. It also stores all the retrieved values in the default array | |
254 | * | |
64bd5a0e TO |
255 | * @param array $params |
256 | * (reference ) an assoc array of name/value pairs. | |
257 | * @param array $defaults | |
258 | * (reference ) an assoc array to hold the name / value pairs. | |
6a488035 | 259 | * in a hierarchical manner |
64bd5a0e TO |
260 | * @param array $ids |
261 | * (reference) the array that holds all the db ids. | |
6a488035 | 262 | * |
c490a46a | 263 | * @return CRM_Case_BAO_Case object |
6a488035 TO |
264 | * @static |
265 | */ | |
00be9182 | 266 | public static function retrieve(&$params, &$defaults, &$ids) { |
6a488035 TO |
267 | $case = CRM_Case_BAO_Case::getValues($params, $defaults, $ids); |
268 | return $case; | |
269 | } | |
270 | ||
271 | /** | |
100fef9d | 272 | * Process case activity add/delete |
6a488035 TO |
273 | * takes an associative array and |
274 | * | |
64bd5a0e TO |
275 | * @param array $params |
276 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 277 | * |
6a488035 TO |
278 | * @static |
279 | */ | |
00be9182 | 280 | public static function processCaseActivity(&$params) { |
6a488035 TO |
281 | $caseActivityDAO = new CRM_Case_DAO_CaseActivity(); |
282 | $caseActivityDAO->activity_id = $params['activity_id']; | |
283 | $caseActivityDAO->case_id = $params['case_id']; | |
284 | ||
285 | $caseActivityDAO->find(TRUE); | |
286 | $caseActivityDAO->save(); | |
287 | } | |
288 | ||
289 | /** | |
100fef9d | 290 | * Get the case subject for Activity |
6a488035 | 291 | * |
64bd5a0e TO |
292 | * @param int $activityId |
293 | * Activity id. | |
6a488035 TO |
294 | * |
295 | * @return case subject or null | |
6a488035 TO |
296 | * @static |
297 | */ | |
00be9182 | 298 | public static function getCaseSubject($activityId) { |
6a488035 TO |
299 | $caseActivity = new CRM_Case_DAO_CaseActivity(); |
300 | $caseActivity->activity_id = $activityId; | |
301 | if ($caseActivity->find(TRUE)) { | |
302 | return CRM_Core_DAO::getFieldValue('CRM_Case_BAO_Case', $caseActivity->case_id, 'subject'); | |
303 | } | |
304 | return NULL; | |
305 | } | |
306 | ||
307 | /** | |
100fef9d | 308 | * Get the case type. |
6a488035 TO |
309 | * |
310 | * @param int $caseId | |
311 | * | |
77b97be7 EM |
312 | * @param string $colName |
313 | * | |
6a488035 | 314 | * @return case type |
6a488035 TO |
315 | * @static |
316 | */ | |
00be9182 | 317 | public static function getCaseType($caseId, $colName = 'title') { |
8ffdec17 ARW |
318 | $query = " |
319 | SELECT civicrm_case_type.{$colName} FROM civicrm_case | |
320 | LEFT JOIN civicrm_case_type ON | |
321 | civicrm_case.case_type_id = civicrm_case_type.id | |
322 | WHERE civicrm_case.id = %1"; | |
6a488035 | 323 | |
8ffdec17 | 324 | $queryParams = array(1 => array($caseId, 'Integer')); |
6a488035 | 325 | |
8ffdec17 | 326 | return CRM_Core_DAO::singleValueQuery($query, $queryParams); |
6a488035 TO |
327 | } |
328 | ||
329 | /** | |
330 | * Delete the record that are associated with this case | |
331 | * record are deleted from case | |
332 | * | |
64bd5a0e TO |
333 | * @param int $caseId |
334 | * Id of the case to delete. | |
6a488035 | 335 | * |
77b97be7 EM |
336 | * @param bool $moveToTrash |
337 | * | |
3d0d359e | 338 | * @return bool is successful |
6a488035 TO |
339 | * @static |
340 | */ | |
00be9182 | 341 | public static function deleteCase($caseId, $moveToTrash = FALSE) { |
6a488035 TO |
342 | CRM_Utils_Hook::pre('delete', 'Case', $caseId, CRM_Core_DAO::$_nullArray); |
343 | ||
344 | //delete activities | |
345 | $activities = self::getCaseActivityDates($caseId); | |
346 | if ($activities) { | |
347 | foreach ($activities as $value) { | |
348 | CRM_Activity_BAO_Activity::deleteActivity($value, $moveToTrash); | |
349 | } | |
350 | } | |
351 | ||
352 | if (!$moveToTrash) { | |
353 | $transaction = new CRM_Core_Transaction(); | |
354 | } | |
355 | $case = new CRM_Case_DAO_Case(); | |
356 | $case->id = $caseId; | |
357 | if (!$moveToTrash) { | |
358 | $result = $case->delete(); | |
359 | $transaction->commit(); | |
360 | } | |
361 | else { | |
362 | $result = $case->is_deleted = 1; | |
363 | $case->save(); | |
364 | } | |
365 | ||
366 | if ($result) { | |
367 | // CRM-7364, disable relationships | |
368 | self::enableDisableCaseRelationships($caseId, FALSE); | |
369 | ||
370 | CRM_Utils_Hook::post('delete', 'Case', $caseId, $case); | |
371 | ||
372 | // remove case from recent items. | |
373 | $caseRecent = array( | |
374 | 'id' => $caseId, | |
375 | 'type' => 'Case', | |
376 | ); | |
377 | CRM_Utils_Recent::del($caseRecent); | |
378 | return TRUE; | |
379 | } | |
380 | ||
381 | return FALSE; | |
382 | } | |
383 | ||
384 | /** | |
100fef9d | 385 | * Enable disable case related relationships |
6a488035 | 386 | * |
64bd5a0e TO |
387 | * @param int $caseId |
388 | * Case id. | |
389 | * @param bool $enable | |
390 | * Action. | |
6a488035 | 391 | * |
e96f025a | 392 | * @return void |
e96f025a | 393 | * @static |
6a488035 | 394 | */ |
00be9182 | 395 | public static function enableDisableCaseRelationships($caseId, $enable) { |
6a488035 TO |
396 | $contactIds = self::retrieveContactIdsByCaseId($caseId); |
397 | if (!empty($contactIds)) { | |
398 | foreach ($contactIds as $cid) { | |
399 | $roles = self::getCaseRoles($cid, $caseId); | |
400 | if (!empty($roles)) { | |
401 | $relationshipIds = implode(',', array_keys($roles)); | |
e96f025a | 402 | $enable = (int) $enable; |
403 | $query = "UPDATE civicrm_relationship SET is_active = {$enable} | |
6a488035 TO |
404 | WHERE id IN ( {$relationshipIds} )"; |
405 | CRM_Core_DAO::executeQuery($query); | |
406 | } | |
407 | } | |
408 | } | |
409 | } | |
410 | ||
411 | /** | |
412 | * Delete the activities related to case | |
413 | * | |
64bd5a0e TO |
414 | * @param int $activityId |
415 | * Id of the activity. | |
6a488035 TO |
416 | * |
417 | * @return void | |
6a488035 TO |
418 | * @static |
419 | */ | |
00be9182 | 420 | public static function deleteCaseActivity($activityId) { |
6a488035 TO |
421 | $case = new CRM_Case_DAO_CaseActivity(); |
422 | $case->activity_id = $activityId; | |
423 | $case->delete(); | |
424 | } | |
425 | ||
426 | /** | |
427 | * Retrieve contact_id by case_id | |
428 | * | |
64bd5a0e TO |
429 | * @param int $caseId |
430 | * ID of the case. | |
77b97be7 | 431 | * |
100fef9d | 432 | * @param int $contactID |
6a488035 TO |
433 | * |
434 | * @return array | |
6a488035 | 435 | */ |
00be9182 | 436 | public static function retrieveContactIdsByCaseId($caseId, $contactID = NULL) { |
6a488035 TO |
437 | $caseContact = new CRM_Case_DAO_CaseContact(); |
438 | $caseContact->case_id = $caseId; | |
439 | $caseContact->find(); | |
440 | $contactArray = array(); | |
441 | $count = 1; | |
442 | while ($caseContact->fetch()) { | |
443 | if ($contactID != $caseContact->contact_id) { | |
444 | $contactArray[$count] = $caseContact->contact_id; | |
445 | $count++; | |
446 | } | |
447 | } | |
448 | ||
449 | return $contactArray; | |
450 | } | |
451 | ||
452 | /** | |
453 | * Look up a case using an activity ID | |
454 | * | |
100fef9d | 455 | * @param int $activityId |
77b97be7 | 456 | * |
6a488035 TO |
457 | * @return int, case ID |
458 | */ | |
00be9182 | 459 | public static function getCaseIdByActivityId($activityId) { |
6a488035 TO |
460 | $originalId = CRM_Core_DAO::singleValueQuery( |
461 | 'SELECT original_id FROM civicrm_activity WHERE id = %1', | |
462 | array('1' => array($activityId, 'Integer')) | |
463 | ); | |
464 | $caseId = CRM_Core_DAO::singleValueQuery( | |
465 | 'SELECT case_id FROM civicrm_case_activity WHERE activity_id in (%1,%2)', | |
466 | array( | |
467 | '1' => array($activityId, 'Integer'), | |
468 | '2' => array($originalId ? $originalId : $activityId, 'Integer'), | |
469 | ) | |
470 | ); | |
471 | return $caseId; | |
472 | } | |
473 | ||
474 | /** | |
475 | * Retrieve contact names by caseId | |
476 | * | |
64bd5a0e TO |
477 | * @param int $caseId |
478 | * ID of the case. | |
6a488035 TO |
479 | * |
480 | * @return array | |
481 | * | |
6a488035 TO |
482 | * |
483 | */ | |
00be9182 | 484 | public static function getContactNames($caseId) { |
6a488035 TO |
485 | $contactNames = array(); |
486 | if (!$caseId) { | |
487 | return $contactNames; | |
488 | } | |
489 | ||
490 | $query = " | |
491 | SELECT contact_a.sort_name name, | |
492 | contact_a.display_name as display_name, | |
493 | contact_a.id cid, | |
494 | contact_a.birth_date as birth_date, | |
495 | ce.email as email, | |
496 | cp.phone as phone | |
497 | FROM civicrm_contact contact_a | |
498 | LEFT JOIN civicrm_case_contact ON civicrm_case_contact.contact_id = contact_a.id | |
499 | LEFT JOIN civicrm_email ce ON ( ce.contact_id = contact_a.id AND ce.is_primary = 1) | |
500 | LEFT JOIN civicrm_phone cp ON ( cp.contact_id = contact_a.id AND cp.is_primary = 1) | |
501 | WHERE civicrm_case_contact.case_id = %1"; | |
502 | ||
503 | $dao = CRM_Core_DAO::executeQuery($query, | |
504 | array(1 => array($caseId, 'Integer')) | |
505 | ); | |
506 | while ($dao->fetch()) { | |
507 | $contactNames[$dao->cid]['contact_id'] = $dao->cid; | |
508 | $contactNames[$dao->cid]['sort_name'] = $dao->name; | |
509 | $contactNames[$dao->cid]['display_name'] = $dao->display_name; | |
510 | $contactNames[$dao->cid]['email'] = $dao->email; | |
511 | $contactNames[$dao->cid]['phone'] = $dao->phone; | |
512 | $contactNames[$dao->cid]['birth_date'] = $dao->birth_date; | |
513 | $contactNames[$dao->cid]['role'] = ts('Client'); | |
514 | } | |
515 | ||
516 | return $contactNames; | |
517 | } | |
518 | ||
519 | /** | |
520 | * Retrieve case_id by contact_id | |
521 | * | |
100fef9d | 522 | * @param int $contactID |
64bd5a0e TO |
523 | * @param bool $includeDeleted |
524 | * Include the deleted cases in result. | |
77b97be7 EM |
525 | * @param null $caseType |
526 | * | |
6a488035 TO |
527 | * @return array |
528 | * | |
6a488035 | 529 | */ |
00be9182 | 530 | public static function retrieveCaseIdsByContactId($contactID, $includeDeleted = FALSE, $caseType = NULL) { |
6a488035 TO |
531 | $query = " |
532 | SELECT ca.id as id | |
533 | FROM civicrm_case_contact cc | |
534 | INNER JOIN civicrm_case ca ON cc.case_id = ca.id | |
6a488035 | 535 | "; |
1d8da9d9 DJ |
536 | if (isset($caseType)) { |
537 | $query .= | |
76304e2f | 538 | "INNER JOIN civicrm_case_type ON civicrm_case_type.id = ca.case_type_id |
8ffdec17 | 539 | WHERE cc.contact_id = %1 AND civicrm_case_type.name = '{$caseType}'"; |
1d8da9d9 | 540 | } |
a2da6067 DJ |
541 | if (!isset($caseType)) { |
542 | $query .= "WHERE cc.contact_id = %1"; | |
543 | } | |
6a488035 TO |
544 | if (!$includeDeleted) { |
545 | $query .= " AND ca.is_deleted = 0"; | |
546 | } | |
547 | ||
548 | $params = array(1 => array($contactID, 'Integer')); | |
549 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
550 | ||
551 | $caseArray = array(); | |
552 | while ($dao->fetch()) { | |
553 | $caseArray[] = $dao->id; | |
554 | } | |
555 | ||
556 | $dao->free(); | |
557 | return $caseArray; | |
558 | } | |
559 | ||
4c6ce474 EM |
560 | /** |
561 | * @param string $type | |
100fef9d | 562 | * @param int $userID |
4c6ce474 EM |
563 | * @param null $condition |
564 | * @param int $isDeleted | |
565 | * | |
566 | * @return string | |
567 | */ | |
00be9182 | 568 | public static function getCaseActivityQuery($type = 'upcoming', $userID = NULL, $condition = NULL, $isDeleted = 0) { |
6a488035 TO |
569 | if (!$userID) { |
570 | $session = CRM_Core_Session::singleton(); | |
571 | $userID = $session->get('userID'); | |
572 | } | |
573 | ||
574 | $actStatus = array_flip(CRM_Core_PseudoConstant::activityStatus('name')); | |
575 | $scheduledStatusId = $actStatus['Scheduled']; | |
576 | ||
577 | $query = "SELECT | |
578 | civicrm_case.id as case_id, | |
579 | civicrm_case.subject as case_subject, | |
580 | civicrm_contact.id as contact_id, | |
581 | civicrm_contact.sort_name as sort_name, | |
582 | civicrm_phone.phone as phone, | |
583 | civicrm_contact.contact_type as contact_type, | |
584 | civicrm_contact.contact_sub_type as contact_sub_type, | |
585 | t_act.activity_type_id, | |
8ffdec17 ARW |
586 | c_type.title as case_type, |
587 | civicrm_case.case_type_id as case_type_id, | |
6a488035 TO |
588 | cov_status.label as case_status, |
589 | cov_status.label as case_status_name, | |
590 | t_act.status_id, | |
591 | civicrm_case.start_date as case_start_date, | |
592 | case_relation_type.label_b_a as case_role, "; | |
593 | ||
594 | if ($type == 'upcoming') { | |
595 | $query .= " | |
596 | t_act.desired_date as case_scheduled_activity_date, | |
597 | t_act.id as case_scheduled_activity_id, | |
598 | t_act.act_type_name as case_scheduled_activity_type_name, | |
599 | t_act.act_type AS case_scheduled_activity_type "; | |
600 | } | |
601 | elseif ($type == 'recent') { | |
602 | $query .= " | |
603 | t_act.desired_date as case_recent_activity_date, | |
604 | t_act.id as case_recent_activity_id, | |
605 | t_act.act_type_name as case_recent_activity_type_name, | |
606 | t_act.act_type AS case_recent_activity_type "; | |
607 | } | |
76304e2f TO |
608 | elseif ($type == 'any') { |
609 | $query .= " | |
90319720 BS |
610 | t_act.desired_date as case_activity_date, |
611 | t_act.id as case_activity_id, | |
612 | t_act.act_type_name as case_activity_type_name, | |
613 | t_act.act_type AS case_activity_type "; | |
614 | } | |
6a488035 TO |
615 | |
616 | $query .= " FROM civicrm_case | |
617 | INNER JOIN civicrm_case_contact ON civicrm_case.id = civicrm_case_contact.case_id | |
618 | INNER JOIN civicrm_contact ON civicrm_case_contact.contact_id = civicrm_contact.id "; | |
619 | ||
620 | if ($type == 'upcoming') { | |
621 | // This gets the earliest activity per case that's scheduled within 14 days from now. | |
622 | // Note we have an inner select to get the min activity id in order to remove duplicates in case there are two with the same datetime. | |
623 | // In this case we don't really care which one, so min(id) works. | |
624 | // optimized in CRM-11837 | |
625 | $query .= " INNER JOIN | |
626 | ( | |
627 | SELECT case_id, act.id, activity_date_time AS desired_date, activity_type_id, status_id, aov.name AS act_type_name, aov.label AS act_type | |
628 | FROM ( | |
629 | SELECT * | |
630 | FROM ( | |
631 | SELECT * | |
632 | FROM civicrm_view_case_activity_upcoming | |
633 | ORDER BY activity_date_time ASC, id ASC | |
634 | ) AS upcomingOrdered | |
635 | GROUP BY case_id | |
636 | ) AS act | |
637 | LEFT JOIN civicrm_option_group aog ON aog.name='activity_type' | |
638 | LEFT JOIN civicrm_option_value aov ON ( aov.option_group_id = aog.id AND aov.value = act.activity_type_id ) | |
639 | ) AS t_act | |
640 | "; | |
641 | } | |
642 | elseif ($type == 'recent') { | |
643 | // Similarly, the most recent activity in the past 14 days, and exclude scheduled. | |
644 | //improve query performance - CRM-10598 | |
645 | $query .= " INNER JOIN | |
646 | ( | |
647 | SELECT case_id, act.id, activity_date_time AS desired_date, activity_type_id, status_id, aov.name AS act_type_name, aov.label AS act_type | |
648 | FROM ( | |
649 | SELECT * | |
650 | FROM ( | |
651 | SELECT * | |
652 | FROM civicrm_view_case_activity_recent | |
653 | ORDER BY activity_date_time DESC, id ASC | |
654 | ) AS recentOrdered | |
655 | GROUP BY case_id | |
656 | ) AS act | |
657 | LEFT JOIN civicrm_option_group aog ON aog.name='activity_type' | |
658 | LEFT JOIN civicrm_option_value aov ON ( aov.option_group_id = aog.id AND aov.value = act.activity_type_id ) | |
659 | ) AS t_act "; | |
660 | } | |
76304e2f | 661 | elseif ($type == 'any') { |
90319720 BS |
662 | $query .= " LEFT JOIN |
663 | ( | |
664 | SELECT ca4.case_id, act4.id AS id, act4.activity_date_time AS desired_date, act4.activity_type_id, act4.status_id, aov.name AS act_type_name, aov.label AS act_type | |
665 | FROM civicrm_activity act4 | |
666 | LEFT JOIN civicrm_case_activity ca4 | |
667 | ON ca4.activity_id = act4.id | |
668 | AND act4.is_current_revision = 1 | |
669 | LEFT JOIN civicrm_option_group aog | |
670 | ON aog.name='activity_type' | |
671 | LEFT JOIN civicrm_option_value aov | |
672 | ON aov.option_group_id = aog.id | |
673 | AND aov.value = act4.activity_type_id | |
674 | ) AS t_act"; | |
675 | } | |
6a488035 TO |
676 | |
677 | $query .= " | |
678 | ON t_act.case_id = civicrm_case.id | |
679 | LEFT JOIN civicrm_phone ON (civicrm_phone.contact_id = civicrm_contact.id AND civicrm_phone.is_primary=1) | |
680 | LEFT JOIN civicrm_relationship case_relationship | |
681 | ON ( case_relationship.contact_id_a = civicrm_case_contact.contact_id AND case_relationship.contact_id_b = {$userID} | |
682 | AND case_relationship.case_id = civicrm_case.id ) | |
683 | ||
684 | LEFT JOIN civicrm_relationship_type case_relation_type | |
685 | ON ( case_relation_type.id = case_relationship.relationship_type_id | |
686 | AND case_relation_type.id = case_relationship.relationship_type_id ) | |
687 | ||
8ffdec17 ARW |
688 | LEFT JOIN civicrm_case_type c_type |
689 | ON civicrm_case.case_type_id = c_type.id | |
6a488035 TO |
690 | |
691 | LEFT JOIN civicrm_option_group cog_status | |
692 | ON cog_status.name = 'case_status' | |
693 | ||
694 | LEFT JOIN civicrm_option_value cov_status | |
695 | ON ( civicrm_case.status_id = cov_status.value | |
696 | AND cog_status.id = cov_status.option_group_id ) | |
697 | "; | |
698 | ||
699 | if ($condition) { | |
700 | // CRM-8749 backwards compatibility - callers of this function expect to start $condition with "AND" | |
701 | $query .= " WHERE (1) $condition "; | |
702 | } | |
703 | ||
704 | if ($type == 'upcoming') { | |
705 | $query .= " ORDER BY case_scheduled_activity_date ASC "; | |
706 | } | |
707 | elseif ($type == 'recent') { | |
708 | $query .= " ORDER BY case_recent_activity_date ASC "; | |
709 | } | |
76304e2f | 710 | elseif ($type == 'any') { |
90319720 BS |
711 | $query .= " ORDER BY case_activity_date ASC "; |
712 | } | |
6a488035 TO |
713 | |
714 | return $query; | |
715 | } | |
716 | ||
717 | /** | |
718 | * Retrieve cases related to particular contact or whole contact | |
719 | * used in Dashboad and Tab | |
720 | * | |
64bd5a0e | 721 | * @param bool $allCases |
6a488035 | 722 | * |
e96f025a | 723 | * @param int $userID |
6a488035 | 724 | * |
64bd5a0e TO |
725 | * @param string $type |
726 | * /upcoming,recent,all/. | |
6a488035 | 727 | * |
77b97be7 EM |
728 | * @param string $context |
729 | * | |
6a488035 TO |
730 | * @return array Array of Cases |
731 | * | |
6a488035 | 732 | */ |
00be9182 | 733 | public static function getCases($allCases = TRUE, $userID = NULL, $type = 'upcoming', $context = 'dashboard') { |
6a488035 TO |
734 | $condition = NULL; |
735 | $casesList = array(); | |
736 | ||
737 | //validate access for own cases. | |
738 | if (!self::accessCiviCase()) { | |
739 | return $casesList; | |
740 | } | |
741 | ||
742 | if (!$userID) { | |
743 | $session = CRM_Core_Session::singleton(); | |
744 | $userID = $session->get('userID'); | |
745 | } | |
746 | ||
747 | //validate access for all cases. | |
748 | if ($allCases && !CRM_Core_Permission::check('access all cases and activities')) { | |
749 | $allCases = FALSE; | |
750 | } | |
751 | ||
6a488035 TO |
752 | $condition = " AND civicrm_case.is_deleted = 0 "; |
753 | ||
754 | if (!$allCases) { | |
755 | $condition .= " AND case_relationship.contact_id_b = {$userID} "; | |
756 | } | |
76304e2f | 757 | if ($type == 'upcoming' || $type == 'any') { |
6a488035 TO |
758 | $closedId = CRM_Core_OptionGroup::getValue('case_status', 'Closed', 'name'); |
759 | $condition .= " | |
760 | AND civicrm_case.status_id != $closedId"; | |
761 | } | |
762 | ||
763 | $query = self::getCaseActivityQuery($type, $userID, $condition); | |
764 | ||
765 | $queryParams = array(); | |
766 | $result = CRM_Core_DAO::executeQuery($query, | |
767 | $queryParams | |
768 | ); | |
769 | ||
770 | $caseStatus = CRM_Core_OptionGroup::values('case_status', FALSE, FALSE, FALSE, " AND v.name = 'Urgent' "); | |
771 | ||
772 | $resultFields = array( | |
773 | 'contact_id', | |
774 | 'contact_type', | |
775 | 'sort_name', | |
776 | 'phone', | |
777 | 'case_id', | |
778 | 'case_subject', | |
779 | 'case_type', | |
8ffdec17 | 780 | 'case_type_id', |
6a488035 TO |
781 | 'status_id', |
782 | 'case_status', | |
783 | 'case_status_name', | |
784 | 'activity_type_id', | |
785 | 'case_start_date', | |
786 | 'case_role', | |
787 | ); | |
788 | ||
789 | if ($type == 'upcoming') { | |
790 | $resultFields[] = 'case_scheduled_activity_date'; | |
791 | $resultFields[] = 'case_scheduled_activity_type_name'; | |
792 | $resultFields[] = 'case_scheduled_activity_type'; | |
793 | $resultFields[] = 'case_scheduled_activity_id'; | |
794 | } | |
795 | elseif ($type == 'recent') { | |
796 | $resultFields[] = 'case_recent_activity_date'; | |
797 | $resultFields[] = 'case_recent_activity_type_name'; | |
798 | $resultFields[] = 'case_recent_activity_type'; | |
799 | $resultFields[] = 'case_recent_activity_id'; | |
800 | } | |
76304e2f | 801 | elseif ($type == 'any') { |
90319720 BS |
802 | $resultFields[] = 'case_activity_date'; |
803 | $resultFields[] = 'case_activity_type_name'; | |
804 | $resultFields[] = 'case_activity_type'; | |
805 | $resultFields[] = 'case_activity_id'; | |
806 | } | |
6a488035 TO |
807 | |
808 | // we're going to use the usual actions, so doesn't make sense to duplicate definitions | |
809 | $actions = CRM_Case_Selector_Search::links(); | |
810 | ||
6a488035 TO |
811 | // check is the user has view/edit signer permission |
812 | $permissions = array(CRM_Core_Permission::VIEW); | |
813 | if (CRM_Core_Permission::check('access all cases and activities') || | |
814 | (!$allCases && CRM_Core_Permission::check('access my cases and activities')) | |
815 | ) { | |
816 | $permissions[] = CRM_Core_Permission::EDIT; | |
817 | } | |
818 | if (CRM_Core_Permission::check('delete in CiviCase')) { | |
819 | $permissions[] = CRM_Core_Permission::DELETE; | |
820 | } | |
821 | $mask = CRM_Core_Action::mask($permissions); | |
822 | ||
823 | while ($result->fetch()) { | |
824 | foreach ($resultFields as $donCare => $field) { | |
825 | $casesList[$result->case_id][$field] = $result->$field; | |
826 | if ($field == 'contact_type') { | |
e547f744 | 827 | $casesList[$result->case_id]['contact_type_icon'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type |
6a488035 TO |
828 | ); |
829 | $casesList[$result->case_id]['action'] = CRM_Core_Action::formLink($actions['primaryActions'], $mask, | |
830 | array( | |
831 | 'id' => $result->case_id, | |
832 | 'cid' => $result->contact_id, | |
833 | 'cxt' => $context, | |
87dab4a4 AH |
834 | ), |
835 | ts('more'), | |
836 | FALSE, | |
837 | 'case.actions.primary', | |
838 | 'Case', | |
839 | $result->case_id | |
6a488035 TO |
840 | ); |
841 | $casesList[$result->case_id]['moreActions'] = CRM_Core_Action::formLink($actions['moreActions'], | |
842 | $mask, | |
843 | array( | |
844 | 'id' => $result->case_id, | |
845 | 'cid' => $result->contact_id, | |
846 | 'cxt' => $context, | |
847 | ), | |
848 | ts('more'), | |
87dab4a4 AH |
849 | TRUE, |
850 | 'case.actions.more', | |
851 | 'Case', | |
852 | $result->case_id | |
6a488035 TO |
853 | ); |
854 | } | |
855 | elseif ($field == 'case_status') { | |
856 | if (in_array($result->$field, $caseStatus)) { | |
857 | $casesList[$result->case_id]['class'] = "status-urgent"; | |
858 | } | |
859 | else { | |
860 | $casesList[$result->case_id]['class'] = "status-normal"; | |
861 | } | |
862 | } | |
863 | } | |
864 | //CRM-4510. | |
d14b35a4 N |
865 | $caseTypes = CRM_Case_PseudoConstant::caseType('name'); |
866 | $caseManagerContact = self::getCaseManagerContact($caseTypes[$result->case_type_id], $result->case_id); | |
6a488035 TO |
867 | if (!empty($caseManagerContact)) { |
868 | $casesList[$result->case_id]['casemanager_id'] = CRM_Utils_Array::value('casemanager_id', $caseManagerContact); | |
869 | $casesList[$result->case_id]['casemanager'] = CRM_Utils_Array::value('casemanager', $caseManagerContact); | |
870 | } | |
871 | ||
872 | //do check user permissions for edit/view activity. | |
873 | if (($actId = CRM_Utils_Array::value('case_scheduled_activity_id', $casesList[$result->case_id])) || | |
874 | ($actId = CRM_Utils_Array::value('case_recent_activity_id', $casesList[$result->case_id])) | |
875 | ) { | |
876 | $casesList[$result->case_id]["case_{$type}_activity_editable"] = self::checkPermission($actId, | |
877 | 'edit', | |
878 | $casesList[$result->case_id]['activity_type_id'], $userID | |
879 | ); | |
880 | $casesList[$result->case_id]["case_{$type}_activity_viewable"] = self::checkPermission($actId, | |
881 | 'view', | |
882 | $casesList[$result->case_id]['activity_type_id'], $userID | |
883 | ); | |
884 | } | |
885 | } | |
886 | ||
887 | return $casesList; | |
888 | } | |
889 | ||
890 | /** | |
100fef9d CW |
891 | * Get the summary of cases counts by type and status. |
892 | * | |
893 | * @param bool $allCases | |
894 | * @param int $userID | |
895 | * @return array | |
6a488035 | 896 | */ |
00be9182 | 897 | public static function getCasesSummary($allCases = TRUE, $userID) { |
6a488035 TO |
898 | $caseSummary = array(); |
899 | ||
900 | //validate access for civicase. | |
901 | if (!self::accessCiviCase()) { | |
902 | return $caseSummary; | |
903 | } | |
904 | ||
905 | //validate access for all cases. | |
906 | if ($allCases && !CRM_Core_Permission::check('access all cases and activities')) { | |
907 | $allCases = FALSE; | |
908 | } | |
909 | ||
e96f025a | 910 | $caseTypes = CRM_Case_PseudoConstant::caseType(); |
6a488035 | 911 | $caseStatuses = CRM_Case_PseudoConstant::caseStatus(); |
e96f025a | 912 | $caseTypes = array_flip($caseTypes); |
6a488035 TO |
913 | |
914 | // get statuses as headers for the table | |
915 | $url = CRM_Utils_System::url('civicrm/case/search', "reset=1&force=1&all=1&status="); | |
916 | foreach ($caseStatuses as $key => $name) { | |
917 | $caseSummary['headers'][$key]['status'] = $name; | |
918 | $caseSummary['headers'][$key]['url'] = $url . $key; | |
919 | } | |
920 | ||
921 | // build rows with actual data | |
922 | $rows = array(); | |
923 | $myGroupByClause = $mySelectClause = $myCaseFromClause = $myCaseWhereClause = ''; | |
924 | ||
925 | if ($allCases) { | |
926 | $userID = 'null'; | |
927 | $all = 1; | |
928 | $case_owner = 1; | |
929 | } | |
930 | else { | |
e96f025a | 931 | $all = 0; |
6a488035 TO |
932 | $case_owner = 2; |
933 | $myCaseWhereClause = " AND case_relationship.contact_id_b = {$userID}"; | |
e96f025a | 934 | $myGroupByClause = " GROUP BY CONCAT(case_relationship.case_id,'-',case_relationship.contact_id_b)"; |
6a488035 TO |
935 | } |
936 | ||
6a488035 | 937 | $query = " |
8ffdec17 ARW |
938 | SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS case_type, |
939 | case_type_id, case_relationship.contact_id_b | |
6a488035 | 940 | FROM civicrm_case |
8ffdec17 | 941 | LEFT JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id |
6a488035 TO |
942 | LEFT JOIN civicrm_option_group option_group_case_status ON ( option_group_case_status.name = 'case_status' ) |
943 | LEFT JOIN civicrm_option_value case_status ON ( civicrm_case.status_id = case_status.value | |
944 | AND option_group_case_status.id = case_status.option_group_id ) | |
945 | LEFT JOIN civicrm_relationship case_relationship ON ( case_relationship.case_id = civicrm_case.id | |
946 | AND case_relationship.contact_id_b = {$userID}) | |
947 | WHERE is_deleted =0 | |
948 | {$myCaseWhereClause} {$myGroupByClause}"; | |
949 | ||
950 | $res = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
951 | while ($res->fetch()) { | |
8cc574cf | 952 | if (!empty($rows[$res->case_type]) && !empty($rows[$res->case_type][$res->case_status])) { |
6a488035 TO |
953 | $rows[$res->case_type][$res->case_status]['count'] = $rows[$res->case_type][$res->case_status]['count'] + 1; |
954 | } | |
955 | else { | |
956 | $rows[$res->case_type][$res->case_status] = array( | |
957 | 'count' => 1, | |
958 | 'url' => CRM_Utils_System::url('civicrm/case/search', | |
959 | "reset=1&force=1&status={$res->status_id}&type={$res->case_type_id}&case_owner={$case_owner}" | |
960 | ), | |
961 | ); | |
962 | } | |
963 | } | |
964 | $caseSummary['rows'] = array_merge($caseTypes, $rows); | |
965 | ||
966 | return $caseSummary; | |
967 | } | |
968 | ||
969 | /** | |
100fef9d | 970 | * Get Case roles |
6a488035 | 971 | * |
64bd5a0e TO |
972 | * @param int $contactID |
973 | * Contact id. | |
974 | * @param int $caseID | |
975 | * Case id. | |
100fef9d | 976 | * @param int $relationshipID |
77b97be7 | 977 | * |
100fef9d | 978 | * @return array case role / relationships |
6a488035 TO |
979 | * |
980 | * @static | |
981 | */ | |
00be9182 | 982 | public static function getCaseRoles($contactID, $caseID, $relationshipID = NULL) { |
6a488035 TO |
983 | $query = ' |
984 | SELECT civicrm_relationship.id as civicrm_relationship_id, | |
985 | civicrm_contact.sort_name as sort_name, | |
986 | civicrm_email.email as email, | |
987 | civicrm_phone.phone as phone, | |
988 | civicrm_relationship.contact_id_b as civicrm_contact_id, | |
989 | civicrm_relationship.contact_id_a as client_id, | |
990 | civicrm_relationship_type.label_a_b as relation, | |
991 | civicrm_relationship_type.id as relation_type | |
992 | FROM civicrm_relationship | |
993 | INNER JOIN civicrm_relationship_type ON civicrm_relationship.relationship_type_id = civicrm_relationship_type.id | |
994 | INNER JOIN civicrm_contact ON civicrm_relationship.contact_id_b = civicrm_contact.id | |
995 | LEFT JOIN civicrm_phone ON (civicrm_phone.contact_id = civicrm_contact.id AND civicrm_phone.is_primary = 1) | |
996 | LEFT JOIN civicrm_email ON (civicrm_email.contact_id = civicrm_contact.id ) | |
997 | WHERE civicrm_relationship.contact_id_a = %1 AND civicrm_relationship.case_id = %2'; | |
998 | ||
6a488035 TO |
999 | $params = array( |
1000 | 1 => array($contactID, 'Positive'), | |
1001 | 2 => array($caseID, 'Positive'), | |
1002 | ); | |
1003 | ||
1004 | if ($relationshipID) { | |
1005 | $query .= ' AND civicrm_relationship.id = %3 '; | |
1006 | $params[3] = array($relationshipID, 'Integer'); | |
1007 | } | |
1008 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1009 | ||
1010 | $values = array(); | |
1011 | while ($dao->fetch()) { | |
1012 | $rid = $dao->civicrm_relationship_id; | |
1013 | $values[$rid]['cid'] = $dao->civicrm_contact_id; | |
1014 | $values[$rid]['relation'] = $dao->relation; | |
1015 | $values[$rid]['name'] = $dao->sort_name; | |
1016 | $values[$rid]['email'] = $dao->email; | |
1017 | $values[$rid]['phone'] = $dao->phone; | |
1018 | $values[$rid]['relation_type'] = $dao->relation_type; | |
1019 | $values[$rid]['rel_id'] = $dao->civicrm_relationship_id; | |
1020 | $values[$rid]['client_id'] = $dao->client_id; | |
1021 | } | |
1022 | ||
1023 | $dao->free(); | |
1024 | return $values; | |
1025 | } | |
1026 | ||
1027 | /** | |
100fef9d | 1028 | * Get Case Activities |
6a488035 | 1029 | * |
64bd5a0e TO |
1030 | * @param int $caseID |
1031 | * Case id. | |
1032 | * @param array $params | |
1033 | * Posted params. | |
1034 | * @param int $contactID | |
1035 | * Contact id. | |
6a488035 | 1036 | * |
77b97be7 | 1037 | * @param null $context |
100fef9d | 1038 | * @param int $userID |
77b97be7 EM |
1039 | * @param null $type |
1040 | * | |
100fef9d | 1041 | * @return array of case activities |
6a488035 TO |
1042 | * |
1043 | * @static | |
1044 | */ | |
00be9182 | 1045 | public static function getCaseActivity($caseID, &$params, $contactID, $context = NULL, $userID = NULL, $type = NULL) { |
6a488035 TO |
1046 | $values = array(); |
1047 | ||
e7e657f0 | 1048 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
9e74e3ce | 1049 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
e96f025a | 1050 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
034500d4 | 1051 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
9e74e3ce | 1052 | |
6a488035 TO |
1053 | // CRM-5081 - formatting the dates to omit seconds. |
1054 | // Note the 00 in the date format string is needed otherwise later on it thinks scheduled ones are overdue. | |
1055 | $select = "SELECT count(ca.id) as ismultiple, ca.id as id, | |
1056 | ca.activity_type_id as type, | |
1057 | ca.activity_type_id as activity_type_id, | |
1058 | cc.sort_name as reporter, | |
1059 | cc.id as reporter_id, | |
1060 | acc.sort_name AS assignee, | |
1061 | acc.id AS assignee_id, | |
1062 | DATE_FORMAT(IF(ca.activity_date_time < NOW() AND ca.status_id=ov.value, | |
1063 | ca.activity_date_time, | |
1064 | DATE_ADD(NOW(), INTERVAL 1 YEAR) | |
1065 | ), '%Y%m%d%H%i00') as overdue_date, | |
1066 | DATE_FORMAT(ca.activity_date_time, '%Y%m%d%H%i00') as display_date, | |
1067 | ca.status_id as status, | |
1068 | ca.subject as subject, | |
1069 | ca.is_deleted as deleted, | |
1070 | ca.priority_id as priority, | |
1071 | ca.weight as weight, | |
1072 | GROUP_CONCAT(ef.file_id) as attachment_ids "; | |
1073 | ||
e96f025a | 1074 | $from = " |
1075 | FROM civicrm_case_activity cca | |
6a488035 | 1076 | INNER JOIN civicrm_activity ca ON ca.id = cca.activity_id |
e96f025a | 1077 | INNER JOIN civicrm_activity_contact cac ON cac.activity_id = ca.id AND cac.record_type_id = {$sourceID} |
5b5da1ef | 1078 | INNER JOIN civicrm_contact cc ON cc.id = cac.contact_id |
e96f025a | 1079 | INNER JOIN civicrm_option_group cog ON cog.name = 'activity_type' |
6a488035 TO |
1080 | INNER JOIN civicrm_option_value cov ON cov.option_group_id = cog.id |
1081 | AND cov.value = ca.activity_type_id AND cov.is_active = 1 | |
e96f025a | 1082 | LEFT JOIN civicrm_entity_file ef on ef.entity_table = 'civicrm_activity' AND ef.entity_id = ca.id |
1083 | LEFT OUTER JOIN civicrm_option_group og ON og.name = 'activity_status' | |
1084 | LEFT OUTER JOIN civicrm_option_value ov ON ov.option_group_id=og.id AND ov.name = 'Scheduled' | |
ca456783 | 1085 | LEFT JOIN civicrm_activity_contact caa |
e96f025a | 1086 | ON caa.activity_id = ca.id AND caa.record_type_id = {$assigneeID} |
1087 | LEFT JOIN civicrm_contact acc ON acc.id = caa.contact_id "; | |
6a488035 TO |
1088 | |
1089 | $where = 'WHERE cca.case_id= %1 | |
1090 | AND ca.is_current_revision = 1'; | |
1091 | ||
a7488080 | 1092 | if (!empty($params['reporter_id'])) { |
ecaec004 | 1093 | $where .= " AND cac.contact_id = " . CRM_Utils_Type::escape($params['reporter_id'], 'Integer'); |
6a488035 TO |
1094 | } |
1095 | ||
a7488080 | 1096 | if (!empty($params['status_id'])) { |
6a488035 TO |
1097 | $where .= " AND ca.status_id = " . CRM_Utils_Type::escape($params['status_id'], 'Integer'); |
1098 | } | |
1099 | ||
a7488080 | 1100 | if (!empty($params['activity_deleted'])) { |
6a488035 TO |
1101 | $where .= " AND ca.is_deleted = 1"; |
1102 | } | |
1103 | else { | |
1104 | $where .= " AND ca.is_deleted = 0"; | |
1105 | } | |
1106 | ||
a7488080 | 1107 | if (!empty($params['activity_type_id'])) { |
6a488035 TO |
1108 | $where .= " AND ca.activity_type_id = " . CRM_Utils_Type::escape($params['activity_type_id'], 'Integer'); |
1109 | } | |
1110 | ||
a7488080 | 1111 | if (!empty($params['activity_date_low'])) { |
6a488035 TO |
1112 | $fromActivityDate = CRM_Utils_Type::escape(CRM_Utils_Date::processDate($params['activity_date_low']), 'Date'); |
1113 | } | |
a7488080 | 1114 | if (!empty($params['activity_date_high'])) { |
6a488035 TO |
1115 | $toActivityDate = CRM_Utils_Type::escape(CRM_Utils_Date::processDate($params['activity_date_high']), 'Date'); |
1116 | $toActivityDate = $toActivityDate ? $toActivityDate + 235959 : NULL; | |
1117 | } | |
1118 | ||
1119 | if (!empty($fromActivityDate)) { | |
1120 | $where .= " AND ca.activity_date_time >= '{$fromActivityDate}'"; | |
1121 | } | |
1122 | ||
1123 | if (!empty($toActivityDate)) { | |
1124 | $where .= " AND ca.activity_date_time <= '{$toActivityDate}'"; | |
1125 | } | |
1126 | ||
1127 | // hack to handle to allow initial sorting to be done by query | |
1128 | if (CRM_Utils_Array::value('sortname', $params) == 'undefined') { | |
1129 | $params['sortname'] = NULL; | |
1130 | } | |
1131 | ||
1132 | if (CRM_Utils_Array::value('sortorder', $params) == 'undefined') { | |
1133 | $params['sortorder'] = NULL; | |
1134 | } | |
1135 | ||
1136 | $sortname = CRM_Utils_Array::value('sortname', $params); | |
1137 | $sortorder = CRM_Utils_Array::value('sortorder', $params); | |
1138 | ||
1139 | $groupBy = " GROUP BY ca.id "; | |
1140 | ||
1141 | if (!$sortname AND !$sortorder) { | |
1142 | // CRM-5081 - added id to act like creation date | |
1143 | $orderBy = " ORDER BY overdue_date ASC, display_date DESC, weight DESC"; | |
1144 | } | |
1145 | else { | |
21d32567 DL |
1146 | $sort = "{$sortname} {$sortorder}"; |
1147 | $sort = CRM_Utils_Type::escape($sort, 'String'); | |
1148 | $orderBy = " ORDER BY $sort "; | |
6a488035 TO |
1149 | if ($sortname != 'display_date') { |
1150 | $orderBy .= ', display_date DESC'; | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | $page = CRM_Utils_Array::value('page', $params); | |
1155 | $rp = CRM_Utils_Array::value('rp', $params); | |
1156 | ||
1157 | if (!$page) { | |
6a488035 | 1158 | $page = 1; |
6a488035 TO |
1159 | } |
1160 | if (!$rp) { | |
1161 | $rp = 10; | |
1162 | } | |
1163 | ||
1164 | $start = (($page - 1) * $rp); | |
6a488035 TO |
1165 | $query = $select . $from . $where . $groupBy . $orderBy; |
1166 | ||
e96f025a | 1167 | $params = array(1 => array($caseID, 'Integer')); |
1168 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
6a488035 TO |
1169 | $params['total'] = $dao->N; |
1170 | ||
1171 | //FIXME: need to optimize/cache these queries | |
1172 | $limit = " LIMIT $start, $rp"; | |
1173 | $query .= $limit; | |
e96f025a | 1174 | |
1175 | //EXIT; | |
6a488035 TO |
1176 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
1177 | ||
e96f025a | 1178 | $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE); |
1179 | $activityStatus = CRM_Core_PseudoConstant::activityStatus(); | |
cbf48754 | 1180 | $activityPriority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'); |
6a488035 TO |
1181 | |
1182 | $url = CRM_Utils_System::url("civicrm/case/activity", | |
1183 | "reset=1&cid={$contactID}&caseid={$caseID}", FALSE, NULL, FALSE | |
1184 | ); | |
1185 | ||
1186 | $contextUrl = ''; | |
1187 | if ($context == 'fulltext') { | |
1188 | $contextUrl = "&context={$context}"; | |
1189 | } | |
e96f025a | 1190 | $editUrl = "{$url}&action=update{$contextUrl}"; |
1191 | $deleteUrl = "{$url}&action=delete{$contextUrl}"; | |
1192 | $restoreUrl = "{$url}&action=renew{$contextUrl}"; | |
15da63a8 | 1193 | $viewTitle = ts('View activity'); |
5a3e23e6 | 1194 | $statusTitle = ts('Edit Status'); |
6a488035 TO |
1195 | |
1196 | $emailActivityTypeIDs = array( | |
1197 | 'Email' => CRM_Core_OptionGroup::getValue('activity_type', | |
1198 | 'Email', | |
1199 | 'name' | |
1200 | ), | |
1201 | 'Inbound Email' => CRM_Core_OptionGroup::getValue('activity_type', | |
1202 | 'Inbound Email', | |
1203 | 'name' | |
1204 | ), | |
1205 | ); | |
1206 | ||
1207 | $emailActivityTypeIDs = array( | |
1208 | 'Email' => CRM_Core_OptionGroup::getValue('activity_type', | |
1209 | 'Email', | |
1210 | 'name' | |
1211 | ), | |
1212 | 'Inbound Email' => CRM_Core_OptionGroup::getValue('activity_type', | |
1213 | 'Inbound Email', | |
1214 | 'name' | |
1215 | ), | |
1216 | ); | |
1217 | ||
1218 | $caseDeleted = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseID, 'is_deleted'); | |
1219 | ||
1220 | // define statuses which are handled like Completed status (others are assumed to be handled like Scheduled status) | |
1221 | $compStatusValues = array(); | |
1222 | $compStatusNames = array('Completed', 'Left Message', 'Cancelled', 'Unreachable', 'Not Required'); | |
1223 | foreach ($compStatusNames as $name) { | |
1224 | $compStatusValues[] = CRM_Core_OptionGroup::getValue('activity_status', $name, 'name'); | |
1225 | } | |
1226 | $contactViewUrl = CRM_Utils_System::url("civicrm/contact/view", | |
1227 | "reset=1&cid=", FALSE, NULL, FALSE | |
1228 | ); | |
1229 | $hasViewContact = CRM_Core_Permission::giveMeAllACLs(); | |
1230 | $clientIds = self::retrieveContactIdsByCaseId($caseID); | |
1231 | ||
1232 | if (!$userID) { | |
1233 | $session = CRM_Core_Session::singleton(); | |
1234 | $userID = $session->get('userID'); | |
1235 | } | |
1236 | ||
1237 | while ($dao->fetch()) { | |
1238 | ||
e96f025a | 1239 | $allowView = self::checkPermission($dao->id, 'view', $dao->activity_type_id, $userID); |
1240 | $allowEdit = self::checkPermission($dao->id, 'edit', $dao->activity_type_id, $userID); | |
6a488035 TO |
1241 | $allowDelete = self::checkPermission($dao->id, 'delete', $dao->activity_type_id, $userID); |
1242 | ||
1243 | //do not have sufficient permission | |
1244 | //to access given case activity record. | |
1245 | if (!$allowView && !$allowEdit && !$allowDelete) { | |
1246 | continue; | |
1247 | } | |
1248 | ||
1249 | $values[$dao->id]['id'] = $dao->id; | |
1250 | $values[$dao->id]['type'] = $activityTypes[$dao->type]['label']; | |
1251 | ||
1252 | $reporterName = $dao->reporter; | |
1253 | if ($hasViewContact) { | |
1254 | $reporterName = '<a href="' . $contactViewUrl . $dao->reporter_id . '">' . $dao->reporter . '</a>'; | |
1255 | } | |
1256 | $values[$dao->id]['reporter'] = $reporterName; | |
034500d4 | 1257 | $targetNames = CRM_Activity_BAO_ActivityContact::getNames($dao->id, $targetID); |
6a488035 TO |
1258 | $targetContactUrls = $withContacts = array(); |
1259 | foreach ($targetNames as $targetId => $targetName) { | |
1260 | if (!in_array($targetId, $clientIds)) { | |
1261 | $withContacts[$targetId] = $targetName; | |
1262 | } | |
1263 | } | |
1264 | foreach ($withContacts as $cid => $name) { | |
1265 | if ($hasViewContact) { | |
1266 | $name = '<a href="' . $contactViewUrl . $cid . '">' . $name . '</a>'; | |
1267 | } | |
1268 | $targetContactUrls[] = $name; | |
1269 | } | |
1270 | $values[$dao->id]['with_contacts'] = implode('; ', $targetContactUrls); | |
1271 | ||
1272 | $values[$dao->id]['display_date'] = CRM_Utils_Date::customFormat($dao->display_date); | |
1273 | $values[$dao->id]['status'] = $activityStatus[$dao->status]; | |
1274 | ||
1275 | //check for view activity. | |
1276 | $subject = (empty($dao->subject)) ? '(' . ts('no subject') . ')' : $dao->subject; | |
1277 | if ($allowView) { | |
56cbe6ae CW |
1278 | $url = CRM_Utils_System::url('civicrm/case/activity/view', array('cid' => $contactID, 'aid' => $dao->id)); |
1279 | $subject = '<a class="crm-popup medium-popup" href="' . $url . '" title="' . $viewTitle . '">' . $subject . '</a>'; | |
6a488035 TO |
1280 | } |
1281 | $values[$dao->id]['subject'] = $subject; | |
1282 | ||
1283 | // add activity assignee to activity selector. CRM-4485. | |
1284 | if (isset($dao->assignee)) { | |
1285 | if ($dao->ismultiple == 1) { | |
1286 | if ($dao->reporter_id != $dao->assignee_id) { | |
1287 | $values[$dao->id]['reporter'] .= ($hasViewContact) ? ' / ' . "<a href='{$contactViewUrl}{$dao->assignee_id}'>$dao->assignee</a>" : ' / ' . $dao->assignee; | |
1288 | } | |
1289 | $values[$dao->id]['assignee'] = $dao->assignee; | |
1290 | } | |
1291 | else { | |
1292 | $values[$dao->id]['reporter'] .= ' / ' . ts('(multiple)'); | |
1293 | } | |
1294 | } | |
56cbe6ae | 1295 | // FIXME: Why are we not using CRM_Core_Action for these links? This is too much manual work and likely to get out-of-sync with core markup. |
6a488035 | 1296 | $url = ""; |
56cbe6ae | 1297 | $css = 'class="action-item crm-hover-button"'; |
6a488035 TO |
1298 | $additionalUrl = "&id={$dao->id}"; |
1299 | if (!$dao->deleted) { | |
1300 | //hide edit link of activity type email.CRM-4530. | |
1301 | if (!in_array($dao->type, $emailActivityTypeIDs)) { | |
1302 | //hide Edit link if activity type is NOT editable (special case activities).CRM-5871 | |
1303 | if ($allowEdit) { | |
56cbe6ae | 1304 | $url = '<a ' . $css . ' href="' . $editUrl . $additionalUrl . '">' . ts('Edit') . '</a> '; |
6a488035 TO |
1305 | } |
1306 | } | |
1307 | if ($allowDelete) { | |
6ce08914 | 1308 | $url .= ' <a ' . str_replace('action-item', 'action-item small-popup', $css) . ' href="' . $deleteUrl . $additionalUrl . '">' . ts('Delete') . '</a>'; |
6a488035 TO |
1309 | } |
1310 | } | |
1311 | elseif (!$caseDeleted) { | |
56cbe6ae | 1312 | $url = ' <a ' . $css . ' href="' . $restoreUrl . $additionalUrl . '">' . ts('Restore') . '</a>'; |
6a488035 TO |
1313 | $values[$dao->id]['status'] = $values[$dao->id]['status'] . '<br /> (deleted)'; |
1314 | } | |
1315 | ||
1316 | //check for operations. | |
1317 | if (self::checkPermission($dao->id, 'Move To Case', $dao->activity_type_id)) { | |
e3756c36 | 1318 | $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'move\',' . $dao->id . ', ' . $caseID . ', this ); return false;">' . ts('Move To Case') . '</a> '; |
6a488035 TO |
1319 | } |
1320 | if (self::checkPermission($dao->id, 'Copy To Case', $dao->activity_type_id)) { | |
e3756c36 | 1321 | $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'copy\',' . $dao->id . ',' . $caseID . ', this ); return false;">' . ts('Copy To Case') . '</a> '; |
6a488035 TO |
1322 | } |
1323 | // if there are file attachments we will return how many and, if only one, add a link to it | |
e96f025a | 1324 | if (!empty($dao->attachment_ids)) { |
1325 | $attachmentIDs = explode(',', $dao->attachment_ids); | |
6a488035 | 1326 | $values[$dao->id]['no_attachments'] = count($attachmentIDs); |
e96f025a | 1327 | if ($values[$dao->id]['no_attachments'] == 1) { |
6a488035 TO |
1328 | // if there is only one it's easy to do a link - otherwise just flag it |
1329 | $attachmentViewUrl = CRM_Utils_System::url( | |
1330 | "civicrm/file", | |
1331 | "reset=1&eid=" . $dao->id . "&id=" . $dao->attachment_ids, | |
1332 | FALSE, | |
1333 | NULL, | |
1334 | FALSE | |
1335 | ); | |
56cbe6ae | 1336 | $url .= " <a href='$attachmentViewUrl' ><span class='icon paper-icon'></span></a>"; |
6a488035 TO |
1337 | } |
1338 | } | |
1339 | ||
6a488035 TO |
1340 | $values[$dao->id]['links'] = $url; |
1341 | $values[$dao->id]['class'] = ""; | |
1342 | ||
1343 | if (!empty($dao->priority)) { | |
1344 | if ($dao->priority == CRM_Core_OptionGroup::getValue('priority', 'Urgent', 'name')) { | |
1345 | $values[$dao->id]['class'] = $values[$dao->id]['class'] . "priority-urgent "; | |
1346 | } | |
1347 | elseif ($dao->priority == CRM_Core_OptionGroup::getValue('priority', 'Low', 'name')) { | |
1348 | $values[$dao->id]['class'] = $values[$dao->id]['class'] . "priority-low "; | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | if (CRM_Utils_Array::crmInArray($dao->status, $compStatusValues)) { | |
1353 | $values[$dao->id]['class'] = $values[$dao->id]['class'] . " status-completed"; | |
1354 | } | |
1355 | else { | |
1356 | if (CRM_Utils_Date::overdue($dao->display_date)) { | |
1357 | $values[$dao->id]['class'] = $values[$dao->id]['class'] . " status-overdue"; | |
1358 | } | |
1359 | else { | |
1360 | $values[$dao->id]['class'] = $values[$dao->id]['class'] . " status-scheduled"; | |
1361 | } | |
1362 | } | |
1363 | ||
1364 | if ($allowEdit) { | |
88d50307 | 1365 | $values[$dao->id]['status'] = '<a class="crm-activity-status crm-activity-status-' . $dao->id . ' ' . $values[$dao->id]['class'] . ' crm-activity-change-status crm-editable-enabled" activity_id=' . $dao->id . ' current_status=' . $dao->status . ' case_id=' . $caseID . ' href="#" title=\'' . $statusTitle . '\'>' . $values[$dao->id]['status'] . '</a>'; |
6a488035 TO |
1366 | } |
1367 | } | |
1368 | $dao->free(); | |
1369 | ||
1370 | return $values; | |
1371 | } | |
1372 | ||
1373 | /** | |
100fef9d | 1374 | * Get Case Related Contacts |
6a488035 | 1375 | * |
64bd5a0e TO |
1376 | * @param int $caseID |
1377 | * Case id. | |
1378 | * @param bool $skipDetails | |
1379 | * If true include details of contacts. | |
6a488035 | 1380 | * |
7c285037 | 1381 | * @return array $searchRows array of return properties |
6a488035 TO |
1382 | * |
1383 | * @static | |
1384 | */ | |
00be9182 | 1385 | public static function getRelatedContacts($caseID, $skipDetails = FALSE) { |
6a488035 | 1386 | $values = array(); |
68783143 BS |
1387 | $query = ' |
1388 | SELECT cc.display_name as name, cc.sort_name as sort_name, cc.id, crt.label_b_a as role, ce.email | |
1389 | FROM civicrm_relationship cr | |
1390 | LEFT JOIN civicrm_relationship_type crt | |
1391 | ON crt.id = cr.relationship_type_id | |
1392 | LEFT JOIN civicrm_contact cc | |
1393 | ON cc.id = cr.contact_id_b | |
1394 | LEFT JOIN civicrm_email ce | |
1395 | ON ce.contact_id = cc.id | |
1396 | AND ce.is_primary= 1 | |
1397 | WHERE cr.case_id = %1 | |
1398 | GROUP BY cc.id'; | |
6a488035 TO |
1399 | |
1400 | $params = array(1 => array($caseID, 'Integer')); | |
1401 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1402 | ||
1403 | while ($dao->fetch()) { | |
1404 | if ($skipDetails) { | |
1405 | $values[$dao->id] = 1; | |
1406 | } | |
1407 | else { | |
1408 | $values[] = array( | |
1409 | 'contact_id' => $dao->id, | |
1410 | 'display_name' => $dao->name, | |
1411 | 'sort_name' => $dao->sort_name, | |
1412 | 'role' => $dao->role, | |
1413 | 'email' => $dao->email, | |
1414 | ); | |
1415 | } | |
1416 | } | |
1417 | $dao->free(); | |
1418 | ||
1419 | return $values; | |
1420 | } | |
1421 | ||
1422 | /** | |
100fef9d | 1423 | * Send e-mail copy of activity |
6a488035 | 1424 | * |
100fef9d | 1425 | * @param int $clientId |
64bd5a0e TO |
1426 | * @param int $activityId |
1427 | * Activity Id. | |
1428 | * @param array $contacts | |
1429 | * Array of related contact. | |
6a488035 | 1430 | * |
77b97be7 | 1431 | * @param null $attachments |
100fef9d | 1432 | * @param int $caseId |
77b97be7 | 1433 | * |
6a488035 | 1434 | * @return void |
6a488035 | 1435 | */ |
00be9182 | 1436 | public static function sendActivityCopy($clientId, $activityId, $contacts, $attachments = NULL, $caseId) { |
6a488035 TO |
1437 | if (!$activityId) { |
1438 | return; | |
1439 | } | |
1440 | ||
1441 | $tplParams = $activityInfo = array(); | |
1442 | //if its a case activity | |
1443 | if ($caseId) { | |
1444 | $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityId, 'activity_type_id'); | |
1445 | $nonCaseActivityTypes = CRM_Core_PseudoConstant::activityType(); | |
a7488080 | 1446 | if (!empty($nonCaseActivityTypes[$activityTypeId])) { |
6a488035 TO |
1447 | $anyActivity = TRUE; |
1448 | } | |
1449 | else { | |
1450 | $anyActivity = FALSE; | |
1451 | } | |
1452 | $tplParams['isCaseActivity'] = 1; | |
1453 | $tplParams['client_id'] = $clientId; | |
1454 | } | |
1455 | else { | |
1456 | $anyActivity = TRUE; | |
1457 | } | |
1458 | ||
1459 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); | |
1460 | $isRedact = $xmlProcessorProcess->getRedactActivityEmail(); | |
1461 | ||
1462 | $xmlProcessorReport = new CRM_Case_XMLProcessor_Report(); | |
1463 | ||
1464 | $activityInfo = $xmlProcessorReport->getActivityInfo($clientId, $activityId, $anyActivity, $isRedact); | |
1465 | if ($caseId) { | |
1466 | $activityInfo['fields'][] = array('label' => 'Case ID', 'type' => 'String', 'value' => $caseId); | |
1467 | } | |
1468 | $tplParams['activity'] = $activityInfo; | |
1469 | foreach ($tplParams['activity']['fields'] as $k => $val) { | |
1470 | if (CRM_Utils_Array::value('label', $val) == ts('Subject')) { | |
1471 | $activitySubject = $val['value']; | |
1472 | break; | |
1473 | } | |
1474 | } | |
1475 | $session = CRM_Core_Session::singleton(); | |
1476 | // CRM-8926 If user is not logged in, use the activity creator as userID | |
1477 | if (!($userID = $session->get('userID'))) { | |
4322672b | 1478 | $userID = CRM_Activity_BAO_Activity::getSourceContactID($activityId); |
6a488035 TO |
1479 | } |
1480 | ||
1481 | //also create activities simultaneously of this copy. | |
1482 | $activityParams = array(); | |
1483 | ||
1484 | $activityParams['source_record_id'] = $activityId; | |
1485 | $activityParams['source_contact_id'] = $userID; | |
1486 | $activityParams['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name'); | |
1487 | $activityParams['activity_date_time'] = date('YmdHis'); | |
1488 | $activityParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name'); | |
1489 | $activityParams['medium_id'] = CRM_Core_OptionGroup::getValue('encounter_medium', 'email', 'name'); | |
1490 | $activityParams['case_id'] = $caseId; | |
1491 | $activityParams['is_auto'] = 0; | |
1492 | $activityParams['target_id'] = $clientId; | |
1493 | ||
1494 | $tplParams['activitySubject'] = $activitySubject; | |
1495 | ||
1496 | // if it’s a case activity, add hashed id to the template (CRM-5916) | |
1497 | if ($caseId) { | |
1498 | $tplParams['idHash'] = substr(sha1(CIVICRM_SITE_KEY . $caseId), 0, 7); | |
1499 | } | |
1500 | ||
1501 | $result = array(); | |
1502 | list($name, $address) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); | |
1503 | ||
1504 | $receiptFrom = "$name <$address>"; | |
1505 | ||
1506 | $recordedActivityParams = array(); | |
1507 | ||
1508 | foreach ($contacts as $mail => $info) { | |
1509 | $tplParams['contact'] = $info; | |
1510 | self::buildPermissionLinks($tplParams, $activityParams); | |
1511 | ||
21827a41 | 1512 | $displayName = CRM_Utils_Array::value('display_name', $info); |
6a488035 | 1513 | |
c6327d7d | 1514 | list($result[CRM_Utils_Array::value('contact_id', $info)], $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
6a488035 TO |
1515 | array( |
1516 | 'groupName' => 'msg_tpl_workflow_case', | |
1517 | 'valueName' => 'case_activity', | |
21827a41 | 1518 | 'contactId' => CRM_Utils_Array::value('contact_id', $info), |
6a488035 TO |
1519 | 'tplParams' => $tplParams, |
1520 | 'from' => $receiptFrom, | |
1521 | 'toName' => $displayName, | |
1522 | 'toEmail' => $mail, | |
1523 | 'attachments' => $attachments, | |
1524 | ) | |
1525 | ); | |
1526 | ||
1527 | $activityParams['subject'] = $activitySubject . ' - copy sent to ' . $displayName; | |
1528 | $activityParams['details'] = $message; | |
1529 | ||
21827a41 | 1530 | if (!empty($result[$info['contact_id']])) { |
6a488035 TO |
1531 | /* |
1532 | * Really only need to record one activity with all the targets combined. | |
1533 | * Originally the template was going to possibly have different content, e.g. depending on permissions, | |
1534 | * but it's always the same content at the moment. | |
1535 | */ | |
1536 | if (empty($recordedActivityParams)) { | |
1537 | $recordedActivityParams = $activityParams; | |
1538 | } | |
1539 | else { | |
1540 | $recordedActivityParams['subject'] .= "; $displayName"; | |
1541 | } | |
1542 | $recordedActivityParams['target_contact_id'][] = $info['contact_id']; | |
1543 | } | |
1544 | else { | |
21827a41 | 1545 | unset($result[CRM_Utils_Array::value('contact_id', $info)]); |
6a488035 TO |
1546 | } |
1547 | } | |
1548 | ||
1549 | if (!empty($recordedActivityParams)) { | |
1550 | $activity = CRM_Activity_BAO_Activity::create($recordedActivityParams); | |
1551 | ||
1552 | //create case_activity record if its case activity. | |
1553 | if ($caseId) { | |
1554 | $caseParams = array( | |
1555 | 'activity_id' => $activity->id, | |
1556 | 'case_id' => $caseId, | |
1557 | ); | |
1558 | self::processCaseActivity($caseParams); | |
1559 | } | |
1560 | } | |
1561 | ||
1562 | return $result; | |
1563 | } | |
1564 | ||
1565 | /** | |
1566 | * Retrieve count of activities having a particular type, and | |
1567 | * associated with a particular case. | |
1568 | * | |
64bd5a0e TO |
1569 | * @param int $caseId |
1570 | * ID of the case. | |
1571 | * @param int $activityTypeId | |
1572 | * ID of the activity type. | |
6a488035 TO |
1573 | * |
1574 | * @return array | |
1575 | * | |
6a488035 TO |
1576 | * |
1577 | */ | |
00be9182 | 1578 | public static function getCaseActivityCount($caseId, $activityTypeId) { |
e96f025a | 1579 | $queryParam = array( |
1580 | 1 => array($caseId, 'Integer'), | |
6a488035 TO |
1581 | 2 => array($activityTypeId, 'Integer'), |
1582 | ); | |
1583 | $query = "SELECT count(ca.id) as countact | |
1584 | FROM civicrm_activity ca | |
1585 | INNER JOIN civicrm_case_activity cca ON ca.id = cca.activity_id | |
1586 | WHERE ca.activity_type_id = %2 | |
1587 | AND cca.case_id = %1 | |
1588 | AND ca.is_deleted = 0"; | |
1589 | ||
1590 | $dao = CRM_Core_DAO::executeQuery($query, $queryParam); | |
1591 | if ($dao->fetch()) { | |
1592 | return $dao->countact; | |
1593 | } | |
1594 | ||
1595 | return FALSE; | |
1596 | } | |
1597 | ||
1598 | /** | |
1599 | * Create an activity for a case via email | |
1600 | * | |
64bd5a0e TO |
1601 | * @param int $file |
1602 | * Email sent. | |
6a488035 | 1603 | * |
77b97be7 | 1604 | * @return array|void $activity object of newly creted activity via email@access public |
6a488035 | 1605 | */ |
00be9182 | 1606 | public static function recordActivityViaEmail($file) { |
6a488035 TO |
1607 | if (!file_exists($file) || |
1608 | !is_readable($file) | |
1609 | ) { | |
1610 | return CRM_Core_Error::fatal(ts('File %1 does not exist or is not readable', | |
d6f468d3 KJ |
1611 | array(1 => $file) |
1612 | )); | |
6a488035 TO |
1613 | } |
1614 | ||
1615 | $result = CRM_Utils_Mail_Incoming::parse($file); | |
1616 | if ($result['is_error']) { | |
1617 | return $result; | |
1618 | } | |
1619 | ||
1620 | foreach ($result['to'] as $to) { | |
1621 | $caseId = NULL; | |
1622 | ||
1623 | $emailPattern = '/^([A-Z0-9._%+-]+)\+([\d]+)@[A-Z0-9.-]+\.[A-Z]{2,4}$/i'; | |
1624 | $replacement = preg_replace($emailPattern, '$2', $to['email']); | |
1625 | ||
1626 | if ($replacement !== $to['email']) { | |
1627 | $caseId = $replacement; | |
1628 | //if caseId is invalid, return as error file | |
1629 | if (!CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseId, 'id')) { | |
1630 | return CRM_Core_Error::createAPIError(ts('Invalid case ID ( %1 ) in TO: field.', | |
d6f468d3 KJ |
1631 | array(1 => $caseId) |
1632 | )); | |
6a488035 TO |
1633 | } |
1634 | } | |
1635 | else { | |
1636 | continue; | |
1637 | } | |
1638 | ||
1639 | // TODO: May want to replace this with a call to getRelatedAndGlobalContacts() when this feature is revisited. | |
1640 | // (Or for efficiency call the global one outside the loop and then union with this each time.) | |
1641 | $contactDetails = self::getRelatedContacts($caseId, TRUE); | |
1642 | ||
a7488080 | 1643 | if (!empty($contactDetails[$result['from']['id']])) { |
6a488035 TO |
1644 | $params = array(); |
1645 | $params['subject'] = $result['subject']; | |
1646 | $params['activity_date_time'] = $result['date']; | |
1647 | $params['details'] = $result['body']; | |
1648 | $params['source_contact_id'] = $result['from']['id']; | |
1649 | $params['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', | |
1650 | 'Completed', | |
1651 | 'name' | |
1652 | ); | |
1653 | ||
1654 | $details = CRM_Case_PseudoConstant::caseActivityType(); | |
1655 | $matches = array(); | |
1656 | preg_match('/^\W+([a-zA-Z0-9_ ]+)(\W+)?\n/i', | |
1657 | $result['body'], $matches | |
1658 | ); | |
1659 | ||
1660 | if (!empty($matches) && isset($matches[1])) { | |
1661 | $activityType = trim($matches[1]); | |
1662 | if (isset($details[$activityType])) { | |
1663 | $params['activity_type_id'] = $details[$activityType]['id']; | |
1664 | } | |
1665 | } | |
1666 | if (!isset($params['activity_type_id'])) { | |
1667 | $params['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Inbound Email', 'name'); | |
1668 | } | |
1669 | ||
1670 | // create activity | |
1671 | $activity = CRM_Activity_BAO_Activity::create($params); | |
1672 | ||
1673 | $caseParams = array( | |
1674 | 'activity_id' => $activity->id, | |
1675 | 'case_id' => $caseId, | |
1676 | ); | |
1677 | self::processCaseActivity($caseParams); | |
1678 | } | |
1679 | else { | |
1680 | return CRM_Core_Error::createAPIError(ts('FROM email contact %1 doesn\'t have a relationship to the referenced case.', | |
d6f468d3 KJ |
1681 | array(1 => $result['from']['email']) |
1682 | )); | |
6a488035 TO |
1683 | } |
1684 | } | |
1685 | } | |
1686 | ||
1687 | /** | |
100fef9d | 1688 | * Retrieve the scheduled activity type and date |
6a488035 | 1689 | * |
64bd5a0e TO |
1690 | * @param array $cases |
1691 | * Array of contact and case id. | |
77b97be7 EM |
1692 | * |
1693 | * @param string $type | |
6a488035 TO |
1694 | * |
1695 | * @return array $activityInfo Array of scheduled activity type and date | |
1696 | * | |
6a488035 TO |
1697 | * |
1698 | * @static | |
1699 | */ | |
00be9182 | 1700 | public static function getNextScheduledActivity($cases, $type = 'upcoming') { |
6a488035 TO |
1701 | $session = CRM_Core_Session::singleton(); |
1702 | $userID = $session->get('userID'); | |
1703 | ||
1704 | $caseID = implode(',', $cases['case_id']); | |
1705 | $contactID = implode(',', $cases['contact_id']); | |
1706 | ||
1707 | $condition = " | |
1708 | AND civicrm_case_contact.contact_id IN( {$contactID} ) | |
1709 | AND civicrm_case.id IN( {$caseID}) | |
1710 | AND civicrm_case.is_deleted = {$cases['case_deleted']}"; | |
1711 | ||
1712 | $query = self::getCaseActivityQuery($type, $userID, $condition, $cases['case_deleted']); | |
1713 | ||
1714 | $res = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
1715 | ||
1716 | $activityInfo = array(); | |
1717 | while ($res->fetch()) { | |
1718 | if ($type == 'upcoming') { | |
1719 | $activityInfo[$res->case_id]['date'] = $res->case_scheduled_activity_date; | |
1720 | $activityInfo[$res->case_id]['type'] = $res->case_scheduled_activity_type; | |
1721 | } | |
1722 | else { | |
1723 | $activityInfo[$res->case_id]['date'] = $res->case_recent_activity_date; | |
1724 | $activityInfo[$res->case_id]['type'] = $res->case_recent_activity_type; | |
1725 | } | |
1726 | } | |
1727 | ||
1728 | return $activityInfo; | |
1729 | } | |
1730 | ||
1731 | /** | |
100fef9d | 1732 | * Combine all the exportable fields from the lower levels object |
6a488035 TO |
1733 | * |
1734 | * @return array array of exportable Fields | |
6a488035 TO |
1735 | * @static |
1736 | */ | |
00be9182 | 1737 | public static function &exportableFields() { |
6a488035 TO |
1738 | if (!self::$_exportableFields) { |
1739 | if (!self::$_exportableFields) { | |
1740 | self::$_exportableFields = array(); | |
1741 | } | |
1742 | ||
e96f025a | 1743 | $fields = CRM_Case_DAO_Case::export(); |
6a488035 | 1744 | $fields['case_role'] = array('title' => ts('Role in Case')); |
e96f025a | 1745 | $fields['case_type'] = array( |
1746 | 'title' => ts('Case Type'), | |
6a488035 TO |
1747 | 'name' => 'case_type', |
1748 | ); | |
e96f025a | 1749 | $fields['case_status'] = array( |
1750 | 'title' => ts('Case Status'), | |
6a488035 TO |
1751 | 'name' => 'case_status', |
1752 | ); | |
1753 | ||
1754 | self::$_exportableFields = $fields; | |
1755 | } | |
1756 | return self::$_exportableFields; | |
1757 | } | |
1758 | ||
1759 | /** | |
1760 | * Restore the record that are associated with this case | |
1761 | * | |
64bd5a0e TO |
1762 | * @param int $caseId |
1763 | * Id of the case to restore. | |
6a488035 TO |
1764 | * |
1765 | * @return true if success. | |
6a488035 TO |
1766 | * @static |
1767 | */ | |
00be9182 | 1768 | public static function restoreCase($caseId) { |
6a488035 TO |
1769 | //restore activities |
1770 | $activities = self::getCaseActivityDates($caseId); | |
1771 | if ($activities) { | |
1772 | foreach ($activities as $value) { | |
1773 | CRM_Activity_BAO_Activity::restoreActivity($value); | |
1774 | } | |
1775 | } | |
1776 | //restore case | |
e96f025a | 1777 | $case = new CRM_Case_DAO_Case(); |
1778 | $case->id = $caseId; | |
6a488035 TO |
1779 | $case->is_deleted = 0; |
1780 | $case->save(); | |
1781 | ||
1782 | //CRM-7364, enable relationships | |
1783 | self::enableDisableCaseRelationships($caseId, TRUE); | |
1784 | return TRUE; | |
1785 | } | |
1786 | ||
4c6ce474 EM |
1787 | /** |
1788 | * @param $groupInfo | |
1789 | * @param null $sort | |
1790 | * @param null $showLinks | |
1791 | * @param bool $returnOnlyCount | |
1792 | * @param int $offset | |
1793 | * @param int $rowCount | |
1794 | * | |
1795 | * @return array | |
1796 | */ | |
00be9182 | 1797 | public static function getGlobalContacts(&$groupInfo, $sort = NULL, $showLinks = NULL, $returnOnlyCount = FALSE, $offset = 0, $rowCount = 25) { |
6a488035 TO |
1798 | $globalContacts = array(); |
1799 | ||
1800 | $settingsProcessor = new CRM_Case_XMLProcessor_Settings(); | |
1801 | $settings = $settingsProcessor->run(); | |
1802 | if (!empty($settings)) { | |
1803 | $groupInfo['name'] = $settings['groupname']; | |
1804 | if ($groupInfo['name']) { | |
1805 | $searchParams = array('name' => $groupInfo['name']); | |
1806 | $results = array(); | |
1807 | CRM_Contact_BAO_Group::retrieve($searchParams, $results); | |
1808 | if ($results) { | |
e96f025a | 1809 | $groupInfo['id'] = $results['id']; |
6a488035 | 1810 | $groupInfo['title'] = $results['title']; |
e96f025a | 1811 | $params = array(array('group', 'IN', array($groupInfo['id'] => 1), 0, 0)); |
e96f025a | 1812 | $return = array('contact_id' => 1, 'sort_name' => 1, 'display_name' => 1, 'email' => 1, 'phone' => 1); |
d79c94d5 | 1813 | list($globalContacts) = CRM_Contact_BAO_Query::apiQuery($params, $return, NULL, $sort, $offset, $rowCount, TRUE, $returnOnlyCount); |
6a488035 TO |
1814 | |
1815 | if ($returnOnlyCount) { | |
1816 | return $globalContacts; | |
1817 | } | |
1818 | ||
1819 | if ($showLinks) { | |
e96f025a | 1820 | foreach ($globalContacts as $idx => $contact) { |
d79c94d5 | 1821 | $globalContacts[$idx]['sort_name'] = '<a href="' . CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$contact['contact_id']}") . '">' . $contact['sort_name'] . '</a>'; |
6a488035 TO |
1822 | } |
1823 | } | |
1824 | } | |
1825 | } | |
1826 | } | |
1827 | return $globalContacts; | |
1828 | } | |
1829 | ||
4c6ce474 | 1830 | /** |
100fef9d CW |
1831 | * Convenience function to get both case contacts and global in one array |
1832 | * @param int $caseId | |
4c6ce474 EM |
1833 | * |
1834 | * @return array | |
1835 | */ | |
00be9182 | 1836 | public static function getRelatedAndGlobalContacts($caseId) { |
6a488035 TO |
1837 | $relatedContacts = self::getRelatedContacts($caseId); |
1838 | ||
1839 | $groupInfo = array(); | |
1840 | $globalContacts = self::getGlobalContacts($groupInfo); | |
1841 | ||
1842 | //unset values which are not required. | |
1843 | foreach ($globalContacts as $k => & $v) { | |
1844 | unset($v['email_id']); | |
1845 | unset($v['group_contact_id']); | |
1846 | unset($v['status']); | |
1847 | unset($v['phone']); | |
1848 | $v['role'] = $groupInfo['title']; | |
1849 | } | |
1850 | //include multiple listings for the same contact/different roles. | |
1851 | $relatedGlobalContacts = array_merge($relatedContacts, $globalContacts); | |
1852 | return $relatedGlobalContacts; | |
1853 | } | |
1854 | ||
1855 | /** | |
100fef9d | 1856 | * Get Case ActivitiesDueDates with given criteria. |
6a488035 | 1857 | * |
64bd5a0e TO |
1858 | * @param int $caseID |
1859 | * Case id. | |
1860 | * @param array $criteriaParams | |
1861 | * Given criteria. | |
1862 | * @param bool $latestDate | |
1863 | * If set newest or oldest date is selceted. | |
6a488035 TO |
1864 | * |
1865 | * @return returns case activities due dates | |
1866 | * | |
1867 | * @static | |
1868 | */ | |
00be9182 | 1869 | public static function getCaseActivityDates($caseID, $criteriaParams = array(), $latestDate = FALSE) { |
e96f025a | 1870 | $values = array(); |
6a488035 | 1871 | $selectDate = " ca.activity_date_time"; |
e96f025a | 1872 | $where = $groupBy = ' '; |
6a488035 TO |
1873 | |
1874 | if (!$caseID) { | |
1875 | return; | |
1876 | } | |
1877 | ||
1878 | if ($latestDate) { | |
a7488080 | 1879 | if (!empty($criteriaParams['activity_type_id'])) { |
6a488035 TO |
1880 | $where .= " AND ca.activity_type_id = " . CRM_Utils_Type::escape($criteriaParams['activity_type_id'], 'Integer'); |
1881 | $where .= " AND ca.is_current_revision = 1"; | |
1882 | $groupBy .= " GROUP BY ca.activity_type_id"; | |
1883 | } | |
1884 | ||
a7488080 | 1885 | if (!empty($criteriaParams['newest'])) { |
6a488035 TO |
1886 | $selectDate = " max(ca.activity_date_time) "; |
1887 | } | |
1888 | else { | |
1889 | $selectDate = " min(ca.activity_date_time) "; | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | $query = "SELECT ca.id, {$selectDate} as activity_date | |
1894 | FROM civicrm_activity ca | |
1895 | LEFT JOIN civicrm_case_activity cca ON cca.activity_id = ca.id LEFT JOIN civicrm_case cc ON cc.id = cca.case_id | |
1896 | WHERE cc.id = %1 {$where} {$groupBy}"; | |
1897 | ||
1898 | $params = array(1 => array($caseID, 'Integer')); | |
1899 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1900 | ||
1901 | while ($dao->fetch()) { | |
1902 | $values[$dao->id]['id'] = $dao->id; | |
1903 | $values[$dao->id]['activity_date'] = $dao->activity_date; | |
1904 | } | |
1905 | $dao->free(); | |
1906 | return $values; | |
1907 | } | |
1908 | ||
1909 | /** | |
100fef9d | 1910 | * Create activities when Case or Other roles assigned/modified/deleted. |
6a488035 | 1911 | * |
100fef9d | 1912 | * @param int $caseId |
64bd5a0e TO |
1913 | * @param int $relationshipId |
1914 | * Relationship id. | |
1915 | * @param int $relContactId | |
1916 | * Case role assignee contactId. | |
100fef9d | 1917 | * @param int $contactId |
77b97be7 | 1918 | * |
6a488035 TO |
1919 | * @return void on success creates activity and case activity |
1920 | * | |
1921 | * @static | |
1922 | */ | |
00be9182 | 1923 | public static function createCaseRoleActivity($caseId, $relationshipId, $relContactId = NULL, $contactId = NULL) { |
6a488035 TO |
1924 | if (!$caseId || !$relationshipId || empty($relationshipId)) { |
1925 | return; | |
1926 | } | |
1927 | ||
1928 | $queryParam = array(); | |
1929 | if (is_array($relationshipId)) { | |
1930 | $relationshipId = implode(',', $relationshipId); | |
1931 | $relationshipClause = " civicrm_relationship.id IN ($relationshipId)"; | |
1932 | } | |
1933 | else { | |
1934 | $relationshipClause = " civicrm_relationship.id = %1"; | |
1935 | $queryParam[1] = array($relationshipId, 'Positive'); | |
1936 | } | |
1937 | ||
1938 | $query = " | |
1939 | SELECT cc.display_name as clientName, | |
1940 | cca.display_name as assigneeContactName, | |
1941 | civicrm_relationship.case_id as caseId, | |
1942 | civicrm_relationship_type.label_a_b as relation_a_b, | |
1943 | civicrm_relationship_type.label_b_a as relation_b_a, | |
1944 | civicrm_relationship.contact_id_b as rel_contact_id, | |
1945 | civicrm_relationship.contact_id_a as assign_contact_id | |
1946 | FROM civicrm_relationship_type, civicrm_relationship | |
1947 | LEFT JOIN civicrm_contact cc ON cc.id = civicrm_relationship.contact_id_b | |
1948 | LEFT JOIN civicrm_contact cca ON cca.id = civicrm_relationship.contact_id_a | |
1949 | WHERE civicrm_relationship.relationship_type_id = civicrm_relationship_type.id AND {$relationshipClause}"; | |
1950 | ||
1951 | $dao = CRM_Core_DAO::executeQuery($query, $queryParam); | |
1952 | ||
1953 | while ($dao->fetch()) { | |
1954 | //to get valid assignee contact(s). | |
1955 | if (isset($dao->caseId) || $dao->rel_contact_id != $contactId) { | |
1956 | $caseRelationship = $dao->relation_a_b; | |
1957 | $assigneContactName = $dao->clientName; | |
1958 | $assigneContactIds[$dao->rel_contact_id] = $dao->rel_contact_id; | |
1959 | } | |
1960 | else { | |
1961 | $caseRelationship = $dao->relation_b_a; | |
1962 | $assigneContactName = $dao->assigneeContactName; | |
1963 | $assigneContactIds[$dao->assign_contact_id] = $dao->assign_contact_id; | |
1964 | } | |
1965 | } | |
1966 | ||
1967 | $session = CRM_Core_Session::singleton(); | |
1968 | $activityParams = array( | |
1969 | 'source_contact_id' => $session->get('userID'), | |
1970 | 'subject' => $caseRelationship . ' : ' . $assigneContactName, | |
1971 | 'activity_date_time' => date('YmdHis'), | |
1972 | 'status_id' => CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name'), | |
1973 | ); | |
1974 | ||
1975 | //if $relContactId is passed, role is added or modified. | |
1976 | if (!empty($relContactId)) { | |
1977 | $activityParams['assignee_contact_id'] = $assigneContactIds; | |
1978 | ||
1979 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', | |
1980 | 'Assign Case Role', | |
1981 | 'name' | |
1982 | ); | |
1983 | } | |
1984 | else { | |
1985 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', | |
1986 | 'Remove Case Role', | |
1987 | 'name' | |
1988 | ); | |
1989 | } | |
1990 | ||
1991 | $activityParams['activity_type_id'] = $activityTypeID; | |
1992 | ||
1993 | $activity = CRM_Activity_BAO_Activity::create($activityParams); | |
1994 | ||
1995 | //create case_activity record. | |
1996 | $caseParams = array( | |
1997 | 'activity_id' => $activity->id, | |
1998 | 'case_id' => $caseId, | |
1999 | ); | |
2000 | ||
2001 | CRM_Case_BAO_Case::processCaseActivity($caseParams); | |
2002 | } | |
2003 | ||
2004 | /** | |
100fef9d | 2005 | * Get case manger |
6a488035 TO |
2006 | * contact which is assigned a case role of case manager. |
2007 | * | |
64bd5a0e TO |
2008 | * @param int $caseType |
2009 | * Case type. | |
2010 | * @param int $caseId | |
2011 | * Case id. | |
6a488035 TO |
2012 | * |
2013 | * @return array $caseManagerContact array of contact on success otherwise empty | |
2014 | * | |
2015 | * @static | |
2016 | */ | |
00be9182 | 2017 | public static function getCaseManagerContact($caseType, $caseId) { |
6a488035 TO |
2018 | if (!$caseType || !$caseId) { |
2019 | return; | |
2020 | } | |
2021 | ||
2022 | $caseManagerContact = array(); | |
2023 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
2024 | ||
2025 | $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseType); | |
2026 | ||
2027 | if (!empty($managerRoleId)) { | |
2028 | $managerRoleQuery = " | |
2029 | SELECT civicrm_contact.id as casemanager_id, | |
2030 | civicrm_contact.sort_name as casemanager | |
2031 | FROM civicrm_contact | |
2032 | LEFT JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = civicrm_contact.id AND civicrm_relationship.relationship_type_id = %1) | |
2033 | LEFT JOIN civicrm_case ON civicrm_case.id = civicrm_relationship.case_id | |
2034 | WHERE civicrm_case.id = %2"; | |
2035 | ||
2036 | $managerRoleParams = array( | |
2037 | 1 => array($managerRoleId, 'Integer'), | |
2038 | 2 => array($caseId, 'Integer'), | |
2039 | ); | |
2040 | ||
2041 | $dao = CRM_Core_DAO::executeQuery($managerRoleQuery, $managerRoleParams); | |
2042 | if ($dao->fetch()) { | |
2043 | $caseManagerContact['casemanager_id'] = $dao->casemanager_id; | |
2044 | $caseManagerContact['casemanager'] = $dao->casemanager; | |
2045 | } | |
2046 | } | |
2047 | ||
2048 | return $caseManagerContact; | |
2049 | } | |
2050 | ||
2051 | /** | |
2052 | * Get all cases with no end dates | |
2053 | * | |
77b97be7 EM |
2054 | * @param array $params |
2055 | * @param array $excludeCaseIds | |
2056 | * @param bool $excludeDeleted | |
2057 | * | |
6a488035 TO |
2058 | * @return array of case and related data keyed on case id |
2059 | */ | |
00be9182 | 2060 | public static function getUnclosedCases($params = array(), $excludeCaseIds = array(), $excludeDeleted = TRUE, $includeClosed = FALSE) { |
6a488035 | 2061 | //params from ajax call. |
3c0b6a40 | 2062 | $where = array($includeClosed ? '(1)' : '(ca.end_date is null)'); |
6a488035 | 2063 | if ($caseType = CRM_Utils_Array::value('case_type', $params)) { |
8ffdec17 | 2064 | $where[] = "( civicrm_case_type.title LIKE '%$caseType%' )"; |
6a488035 TO |
2065 | } |
2066 | if ($sortName = CRM_Utils_Array::value('sort_name', $params)) { | |
e96f025a | 2067 | $config = CRM_Core_Config::singleton(); |
2068 | $search = ($config->includeWildCardInName) ? "%$sortName%" : "$sortName%"; | |
6a488035 TO |
2069 | $where[] = "( sort_name LIKE '$search' )"; |
2070 | } | |
bd343dcc CW |
2071 | if ($cid = CRM_Utils_Array::value('contact_id', $params)) { |
2072 | $where[] = " c.id = $cid "; | |
2073 | } | |
6a488035 TO |
2074 | if (is_array($excludeCaseIds) && |
2075 | !CRM_Utils_System::isNull($excludeCaseIds) | |
2076 | ) { | |
2077 | $where[] = ' ( ca.id NOT IN ( ' . implode(',', $excludeCaseIds) . ' ) ) '; | |
2078 | } | |
2079 | if ($excludeDeleted) { | |
2080 | $where[] = ' ( ca.is_deleted = 0 OR ca.is_deleted IS NULL ) '; | |
2081 | } | |
2082 | ||
2083 | //filter for permissioned cases. | |
2084 | $filterCases = array(); | |
2085 | $doFilterCases = FALSE; | |
2086 | if (!CRM_Core_Permission::check('access all cases and activities')) { | |
2087 | $doFilterCases = TRUE; | |
e96f025a | 2088 | $session = CRM_Core_Session::singleton(); |
2089 | $filterCases = CRM_Case_BAO_Case::getCases(FALSE, $session->get('userID')); | |
6a488035 TO |
2090 | } |
2091 | $whereClause = implode(' AND ', $where); | |
2092 | ||
2093 | $limitClause = ''; | |
2094 | if ($limit = CRM_Utils_Array::value('limit', $params)) { | |
2095 | $limitClause = "LIMIT 0, $limit"; | |
2096 | } | |
2097 | ||
2098 | $query = " | |
2099 | SELECT c.id as contact_id, | |
2100 | c.sort_name, | |
2101 | ca.id, | |
2102 | ca.subject as case_subject, | |
8ffdec17 | 2103 | civicrm_case_type.title as case_type, |
3c0b6a40 CW |
2104 | ca.start_date as start_date, |
2105 | ca.end_date as end_date, | |
2106 | ca.status_id | |
6a488035 TO |
2107 | FROM civicrm_case ca INNER JOIN civicrm_case_contact cc ON ca.id=cc.case_id |
2108 | INNER JOIN civicrm_contact c ON cc.contact_id=c.id | |
8ffdec17 | 2109 | INNER JOIN civicrm_case_type ON ca.case_type_id = civicrm_case_type.id |
6a488035 | 2110 | WHERE {$whereClause} |
3c0b6a40 | 2111 | ORDER BY c.sort_name, ca.end_date |
6a488035 TO |
2112 | {$limitClause} |
2113 | "; | |
2114 | $dao = CRM_Core_DAO::executeQuery($query); | |
16fb2340 C |
2115 | $statuses = CRM_Case_BAO_Case::buildOptions('status_id', 'create'); |
2116 | ||
6a488035 TO |
2117 | $unclosedCases = array(); |
2118 | while ($dao->fetch()) { | |
2119 | if ($doFilterCases && !array_key_exists($dao->id, $filterCases)) { | |
2120 | continue; | |
2121 | } | |
2122 | $unclosedCases[$dao->id] = array( | |
2123 | 'sort_name' => $dao->sort_name, | |
2124 | 'case_type' => $dao->case_type, | |
2125 | 'contact_id' => $dao->contact_id, | |
2126 | 'start_date' => $dao->start_date, | |
3c0b6a40 | 2127 | 'end_date' => $dao->end_date, |
6a488035 | 2128 | 'case_subject' => $dao->case_subject, |
3c0b6a40 | 2129 | 'case_status' => $statuses[$dao->status_id], |
6a488035 TO |
2130 | ); |
2131 | } | |
2132 | $dao->free(); | |
2133 | ||
2134 | return $unclosedCases; | |
2135 | } | |
2136 | ||
4c6ce474 | 2137 | /** |
100fef9d | 2138 | * @param int $contactId |
4c6ce474 EM |
2139 | * @param bool $excludeDeleted |
2140 | * | |
2141 | * @return null|string | |
2142 | */ | |
00be9182 | 2143 | public static function caseCount($contactId = NULL, $excludeDeleted = TRUE) { |
6a488035 TO |
2144 | $whereConditions = array(); |
2145 | if ($excludeDeleted) { | |
2146 | $whereConditions[] = "( civicrm_case.is_deleted = 0 OR civicrm_case.is_deleted IS NULL )"; | |
2147 | } | |
2148 | if ($contactId) { | |
2149 | $whereConditions[] = "civicrm_case_contact.contact_id = {$contactId}"; | |
2150 | } | |
2151 | if (!CRM_Core_Permission::check('access all cases and activities')) { | |
2152 | static $accessibleCaseIds; | |
2153 | if (!is_array($accessibleCaseIds)) { | |
2154 | $session = CRM_Core_Session::singleton(); | |
ef0ae32d | 2155 | $accessibleCaseIds = array_keys(self::getCases(FALSE, $session->get('userID'), 'any')); |
6a488035 TO |
2156 | } |
2157 | //no need of further processing. | |
2158 | if (empty($accessibleCaseIds)) { | |
2159 | return 0; | |
2160 | } | |
2161 | $whereConditions[] = "( civicrm_case.id in (" . implode(',', $accessibleCaseIds) . ") )"; | |
2162 | } | |
2163 | ||
2164 | $whereClause = ''; | |
2165 | if (!empty($whereConditions)) { | |
2166 | $whereClause = "WHERE " . implode(' AND ', $whereConditions); | |
2167 | } | |
2168 | ||
2169 | $query = " | |
2170 | SELECT count( civicrm_case.id ) | |
2171 | FROM civicrm_case | |
2172 | LEFT JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.case_id ) | |
2173 | {$whereClause}"; | |
2174 | ||
2175 | return CRM_Core_DAO::singleValueQuery($query); | |
2176 | } | |
2177 | ||
2178 | /** | |
2179 | * Retrieve cases related to particular contact. | |
2180 | * | |
64bd5a0e TO |
2181 | * @param int $contactId |
2182 | * Contact id. | |
2183 | * @param bool $excludeDeleted | |
2184 | * Do not include deleted cases. | |
6a488035 TO |
2185 | * |
2186 | * @return an array of cases. | |
2187 | * | |
6a488035 | 2188 | */ |
00be9182 | 2189 | public static function getContactCases($contactId, $excludeDeleted = TRUE) { |
6a488035 TO |
2190 | $cases = array(); |
2191 | if (!$contactId) { | |
2192 | return $cases; | |
2193 | } | |
2194 | ||
2195 | $whereClause = "civicrm_case_contact.contact_id = %1"; | |
2196 | if ($excludeDeleted) { | |
2197 | $whereClause .= " AND ( civicrm_case.is_deleted = 0 OR civicrm_case.is_deleted IS NULL )"; | |
2198 | } | |
2199 | ||
2200 | $query = " | |
8ffdec17 | 2201 | SELECT civicrm_case.id, civicrm_case_type.title as case_type, civicrm_case.start_date |
6a488035 TO |
2202 | FROM civicrm_case |
2203 | INNER JOIN civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.case_id ) | |
8ffdec17 | 2204 | LEFT JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id |
6a488035 TO |
2205 | WHERE {$whereClause}"; |
2206 | ||
2207 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($contactId, 'Integer'))); | |
2208 | while ($dao->fetch()) { | |
2209 | $cases[$dao->id] = array( | |
2210 | 'case_id' => $dao->id, | |
2211 | 'case_type' => $dao->case_type, | |
2212 | 'case_start_date' => $dao->start_date, | |
2213 | ); | |
2214 | } | |
2215 | $dao->free(); | |
2216 | ||
2217 | return $cases; | |
2218 | } | |
2219 | ||
2220 | /** | |
2221 | * Retrieve related cases for give case. | |
2222 | * | |
64bd5a0e TO |
2223 | * @param int $mainCaseId |
2224 | * Id of main case. | |
2225 | * @param int $contactId | |
2226 | * Id of contact. | |
2227 | * @param bool $excludeDeleted | |
2228 | * Do not include deleted cases. | |
6a488035 TO |
2229 | * |
2230 | * @return an array of related cases. | |
2231 | * | |
6a488035 | 2232 | */ |
00be9182 | 2233 | public static function getRelatedCases($mainCaseId, $contactId, $excludeDeleted = TRUE) { |
6a488035 TO |
2234 | //FIXME : do check for permissions. |
2235 | ||
2236 | $relatedCases = array(); | |
2237 | if (!$mainCaseId || !$contactId) { | |
2238 | return $relatedCases; | |
2239 | } | |
2240 | ||
2241 | $linkActType = array_search('Link Cases', | |
2242 | CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name') | |
2243 | ); | |
2244 | if (!$linkActType) { | |
2245 | return $relatedCases; | |
2246 | } | |
2247 | ||
2248 | $whereClause = "mainCase.id = %2"; | |
2249 | if ($excludeDeleted) { | |
2250 | $whereClause .= " AND ( relAct.is_deleted = 0 OR relAct.is_deleted IS NULL )"; | |
2251 | } | |
2252 | ||
2253 | //1. first fetch related case ids. | |
2254 | $query = " | |
2255 | SELECT relCaseAct.case_id | |
2256 | FROM civicrm_case mainCase | |
2257 | INNER JOIN civicrm_case_activity mainCaseAct ON (mainCaseAct.case_id = mainCase.id) | |
2258 | INNER JOIN civicrm_activity mainAct ON (mainCaseAct.activity_id = mainAct.id AND mainAct.activity_type_id = %1) | |
2259 | INNER JOIN civicrm_case_activity relCaseAct ON (relCaseAct.activity_id = mainAct.id AND mainCaseAct.id != relCaseAct.id) | |
2260 | INNER JOIN civicrm_activity relAct ON (relCaseAct.activity_id = relAct.id AND relAct.activity_type_id = %1) | |
2261 | WHERE $whereClause"; | |
2262 | ||
2263 | $dao = CRM_Core_DAO::executeQuery($query, array( | |
2264 | 1 => array($linkActType, 'Integer'), | |
2265 | 2 => array($mainCaseId, 'Integer'), | |
2266 | )); | |
2267 | $relatedCaseIds = array(); | |
2268 | while ($dao->fetch()) { | |
2269 | $relatedCaseIds[$dao->case_id] = $dao->case_id; | |
2270 | } | |
2271 | $dao->free(); | |
2272 | ||
2273 | // there are no related cases. | |
2274 | if (empty($relatedCaseIds)) { | |
2275 | return $relatedCases; | |
2276 | } | |
2277 | ||
2278 | $whereClause = 'relCase.id IN ( ' . implode(',', $relatedCaseIds) . ' )'; | |
2279 | if ($excludeDeleted) { | |
2280 | $whereClause .= " AND ( relCase.is_deleted = 0 OR relCase.is_deleted IS NULL )"; | |
2281 | } | |
2282 | ||
2283 | //filter for permissioned cases. | |
2284 | $filterCases = array(); | |
2285 | $doFilterCases = FALSE; | |
2286 | if (!CRM_Core_Permission::check('access all cases and activities')) { | |
2287 | $doFilterCases = TRUE; | |
e96f025a | 2288 | $session = CRM_Core_Session::singleton(); |
2289 | $filterCases = CRM_Case_BAO_Case::getCases(FALSE, $session->get('userID')); | |
6a488035 TO |
2290 | } |
2291 | ||
2292 | //2. fetch the details of related cases. | |
2293 | $query = " | |
2294 | SELECT relCase.id as id, | |
8ffdec17 | 2295 | civicrm_case_type.title as case_type, |
6a488035 TO |
2296 | client.display_name as client_name, |
2297 | client.id as client_id | |
2298 | FROM civicrm_case relCase | |
2299 | INNER JOIN civicrm_case_contact relCaseContact ON ( relCase.id = relCaseContact.case_id ) | |
2300 | INNER JOIN civicrm_contact client ON ( client.id = relCaseContact.contact_id ) | |
8ffdec17 | 2301 | LEFT JOIN civicrm_case_type ON relCase.case_type_id = civicrm_case_type.id |
6a488035 TO |
2302 | WHERE {$whereClause}"; |
2303 | ||
e96f025a | 2304 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
2305 | $contactViewUrl = CRM_Utils_System::url("civicrm/contact/view", "reset=1&cid="); |
2306 | $hasViewContact = CRM_Core_Permission::giveMeAllACLs(); | |
2307 | ||
2308 | while ($dao->fetch()) { | |
2309 | $caseView = NULL; | |
2310 | if (!$doFilterCases || array_key_exists($dao->id, $filterCases)) { | |
2311 | $caseViewStr = "reset=1&id={$dao->id}&cid={$dao->client_id}&action=view&context=case&selectedChild=case"; | |
2312 | $caseViewUrl = CRM_Utils_System::url("civicrm/contact/view/case", $caseViewStr); | |
6ce08914 | 2313 | $caseView = "<a class='action-item no-popup crm-hover-button' href='{$caseViewUrl}'>" . ts('View Case') . "</a>"; |
6a488035 TO |
2314 | } |
2315 | $clientView = $dao->client_name; | |
2316 | if ($hasViewContact) { | |
2317 | $clientView = "<a href='{$contactViewUrl}{$dao->client_id}'>$dao->client_name</a>"; | |
2318 | } | |
2319 | ||
2320 | $relatedCases[$dao->id] = array( | |
2321 | 'case_id' => $dao->id, | |
2322 | 'case_type' => $dao->case_type, | |
2323 | 'client_name' => $clientView, | |
2324 | 'links' => $caseView, | |
2325 | ); | |
2326 | } | |
2327 | $dao->free(); | |
2328 | ||
2329 | return $relatedCases; | |
2330 | } | |
2331 | ||
2332 | /** | |
2333 | * Merge two duplicate contacts' cases - follow CRM-5758 rules. | |
2334 | * | |
2335 | * @see CRM_Dedupe_Merger::cpTables() | |
2336 | * | |
2337 | * TODO: use the 3rd $sqls param to append sql statements rather than executing them here | |
2338 | */ | |
00be9182 | 2339 | public static function mergeContacts($mainContactId, $otherContactId) { |
6a488035 TO |
2340 | self::mergeCases($mainContactId, NULL, $otherContactId); |
2341 | } | |
2342 | ||
2343 | /** | |
2344 | * Function perform two task. | |
2345 | * 1. Merge two duplicate contacts cases - follow CRM-5758 rules. | |
2346 | * 2. Merge two cases of same contact - follow CRM-5598 rules. | |
2347 | * | |
64bd5a0e TO |
2348 | * @param int $mainContactId |
2349 | * Contact id of main contact record. | |
2350 | * @param int $mainCaseId | |
2351 | * Case id of main case record. | |
2352 | * @param int $otherContactId | |
2353 | * Contact id of record which is going to merge. | |
2354 | * @param int $otherCaseId | |
2355 | * Case id of record which is going to merge. | |
77b97be7 EM |
2356 | * |
2357 | * @param bool $changeClient | |
6a488035 | 2358 | * |
3d0d359e | 2359 | * @return integer|NULL |
6a488035 TO |
2360 | * @static |
2361 | */ | |
28d4d481 TO |
2362 | static function mergeCases( |
2363 | $mainContactId, $mainCaseId = NULL, $otherContactId = NULL, | |
2364 | $otherCaseId = NULL, $changeClient = FALSE) { | |
6a488035 TO |
2365 | $moveToTrash = TRUE; |
2366 | ||
2367 | $duplicateContacts = FALSE; | |
2368 | if ($mainContactId && $otherContactId && | |
2369 | $mainContactId != $otherContactId | |
2370 | ) { | |
2371 | $duplicateContacts = TRUE; | |
2372 | } | |
2373 | ||
2374 | $duplicateCases = FALSE; | |
2375 | if ($mainCaseId && $otherCaseId && | |
2376 | $mainCaseId != $otherCaseId | |
2377 | ) { | |
2378 | $duplicateCases = TRUE; | |
2379 | } | |
2380 | ||
2381 | $mainCaseIds = array(); | |
2382 | if (!$duplicateContacts && !$duplicateCases) { | |
2383 | return $mainCaseIds; | |
2384 | } | |
2385 | ||
2386 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name'); | |
2387 | $activityStatuses = CRM_Core_PseudoConstant::activityStatus('name'); | |
e7e657f0 | 2388 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
9e74e3ce | 2389 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
2390 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
2391 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
8ef12e64 | 2392 | |
6a488035 TO |
2393 | $processCaseIds = array($otherCaseId); |
2394 | if ($duplicateContacts && !$duplicateCases) { | |
2395 | if ($changeClient) { | |
2396 | $processCaseIds = array($mainCaseId); | |
2397 | } | |
2398 | else { | |
2399 | //get all case ids for other contact. | |
2400 | $processCaseIds = self::retrieveCaseIdsByContactId($otherContactId, TRUE); | |
2401 | } | |
2402 | if (!is_array($processCaseIds)) { | |
2403 | return; | |
2404 | } | |
2405 | } | |
2406 | ||
2407 | $session = CRM_Core_Session::singleton(); | |
2408 | $currentUserId = $session->get('userID'); | |
2409 | ||
02094cdb JJ |
2410 | CRM_Utils_Hook::pre_case_merge($mainContactId, $mainCaseId, $otherContactId, $otherCaseId, $changeClient); |
2411 | ||
6a488035 TO |
2412 | // copy all cases and connect to main contact id. |
2413 | foreach ($processCaseIds as $otherCaseId) { | |
2414 | if ($duplicateContacts) { | |
2415 | $mainCase = CRM_Core_DAO::copyGeneric('CRM_Case_DAO_Case', array('id' => $otherCaseId)); | |
2416 | $mainCaseId = $mainCase->id; | |
2417 | if (!$mainCaseId) { | |
2418 | continue; | |
2419 | } | |
8bd86283 DG |
2420 | |
2421 | // CRM-11662 Copy Case custom data | |
2422 | $extends = array('case'); | |
2423 | $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends); | |
2424 | if ($groupTree) { | |
2425 | foreach ($groupTree as $groupID => $group) { | |
2426 | $table[$groupTree[$groupID]['table_name']] = array('entity_id'); | |
2427 | foreach ($group['fields'] as $fieldID => $field) { | |
2428 | $table[$groupTree[$groupID]['table_name']][] = $groupTree[$groupID]['fields'][$fieldID]['column_name']; | |
2429 | } | |
2430 | } | |
2431 | ||
2432 | foreach ($table as $tableName => $tableColumns) { | |
e96f025a | 2433 | $insert = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $tableColumns) . ') '; |
8bd86283 | 2434 | $tableColumns[0] = $mainCaseId; |
e96f025a | 2435 | $select = 'SELECT ' . implode(', ', $tableColumns); |
2436 | $from = ' FROM ' . $tableName; | |
2437 | $where = " WHERE {$tableName}.entity_id = {$otherCaseId}"; | |
2438 | $query = $insert . $select . $from . $where; | |
2439 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
8bd86283 DG |
2440 | } |
2441 | } | |
e96f025a | 2442 | |
6a488035 | 2443 | $mainCase->free(); |
e96f025a | 2444 | |
6a488035 TO |
2445 | $mainCaseIds[] = $mainCaseId; |
2446 | //insert record for case contact. | |
2447 | $otherCaseContact = new CRM_Case_DAO_CaseContact(); | |
2448 | $otherCaseContact->case_id = $otherCaseId; | |
2449 | $otherCaseContact->find(); | |
2450 | while ($otherCaseContact->fetch()) { | |
2451 | $mainCaseContact = new CRM_Case_DAO_CaseContact(); | |
2452 | $mainCaseContact->case_id = $mainCaseId; | |
2453 | $mainCaseContact->contact_id = $otherCaseContact->contact_id; | |
2454 | if ($mainCaseContact->contact_id == $otherContactId) { | |
2455 | $mainCaseContact->contact_id = $mainContactId; | |
2456 | } | |
2457 | //avoid duplicate object. | |
2458 | if (!$mainCaseContact->find(TRUE)) { | |
2459 | $mainCaseContact->save(); | |
2460 | } | |
2461 | $mainCaseContact->free(); | |
2462 | } | |
2463 | $otherCaseContact->free(); | |
2464 | } | |
2465 | elseif (!$otherContactId) { | |
2466 | $otherContactId = $mainContactId; | |
2467 | } | |
2468 | ||
2469 | if (!$mainCaseId || !$otherCaseId || | |
2470 | !$mainContactId || !$otherContactId | |
2471 | ) { | |
2472 | continue; | |
2473 | } | |
2474 | ||
2475 | // get all activities for other case. | |
2476 | $otherCaseActivities = array(); | |
2477 | CRM_Core_DAO::commonRetrieveAll('CRM_Case_DAO_CaseActivity', 'case_id', $otherCaseId, $otherCaseActivities); | |
2478 | ||
2479 | //for duplicate cases do not process singleton activities. | |
2480 | $otherActivityIds = $singletonActivityIds = array(); | |
2481 | foreach ($otherCaseActivities as $caseActivityId => $otherIds) { | |
2482 | $otherActId = CRM_Utils_Array::value('activity_id', $otherIds); | |
2483 | if (!$otherActId || in_array($otherActId, $otherActivityIds)) { | |
2484 | continue; | |
2485 | } | |
2486 | $otherActivityIds[] = $otherActId; | |
2487 | } | |
2488 | if ($duplicateCases) { | |
2489 | if ($openCaseType = array_search('Open Case', $activityTypes)) { | |
2490 | $sql = " | |
2491 | SELECT id | |
2492 | FROM civicrm_activity | |
2493 | WHERE activity_type_id = $openCaseType | |
2494 | AND id IN ( " . implode(',', array_values($otherActivityIds)) . ');'; | |
2495 | $dao = CRM_Core_DAO::executeQuery($sql); | |
2496 | while ($dao->fetch()) { | |
2497 | $singletonActivityIds[] = $dao->id; | |
2498 | } | |
2499 | $dao->free(); | |
2500 | } | |
2501 | } | |
2502 | ||
2503 | // migrate all activities and connect to main contact. | |
2504 | $copiedActivityIds = $activityMappingIds = array(); | |
2505 | sort($otherActivityIds); | |
2506 | foreach ($otherActivityIds as $otherActivityId) { | |
2507 | ||
2508 | //for duplicate cases - | |
2509 | //do not migrate singleton activities. | |
2510 | if (!$otherActivityId || in_array($otherActivityId, $singletonActivityIds)) { | |
2511 | continue; | |
2512 | } | |
2513 | ||
2514 | //migrate activity record. | |
2515 | $otherActivity = new CRM_Activity_DAO_Activity(); | |
2516 | $otherActivity->id = $otherActivityId; | |
2517 | if (!$otherActivity->find(TRUE)) { | |
2518 | continue; | |
2519 | } | |
2520 | ||
2521 | $mainActVals = array(); | |
2522 | $mainActivity = new CRM_Activity_DAO_Activity(); | |
2523 | CRM_Core_DAO::storeValues($otherActivity, $mainActVals); | |
2524 | $mainActivity->copyValues($mainActVals); | |
2525 | $mainActivity->id = NULL; | |
2526 | $mainActivity->activity_date_time = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time); | |
6a488035 TO |
2527 | $mainActivity->source_record_id = CRM_Utils_Array::value($mainActivity->source_record_id, |
2528 | $activityMappingIds | |
2529 | ); | |
2530 | ||
2531 | $mainActivity->original_id = CRM_Utils_Array::value($mainActivity->original_id, | |
2532 | $activityMappingIds | |
2533 | ); | |
2534 | ||
2535 | $mainActivity->parent_id = CRM_Utils_Array::value($mainActivity->parent_id, | |
2536 | $activityMappingIds | |
2537 | ); | |
2538 | $mainActivity->save(); | |
2539 | $mainActivityId = $mainActivity->id; | |
2540 | if (!$mainActivityId) { | |
2541 | continue; | |
2542 | } | |
2543 | ||
2544 | $activityMappingIds[$otherActivityId] = $mainActivityId; | |
4322672b | 2545 | // insert log of all activities |
6a488035 TO |
2546 | CRM_Activity_BAO_Activity::logActivityAction($mainActivity); |
2547 | ||
2548 | $otherActivity->free(); | |
2549 | $mainActivity->free(); | |
2550 | $copiedActivityIds[] = $otherActivityId; | |
2551 | ||
2552 | //create case activity record. | |
2553 | $mainCaseActivity = new CRM_Case_DAO_CaseActivity(); | |
2554 | $mainCaseActivity->case_id = $mainCaseId; | |
2555 | $mainCaseActivity->activity_id = $mainActivityId; | |
2556 | $mainCaseActivity->save(); | |
2557 | $mainCaseActivity->free(); | |
2558 | ||
4322672b | 2559 | //migrate source activity. |
2560 | $otherSourceActivity = new CRM_Activity_DAO_ActivityContact(); | |
2561 | $otherSourceActivity->activity_id = $otherActivityId; | |
2562 | $otherSourceActivity->record_type_id = $sourceID; | |
2563 | $otherSourceActivity->find(); | |
2564 | while ($otherSourceActivity->fetch()) { | |
2565 | $mainActivitySource = new CRM_Activity_DAO_ActivityContact(); | |
2566 | $mainActivitySource->record_type_id = $sourceID; | |
2567 | $mainActivitySource->activity_id = $mainActivityId; | |
2568 | $mainActivitySource->contact_id = $otherSourceActivity->contact_id; | |
2569 | if ($mainActivitySource->contact_id == $otherContactId) { | |
2570 | $mainActivitySource->contact_id = $mainContactId; | |
2571 | } | |
2572 | //avoid duplicate object. | |
2573 | if (!$mainActivitySource->find(TRUE)) { | |
2574 | $mainActivitySource->save(); | |
2575 | } | |
2576 | $mainActivitySource->free(); | |
2577 | } | |
2578 | $otherSourceActivity->free(); | |
2579 | ||
6a488035 | 2580 | //migrate target activities. |
4e3d3cfc | 2581 | $otherTargetActivity = new CRM_Activity_DAO_ActivityContact(); |
6a488035 | 2582 | $otherTargetActivity->activity_id = $otherActivityId; |
9e74e3ce | 2583 | $otherTargetActivity->record_type_id = $targetID; |
6a488035 TO |
2584 | $otherTargetActivity->find(); |
2585 | while ($otherTargetActivity->fetch()) { | |
4e3d3cfc | 2586 | $mainActivityTarget = new CRM_Activity_DAO_ActivityContact(); |
9e74e3ce | 2587 | $mainActivityTarget->record_type_id = $targetID; |
6a488035 | 2588 | $mainActivityTarget->activity_id = $mainActivityId; |
00bf7e59 | 2589 | $mainActivityTarget->contact_id = $otherTargetActivity->contact_id; |
2590 | if ($mainActivityTarget->contact_id == $otherContactId) { | |
2591 | $mainActivityTarget->contact_id = $mainContactId; | |
6a488035 TO |
2592 | } |
2593 | //avoid duplicate object. | |
2594 | if (!$mainActivityTarget->find(TRUE)) { | |
2595 | $mainActivityTarget->save(); | |
2596 | } | |
2597 | $mainActivityTarget->free(); | |
2598 | } | |
2599 | $otherTargetActivity->free(); | |
2600 | ||
2601 | //migrate assignee activities. | |
4e3d3cfc | 2602 | $otherAssigneeActivity = new CRM_Activity_DAO_ActivityContact(); |
6a488035 | 2603 | $otherAssigneeActivity->activity_id = $otherActivityId; |
9e74e3ce | 2604 | $otherAssigneeActivity->record_type_id = $assigneeID; |
6a488035 TO |
2605 | $otherAssigneeActivity->find(); |
2606 | while ($otherAssigneeActivity->fetch()) { | |
4e3d3cfc | 2607 | $mainAssigneeActivity = new CRM_Activity_DAO_ActivityContact(); |
6a488035 | 2608 | $mainAssigneeActivity->activity_id = $mainActivityId; |
9e74e3ce | 2609 | $mainAssigneeActivity->record_type_id = $assigneeID; |
00bf7e59 | 2610 | $mainAssigneeActivity->contact_id = $otherAssigneeActivity->contact_id; |
2611 | if ($mainAssigneeActivity->contact_id == $otherContactId) { | |
2612 | $mainAssigneeActivity->contact_id = $mainContactId; | |
6a488035 TO |
2613 | } |
2614 | //avoid duplicate object. | |
2615 | if (!$mainAssigneeActivity->find(TRUE)) { | |
2616 | $mainAssigneeActivity->save(); | |
2617 | } | |
2618 | $mainAssigneeActivity->free(); | |
2619 | } | |
2620 | $otherAssigneeActivity->free(); | |
8c31eef5 D |
2621 | |
2622 | // copy custom fields and attachments | |
4322672b | 2623 | $aparams = array( |
2624 | 'activityID' => $otherActivityId, | |
2625 | 'mainActivityId' => $mainActivityId, | |
2626 | ); | |
8c31eef5 | 2627 | CRM_Activity_BAO_Activity::copyExtendedActivityData($aparams); |
6a488035 TO |
2628 | } |
2629 | ||
2630 | //copy case relationship. | |
2631 | if ($duplicateContacts) { | |
2632 | //migrate relationship records. | |
2633 | $otherRelationship = new CRM_Contact_DAO_Relationship(); | |
2634 | $otherRelationship->case_id = $otherCaseId; | |
2635 | $otherRelationship->find(); | |
2636 | $otherRelationshipIds = array(); | |
2637 | while ($otherRelationship->fetch()) { | |
2638 | $otherRelVals = array(); | |
2639 | $updateOtherRel = FALSE; | |
2640 | CRM_Core_DAO::storeValues($otherRelationship, $otherRelVals); | |
2641 | ||
2642 | $mainRelationship = new CRM_Contact_DAO_Relationship(); | |
2643 | $mainRelationship->copyValues($otherRelVals); | |
2644 | $mainRelationship->id = NULL; | |
2645 | $mainRelationship->case_id = $mainCaseId; | |
2646 | if ($mainRelationship->contact_id_a == $otherContactId) { | |
2647 | $updateOtherRel = TRUE; | |
2648 | $mainRelationship->contact_id_a = $mainContactId; | |
2649 | } | |
2650 | ||
2651 | //case creator change only when we merge user contact. | |
2652 | if ($mainRelationship->contact_id_b == $otherContactId) { | |
2653 | //do not change creator for change client. | |
2654 | if (!$changeClient) { | |
2655 | $updateOtherRel = TRUE; | |
2656 | $mainRelationship->contact_id_b = ($currentUserId) ? $currentUserId : $mainContactId; | |
2657 | } | |
2658 | } | |
2659 | $mainRelationship->end_date = CRM_Utils_Date::isoToMysql($otherRelationship->end_date); | |
2660 | $mainRelationship->start_date = CRM_Utils_Date::isoToMysql($otherRelationship->start_date); | |
2661 | ||
2662 | //avoid duplicate object. | |
2663 | if (!$mainRelationship->find(TRUE)) { | |
2664 | $mainRelationship->save(); | |
2665 | } | |
2666 | $mainRelationship->free(); | |
2667 | ||
2668 | //get the other relationship ids to update end date. | |
2669 | if ($updateOtherRel) { | |
2670 | $otherRelationshipIds[$otherRelationship->id] = $otherRelationship->id; | |
2671 | } | |
2672 | } | |
2673 | $otherRelationship->free(); | |
2674 | ||
2675 | //update other relationships end dates | |
2676 | if (!empty($otherRelationshipIds)) { | |
2677 | $sql = 'UPDATE civicrm_relationship | |
2678 | SET end_date = CURDATE() | |
2679 | WHERE id IN ( ' . implode(',', $otherRelationshipIds) . ')'; | |
2680 | CRM_Core_DAO::executeQuery($sql); | |
2681 | } | |
2682 | } | |
2683 | ||
2684 | //move other case to trash. | |
2685 | $mergeCase = self::deleteCase($otherCaseId, $moveToTrash); | |
2686 | if (!$mergeCase) { | |
2687 | continue; | |
2688 | } | |
2689 | ||
2690 | $mergeActSubject = $mergeActSubjectDetails = $mergeActType = ''; | |
2691 | if ($changeClient) { | |
2692 | $mainContactDisplayName = CRM_Contact_BAO_Contact::displayName($mainContactId); | |
2693 | $otherContactDisplayName = CRM_Contact_BAO_Contact::displayName($otherContactId); | |
2694 | ||
2695 | $mergeActType = array_search('Reassigned Case', $activityTypes); | |
2696 | $mergeActSubject = ts("Case %1 reassigned client from %2 to %3. New Case ID is %4.", | |
2697 | array( | |
e96f025a | 2698 | 1 => $otherCaseId, |
2699 | 2 => $otherContactDisplayName, | |
2700 | 3 => $mainContactDisplayName, | |
2701 | 4 => $mainCaseId | |
6a488035 TO |
2702 | ) |
2703 | ); | |
2704 | } | |
2705 | elseif ($duplicateContacts) { | |
2706 | $mergeActType = array_search('Merge Case', $activityTypes); | |
2707 | $mergeActSubject = ts("Case %1 copied from contact id %2 to contact id %3 via merge. New Case ID is %4.", | |
2708 | array( | |
e96f025a | 2709 | 1 => $otherCaseId, |
2710 | 2 => $otherContactId, | |
2711 | 3 => $mainContactId, | |
2712 | 4 => $mainCaseId | |
6a488035 TO |
2713 | ) |
2714 | ); | |
2715 | } | |
2716 | else { | |
2717 | $mergeActType = array_search('Merge Case', $activityTypes); | |
2718 | $mergeActSubject = ts("Case %1 merged into case %2", array(1 => $otherCaseId, 2 => $mainCaseId)); | |
2719 | if (!empty($copiedActivityIds)) { | |
2720 | $sql = ' | |
2721 | SELECT id, subject, activity_date_time, activity_type_id | |
2722 | FROM civicrm_activity | |
2723 | WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; | |
2724 | $dao = CRM_Core_DAO::executeQuery($sql); | |
2725 | while ($dao->fetch()) { | |
2726 | $mergeActSubjectDetails .= "{$dao->activity_date_time} :: {$activityTypes[$dao->activity_type_id]}"; | |
2727 | if ($dao->subject) { | |
2728 | $mergeActSubjectDetails .= " :: {$dao->subject}"; | |
2729 | } | |
2730 | $mergeActSubjectDetails .= "<br />"; | |
2731 | } | |
2732 | } | |
2733 | } | |
2734 | ||
2735 | //create merge activity record. | |
2736 | $activityParams = array( | |
2737 | 'subject' => $mergeActSubject, | |
2738 | 'details' => $mergeActSubjectDetails, | |
2739 | 'status_id' => array_search('Completed', $activityStatuses), | |
2740 | 'activity_type_id' => $mergeActType, | |
2741 | 'source_contact_id' => $mainContactId, | |
2742 | 'activity_date_time' => date('YmdHis'), | |
2743 | ); | |
2744 | ||
2745 | $mergeActivity = CRM_Activity_BAO_Activity::create($activityParams); | |
2746 | $mergeActivityId = $mergeActivity->id; | |
2747 | if (!$mergeActivityId) { | |
2748 | continue; | |
2749 | } | |
2750 | $mergeActivity->free(); | |
2751 | ||
2752 | //connect merge activity to case. | |
2753 | $mergeCaseAct = array( | |
2754 | 'case_id' => $mainCaseId, | |
2755 | 'activity_id' => $mergeActivityId, | |
2756 | ); | |
2757 | ||
2758 | self::processCaseActivity($mergeCaseAct); | |
2759 | } | |
02094cdb JJ |
2760 | |
2761 | CRM_Utils_Hook::post_case_merge($mainContactId, $mainCaseId, $otherContactId, $otherCaseId, $changeClient); | |
2762 | ||
6a488035 TO |
2763 | return $mainCaseIds; |
2764 | } | |
2765 | ||
2766 | /** | |
2767 | * Validate contact permission for | |
2768 | * edit/view on activity record and build links. | |
2769 | * | |
64bd5a0e TO |
2770 | * @param array $tplParams |
2771 | * Params to be sent to template for sending email. | |
2772 | * @param array $activityParams | |
2773 | * Info of the activity. | |
6a488035 TO |
2774 | * |
2775 | * @return void | |
2776 | * @static | |
2777 | */ | |
00be9182 | 2778 | public static function buildPermissionLinks(&$tplParams, $activityParams) { |
6a488035 TO |
2779 | $activityTypeId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityParams['source_record_id'], |
2780 | 'activity_type_id', 'id' | |
2781 | ); | |
2782 | ||
a7488080 | 2783 | if (!empty($tplParams['isCaseActivity'])) { |
6a488035 TO |
2784 | $tplParams['editActURL'] = CRM_Utils_System::url('civicrm/case/activity', |
2785 | "reset=1&cid={$activityParams['target_id']}&caseid={$activityParams['case_id']}&action=update&id={$activityParams['source_record_id']}", TRUE | |
2786 | ); | |
2787 | ||
2788 | $tplParams['viewActURL'] = CRM_Utils_System::url('civicrm/case/activity/view', | |
2789 | "reset=1&aid={$activityParams['source_record_id']}&cid={$activityParams['target_id']}&caseID={$activityParams['case_id']}", TRUE | |
2790 | ); | |
2791 | ||
2792 | $tplParams['manageCaseURL'] = CRM_Utils_System::url('civicrm/contact/view/case', | |
2793 | "reset=1&id={$activityParams['case_id']}&cid={$activityParams['target_id']}&action=view&context=home", TRUE | |
2794 | ); | |
2795 | } | |
2796 | else { | |
2797 | $tplParams['editActURL'] = CRM_Utils_System::url('civicrm/contact/view/activity', | |
2798 | "atype=$activityTypeId&action=update&reset=1&id={$activityParams['source_record_id']}&cid={$tplParams['contact']['contact_id']}&context=activity", TRUE | |
2799 | ); | |
2800 | ||
2801 | $tplParams['viewActURL'] = CRM_Utils_System::url('civicrm/contact/view/activity', | |
2802 | "atype=$activityTypeId&action=view&reset=1&id={$activityParams['source_record_id']}&cid={$tplParams['contact']['contact_id']}&context=activity", TRUE | |
2803 | ); | |
2804 | } | |
2805 | } | |
2806 | ||
2807 | /** | |
2808 | * Validate contact permission for | |
2809 | * given operation on activity record. | |
2810 | * | |
64bd5a0e TO |
2811 | * @param int $activityId |
2812 | * Activity record id. | |
2813 | * @param string $operation | |
2814 | * User operation. | |
2815 | * @param int $actTypeId | |
2816 | * Activity type id. | |
2817 | * @param int $contactId | |
2818 | * Contact id/if not pass consider logged in. | |
2819 | * @param bool $checkComponent | |
2820 | * Do we need to check component enabled. | |
6a488035 TO |
2821 | * |
2822 | * @return boolean $allow true/false | |
2823 | * @static | |
2824 | */ | |
00be9182 | 2825 | public static function checkPermission($activityId, $operation, $actTypeId = NULL, $contactId = NULL, $checkComponent = TRUE) { |
6a488035 TO |
2826 | $allow = FALSE; |
2827 | if (!$actTypeId && $activityId) { | |
2828 | $actTypeId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activityId, 'activity_type_id'); | |
2829 | } | |
2830 | ||
2831 | if (!$activityId || !$operation || !$actTypeId) { | |
2832 | return $allow; | |
2833 | } | |
2834 | ||
2835 | //do check for civicase component enabled. | |
077dbf5e CW |
2836 | if ($checkComponent && !self::enabled()) { |
2837 | return $allow; | |
6a488035 TO |
2838 | } |
2839 | ||
2840 | //do check for cases. | |
2841 | $caseActOperations = array( | |
2842 | 'File On Case', | |
2843 | 'Link Cases', | |
2844 | 'Move To Case', | |
2845 | 'Copy To Case', | |
2846 | ); | |
2847 | ||
2848 | if (in_array($operation, $caseActOperations)) { | |
2849 | static $unclosedCases; | |
2850 | if (!is_array($unclosedCases)) { | |
2851 | $unclosedCases = self::getUnclosedCases(); | |
2852 | } | |
2853 | if ($operation == 'File On Case') { | |
2854 | $allow = (empty($unclosedCases)) ? FALSE : TRUE; | |
2855 | } | |
2856 | else { | |
2857 | $allow = (count($unclosedCases) > 1) ? TRUE : FALSE; | |
2858 | } | |
2859 | } | |
2860 | ||
2861 | $actionOperations = array('view', 'edit', 'delete'); | |
2862 | if (in_array($operation, $actionOperations)) { | |
2863 | ||
2864 | //do cache when user has non/supper permission. | |
2865 | static $allowOperations; | |
2866 | ||
2867 | if (!is_array($allowOperations) || | |
2868 | !array_key_exists($operation, $allowOperations) | |
2869 | ) { | |
2870 | ||
2871 | if (!$contactId) { | |
2872 | $session = CRM_Core_Session::singleton(); | |
2873 | $contactId = $session->get('userID'); | |
2874 | } | |
2875 | ||
2876 | //check for permissions. | |
2877 | $permissions = array( | |
2878 | 'view' => array( | |
2879 | 'access my cases and activities', | |
2880 | 'access all cases and activities', | |
2881 | ), | |
2882 | 'edit' => array( | |
2883 | 'access my cases and activities', | |
2884 | 'access all cases and activities', | |
2885 | ), | |
2886 | 'delete' => array('delete activities'), | |
2887 | ); | |
2888 | ||
2889 | //check for core permission. | |
2890 | $hasPermissions = array(); | |
2891 | $checkPermissions = CRM_Utils_Array::value($operation, $permissions); | |
2892 | if (is_array($checkPermissions)) { | |
2893 | foreach ($checkPermissions as $per) { | |
2894 | if (CRM_Core_Permission::check($per)) { | |
2895 | $hasPermissions[$operation][] = $per; | |
2896 | } | |
2897 | } | |
2898 | } | |
2899 | ||
2900 | //has permissions. | |
2901 | if (!empty($hasPermissions)) { | |
2902 | //need to check activity object specific. | |
2903 | if (in_array($operation, array( | |
e96f025a | 2904 | 'view', |
2905 | 'edit' | |
2906 | )) | |
2907 | ) { | |
6a488035 TO |
2908 | //do we have supper permission. |
2909 | if (in_array('access all cases and activities', $hasPermissions[$operation])) { | |
2910 | $allowOperations[$operation] = $allow = TRUE; | |
2911 | } | |
2912 | else { | |
2913 | //user has only access to my cases and activity. | |
2914 | //here object specific permmions come in picture. | |
2915 | ||
2916 | //edit - contact must be source or assignee | |
2917 | //view - contact must be source/assignee/target | |
2918 | $isTarget = $isAssignee = $isSource = FALSE; | |
499e172c | 2919 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
4322672b | 2920 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
2921 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
2922 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
6a488035 | 2923 | |
4e3d3cfc | 2924 | $target = new CRM_Activity_DAO_ActivityContact(); |
9e74e3ce | 2925 | $target->record_type_id = $targetID; |
6a488035 | 2926 | $target->activity_id = $activityId; |
00bf7e59 | 2927 | $target->contact_id = $contactId; |
6a488035 TO |
2928 | if ($target->find(TRUE)) { |
2929 | $isTarget = TRUE; | |
2930 | } | |
2931 | ||
4e3d3cfc | 2932 | $assignee = new CRM_Activity_DAO_ActivityContact(); |
6a488035 | 2933 | $assignee->activity_id = $activityId; |
9e74e3ce | 2934 | $assignee->record_type_id = $assigneeID; |
00bf7e59 | 2935 | $assignee->contact_id = $contactId; |
6a488035 TO |
2936 | if ($assignee->find(TRUE)) { |
2937 | $isAssignee = TRUE; | |
2938 | } | |
2939 | ||
4322672b | 2940 | $source = new CRM_Activity_DAO_ActivityContact(); |
2941 | $source->activity_id = $activityId; | |
2942 | $source->record_type_id = $sourceID; | |
2943 | $source->contact_id = $contactId; | |
2944 | if ($source->find(TRUE)) { | |
6a488035 TO |
2945 | $isSource = TRUE; |
2946 | } | |
2947 | ||
2948 | if ($operation == 'edit') { | |
2949 | if ($isAssignee || $isSource) { | |
2950 | $allow = TRUE; | |
2951 | } | |
2952 | } | |
2953 | if ($operation == 'view') { | |
2954 | if ($isTarget || $isAssignee || $isSource) { | |
2955 | $allow = TRUE; | |
2956 | } | |
2957 | } | |
2958 | } | |
2959 | } | |
2960 | elseif (is_array($hasPermissions[$operation])) { | |
2961 | $allowOperations[$operation] = $allow = TRUE; | |
2962 | } | |
2963 | } | |
2964 | else { | |
2965 | //contact do not have permission. | |
2966 | $allowOperations[$operation] = FALSE; | |
2967 | } | |
2968 | } | |
2969 | else { | |
2970 | //use cache. | |
2971 | //here contact might have supper/non permission. | |
2972 | $allow = $allowOperations[$operation]; | |
2973 | } | |
2974 | } | |
2975 | ||
2976 | //do further only when operation is granted. | |
2977 | if ($allow) { | |
2978 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name'); | |
2979 | ||
2980 | //get the activity type name. | |
2981 | $actTypeName = CRM_Utils_Array::value($actTypeId, $activityTypes); | |
2982 | ||
2983 | //do not allow multiple copy / edit action. | |
e96f025a | 2984 | $singletonNames = array( |
2985 | 'Open Case', | |
2986 | 'Reassigned Case', | |
2987 | 'Merge Case', | |
2988 | 'Link Cases', | |
2989 | 'Assign Case Role', | |
2990 | 'Email', | |
2991 | 'Inbound Email' | |
2992 | ); | |
6a488035 TO |
2993 | |
2994 | //do not allow to delete these activities, CRM-4543 | |
2995 | $doNotDeleteNames = array('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date'); | |
2996 | ||
2997 | //allow edit operation. | |
2998 | $allowEditNames = array('Open Case'); | |
2999 | ||
3000 | // do not allow File on Case | |
e96f025a | 3001 | $doNotFileNames = array( |
3002 | 'Open Case', | |
3003 | 'Change Case Type', | |
3004 | 'Change Case Status', | |
3005 | 'Change Case Start Date', | |
3006 | 'Reassigned Case', | |
3007 | 'Merge Case', | |
3008 | 'Link Cases', | |
3009 | 'Assign Case Role' | |
3010 | ); | |
6a488035 TO |
3011 | |
3012 | if (in_array($actTypeName, $singletonNames)) { | |
3013 | $allow = FALSE; | |
3014 | if ($operation == 'File On Case') { | |
3015 | $allow = (in_array($actTypeName, $doNotFileNames)) ? FALSE : TRUE; | |
3016 | } | |
3017 | if (in_array($operation, $actionOperations)) { | |
3018 | $allow = TRUE; | |
3019 | if ($operation == 'edit') { | |
3020 | $allow = (in_array($actTypeName, $allowEditNames)) ? TRUE : FALSE; | |
3021 | } | |
3022 | elseif ($operation == 'delete') { | |
3023 | $allow = (in_array($actTypeName, $doNotDeleteNames)) ? FALSE : TRUE; | |
3024 | } | |
3025 | } | |
3026 | } | |
3027 | if ($allow && ($operation == 'delete') && | |
3028 | in_array($actTypeName, $doNotDeleteNames) | |
3029 | ) { | |
3030 | $allow = FALSE; | |
3031 | } | |
3032 | ||
3033 | if ($allow && ($operation == 'File On Case') && | |
3034 | in_array($actTypeName, $doNotFileNames) | |
3035 | ) { | |
3036 | $allow = FALSE; | |
3037 | } | |
3038 | ||
3039 | //check settings file for masking actions | |
3040 | //on the basis the activity types | |
3041 | //hide Edit link if activity type is NOT editable | |
3042 | //(special case activities).CRM-5871 | |
3043 | if ($allow && in_array($operation, $actionOperations)) { | |
3044 | static $actionFilter = array(); | |
3045 | if (!array_key_exists($operation, $actionFilter)) { | |
3046 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
3047 | $actionFilter[$operation] = $xmlProcessor->get('Settings', 'ActivityTypes', FALSE, $operation); | |
3048 | } | |
3049 | if (array_key_exists($operation, $actionFilter[$operation]) && | |
3050 | in_array($actTypeId, $actionFilter[$operation][$operation]) | |
3051 | ) { | |
3052 | $allow = FALSE; | |
3053 | } | |
3054 | } | |
3055 | } | |
3056 | ||
3057 | return $allow; | |
3058 | } | |
3059 | ||
3060 | /** | |
100fef9d | 3061 | * Since we drop 'access CiviCase', allow access |
6a488035 TO |
3062 | * if user has 'access my cases and activities' |
3063 | * or 'access all cases and activities' | |
3064 | */ | |
00be9182 | 3065 | public static function accessCiviCase() { |
077dbf5e | 3066 | if (!self::enabled()) { |
6a488035 TO |
3067 | return FALSE; |
3068 | } | |
3069 | ||
3070 | if (CRM_Core_Permission::check('access my cases and activities') || | |
3071 | CRM_Core_Permission::check('access all cases and activities') | |
3072 | ) { | |
3073 | return TRUE; | |
3074 | } | |
3075 | ||
3076 | return FALSE; | |
3077 | } | |
3078 | ||
3079 | /** | |
077dbf5e CW |
3080 | * Verify user has permission to access a case |
3081 | * | |
3082 | * @param int $caseId | |
64bd5a0e TO |
3083 | * @param bool $denyClosed |
3084 | * Set TRUE if one wants closed cases to be treated as inaccessible. | |
077dbf5e CW |
3085 | * |
3086 | * @return bool | |
3087 | */ | |
00be9182 | 3088 | public static function accessCase($caseId, $denyClosed = TRUE) { |
077dbf5e CW |
3089 | if (!$caseId || !self::enabled()) { |
3090 | return FALSE; | |
3091 | } | |
3092 | ||
3093 | // This permission always has access | |
3094 | if (CRM_Core_Permission::check('access all cases and activities')) { | |
3095 | return TRUE; | |
3096 | } | |
3097 | ||
3098 | // This permission is required at minimum | |
3099 | if (!CRM_Core_Permission::check('access my cases and activities')) { | |
3100 | return FALSE; | |
3101 | } | |
3102 | ||
546abeb1 TO |
3103 | $session = CRM_Core_Session::singleton(); |
3104 | $userID = CRM_Utils_Type::validate($session->get('userID'), 'Positive'); | |
3105 | $caseId = CRM_Utils_Type::validate($caseId, 'Positive'); | |
3106 | ||
3107 | $condition = " AND civicrm_case.is_deleted = 0 "; | |
3108 | $condition .= " AND case_relationship.contact_id_b = {$userID} "; | |
3109 | $condition .= " AND civicrm_case.id = {$caseId}"; | |
3110 | ||
3111 | if ($denyClosed) { | |
3112 | $closedId = CRM_Core_OptionGroup::getValue('case_status', 'Closed', 'name'); | |
3113 | $condition .= " AND civicrm_case.status_id != $closedId"; | |
3114 | } | |
3115 | ||
3116 | // We don't actually care about activities in the case, but the underlying | |
3117 | // query is verbose, and this allows us to share the basic query with | |
3118 | // getCases(). $type=='any' means that activities will be left-joined. | |
3119 | $query = self::getCaseActivityQuery('any', $userID, $condition); | |
3120 | $queryParams = array(); | |
3121 | $dao = CRM_Core_DAO::executeQuery($query, | |
3122 | $queryParams | |
3123 | ); | |
077dbf5e | 3124 | |
546abeb1 | 3125 | return (bool) $dao->fetch(); |
077dbf5e CW |
3126 | } |
3127 | ||
6a488035 | 3128 | /** |
100fef9d | 3129 | * Check whether activity is a case Activity |
6a488035 | 3130 | * |
64bd5a0e TO |
3131 | * @param int $activityID |
3132 | * Activity id. | |
6a488035 TO |
3133 | * |
3134 | * @return boolean $isCaseActivity true/false | |
3135 | */ | |
00be9182 | 3136 | public static function isCaseActivity($activityID) { |
6a488035 TO |
3137 | $isCaseActivity = FALSE; |
3138 | if ($activityID) { | |
3139 | $params = array(1 => array($activityID, 'Integer')); | |
3140 | $query = "SELECT id FROM civicrm_case_activity WHERE activity_id = %1"; | |
3141 | if (CRM_Core_DAO::singleValueQuery($query, $params)) { | |
3142 | $isCaseActivity = TRUE; | |
3143 | } | |
3144 | } | |
3145 | ||
3146 | return $isCaseActivity; | |
3147 | } | |
3148 | ||
3149 | /** | |
100fef9d | 3150 | * Get all the case type ids currently in use |
6a488035 TO |
3151 | * |
3152 | * | |
3153 | * @return array $caseTypeIds | |
3154 | */ | |
00be9182 | 3155 | public static function getUsedCaseType() { |
6a488035 TO |
3156 | static $caseTypeIds; |
3157 | ||
3158 | if (!is_array($caseTypeIds)) { | |
3159 | $query = "SELECT DISTINCT( civicrm_case.case_type_id ) FROM civicrm_case"; | |
3160 | ||
3161 | $dao = CRM_Core_DAO::executeQuery($query); | |
3162 | $caseTypeIds = array(); | |
3163 | while ($dao->fetch()) { | |
3164 | $typeId = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
3165 | $dao->case_type_id | |
3166 | ); | |
3167 | $caseTypeIds[] = $typeId[1]; | |
3168 | } | |
3169 | } | |
3170 | ||
3171 | return $caseTypeIds; | |
3172 | } | |
3173 | ||
3174 | /** | |
100fef9d | 3175 | * Get all the case status ids currently in use |
6a488035 TO |
3176 | * |
3177 | * | |
3178 | * @return array $caseStatusIds | |
3179 | */ | |
00be9182 | 3180 | public static function getUsedCaseStatuses() { |
6a488035 TO |
3181 | static $caseStatusIds; |
3182 | ||
3183 | if (!is_array($caseStatusIds)) { | |
3184 | $query = "SELECT DISTINCT( civicrm_case.status_id ) FROM civicrm_case"; | |
3185 | ||
3186 | $dao = CRM_Core_DAO::executeQuery($query); | |
3187 | $caseStatusIds = array(); | |
3188 | while ($dao->fetch()) { | |
3189 | $caseStatusIds[] = $dao->status_id; | |
3190 | } | |
3191 | } | |
3192 | ||
3193 | return $caseStatusIds; | |
3194 | } | |
3195 | ||
3196 | /** | |
100fef9d | 3197 | * Get all the encounter medium ids currently in use |
6a488035 TO |
3198 | * @return array |
3199 | */ | |
00be9182 | 3200 | public static function getUsedEncounterMediums() { |
6a488035 TO |
3201 | static $mediumIds; |
3202 | ||
3203 | if (!is_array($mediumIds)) { | |
3204 | $query = "SELECT DISTINCT( civicrm_activity.medium_id ) FROM civicrm_activity"; | |
3205 | ||
3206 | $dao = CRM_Core_DAO::executeQuery($query); | |
3207 | $mediumIds = array(); | |
3208 | while ($dao->fetch()) { | |
3209 | $mediumIds[] = $dao->medium_id; | |
3210 | } | |
3211 | } | |
3212 | ||
3213 | return $mediumIds; | |
3214 | } | |
3215 | ||
3216 | /** | |
100fef9d | 3217 | * Check case configuration. |
6a488035 | 3218 | * |
100fef9d | 3219 | * @param int $contactId |
77b97be7 | 3220 | * |
d6f468d3 | 3221 | * @return array $configured |
6a488035 | 3222 | */ |
00be9182 | 3223 | public static function isCaseConfigured($contactId = NULL) { |
6a488035 TO |
3224 | $configured = array_fill_keys(array('configured', 'allowToAddNewCase', 'redirectToCaseAdmin'), FALSE); |
3225 | ||
3226 | //lets check for case configured. | |
3227 | $allCasesCount = CRM_Case_BAO_Case::caseCount(NULL, FALSE); | |
3228 | $configured['configured'] = ($allCasesCount) ? TRUE : FALSE; | |
3229 | if (!$configured['configured']) { | |
3230 | //do check for case type and case status. | |
0372ffa2 | 3231 | $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE); |
6a488035 TO |
3232 | if (!empty($caseTypes)) { |
3233 | $configured['configured'] = TRUE; | |
3234 | if (!$configured['configured']) { | |
3235 | $caseStatuses = CRM_Case_PseudoConstant::caseStatus('label', FALSE); | |
3236 | if (!empty($caseStatuses)) { | |
3237 | $configured['configured'] = TRUE; | |
3238 | } | |
3239 | } | |
3240 | } | |
3241 | } | |
3242 | if ($configured['configured']) { | |
3243 | //do check for active case type and case status. | |
3244 | $caseTypes = CRM_Case_PseudoConstant::caseType(); | |
3245 | if (!empty($caseTypes)) { | |
3246 | $caseStatuses = CRM_Case_PseudoConstant::caseStatus(); | |
3247 | if (!empty($caseStatuses)) { | |
3248 | $configured['allowToAddNewCase'] = TRUE; | |
3249 | } | |
3250 | } | |
3251 | ||
3252 | //do we need to redirect user to case admin. | |
3253 | if (!$configured['allowToAddNewCase'] && $contactId) { | |
3254 | //check for current contact case count. | |
3255 | $currentContatCasesCount = CRM_Case_BAO_Case::caseCount($contactId); | |
3256 | //redirect user to case admin page. | |
3257 | if (!$currentContatCasesCount) { | |
3258 | $configured['redirectToCaseAdmin'] = TRUE; | |
3259 | } | |
3260 | } | |
3261 | } | |
3262 | ||
3263 | return $configured; | |
3264 | } | |
3265 | ||
d6f468d3 | 3266 | /** |
6a488035 TO |
3267 | * Used during case component enablement and during ugprade |
3268 | */ | |
00be9182 | 3269 | public static function createCaseViews() { |
6a4257d4 | 3270 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
a19fc402 TO |
3271 | $dao = new CRM_Core_DAO(); |
3272 | ||
6a488035 | 3273 | $sql = self::createCaseViewsQuery('upcoming'); |
6a488035 TO |
3274 | $dao->query($sql); |
3275 | if (PEAR::getStaticProperty('DB_DataObject', 'lastError')) { | |
6a488035 TO |
3276 | return FALSE; |
3277 | } | |
3278 | ||
3279 | // Above error doesn't get caught? | |
3280 | $doublecheck = $dao->singleValueQuery("SELECT count(id) FROM civicrm_view_case_activity_upcoming"); | |
3281 | if (is_null($doublecheck)) { | |
3282 | return FALSE; | |
3283 | } | |
3284 | ||
3285 | $sql = self::createCaseViewsQuery('recent'); | |
6a488035 TO |
3286 | $dao->query($sql); |
3287 | if (PEAR::getStaticProperty('DB_DataObject', 'lastError')) { | |
6a488035 TO |
3288 | return FALSE; |
3289 | } | |
3290 | ||
3291 | // Above error doesn't get caught? | |
3292 | $doublecheck = $dao->singleValueQuery("SELECT count(id) FROM civicrm_view_case_activity_recent"); | |
3293 | if (is_null($doublecheck)) { | |
3294 | return FALSE; | |
3295 | } | |
3296 | ||
3297 | return TRUE; | |
3298 | } | |
3299 | ||
d6f468d3 | 3300 | /** |
100fef9d | 3301 | * Helper function, also used by the upgrade in case of error |
6a488035 | 3302 | */ |
00be9182 | 3303 | public static function createCaseViewsQuery($section = 'upcoming') { |
6a488035 TO |
3304 | $sql = ""; |
3305 | $scheduled_id = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name'); | |
3306 | switch ($section) { | |
3307 | case 'upcoming': | |
3308 | $sql = "CREATE OR REPLACE VIEW `civicrm_view_case_activity_upcoming` | |
3309 | AS SELECT ca.case_id, a.id, a.activity_date_time, a.status_id, a.activity_type_id | |
3310 | FROM civicrm_case_activity ca | |
3311 | INNER JOIN civicrm_activity a ON ca.activity_id=a.id | |
3312 | WHERE a.activity_date_time <= DATE_ADD( NOW(), INTERVAL 14 DAY ) | |
3313 | AND a.is_current_revision = 1 AND a.is_deleted=0 AND a.status_id = $scheduled_id"; | |
3314 | break; | |
3315 | ||
3316 | case 'recent': | |
3317 | $sql = "CREATE OR REPLACE VIEW `civicrm_view_case_activity_recent` | |
3318 | AS SELECT ca.case_id, a.id, a.activity_date_time, a.status_id, a.activity_type_id | |
3319 | FROM civicrm_case_activity ca | |
3320 | INNER JOIN civicrm_activity a ON ca.activity_id=a.id | |
3321 | WHERE a.activity_date_time <= NOW() | |
3322 | AND a.activity_date_time >= DATE_SUB( NOW(), INTERVAL 14 DAY ) | |
3323 | AND a.is_current_revision = 1 AND a.is_deleted=0 AND a.status_id <> $scheduled_id"; | |
3324 | break; | |
3325 | } | |
6a488035 | 3326 | return $sql; |
e96f025a | 3327 | } |
3328 | ||
3329 | /** | |
100fef9d | 3330 | * Add/copy relationships, when new client is added for a case |
e96f025a | 3331 | * |
64bd5a0e TO |
3332 | * @param int $caseId |
3333 | * Case id. | |
3334 | * @param int $contactId | |
3335 | * Contact id / new client id. | |
e96f025a | 3336 | * |
3337 | * @return void | |
3338 | */ | |
00be9182 | 3339 | public static function addCaseRelationships($caseId, $contactId) { |
44466d80 KJ |
3340 | // get the case role / relationships for the case |
3341 | $caseRelationships = new CRM_Contact_DAO_Relationship(); | |
3342 | $caseRelationships->case_id = $caseId; | |
3343 | $caseRelationships->find(); | |
3344 | $relationshipTypes = array(); | |
3345 | ||
3346 | // make sure we don't add duplicate relationships of same relationship type. | |
3347 | while ($caseRelationships->fetch() && !in_array($caseRelationships->relationship_type_id, $relationshipTypes)) { | |
3348 | $values = array(); | |
3349 | CRM_Core_DAO::storeValues($caseRelationships, $values); | |
3350 | ||
3351 | // add relationship for new client. | |
3352 | $newRelationship = new CRM_Contact_DAO_Relationship(); | |
3353 | $newRelationship->copyValues($values); | |
3354 | $newRelationship->id = NULL; | |
3355 | $newRelationship->case_id = $caseId; | |
3356 | $newRelationship->contact_id_a = $contactId; | |
3357 | $newRelationship->end_date = CRM_Utils_Date::isoToMysql($caseRelationships->end_date); | |
3358 | $newRelationship->start_date = CRM_Utils_Date::isoToMysql($caseRelationships->start_date); | |
3359 | ||
3360 | // another check to avoid duplicate relationship, in cases where client is removed and re-added again. | |
3361 | if (!$newRelationship->find(TRUE)) { | |
3362 | $newRelationship->save(); | |
3363 | } | |
3364 | $newRelationship->free(); | |
3365 | ||
3366 | // store relationship type of newly created relationship | |
3367 | $relationshipTypes[] = $caseRelationships->relationship_type_id; | |
3368 | } | |
6a488035 | 3369 | } |
14a679f1 KJ |
3370 | |
3371 | /** | |
100fef9d | 3372 | * Get the list of clients for a case |
14a679f1 KJ |
3373 | * |
3374 | * @param int $caseId | |
3375 | * | |
3376 | * @return array $clients associated array with client ids | |
3377 | * @static | |
3378 | */ | |
00be9182 | 3379 | public static function getCaseClients($caseId) { |
14a679f1 KJ |
3380 | $clients = array(); |
3381 | $caseContact = new CRM_Case_DAO_CaseContact(); | |
3382 | $caseContact->case_id = $caseId; | |
3383 | $caseContact->find(); | |
3384 | ||
e96f025a | 3385 | while ($caseContact->fetch()) { |
14a679f1 KJ |
3386 | $clients[] = $caseContact->contact_id; |
3387 | } | |
3388 | ||
3389 | return $clients; | |
3390 | } | |
16c0ec8d CW |
3391 | |
3392 | /** | |
3393 | * Get options for a given case field. | |
3394 | * @see CRM_Core_DAO::buildOptions | |
3395 | * | |
64bd5a0e TO |
3396 | * @param string $fieldName |
3397 | * @param string $context | |
3398 | * : @see CRM_Core_DAO::buildOptionsContext. | |
3399 | * @param array $props | |
3400 | * : whatever is known about this dao object. | |
77b97be7 EM |
3401 | * |
3402 | * @return Array|bool | |
16c0ec8d CW |
3403 | */ |
3404 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { | |
3405 | $className = __CLASS__; | |
3406 | $params = array(); | |
3407 | switch ($fieldName) { | |
3408 | // This field is not part of this object but the api supports it | |
3409 | case 'medium_id': | |
3410 | $className = 'CRM_Activity_BAO_Activity'; | |
3411 | break; | |
3412 | } | |
3413 | return CRM_Core_PseudoConstant::get($className, $fieldName, $params, $context); | |
3414 | } | |
6a488035 | 3415 | } |