Merge pull request #4977 from colemanw/CRM-15812
[civicrm-core.git] / CRM / Case / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 *
33 */
34
35/**
36 * This class contains all case related functions that are called using AJAX (jQuery)
37 */
38class CRM_Case_Page_AJAX {
39
40 /**
41 * Retrieve unclosed cases.
42 */
00be9182 43 public static function unclosedCases() {
6a488035 44 $params = array(
f72119d9
CW
45 'limit' => CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10),
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' : '',
60 'description' => array($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
00be9182 67 public function processCaseTags() {
6a488035 68
077dbf5e 69 $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Positive');
6a488035 70 $tags = CRM_Utils_Type::escape($_POST['tag'], 'String');
6fd5424b 71 $tagList = $_POST['taglist'];
6a488035 72
077dbf5e
CW
73 if (!CRM_Case_BAO_Case::accessCase($caseId)) {
74 CRM_Utils_System::permissionDenied();
6a488035
TO
75 }
76
77 $tagIds = array();
78 if ($tags) {
79 $tagIds = explode(',', $tags);
80 }
81
6fd5424b 82 if (!empty($tagIds)) {
6fd5424b 83 $params = array(
84 'entity_id' => $caseId,
85 'entity_table' => 'civicrm_case',
86 );
6a488035 87
6fd5424b 88 CRM_Core_BAO_EntityTag::del($params);
6a488035 89
6fd5424b 90 foreach ($tagIds as $tagid) {
91 if (is_numeric($tagid)) {
92 $params['tag_id'] = $tagid;
93 CRM_Core_BAO_EntityTag::add($params);
94 }
6a488035
TO
95 }
96 }
97
6fd5424b 98 if (!empty($tagList)) {
ca4f7cc8 99 CRM_Core_Form_Tag::postProcess($tagList, $caseId, 'civicrm_case', CRM_Core_DAO::$_nullObject);
6fd5424b 100 }
101
6a488035
TO
102 $session = CRM_Core_Session::singleton();
103
104 $activityParams = array();
105 $activityParams['source_contact_id'] = $session->get('userID');
106 $activityParams['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Change Case Tags', 'name');
107 $activityParams['activity_date_time'] = date('YmdHis');
108 $activityParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
109 $activityParams['case_id'] = $caseId;
110 $activityParams['is_auto'] = 0;
111 $activityParams['subject'] = 'Change Case Tags';
112
113 $activity = CRM_Activity_BAO_Activity::create($activityParams);
114
115 $caseParams = array(
116 'activity_id' => $activity->id,
117 'case_id' => $caseId,
118 );
119
120 CRM_Case_BAO_Case::processCaseActivity($caseParams);
121
122 echo 'true';
123 CRM_Utils_System::civiExit();
124 }
125
00be9182 126 public function caseDetails() {
077dbf5e
CW
127 $caseId = CRM_Utils_Type::escape($_GET['caseId'], 'Positive');
128
546abeb1 129 if (!CRM_Case_BAO_Case::accessCase($caseId, FALSE)) {
077dbf5e
CW
130 CRM_Utils_System::permissionDenied();
131 }
132
353ffa53 133 $sql = "SELECT civicrm_case.*, civicrm_case_type.title as case_type
8ffdec17
ARW
134 FROM civicrm_case
135 INNER JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id
136 WHERE civicrm_case.id = %1";
353ffa53 137 $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($caseId, 'Integer')));
6a488035
TO
138
139 if ($dao->fetch()) {
6a488035 140 $caseStatuses = CRM_Case_PseudoConstant::caseStatus();
353ffa53
TO
141 $cs = $caseStatuses[$dao->status_id];
142 $caseDetails = "<table><tr><td>" . ts('Case Subject') . "</td><td>{$dao->subject}</td></tr>
8ffdec17 143 <tr><td>" . ts('Case Type') . "</td><td>{$dao->case_type}</td></tr>
6a488035
TO
144 <tr><td>" . ts('Case Status') . "</td><td>{$cs}</td></tr>
145 <tr><td>" . ts('Case Start Date') . "</td><td>" . CRM_Utils_Date::customFormat($dao->start_date) . "</td></tr>
146 <tr><td>" . ts('Case End Date') . "</td><td></td></tr>" . CRM_Utils_Date::customFormat($dao->end_date) . "</table>";
76bec9f7
CW
147 if (CRM_Utils_Array::value('snippet', $_GET) == 'json') {
148 CRM_Core_Page_AJAX::returnJsonResponse($caseDetails);
149 }
150 else {
151 echo $caseDetails;
152 }
6a488035
TO
153 }
154 else {
76bec9f7 155 CRM_Core_Error::fatal('Could not find valid Case.');
6a488035
TO
156 }
157 CRM_Utils_System::civiExit();
158 }
159
00be9182 160 public function addClient() {
077dbf5e
CW
161 $caseId = CRM_Utils_Type::escape($_POST['caseID'], 'Positive');
162 $contactId = CRM_Utils_Type::escape($_POST['contactID'], 'Positive');
163
164 if (!$contactId || !CRM_Case_BAO_Case::accessCase($caseId)) {
165 CRM_Utils_System::permissionDenied();
166 }
6a488035
TO
167
168 $params = array(
169 'case_id' => $caseId,
170 'contact_id' => $contactId,
171 );
172
173 CRM_Case_BAO_Case::addCaseToContact($params);
174
44466d80
KJ
175 // add case relationships
176 CRM_Case_BAO_Case::addCaseRelationships($caseId, $contactId);
177
6a488035
TO
178 $session = CRM_Core_Session::singleton();
179
180 $activityParams = array();
181 $activityParams['source_contact_id'] = $session->get('userID');
182 $activityParams['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Add Client To Case', 'name');
183 $activityParams['activity_date_time'] = date('YmdHis');
184 $activityParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
185 $activityParams['case_id'] = $caseId;
186 $activityParams['is_auto'] = 0;
187 $activityParams['subject'] = 'Client Added To Case';
188
189 $activity = CRM_Activity_BAO_Activity::create($activityParams);
190
191 $caseParams = array(
192 'activity_id' => $activity->id,
193 'case_id' => $caseId,
194 );
195
196 CRM_Case_BAO_Case::processCaseActivity($caseParams);
ecdef330 197 CRM_Utils_JSON::output(TRUE);
6a488035 198 }
4cd35744
KJ
199
200 /**
100fef9d 201 * Delete relationships specific to case and relationship type
4cd35744 202 */
00be9182 203 public static function deleteCaseRoles() {
353ffa53 204 $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Positive');
077dbf5e
CW
205 $relType = CRM_Utils_Type::escape($_POST['rel_type'], 'Positive');
206
207 if (!$relType || !CRM_Case_BAO_Case::accessCase($caseId)) {
208 CRM_Utils_System::permissionDenied();
209 }
4cd35744
KJ
210
211 $sql = "DELETE FROM civicrm_relationship WHERE case_id={$caseId} AND relationship_type_id={$relType}";
212 CRM_Core_DAO::executeQuery($sql);
213
214 CRM_Utils_System::civiExit();
215 }
6a488035 216}