CRM-15789 - Remove civicrm.css styles redundant with jQuery UI
[civicrm-core.git] / CRM / Activity / 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 the function that are called using AJAX (jQuery)
37 */
38class CRM_Activity_Page_AJAX {
00be9182 39 public static function getCaseActivity() {
6a488035
TO
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(
32864ccf
TO
46 0 => 'display_date',
47 1 => 'ca.subject',
48 2 => 'ca.activity_type_id',
49 3 => 'acc.sort_name',
50 4 => 'cc.sort_name',
51 5 => 'ca.status_id',
6a488035
TO
52 );
53
54 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
55 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
56 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
57 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
58 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
59
60 $params = $_POST;
61 if ($sort && $sortOrder) {
62 $params['sortname'] = $sort;
63 $params['sortorder'] = $sortOrder;
64 }
65 $params['page'] = ($offset / $rowCount) + 1;
66 $params['rp'] = $rowCount;
67
68 // get the activities related to given case
69 $activities = CRM_Case_BAO_Case::getCaseActivity($caseID, $params, $contactID, $context, $userID);
70
71 $iFilteredTotal = $iTotal = $params['total'];
72 $selectorElements = array('display_date', 'subject', 'type', 'with_contacts', 'reporter', 'status', 'links', 'class');
73
74 echo CRM_Utils_JSON::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
75 CRM_Utils_System::civiExit();
76 }
77
00be9182 78 public static function getCaseGlobalRelationships() {
6a488035 79 $sortMapper = array(
32864ccf
TO
80 0 => 'sort_name',
81 1 => 'phone',
82 2 => 'email',
6a488035 83 );
1d85d241 84
6a488035
TO
85 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
86 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
87 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
88 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
89 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
5b6db2c7 90
6a488035 91 $params = $_POST;
5b6db2c7 92 //CRM-14466 initialize variable to avoid php notice
7a53d599 93 $sortSQL = "";
6a488035 94 if ($sort && $sortOrder) {
92fcb95f 95 $sortSQL = $sort . ' ' . $sortOrder;
6a488035 96 }
1d85d241 97
6a488035
TO
98 // get the activities related to given case
99 $globalGroupInfo = array();
1d85d241 100
6a488035
TO
101 // get the total row count
102 $relGlobalTotalCount = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
103 // limit the rows
1d85d241
DL
104 $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, $sortSQL, $showLinks = TRUE, FALSE, $offset, $rowCount);
105
6a488035
TO
106 $iFilteredTotal = $iTotal = $relGlobalTotalCount;
107 $selectorElements = array('sort_name', 'phone', 'email');
1d85d241 108
6a488035
TO
109 echo CRM_Utils_JSON::encodeDataTableSelector($relGlobal, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
110 CRM_Utils_System::civiExit();
1d85d241
DL
111 }
112
00be9182 113 public static function getCaseClientRelationships() {
6a488035
TO
114 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
115 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
1d85d241 116
6a488035 117 $sortMapper = array(
32864ccf
TO
118 0 => 'relation',
119 1 => 'name',
120 2 => 'phone',
121 3 => 'email'
6a488035 122 );
1d85d241 123
6a488035
TO
124 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
125 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
126 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
127 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation';
128 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
1d85d241 129
6a488035
TO
130 $params = $_POST;
131 if ($sort && $sortOrder) {
92fcb95f 132 $sortSQL = $sort . ' ' . $sortOrder;
6a488035
TO
133 }
134
135 // Retrieve ALL client relationships
136 $relClient = CRM_Contact_BAO_Relationship::getRelationship($contactID,
137 CRM_Contact_BAO_Relationship::CURRENT,
138 0, 0, 0, NULL, NULL, FALSE
139 );
1d85d241 140
6a488035 141 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
1d85d241 142
6a488035
TO
143 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
144 // so they don't show up twice.
145 $clientRelationships = array();
146 foreach ($relClient as $r) {
147 if (!array_key_exists($r['id'], $caseRelationships)) {
148 $clientRelationships[] = $r;
149 }
150 }
1d85d241 151
6a488035
TO
152 // sort clientRelationships array using jquery call params
153 foreach ($clientRelationships as $key => $row) {
1d85d241 154 $sortArray[$key] = $row[$sort];
6a488035 155 }
0e2079a1
CW
156 $sort_type = "SORT_" . strtoupper($sortOrder);
157 array_multisort($sortArray, constant($sort_type), $clientRelationships);
1d85d241 158
6a488035
TO
159 //limit the rows
160 $allClientRelationships = $clientRelationships;
161 $clientRelationships = array_slice($allClientRelationships, $offset, $rowCount, TRUE);
1d85d241 162
6a488035 163 // after sort we can update username fields to be a url
22e263ad 164 foreach ($clientRelationships as $key => $value) {
92fcb95f
TO
165 $clientRelationships[$key]['name'] = '<a href=' . CRM_Utils_System::url('civicrm/contact/view',
166 'action=view&reset=1&cid=' . $clientRelationships[$key]['cid']) . '>' . $clientRelationships[$key]['name'] . '</a>';
6a488035 167 }
1d85d241 168
6a488035
TO
169 $iFilteredTotal = $iTotal = $params['total'] = count($allClientRelationships);
170 $selectorElements = array('relation', 'name', 'phone', 'email');
1d85d241 171
6a488035
TO
172 echo CRM_Utils_JSON::encodeDataTableSelector($clientRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
173 CRM_Utils_System::civiExit();
1d85d241
DL
174 }
175
176
00be9182 177 public static function getCaseRoles() {
6a488035
TO
178 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
179 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
1d85d241 180
6a488035 181 $sortMapper = array(
32864ccf
TO
182 0 => 'relation',
183 1 => 'name',
184 2 => 'phone',
185 3 => 'email',
186 4 => 'actions'
6a488035 187 );
1d85d241 188
6a488035
TO
189 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
190 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
191 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
192 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation';
193 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
1d85d241 194
6a488035
TO
195 $params = $_POST;
196 if ($sort && $sortOrder) {
92fcb95f 197 $sortSQL = $sort . ' ' . $sortOrder;
6a488035
TO
198 }
199
200 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
201 $caseTypeName = CRM_Case_BAO_Case::getCaseType($caseID, 'name');
202 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
203 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
1d85d241 204
6a488035 205 $hasAccessToAllCases = CRM_Core_Permission::check('access all cases and activities');
1d85d241 206
6a488035
TO
207 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
208 if (!empty($managerRoleId)) {
209 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
210 }
1d85d241 211
6a488035
TO
212 foreach ($caseRelationships as $key => $value) {
213 //calculate roles that don't have relationships
a7488080 214 if (!empty($caseRoles[$value['relation_type']])) {
6a488035
TO
215 //keep naming from careRoles array
216 $caseRelationships[$key]['relation'] = $caseRoles[$value['relation_type']];
217 unset($caseRoles[$value['relation_type']]);
218 }
219 // mark orginal case relationships record to use on setting edit links below
220 $caseRelationships[$key]['source'] = 'caseRel';
221 }
1d85d241 222
6a488035
TO
223 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
224
225 // move/transform caseRoles array data to caseRelationships
226 // for sorting and display
5b6db2c7 227 // CRM-14466 added cid to the non-client array to avoid php notice
22e263ad 228 foreach ($caseRoles as $id => $value) {
6a488035
TO
229 if ($id != "client") {
230 $rel = array();
231 $rel['relation'] = $value;
232 $rel['relation_type'] = $id;
233 $rel['name'] = '(not assigned)';
234 $rel['phone'] = '';
235 $rel['email'] = '';
236 $rel['source'] = 'caseRoles';
237 $caseRelationships[] = $rel;
0db6c3e1
TO
238 }
239 else {
22e263ad 240 foreach ($value as $clientRole) {
6a488035
TO
241 $relClient = array();
242 $relClient['relation'] = 'Client';
243 $relClient['name'] = $clientRole['sort_name'];
244 $relClient['phone'] = $clientRole['phone'];
245 $relClient['email'] = $clientRole['email'];
246 $relClient['cid'] = $clientRole['contact_id'];
247 $relClient['source'] = 'contact';
248 $caseRelationships[] = $relClient;
249 }
250 }
251 }
252
253 // sort clientRelationships array using jquery call params
254 foreach ($caseRelationships as $key => $row) {
255 $sortArray[$key] = $row[$sort];
256 }
257
0e2079a1
CW
258 $sort_type = "SORT_" . strtoupper($sortOrder);
259 array_multisort($sortArray, constant($sort_type), $caseRelationships);
6a488035
TO
260
261 //limit rows display
262 $allCaseRelationships = $caseRelationships;
263 $caseRelationships = array_slice($allCaseRelationships, $offset, $rowCount, TRUE);
5b6db2c7 264
6a488035
TO
265 // set user name, email and edit columns links
266 // idx will count number of current row / needed by edit links
267 $idx = 1;
c91df8b4
CW
268 foreach ($caseRelationships as &$row) {
269 // Get rid of the "<br />(Case Manager)" from label
270 list($typeLabel) = explode('<', $row['relation']);
6a488035 271 // view user links
3662628a 272 if (!empty($row['cid'])) {
92fcb95f
TO
273 $row['name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System::url('civicrm/contact/view',
274 'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['name'] . '</a>';
5b6db2c7 275 }
6a488035 276 // email column links/icon
c91df8b4 277 if ($row['email']) {
52604b19 278 $row['email'] = '<a class="crm-hover-button crm-popup" href="' . CRM_Utils_System::url('civicrm/activity/email/add', 'reset=1&action=add&atype=3&cid=' . $row['cid']) . '&caseid=' . $caseID . '" title="' . ts('Send an Email') . '"><span class="icon ui-icon-mail-closed"></span></a>';
6a488035
TO
279 }
280 // edit links
3662628a 281 $row['actions'] = '';
6a488035 282 if ($hasAccessToAllCases) {
e3756c36
CW
283 $contactType = empty($row['relation_type']) ? '' : (string) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $row['relation_type'], 'contact_type_b');
284 $contactType = $contactType == 'Contact' ? '' : $contactType;
22e263ad 285 switch ($row['source']) {
32864ccf
TO
286 case 'caseRel':
287 $row['actions'] =
92fcb95f 288 '<a href="#editCaseRoleDialog" title="' . ts('Reassign %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '" data-rel_id="' . $row['rel_id'] . '"data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' .
52604b19 289 '<span class="icon ui-icon-pencil"></span>' .
92fcb95f
TO
290 '</a>' .
291 '<a href="#deleteCaseRoleDialog" title="' . ts('Remove %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '" data-key="' . CRM_Core_Key::get('civicrm/ajax/delcaserole') . '">' .
292 '<span class="icon delete-icon"></span>' .
c91df8b4 293 '</a>';
32864ccf 294 break;
1d85d241 295
32864ccf
TO
296 case 'caseRoles':
297 $row['actions'] =
92fcb95f 298 '<a href="#editCaseRoleDialog" title="' . ts('Assign %1', array(1 => $typeLabel)) . '" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="' . $row['relation_type'] . '" data-key="' . CRM_Core_Key::get('civicrm/ajax/relation') . '">' .
52604b19 299 '<span class="icon ui-icon-pencil"></span>' .
c91df8b4 300 '</a>';
32864ccf 301 break;
6a488035 302 }
6a488035
TO
303 }
304 $idx++;
305 }
306 $iFilteredTotal = $iTotal = $params['total'] = count($allCaseRelationships);
307 $selectorElements = array('relation', 'name', 'phone', 'email', 'actions');
ffd93213 308
6a488035
TO
309 echo CRM_Utils_JSON::encodeDataTableSelector($caseRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
310 CRM_Utils_System::civiExit();
1d85d241
DL
311 }
312
00be9182 313 public static function convertToCaseActivity() {
6a488035 314 $params = array('caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode');
ecdef330 315 $vals = array();
6a488035
TO
316 foreach ($params as $param) {
317 $vals[$param] = CRM_Utils_Array::value($param, $_POST);
318 }
319
ecdef330 320 CRM_Utils_JSON::output(self::_convertToCaseActivity($vals));
6a488035
TO
321 }
322
ffd93213 323 /**
c490a46a 324 * @param array $params
ffd93213
EM
325 *
326 * @return array
327 */
00be9182 328 public static function _convertToCaseActivity($params) {
6a488035
TO
329 if (!$params['activityID'] || !$params['caseID']) {
330 return (array('error_msg' => 'required params missing.'));
331 }
332
333 $otherActivity = new CRM_Activity_DAO_Activity();
334 $otherActivity->id = $params['activityID'];
335 if (!$otherActivity->find(TRUE)) {
336 return (array('error_msg' => 'activity record is missing.'));
337 }
338 $actDateTime = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time);
339
340 //create new activity record.
341 $mainActivity = new CRM_Activity_DAO_Activity();
342 $mainActVals = array();
343 CRM_Core_DAO::storeValues($otherActivity, $mainActVals);
344
345 //get new activity subject.
346 if (!empty($params['newSubject'])) {
347 $mainActVals['subject'] = $params['newSubject'];
348 }
349
350 $mainActivity->copyValues($mainActVals);
351 $mainActivity->id = NULL;
352 $mainActivity->activity_date_time = $actDateTime;
353 //make sure this is current revision.
354 $mainActivity->is_current_revision = TRUE;
355 //drop all relations.
356 $mainActivity->parent_id = $mainActivity->original_id = NULL;
357
358 $mainActivity->save();
359 $mainActivityId = $mainActivity->id;
360 CRM_Activity_BAO_Activity::logActivityAction($mainActivity);
361 $mainActivity->free();
362
363 /* Mark previous activity as deleted. If it was a non-case activity
7b1ec1c6 364 * then just change the subject.
365 */
6a488035
TO
366
367 if (in_array($params['mode'], array(
368 'move', 'file'))) {
369 $caseActivity = new CRM_Case_DAO_CaseActivity();
370 $caseActivity->case_id = $params['caseID'];
371 $caseActivity->activity_id = $otherActivity->id;
372 if ($params['mode'] == 'move' || $caseActivity->find(TRUE)) {
373 $otherActivity->is_deleted = 1;
374 }
375 else {
376 $otherActivity->subject = ts('(Filed on case %1)', array(
7b1ec1c6 377 1 => $params['caseID']
378 )) . ' ' . $otherActivity->subject;
6a488035
TO
379 }
380 $otherActivity->activity_date_time = $actDateTime;
381 $otherActivity->save();
382
383 $caseActivity->free();
384 }
385 $otherActivity->free();
386
387 $targetContacts = array();
388 if (!empty($params['targetContactIds'])) {
389 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
390 }
f813f78e 391
e7e657f0 392 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 393 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
394 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
395 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
396
7b1ec1c6 397 $sourceContactID = CRM_Activity_BAO_Activity::getSourceContactID($params['activityID']);
398 $src_params = array(
399 'activity_id' => $mainActivityId,
400 'contact_id' => $sourceContactID,
401 'record_type_id' => $sourceID
402 );
403 CRM_Activity_BAO_ActivityContact::create($src_params);
404
6a488035
TO
405 foreach ($targetContacts as $key => $value) {
406 $targ_params = array(
407 'activity_id' => $mainActivityId,
1d85d241 408 'contact_id' => $value,
a24b3694 409 'record_type_id' => $targetID
6a488035 410 );
1d85d241 411 CRM_Activity_BAO_ActivityContact::create($targ_params);
6a488035
TO
412 }
413
414 // typically this will be empty, since assignees on another case may be completely different
415 $assigneeContacts = array();
416 if (!empty($params['assigneeContactIds'])) {
417 $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
418 }
419 foreach ($assigneeContacts as $key => $value) {
420 $assigneeParams = array(
421 'activity_id' => $mainActivityId,
1d85d241 422 'contact_id' => $value,
a24b3694 423 'record_type_id' => $assigneeID
6a488035 424 );
1d85d241 425 CRM_Activity_BAO_ActivityContact::create($assigneeParams);
6a488035
TO
426 }
427
428 //attach newly created activity to case.
429 $caseActivity = new CRM_Case_DAO_CaseActivity();
430 $caseActivity->case_id = $params['caseID'];
431 $caseActivity->activity_id = $mainActivityId;
432 $caseActivity->save();
433 $error_msg = $caseActivity->_lastError;
434 $caseActivity->free();
435
436 $params['mainActivityId'] = $mainActivityId;
437 CRM_Activity_BAO_Activity::copyExtendedActivityData($params);
438
439 return (array('error_msg' => $error_msg, 'newId' => $mainActivity->id));
440 }
441
00be9182 442 public static function getContactActivity() {
6a488035
TO
443 $contactID = CRM_Utils_Type::escape($_POST['contact_id'], 'Integer');
444 $context = CRM_Utils_Type::escape(CRM_Utils_Array::value('context', $_GET), 'String');
445
446 $sortMapper = array(
f3548d18
BS
447 0 => 'activity_type',
448 1 => 'subject',
449 2 => 'source_contact_name',
450 3 => '',
8c7ca479 451 4 => '',
452 5 => 'activity_date_time',
453 6 => 'status_id',
6a488035
TO
454 );
455
f3548d18
BS
456 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
457 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
458 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
459 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
6a488035
TO
460 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
461
462 $params = $_POST;
463 if ($sort && $sortOrder) {
464 $params['sortBy'] = $sort . ' ' . $sortOrder;
465 }
466
467 $params['page'] = ($offset / $rowCount) + 1;
468 $params['rp'] = $rowCount;
469
470 $params['contact_id'] = $contactID;
471 $params['context'] = $context;
472
473 // get the contact activities
474 $activities = CRM_Activity_BAO_Activity::getContactActivitySelector($params);
475
97c7504f 476 foreach ($activities as $key => $value) {
477 //Check if recurring activity
478 if (CRM_Utils_Array::value('is_recurring_activity', $value)) {
479 if ($key == $value['is_recurring_activity']) {
92fcb95f 480 $activities[$key]['activity_type'] = $activities[$key]['activity_type'] . '<br/><span class="bold">Recurring Activity - (Parent)</span>';
97c7504f 481 }
482 else {
92fcb95f 483 $activities[$key]['activity_type'] = $activities[$key]['activity_type'] . '<br/><span class="bold">Recurring Activity - (Child)</span>';
97c7504f 484 }
485 }
486 }
487
6a488035
TO
488 // store the activity filter preference CRM-11761
489 $session = CRM_Core_Session::singleton();
490 $userID = $session->get('userID');
491 if ($userID) {
f3548d18
BS
492 //flush cache before setting filter to account for global cache (memcache)
493 $domainID = CRM_Core_Config::domainID();
494 $cacheKey = CRM_Core_BAO_Setting::inCache(
495 CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME,
496 'activity_tab_filter',
497 NULL,
498 $userID,
499 TRUE,
500 $domainID,
501 TRUE
502 );
481a74f4 503 if ($cacheKey) {
f3548d18
BS
504 CRM_Core_BAO_Setting::flushCache($cacheKey);
505 }
506
507 $activityFilter = array(
32864ccf
TO
508 'activity_type_filter_id' => empty($params['activity_type_id']) ? '' : CRM_Utils_Type::escape($params['activity_type_id'], 'Integer'),
509 'activity_type_exclude_filter_id' => empty($params['activity_type_exclude_id']) ? '' : CRM_Utils_Type::escape($params['activity_type_exclude_id'], 'Integer'),
f3548d18
BS
510 );
511
512 CRM_Core_BAO_Setting::setItem(
513 $activityFilter,
514 CRM_Core_BAO_Setting::PERSONAL_PREFERENCES_NAME,
515 'activity_tab_filter',
516 NULL,
517 $userID,
518 $userID
519 );
6a488035 520 }
1d85d241 521
6a488035
TO
522 $iFilteredTotal = $iTotal = $params['total'];
523 $selectorElements = array(
524 'activity_type', 'subject', 'source_contact',
525 'target_contact', 'assignee_contact',
32864ccf 526 'activity_date', 'status', 'links', 'class',
6a488035
TO
527 );
528
529 echo CRM_Utils_JSON::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
530 CRM_Utils_System::civiExit();
531 }
532}