CRM-17645 - Convert case autocompletes to use EntityRef
[civicrm-core.git] / CRM / Case / BAO / CaseContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * This class contains the functions for Case Contact management.
36 */
37 class CRM_Case_BAO_CaseContact extends CRM_Case_DAO_CaseContact {
38
39 /**
40 * Create case contact record.
41 *
42 * @param array $params
43 * case_id, contact_id
44 *
45 * @return CRM_Case_BAO_CaseContact
46 */
47 public static function create($params) {
48 $caseContact = new self();
49 $caseContact->copyValues($params);
50 $caseContact->save();
51
52 // add to recently viewed
53 $caseType = CRM_Case_BAO_Case::getCaseType($caseContact->case_id);
54 $url = CRM_Utils_System::url('civicrm/contact/view/case',
55 "action=view&reset=1&id={$caseContact->case_id}&cid={$caseContact->contact_id}&context=home"
56 );
57
58 $title = CRM_Contact_BAO_Contact::displayName($caseContact->contact_id) . ' - ' . $caseType;
59
60 $recentOther = array();
61 if (CRM_Core_Permission::checkActionPermission('CiviCase', CRM_Core_Action::DELETE)) {
62 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/case',
63 "action=delete&reset=1&id={$caseContact->case_id}&cid={$caseContact->contact_id}&context=home"
64 );
65 }
66
67 // add the recently created case
68 CRM_Utils_Recent::add($title,
69 $url,
70 $caseContact->case_id,
71 'Case',
72 $caseContact->contact_id,
73 NULL,
74 $recentOther
75 );
76
77 return $caseContact;
78 }
79
80 /**
81 * @inheritDoc
82 */
83 public function apiWhereClause() {
84 // In order to make things easier for downstream developers, we reuse and adapt case acls here.
85 // This doesn't yield the most straightforward query, but hopefully the sql engine will sort it out.
86 $clauses = array(
87 // Case acls already check for contact access so we can just mark contact_id as handled
88 'contact_id' => NULL,
89 'case_id' => array(),
90 );
91 $caseSubclauses = array();
92 $caseBao = new CRM_Case_BAO_Case();
93 foreach ($caseBao->apiWhereClause() as $field => $fieldClauses) {
94 if ($field == 'id' && $fieldClauses) {
95 $clauses['case_id'] = array_merge($clauses['case_id'], (array) $fieldClauses);
96 }
97 elseif ($fieldClauses) {
98 $caseSubclauses[] = "$field " . implode(" AND $field ", (array) $fieldClauses);
99 }
100 }
101 if ($caseSubclauses) {
102 $clauses['case_id'][] = 'IN (SELECT id FROM civicrm_case WHERE ' . implode(' AND ', $caseSubclauses) . ')';
103 }
104 return $clauses;
105 }
106
107 }