CRM-17645 - Add entityRef support for autocompleting Cases
[civicrm-core.git] / CRM / Case / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 *
33 */
34
35/**
3924e596 36 * This class contains all case related functions that are called using AJAX
6a488035
TO
37 */
38class CRM_Case_Page_AJAX {
39
40 /**
41 * Retrieve unclosed cases.
42 */
00be9182 43 public static function unclosedCases() {
6a488035 44 $params = array(
89595c92 45 'limit' => Civi::settings()->get('search_autocomplete_count'),
f72119d9 46 'sort_name' => CRM_Utils_Type::escape(CRM_Utils_Array::value('term', $_GET, ''), 'String'),
6a488035
TO
47 );
48
49 $excludeCaseIds = array();
f72119d9
CW
50 if (!empty($_GET['excludeCaseIds'])) {
51 $excludeCaseIds = explode(',', CRM_Utils_Type::escape($_GET['excludeCaseIds'], 'String'));
6a488035 52 }
3c0b6a40 53 $unclosedCases = CRM_Case_BAO_Case::getUnclosedCases($params, $excludeCaseIds, TRUE, TRUE);
d6408252 54 $results = array();
6a488035 55 foreach ($unclosedCases as $caseId => $details) {
f72119d9
CW
56 $results[] = array(
57 'id' => $caseId,
3c0b6a40
CW
58 'label' => $details['sort_name'] . ' - ' . $details['case_type'] . ($details['end_date'] ? ' (' . ts('closed') . ')' : ''),
59 'label_class' => $details['end_date'] ? 'strikethrough' : '',
6f1e2d00 60 'description' => array("#$caseId: " . $details['case_subject'] . ' (' . $details['case_status'] . ')'),
f72119d9
CW
61 'extra' => $details,
62 );
6a488035 63 }
ecdef330 64 CRM_Utils_JSON::output($results);
6a488035
TO
65 }
66
3924e596
CW
67 /**
68 * @throws \CRM_Core_Exception
69 */
00be9182 70 public function processCaseTags() {
6a488035 71
077dbf5e 72 $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Positive');
6a488035 73 $tags = CRM_Utils_Type::escape($_POST['tag'], 'String');
6fd5424b 74 $tagList = $_POST['taglist'];
6a488035 75
077dbf5e
CW
76 if (!CRM_Case_BAO_Case::accessCase($caseId)) {
77 CRM_Utils_System::permissionDenied();
6a488035
TO
78 }
79
80 $tagIds = array();
81 if ($tags) {
82 $tagIds = explode(',', $tags);
83 }
84
6fd5424b 85 if (!empty($tagIds)) {
6fd5424b 86 $params = array(
87 'entity_id' => $caseId,
88 'entity_table' => 'civicrm_case',
89 );
6a488035 90
6fd5424b 91 CRM_Core_BAO_EntityTag::del($params);
6a488035 92
6fd5424b 93 foreach ($tagIds as $tagid) {
94 if (is_numeric($tagid)) {
95 $params['tag_id'] = $tagid;
96 CRM_Core_BAO_EntityTag::add($params);
97 }
6a488035
TO
98 }
99 }
100
6fd5424b 101 if (!empty($tagList)) {
ca4f7cc8 102 CRM_Core_Form_Tag::postProcess($tagList, $caseId, 'civicrm_case', CRM_Core_DAO::$_nullObject);
6fd5424b 103 }
104
6a488035
TO
105 $session = CRM_Core_Session::singleton();
106
107 $activityParams = array();
108 $activityParams['source_contact_id'] = $session->get('userID');
109 $activityParams['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Change Case Tags', 'name');
110 $activityParams['activity_date_time'] = date('YmdHis');
111 $activityParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
112 $activityParams['case_id'] = $caseId;
113 $activityParams['is_auto'] = 0;
114 $activityParams['subject'] = 'Change Case Tags';
115
116 $activity = CRM_Activity_BAO_Activity::create($activityParams);
117
118 $caseParams = array(
119 'activity_id' => $activity->id,
120 'case_id' => $caseId,
121 );
122
123 CRM_Case_BAO_Case::processCaseActivity($caseParams);
124
125 echo 'true';
126 CRM_Utils_System::civiExit();
127 }
128
3924e596
CW
129 /**
130 * @throws \CiviCRM_API3_Exception
131 */
00be9182 132 public function caseDetails() {
077dbf5e
CW
133 $caseId = CRM_Utils_Type::escape($_GET['caseId'], 'Positive');
134
3924e596
CW
135 $case = civicrm_api3('Case', 'getsingle',
136 array('id' => $caseId, 'return' => array('subject', 'case_type_id', 'status_id', 'start_date', 'end_date')));
077dbf5e 137
3924e596
CW
138 $caseStatuses = CRM_Case_PseudoConstant::caseStatus();
139 $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE);
140 $caseDetails = "<table><tr><td>" . ts('Case Subject') . "</td><td>{$case['subject']}</td></tr>
141 <tr><td>" . ts('Case Type') . "</td><td>{$caseTypes[$case['case_type_id']]}</td></tr>
142 <tr><td>" . ts('Case Status') . "</td><td>{$caseStatuses[$case['status_id']]}</td></tr>
143 <tr><td>" . ts('Case Start Date') . "</td><td>" . CRM_Utils_Date::customFormat($case['start_date']) . "</td></tr>
144 <tr><td>" . ts('Case End Date') . "</td><td></td></tr>" . CRM_Utils_Date::customFormat($case['end_date']) . "</table>";
145
146 if (CRM_Utils_Array::value('snippet', $_GET) == 'json') {
147 CRM_Core_Page_AJAX::returnJsonResponse($caseDetails);
6a488035 148 }
3924e596
CW
149
150 echo $caseDetails;
6a488035
TO
151 CRM_Utils_System::civiExit();
152 }
153
3924e596
CW
154 /**
155 * @throws \CRM_Core_Exception
156 */
00be9182 157 public function addClient() {
077dbf5e
CW
158 $caseId = CRM_Utils_Type::escape($_POST['caseID'], 'Positive');
159 $contactId = CRM_Utils_Type::escape($_POST['contactID'], 'Positive');
160
161 if (!$contactId || !CRM_Case_BAO_Case::accessCase($caseId)) {
162 CRM_Utils_System::permissionDenied();
163 }
6a488035
TO
164
165 $params = array(
166 'case_id' => $caseId,
167 'contact_id' => $contactId,
168 );
169
ff9340a4 170 CRM_Case_BAO_CaseContact::create($params);
6a488035 171
44466d80
KJ
172 // add case relationships
173 CRM_Case_BAO_Case::addCaseRelationships($caseId, $contactId);
174
6a488035
TO
175 $session = CRM_Core_Session::singleton();
176
177 $activityParams = array();
178 $activityParams['source_contact_id'] = $session->get('userID');
179 $activityParams['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Add Client To Case', 'name');
180 $activityParams['activity_date_time'] = date('YmdHis');
181 $activityParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
182 $activityParams['case_id'] = $caseId;
183 $activityParams['is_auto'] = 0;
184 $activityParams['subject'] = 'Client Added To Case';
185
186 $activity = CRM_Activity_BAO_Activity::create($activityParams);
187
188 $caseParams = array(
189 'activity_id' => $activity->id,
190 'case_id' => $caseId,
191 );
192
193 CRM_Case_BAO_Case::processCaseActivity($caseParams);
ecdef330 194 CRM_Utils_JSON::output(TRUE);
6a488035 195 }
4cd35744
KJ
196
197 /**
fe482240 198 * Delete relationships specific to case and relationship type.
4cd35744 199 */
00be9182 200 public static function deleteCaseRoles() {
353ffa53 201 $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Positive');
3b1c37fe
CW
202 $cid = CRM_Utils_Type::escape($_POST['cid'], 'Positive');
203 $relType = CRM_Utils_Request::retrieve('rel_type', 'String', CRM_Core_DAO::$_nullObject, TRUE);
077dbf5e 204
3b1c37fe 205 if (!$cid || !CRM_Case_BAO_Case::accessCase($caseId)) {
077dbf5e
CW
206 CRM_Utils_System::permissionDenied();
207 }
4cd35744 208
3b1c37fe 209 list($relTypeId, $a, $b) = explode('_', $relType);
4cd35744 210
3b1c37fe 211 CRM_Case_BAO_Case::endCaseRole($caseId, $b, $cid, $relTypeId);
4cd35744
KJ
212 CRM_Utils_System::civiExit();
213 }
96025800 214
6a488035 215}