Merge pull request #8189 from kenwest/CRM-18448
[civicrm-core.git] / CRM / Case / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
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
3924e596
CW
40 /**
41 * @throws \CRM_Core_Exception
42 */
00be9182 43 public function processCaseTags() {
6a488035 44
077dbf5e 45 $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Positive');
6a488035 46 $tags = CRM_Utils_Type::escape($_POST['tag'], 'String');
6fd5424b 47 $tagList = $_POST['taglist'];
6a488035 48
077dbf5e
CW
49 if (!CRM_Case_BAO_Case::accessCase($caseId)) {
50 CRM_Utils_System::permissionDenied();
6a488035
TO
51 }
52
53 $tagIds = array();
54 if ($tags) {
55 $tagIds = explode(',', $tags);
56 }
57
6fd5424b 58 if (!empty($tagIds)) {
6fd5424b 59 $params = array(
60 'entity_id' => $caseId,
61 'entity_table' => 'civicrm_case',
62 );
6a488035 63
6fd5424b 64 CRM_Core_BAO_EntityTag::del($params);
6a488035 65
6fd5424b 66 foreach ($tagIds as $tagid) {
67 if (is_numeric($tagid)) {
68 $params['tag_id'] = $tagid;
69 CRM_Core_BAO_EntityTag::add($params);
70 }
6a488035
TO
71 }
72 }
73
6fd5424b 74 if (!empty($tagList)) {
ca4f7cc8 75 CRM_Core_Form_Tag::postProcess($tagList, $caseId, 'civicrm_case', CRM_Core_DAO::$_nullObject);
6fd5424b 76 }
77
6a488035
TO
78 $session = CRM_Core_Session::singleton();
79
80 $activityParams = array();
81 $activityParams['source_contact_id'] = $session->get('userID');
82 $activityParams['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Change Case Tags', 'name');
83 $activityParams['activity_date_time'] = date('YmdHis');
84 $activityParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
85 $activityParams['case_id'] = $caseId;
86 $activityParams['is_auto'] = 0;
87 $activityParams['subject'] = 'Change Case Tags';
88
89 $activity = CRM_Activity_BAO_Activity::create($activityParams);
90
91 $caseParams = array(
92 'activity_id' => $activity->id,
93 'case_id' => $caseId,
94 );
95
96 CRM_Case_BAO_Case::processCaseActivity($caseParams);
97
98 echo 'true';
99 CRM_Utils_System::civiExit();
100 }
101
3924e596
CW
102 /**
103 * @throws \CiviCRM_API3_Exception
104 */
00be9182 105 public function caseDetails() {
077dbf5e
CW
106 $caseId = CRM_Utils_Type::escape($_GET['caseId'], 'Positive');
107
0a1a8b63
CW
108 $case = civicrm_api3('Case', 'getsingle', array(
109 'id' => $caseId,
110 'check_permissions' => TRUE,
111 'return' => array('subject', 'case_type_id', 'status_id', 'start_date', 'end_date'))
112 );
077dbf5e 113
3924e596
CW
114 $caseStatuses = CRM_Case_PseudoConstant::caseStatus();
115 $caseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE);
116 $caseDetails = "<table><tr><td>" . ts('Case Subject') . "</td><td>{$case['subject']}</td></tr>
117 <tr><td>" . ts('Case Type') . "</td><td>{$caseTypes[$case['case_type_id']]}</td></tr>
118 <tr><td>" . ts('Case Status') . "</td><td>{$caseStatuses[$case['status_id']]}</td></tr>
119 <tr><td>" . ts('Case Start Date') . "</td><td>" . CRM_Utils_Date::customFormat($case['start_date']) . "</td></tr>
120 <tr><td>" . ts('Case End Date') . "</td><td></td></tr>" . CRM_Utils_Date::customFormat($case['end_date']) . "</table>";
121
122 if (CRM_Utils_Array::value('snippet', $_GET) == 'json') {
123 CRM_Core_Page_AJAX::returnJsonResponse($caseDetails);
6a488035 124 }
3924e596
CW
125
126 echo $caseDetails;
6a488035
TO
127 CRM_Utils_System::civiExit();
128 }
129
3924e596
CW
130 /**
131 * @throws \CRM_Core_Exception
132 */
00be9182 133 public function addClient() {
077dbf5e
CW
134 $caseId = CRM_Utils_Type::escape($_POST['caseID'], 'Positive');
135 $contactId = CRM_Utils_Type::escape($_POST['contactID'], 'Positive');
136
137 if (!$contactId || !CRM_Case_BAO_Case::accessCase($caseId)) {
138 CRM_Utils_System::permissionDenied();
139 }
6a488035
TO
140
141 $params = array(
142 'case_id' => $caseId,
143 'contact_id' => $contactId,
144 );
145
ff9340a4 146 CRM_Case_BAO_CaseContact::create($params);
6a488035 147
44466d80
KJ
148 // add case relationships
149 CRM_Case_BAO_Case::addCaseRelationships($caseId, $contactId);
150
6a488035
TO
151 $session = CRM_Core_Session::singleton();
152
153 $activityParams = array();
154 $activityParams['source_contact_id'] = $session->get('userID');
155 $activityParams['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Add Client To Case', 'name');
156 $activityParams['activity_date_time'] = date('YmdHis');
157 $activityParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
158 $activityParams['case_id'] = $caseId;
159 $activityParams['is_auto'] = 0;
160 $activityParams['subject'] = 'Client Added To Case';
161
162 $activity = CRM_Activity_BAO_Activity::create($activityParams);
163
164 $caseParams = array(
165 'activity_id' => $activity->id,
166 'case_id' => $caseId,
167 );
168
169 CRM_Case_BAO_Case::processCaseActivity($caseParams);
ecdef330 170 CRM_Utils_JSON::output(TRUE);
6a488035 171 }
4cd35744
KJ
172
173 /**
fe482240 174 * Delete relationships specific to case and relationship type.
4cd35744 175 */
00be9182 176 public static function deleteCaseRoles() {
353ffa53 177 $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Positive');
3b1c37fe
CW
178 $cid = CRM_Utils_Type::escape($_POST['cid'], 'Positive');
179 $relType = CRM_Utils_Request::retrieve('rel_type', 'String', CRM_Core_DAO::$_nullObject, TRUE);
077dbf5e 180
3b1c37fe 181 if (!$cid || !CRM_Case_BAO_Case::accessCase($caseId)) {
077dbf5e
CW
182 CRM_Utils_System::permissionDenied();
183 }
4cd35744 184
3b1c37fe 185 list($relTypeId, $a, $b) = explode('_', $relType);
4cd35744 186
3b1c37fe 187 CRM_Case_BAO_Case::endCaseRole($caseId, $b, $cid, $relTypeId);
4cd35744
KJ
188 CRM_Utils_System::civiExit();
189 }
96025800 190
6a488035 191}