a few more trailing space removals, //ids removals, tabs
[civicrm-core.git] / CRM / Activity / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 *
33 */
34
35/**
36 * This class contains all the function that are called using AJAX (jQuery)
37 */
38class CRM_Activity_Page_AJAX {
39 static function getCaseActivity() {
40 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
41 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
42 $userID = CRM_Utils_Type::escape($_GET['userID'], 'Integer');
43 $context = CRM_Utils_Type::escape(CRM_Utils_Array::value('context', $_GET), 'String');
44
45 $sortMapper = array(
46 0 => 'display_date', 1 => 'ca.subject', 2 => 'ca.activity_type_id',
47 3 => 'acc.sort_name', 4 => 'cc.sort_name', 5 => 'ca.status_id',
48 );
49
50 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
51 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
52 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
53 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
54 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
55
56 $params = $_POST;
57 if ($sort && $sortOrder) {
58 $params['sortname'] = $sort;
59 $params['sortorder'] = $sortOrder;
60 }
61 $params['page'] = ($offset / $rowCount) + 1;
62 $params['rp'] = $rowCount;
63
64 // get the activities related to given case
65 $activities = CRM_Case_BAO_Case::getCaseActivity($caseID, $params, $contactID, $context, $userID);
66
67 $iFilteredTotal = $iTotal = $params['total'];
68 $selectorElements = array('display_date', 'subject', 'type', 'with_contacts', 'reporter', 'status', 'links', 'class');
69
70 echo CRM_Utils_JSON::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
71 CRM_Utils_System::civiExit();
72 }
73
74 static function getCaseGlobalRelationships() {
75 $sortMapper = array(
76 0 => 'sort_name', 1 => 'phone', 2 => 'email',
77 );
1d85d241 78
6a488035
TO
79 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
80 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
81 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
82 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
83 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
1d85d241 84
6a488035
TO
85 $params = $_POST;
86 if ($sort && $sortOrder) {
87 $sortSQL = $sort .' '.$sortOrder;
88 }
1d85d241 89
6a488035
TO
90 // get the activities related to given case
91 $globalGroupInfo = array();
1d85d241 92
6a488035
TO
93 // get the total row count
94 $relGlobalTotalCount = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
95 // limit the rows
1d85d241
DL
96 $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, $sortSQL, $showLinks = TRUE, FALSE, $offset, $rowCount);
97
6a488035
TO
98 $iFilteredTotal = $iTotal = $relGlobalTotalCount;
99 $selectorElements = array('sort_name', 'phone', 'email');
1d85d241 100
6a488035
TO
101 echo CRM_Utils_JSON::encodeDataTableSelector($relGlobal, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
102 CRM_Utils_System::civiExit();
1d85d241
DL
103 }
104
6a488035
TO
105 static function getCaseClientRelationships() {
106 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
107 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
1d85d241 108
6a488035
TO
109 $sortMapper = array(
110 0 => 'relation', 1 => 'name', 2 => 'phone', 3 => 'email'
111 );
1d85d241 112
6a488035
TO
113 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
114 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
115 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
116 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation';
117 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
1d85d241 118
6a488035
TO
119 $params = $_POST;
120 if ($sort && $sortOrder) {
121 $sortSQL = $sort .' '.$sortOrder;
122 }
123
124 // Retrieve ALL client relationships
125 $relClient = CRM_Contact_BAO_Relationship::getRelationship($contactID,
126 CRM_Contact_BAO_Relationship::CURRENT,
127 0, 0, 0, NULL, NULL, FALSE
128 );
1d85d241 129
6a488035 130 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
1d85d241 131
6a488035
TO
132 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
133 // so they don't show up twice.
134 $clientRelationships = array();
135 foreach ($relClient as $r) {
136 if (!array_key_exists($r['id'], $caseRelationships)) {
137 $clientRelationships[] = $r;
138 }
139 }
1d85d241 140
6a488035
TO
141 // sort clientRelationships array using jquery call params
142 foreach ($clientRelationships as $key => $row) {
1d85d241 143 $sortArray[$key] = $row[$sort];
6a488035
TO
144 }
145 $sort_type = "SORT_".strtoupper($sortOrder);
146 $sort_function = "array_multisort(\$sortArray, ".$sort_type.", \$clientRelationships);";
147 eval($sort_function);
1d85d241 148
6a488035
TO
149 //limit the rows
150 $allClientRelationships = $clientRelationships;
151 $clientRelationships = array_slice($allClientRelationships, $offset, $rowCount, TRUE);
1d85d241 152
6a488035
TO
153 // after sort we can update username fields to be a url
154 foreach($clientRelationships as $key => $value) {
155 $clientRelationships[$key]['name'] = '<a href='.CRM_Utils_System::url('civicrm/contact/view',
156 'action=view&reset=1&cid='.$clientRelationships[$key]['cid']).'>'.$clientRelationships[$key]['name'].'</a>';
157 }
1d85d241 158
6a488035
TO
159 $iFilteredTotal = $iTotal = $params['total'] = count($allClientRelationships);
160 $selectorElements = array('relation', 'name', 'phone', 'email');
1d85d241 161
6a488035
TO
162 echo CRM_Utils_JSON::encodeDataTableSelector($clientRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
163 CRM_Utils_System::civiExit();
1d85d241
DL
164 }
165
166
6a488035
TO
167 static function getCaseRoles() {
168 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
169 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
1d85d241 170
6a488035
TO
171 $sortMapper = array(
172 0 => 'relation', 1 => 'name', 2 => 'phone', 3 => 'email', 4 => 'actions'
173 );
1d85d241 174
6a488035
TO
175 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
176 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
177 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
178 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation';
179 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
1d85d241 180
6a488035
TO
181 $params = $_POST;
182 if ($sort && $sortOrder) {
183 $sortSQL = $sort .' '.$sortOrder;
184 }
185
186 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
187 $caseTypeName = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
188 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
189 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
1d85d241 190
6a488035 191 $hasAccessToAllCases = CRM_Core_Permission::check('access all cases and activities');
1d85d241 192
6a488035
TO
193 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
194 if (!empty($managerRoleId)) {
195 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
196 }
1d85d241 197
6a488035
TO
198 foreach ($caseRelationships as $key => $value) {
199 //calculate roles that don't have relationships
200 if (CRM_Utils_Array::value($value['relation_type'], $caseRoles)) {
201 //keep naming from careRoles array
202 $caseRelationships[$key]['relation'] = $caseRoles[$value['relation_type']];
203 unset($caseRoles[$value['relation_type']]);
204 }
205 // mark orginal case relationships record to use on setting edit links below
206 $caseRelationships[$key]['source'] = 'caseRel';
207 }
1d85d241 208
6a488035
TO
209 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
210
211 // move/transform caseRoles array data to caseRelationships
212 // for sorting and display
213 foreach($caseRoles as $id => $value) {
214 if ($id != "client") {
215 $rel = array();
216 $rel['relation'] = $value;
217 $rel['relation_type'] = $id;
218 $rel['name'] = '(not assigned)';
219 $rel['phone'] = '';
220 $rel['email'] = '';
221 $rel['source'] = 'caseRoles';
222 $caseRelationships[] = $rel;
223 } else {
224 foreach($value as $clientRole) {
225 $relClient = array();
226 $relClient['relation'] = 'Client';
227 $relClient['name'] = $clientRole['sort_name'];
228 $relClient['phone'] = $clientRole['phone'];
229 $relClient['email'] = $clientRole['email'];
230 $relClient['cid'] = $clientRole['contact_id'];
231 $relClient['source'] = 'contact';
232 $caseRelationships[] = $relClient;
233 }
234 }
235 }
236
237 // sort clientRelationships array using jquery call params
238 foreach ($caseRelationships as $key => $row) {
239 $sortArray[$key] = $row[$sort];
240 }
241
242 $sort_type = "SORT_".strtoupper($sortOrder);
243
244 $sort_function = "array_multisort(\$sortArray, ".$sort_type.", \$caseRelationships);";
245 eval($sort_function);
246
247 //limit rows display
248 $allCaseRelationships = $caseRelationships;
249 $caseRelationships = array_slice($allCaseRelationships, $offset, $rowCount, TRUE);
250
251 // set user name, email and edit columns links
252 // idx will count number of current row / needed by edit links
253 $idx = 1;
254 foreach ($caseRelationships as $key => $row) {
255 // view user links
256 if ($caseRelationships[$key]['cid']) {
4cd35744 257 $caseRelationships[$key]['name'] = '<a href='.CRM_Utils_System::url('civicrm/contact/view',
6a488035
TO
258 'action=view&reset=1&cid='.$caseRelationships[$key]['cid']).'>'.$caseRelationships[$key]['name'].'</a>';
259 }
260 // email column links/icon
261 if ($caseRelationships[$key]['email']) {
4cd35744 262 $caseRelationships[$key]['email'] = '<a href="'.CRM_Utils_System::url('civicrm/contact/view/activity', 'action=reset=1&action=add&atype=3&cid='.$caseRelationships[$key]['cid']).'" title="compose and send an email"><div class="icon email-icon" title="compose and send an email"></div>
6a488035
TO
263 </a>';
264 }
265 // edit links
266 if ($hasAccessToAllCases) {
267 switch($caseRelationships[$key]['source']){
268 case 'caseRel':
1d85d241 269 $caseRelationships[$key]['actions'] =
4cd35744 270 '<a href="#" title="edit case role" onclick="createRelationship( '.$caseRelationships[$key]['relation_type'].', '.$caseRelationships[$key]['cid'].', '.$caseRelationships[$key]['rel_id'].', '.$idx.', \''.$caseRelationships[$key]['relation'].'\' );return false;"><div class="icon edit-icon" ></div></a>&nbsp;&nbsp;<a href="#" class="case-role-delete" case_id="'.$caseID.'" rel_type="'.$caseRelationships[$key]['relation_type'].'"><div class="icon delete-icon" title="remove contact from case role"></div></a>';
6a488035 271 break;
1d85d241 272
6a488035 273 case 'caseRoles':
1d85d241 274 $caseRelationships[$key]['actions'] =
4cd35744 275 '<a href="#" title="edit case role" onclick="createRelationship('.$caseRelationships[$key]['relation_type'].', null, null, '.$idx.', \''.$caseRelationships[$key]['relation'].'\');return false;"><div class="icon edit-icon"></div></a>';
6a488035
TO
276 break;
277 }
278 } else {
279 $caseRelationships[$key]['actions'] = '';
280 }
281 $idx++;
282 }
283 $iFilteredTotal = $iTotal = $params['total'] = count($allCaseRelationships);
284 $selectorElements = array('relation', 'name', 'phone', 'email', 'actions');
1d85d241 285
6a488035
TO
286 echo CRM_Utils_JSON::encodeDataTableSelector($caseRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
287 CRM_Utils_System::civiExit();
1d85d241
DL
288 }
289
6a488035
TO
290 static function convertToCaseActivity() {
291 $params = array('caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode');
292 foreach ($params as $param) {
293 $vals[$param] = CRM_Utils_Array::value($param, $_POST);
294 }
295
296 $retval = self::_convertToCaseActivity($vals);
297
298 echo json_encode($retval);
299 CRM_Utils_System::civiExit();
300 }
301
302 static function _convertToCaseActivity($params) {
303 if (!$params['activityID'] || !$params['caseID']) {
304 return (array('error_msg' => 'required params missing.'));
305 }
306
307 $otherActivity = new CRM_Activity_DAO_Activity();
308 $otherActivity->id = $params['activityID'];
309 if (!$otherActivity->find(TRUE)) {
310 return (array('error_msg' => 'activity record is missing.'));
311 }
312 $actDateTime = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time);
313
314 //create new activity record.
315 $mainActivity = new CRM_Activity_DAO_Activity();
316 $mainActVals = array();
317 CRM_Core_DAO::storeValues($otherActivity, $mainActVals);
318
319 //get new activity subject.
320 if (!empty($params['newSubject'])) {
321 $mainActVals['subject'] = $params['newSubject'];
322 }
323
324 $mainActivity->copyValues($mainActVals);
325 $mainActivity->id = NULL;
326 $mainActivity->activity_date_time = $actDateTime;
327 //make sure this is current revision.
328 $mainActivity->is_current_revision = TRUE;
329 //drop all relations.
330 $mainActivity->parent_id = $mainActivity->original_id = NULL;
331
332 $mainActivity->save();
333 $mainActivityId = $mainActivity->id;
334 CRM_Activity_BAO_Activity::logActivityAction($mainActivity);
335 $mainActivity->free();
336
337 /* Mark previous activity as deleted. If it was a non-case activity
338 * then just change the subject.
339 */
340
341 if (in_array($params['mode'], array(
342 'move', 'file'))) {
343 $caseActivity = new CRM_Case_DAO_CaseActivity();
344 $caseActivity->case_id = $params['caseID'];
345 $caseActivity->activity_id = $otherActivity->id;
346 if ($params['mode'] == 'move' || $caseActivity->find(TRUE)) {
347 $otherActivity->is_deleted = 1;
348 }
349 else {
350 $otherActivity->subject = ts('(Filed on case %1)', array(
351 1 => $params['caseID'])) . ' ' . $otherActivity->subject;
352 }
353 $otherActivity->activity_date_time = $actDateTime;
354 $otherActivity->save();
355
356 $caseActivity->free();
357 }
358 $otherActivity->free();
359
360 $targetContacts = array();
361 if (!empty($params['targetContactIds'])) {
362 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
363 }
f813f78e 364
a24b3694 365 $activityContacts = CRM_Core_PseudoConstant::activityContacts('name');
366 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
367 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
368 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
369
6a488035
TO
370 foreach ($targetContacts as $key => $value) {
371 $targ_params = array(
372 'activity_id' => $mainActivityId,
1d85d241 373 'contact_id' => $value,
a24b3694 374 'record_type_id' => $targetID
6a488035 375 );
1d85d241 376 CRM_Activity_BAO_ActivityContact::create($targ_params);
6a488035
TO
377 }
378
379 // typically this will be empty, since assignees on another case may be completely different
380 $assigneeContacts = array();
381 if (!empty($params['assigneeContactIds'])) {
382 $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
383 }
384 foreach ($assigneeContacts as $key => $value) {
385 $assigneeParams = array(
386 'activity_id' => $mainActivityId,
1d85d241 387 'contact_id' => $value,
a24b3694 388 'record_type_id' => $assigneeID
6a488035 389 );
1d85d241 390 CRM_Activity_BAO_ActivityContact::create($assigneeParams);
6a488035
TO
391 }
392
393 //attach newly created activity to case.
394 $caseActivity = new CRM_Case_DAO_CaseActivity();
395 $caseActivity->case_id = $params['caseID'];
396 $caseActivity->activity_id = $mainActivityId;
397 $caseActivity->save();
398 $error_msg = $caseActivity->_lastError;
399 $caseActivity->free();
400
401 $params['mainActivityId'] = $mainActivityId;
402 CRM_Activity_BAO_Activity::copyExtendedActivityData($params);
403
404 return (array('error_msg' => $error_msg, 'newId' => $mainActivity->id));
405 }
406
407 static function getContactActivity() {
408 $contactID = CRM_Utils_Type::escape($_POST['contact_id'], 'Integer');
409 $context = CRM_Utils_Type::escape(CRM_Utils_Array::value('context', $_GET), 'String');
410
411 $sortMapper = array(
412 0 => 'activity_type', 1 => 'subject', 2 => 'source_contact_name',
50f5a393 413 3 => '', 4 => 'activity_date_time', 5 => 'status_id',
6a488035
TO
414 );
415
416 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
417 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
418 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
419 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
420 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
421
422 $params = $_POST;
423 if ($sort && $sortOrder) {
424 $params['sortBy'] = $sort . ' ' . $sortOrder;
425 }
426
427 $params['page'] = ($offset / $rowCount) + 1;
428 $params['rp'] = $rowCount;
429
430 $params['contact_id'] = $contactID;
431 $params['context'] = $context;
432
433 // get the contact activities
434 $activities = CRM_Activity_BAO_Activity::getContactActivitySelector($params);
435
436 // store the activity filter preference CRM-11761
437 $session = CRM_Core_Session::singleton();
438 $userID = $session->get('userID');
439 if ($userID) {
1d85d241 440 $activityFilter =
6a488035
TO
441 array('activity_type_filter_id' => CRM_Utils_Array::value('activity_type_id', $params),
442 'activity_type_exclude_filter_id' => CRM_Utils_Array::value('activity_type_exclude_id', $params));
443 CRM_Core_BAO_Setting::setItem($activityFilter,
444 CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME,
445 'activity_tab_filter',
446 NULL,
447 $userID,
448 $userID
449 );
450 }
1d85d241 451
6a488035
TO
452 $iFilteredTotal = $iTotal = $params['total'];
453 $selectorElements = array(
454 'activity_type', 'subject', 'source_contact',
455 'target_contact', 'assignee_contact',
50f5a393 456 'activity_date', 'status','links', 'class',
6a488035
TO
457 );
458
459 echo CRM_Utils_JSON::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
460 CRM_Utils_System::civiExit();
461 }
462}
463