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