Merge pull request #3848 from totten/master-debug-ui
[civicrm-core.git] / CRM / Case / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 *
33 */
34
35 /**
36 * This class contains all case related functions that are called using AJAX (jQuery)
37 */
38 class CRM_Case_Page_AJAX {
39
40 /**
41 * Retrieve unclosed cases.
42 */
43 static function unclosedCases() {
44 $params = array(
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'),
47 );
48
49 $excludeCaseIds = array();
50 if (!empty($_GET['excludeCaseIds'])) {
51 $excludeCaseIds = explode(',', CRM_Utils_Type::escape($_GET['excludeCaseIds'], 'String'));
52 }
53 $unclosedCases = CRM_Case_BAO_Case::getUnclosedCases($params, $excludeCaseIds, TRUE, TRUE);
54 $results = array();
55 foreach ($unclosedCases as $caseId => $details) {
56 $results[] = array(
57 'id' => $caseId,
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'] . ')'),
61 'extra' => $details,
62 );
63 }
64 print json_encode($results);
65 CRM_Utils_System::civiExit();
66 }
67
68 function processCaseTags() {
69
70 $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Integer');
71 $tags = CRM_Utils_Type::escape($_POST['tag'], 'String');
72 $tagList = $_POST['taglist'];
73
74 if (empty($caseId)) {
75 echo 'false';
76 CRM_Utils_System::civiExit();
77 }
78
79 $tagIds = array();
80 if ($tags) {
81 $tagIds = explode(',', $tags);
82 }
83
84 if (!empty($tagIds)) {
85 $params = array(
86 'entity_id' => $caseId,
87 'entity_table' => 'civicrm_case',
88 );
89
90 CRM_Core_BAO_EntityTag::del($params);
91
92 foreach ($tagIds as $tagid) {
93 if (is_numeric($tagid)) {
94 $params['tag_id'] = $tagid;
95 CRM_Core_BAO_EntityTag::add($params);
96 }
97 }
98 }
99
100 if (!empty($tagList)) {
101 CRM_Core_Form_Tag::postProcess($tagList, $caseId, 'civicrm_case', CRM_Core_DAO::$_nullObject);
102 }
103
104 $session = CRM_Core_Session::singleton();
105
106 $activityParams = array();
107 $activityParams['source_contact_id'] = $session->get('userID');
108 $activityParams['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Change Case Tags', 'name');
109 $activityParams['activity_date_time'] = date('YmdHis');
110 $activityParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
111 $activityParams['case_id'] = $caseId;
112 $activityParams['is_auto'] = 0;
113 $activityParams['subject'] = 'Change Case Tags';
114
115 $activity = CRM_Activity_BAO_Activity::create($activityParams);
116
117 $caseParams = array(
118 'activity_id' => $activity->id,
119 'case_id' => $caseId,
120 );
121
122 CRM_Case_BAO_Case::processCaseActivity($caseParams);
123
124 echo 'true';
125 CRM_Utils_System::civiExit();
126 }
127
128 function caseDetails() {
129 $caseId = CRM_Utils_Type::escape($_GET['caseId'], 'Integer');
130 $sql = "SELECT civicrm_case.*, civicrm_case_type.title as case_type
131 FROM civicrm_case
132 INNER JOIN civicrm_case_type ON civicrm_case.case_type_id = civicrm_case_type.id
133 WHERE civicrm_case.id = %1";
134 $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($caseId, 'Integer')));
135
136 if ($dao->fetch()) {
137 $caseStatuses = CRM_Case_PseudoConstant::caseStatus();
138 $cs = $caseStatuses[$dao->status_id];
139 $caseDetails = "<table><tr><td>" . ts('Case Subject') . "</td><td>{$dao->subject}</td></tr>
140 <tr><td>" . ts('Case Type') . "</td><td>{$dao->case_type}</td></tr>
141 <tr><td>" . ts('Case Status') . "</td><td>{$cs}</td></tr>
142 <tr><td>" . ts('Case Start Date') . "</td><td>" . CRM_Utils_Date::customFormat($dao->start_date) . "</td></tr>
143 <tr><td>" . ts('Case End Date') . "</td><td></td></tr>" . CRM_Utils_Date::customFormat($dao->end_date) . "</table>";
144 echo $caseDetails;
145 }
146 else {
147 echo ts('Could not find valid Case!');
148 }
149 CRM_Utils_System::civiExit();
150 }
151
152 function addClient() {
153 $caseId = CRM_Utils_Type::escape($_POST['caseID'], 'Integer');
154 $contactId = CRM_Utils_Type::escape($_POST['contactID'], 'Integer');
155
156 $params = array(
157 'case_id' => $caseId,
158 'contact_id' => $contactId,
159 );
160
161 CRM_Case_BAO_Case::addCaseToContact($params);
162
163 // add case relationships
164 CRM_Case_BAO_Case::addCaseRelationships($caseId, $contactId);
165
166 $session = CRM_Core_Session::singleton();
167
168 $activityParams = array();
169 $activityParams['source_contact_id'] = $session->get('userID');
170 $activityParams['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Add Client To Case', 'name');
171 $activityParams['activity_date_time'] = date('YmdHis');
172 $activityParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
173 $activityParams['case_id'] = $caseId;
174 $activityParams['is_auto'] = 0;
175 $activityParams['subject'] = 'Client Added To Case';
176
177 $activity = CRM_Activity_BAO_Activity::create($activityParams);
178
179 $caseParams = array(
180 'activity_id' => $activity->id,
181 'case_id' => $caseId,
182 );
183
184 CRM_Case_BAO_Case::processCaseActivity($caseParams);
185 echo json_encode(TRUE);
186 CRM_Utils_System::civiExit();
187 }
188
189 /**
190 * Function to delete relationships specific to case and relationship type
191 */
192 static function deleteCaseRoles() {
193 $caseId = CRM_Utils_Type::escape($_POST['case_id'], 'Integer');
194 $relType = CRM_Utils_Type::escape($_POST['rel_type'], 'Integer');
195
196 $sql = "DELETE FROM civicrm_relationship WHERE case_id={$caseId} AND relationship_type_id={$relType}";
197 CRM_Core_DAO::executeQuery($sql);
198
199 CRM_Utils_System::civiExit();
200 }
201 }
202