Merge pull request #12009 from yashodha/translate_strings
[civicrm-core.git] / CRM / Case / Page / AJAX.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 /**
36 * This class contains all case related functions that are called using AJAX
37 */
38 class CRM_Case_Page_AJAX {
39
40 /**
41 * @throws \CRM_Core_Exception
42 */
43 public function processCaseTags() {
44
45 $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Positive');
46 $tags = CRM_Utils_Type::escape($_POST['tag'], 'String');
47 $tagList = $_POST['taglist'];
48
49 if (!CRM_Case_BAO_Case::accessCase($caseId)) {
50 CRM_Utils_System::permissionDenied();
51 }
52
53 $tagIds = array();
54 if ($tags) {
55 $tagIds = explode(',', $tags);
56 }
57
58 $params = array(
59 'entity_id' => $caseId,
60 'entity_table' => 'civicrm_case',
61 );
62
63 CRM_Core_BAO_EntityTag::del($params);
64
65 foreach ($tagIds as $tagid) {
66 if (is_numeric($tagid)) {
67 $params['tag_id'] = $tagid;
68 CRM_Core_BAO_EntityTag::add($params);
69 }
70 }
71
72 if (!empty($tagList)) {
73 CRM_Core_Form_Tag::postProcess($tagList, $caseId, 'civicrm_case');
74 }
75
76 $session = CRM_Core_Session::singleton();
77
78 $activityParams = array();
79 $activityParams['source_contact_id'] = $session->get('userID');
80 $activityParams['activity_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Case Tags');
81 $activityParams['activity_date_time'] = date('YmdHis');
82 $activityParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
83 $activityParams['case_id'] = $caseId;
84 $activityParams['is_auto'] = 0;
85 $activityParams['subject'] = 'Change Case Tags';
86
87 $activity = CRM_Activity_BAO_Activity::create($activityParams);
88
89 $caseParams = array(
90 'activity_id' => $activity->id,
91 'case_id' => $caseId,
92 );
93
94 CRM_Case_BAO_Case::processCaseActivity($caseParams);
95
96 echo 'true';
97 CRM_Utils_System::civiExit();
98 }
99
100 /**
101 * @throws \CiviCRM_API3_Exception
102 */
103 public function caseDetails() {
104 $caseId = CRM_Utils_Type::escape($_GET['caseId'], 'Positive');
105
106 $case = civicrm_api3('Case', 'getsingle', array(
107 'id' => $caseId,
108 'check_permissions' => TRUE,
109 'return' => array('subject', 'case_type_id', 'status_id', 'start_date', 'end_date'))
110 );
111
112 $caseStatuses = CRM_Case_PseudoConstant::caseStatus();
113 $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE);
114 $caseDetails = "<table><tr><td>" . ts('Case Subject') . "</td><td>{$case['subject']}</td></tr>
115 <tr><td>" . ts('Case Type') . "</td><td>{$caseTypes[$case['case_type_id']]}</td></tr>
116 <tr><td>" . ts('Case Status') . "</td><td>{$caseStatuses[$case['status_id']]}</td></tr>
117 <tr><td>" . ts('Case Start Date') . "</td><td>" . CRM_Utils_Date::customFormat($case['start_date']) . "</td></tr>
118 <tr><td>" . ts('Case End Date') . "</td><td></td></tr>" . CRM_Utils_Date::customFormat($case['end_date']) . "</table>";
119
120 if (CRM_Utils_Array::value('snippet', $_GET) == 'json') {
121 CRM_Core_Page_AJAX::returnJsonResponse($caseDetails);
122 }
123
124 echo $caseDetails;
125 CRM_Utils_System::civiExit();
126 }
127
128 /**
129 * @throws \CRM_Core_Exception
130 */
131 public function addClient() {
132 $caseId = CRM_Utils_Type::escape($_POST['caseID'], 'Positive');
133 $contactId = CRM_Utils_Type::escape($_POST['contactID'], 'Positive');
134
135 if (!$contactId || !CRM_Case_BAO_Case::accessCase($caseId)) {
136 CRM_Utils_System::permissionDenied();
137 }
138
139 $params = array(
140 'case_id' => $caseId,
141 'contact_id' => $contactId,
142 );
143
144 CRM_Case_BAO_CaseContact::create($params);
145
146 // add case relationships
147 CRM_Case_BAO_Case::addCaseRelationships($caseId, $contactId);
148
149 $session = CRM_Core_Session::singleton();
150
151 $activityParams = array();
152 $activityParams['source_contact_id'] = $session->get('userID');
153 $activityParams['activity_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Add Client To Case');
154 $activityParams['activity_date_time'] = date('YmdHis');
155 $activityParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
156 $activityParams['case_id'] = $caseId;
157 $activityParams['is_auto'] = 0;
158 $activityParams['subject'] = 'Client Added To Case';
159
160 $activity = CRM_Activity_BAO_Activity::create($activityParams);
161
162 $caseParams = array(
163 'activity_id' => $activity->id,
164 'case_id' => $caseId,
165 );
166
167 CRM_Case_BAO_Case::processCaseActivity($caseParams);
168 CRM_Utils_JSON::output(TRUE);
169 }
170
171 /**
172 * Delete relationships specific to case and relationship type.
173 */
174 public static function deleteCaseRoles() {
175 $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Positive');
176 $cid = CRM_Utils_Type::escape($_POST['cid'], 'Positive');
177 $relType = CRM_Utils_Request::retrieve('rel_type', 'String', CRM_Core_DAO::$_nullObject, TRUE);
178
179 if (!$cid || !CRM_Case_BAO_Case::accessCase($caseId)) {
180 CRM_Utils_System::permissionDenied();
181 }
182
183 list($relTypeId, $a, $b) = explode('_', $relType);
184
185 CRM_Case_BAO_Case::endCaseRole($caseId, $b, $cid, $relTypeId);
186 CRM_Utils_System::civiExit();
187 }
188
189 public static function getCases() {
190 $requiredParameters = array(
191 'type' => 'String',
192 );
193 $optionalParameters = array(
194 'case_type_id' => 'CommaSeparatedIntegers',
195 'status_id' => 'CommaSeparatedIntegers',
196 'all' => 'Positive',
197 );
198 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
199 $params += CRM_Core_Page_AJAX::validateParams($requiredParameters, $optionalParameters);
200
201 $allCases = (bool) $params['all'];
202
203 $cases = CRM_Case_BAO_Case::getCases($allCases, $params);
204
205 $casesDT = array(
206 'recordsFiltered' => $cases['total'],
207 'recordsTotal' => $cases['total'],
208 );
209 unset($cases['total']);
210 $casesDT['data'] = $cases;
211
212 CRM_Utils_JSON::output($casesDT);
213 }
214
215 }