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