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