3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
36 * This class contains all the function that are called using AJAX (jQuery)
38 class CRM_Activity_Page_AJAX
{
39 public 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');
48 2 => 'ca.activity_type_id',
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';
61 if ($sort && $sortOrder) {
62 $params['sortname'] = $sort;
63 $params['sortorder'] = $sortOrder;
65 $params['page'] = ($offset / $rowCount) +
1;
66 $params['rp'] = $rowCount;
68 // get the activities related to given case
69 $activities = CRM_Case_BAO_Case
::getCaseActivity($caseID, $params, $contactID, $context, $userID);
71 $iFilteredTotal = $iTotal = $params['total'];
72 $selectorElements = array(
83 echo CRM_Utils_JSON
::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
84 CRM_Utils_System
::civiExit();
87 public static function getCaseGlobalRelationships() {
94 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
95 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
96 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
97 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
98 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
101 //CRM-14466 initialize variable to avoid php notice
103 if ($sort && $sortOrder) {
104 $sortSQL = $sort . ' ' . $sortOrder;
107 // get the activities related to given case
108 $globalGroupInfo = array();
110 // get the total row count
111 $relGlobalTotalCount = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
113 $relGlobal = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, $sortSQL, $showLinks = TRUE, FALSE, $offset, $rowCount);
115 $iFilteredTotal = $iTotal = $relGlobalTotalCount;
116 $selectorElements = array('sort_name', 'phone', 'email');
118 echo CRM_Utils_JSON
::encodeDataTableSelector($relGlobal, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
119 CRM_Utils_System
::civiExit();
122 public static function getCaseClientRelationships() {
123 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
124 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
133 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
134 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
135 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
136 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation';
137 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
140 if ($sort && $sortOrder) {
141 $sortSQL = $sort . ' ' . $sortOrder;
144 // Retrieve ALL client relationships
145 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($contactID,
146 CRM_Contact_BAO_Relationship
::CURRENT
,
147 0, 0, 0, NULL, NULL, FALSE
150 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID);
152 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
153 // so they don't show up twice.
154 $clientRelationships = array();
155 foreach ($relClient as $r) {
156 if (!array_key_exists($r['id'], $caseRelationships)) {
157 $clientRelationships[] = $r;
161 // sort clientRelationships array using jquery call params
162 foreach ($clientRelationships as $key => $row) {
163 $sortArray[$key] = $row[$sort];
165 $sort_type = "SORT_" . strtoupper($sortOrder);
166 array_multisort($sortArray, constant($sort_type), $clientRelationships);
169 $allClientRelationships = $clientRelationships;
170 $clientRelationships = array_slice($allClientRelationships, $offset, $rowCount, TRUE);
172 // after sort we can update username fields to be a url
173 foreach ($clientRelationships as $key => $value) {
174 $clientRelationships[$key]['name'] = '<a href=' . CRM_Utils_System
::url('civicrm/contact/view',
175 'action=view&reset=1&cid=' . $clientRelationships[$key]['cid']) . '>' . $clientRelationships[$key]['name'] . '</a>';
178 $iFilteredTotal = $iTotal = $params['total'] = count($allClientRelationships);
179 $selectorElements = array('relation', 'name', 'phone', 'email');
181 echo CRM_Utils_JSON
::encodeDataTableSelector($clientRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
182 CRM_Utils_System
::civiExit();
186 public static function getCaseRoles() {
187 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
188 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
198 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
199 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
200 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
201 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : 'relation';
202 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
205 if ($sort && $sortOrder) {
206 $sortSQL = $sort . ' ' . $sortOrder;
209 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID);
210 $caseTypeName = CRM_Case_BAO_Case
::getCaseType($caseID, 'name');
211 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
212 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
214 $hasAccessToAllCases = CRM_Core_Permission
::check('access all cases and activities');
216 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
217 if (!empty($managerRoleId)) {
218 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
221 foreach ($caseRelationships as $key => $value) {
222 //calculate roles that don't have relationships
223 if (!empty($caseRoles[$value['relation_type']])) {
224 //keep naming from careRoles array
225 $caseRelationships[$key]['relation'] = $caseRoles[$value['relation_type']];
226 unset($caseRoles[$value['relation_type']]);
228 // mark orginal case relationships record to use on setting edit links below
229 $caseRelationships[$key]['source'] = 'caseRel';
232 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($caseID);
234 // move/transform caseRoles array data to caseRelationships
235 // for sorting and display
236 // CRM-14466 added cid to the non-client array to avoid php notice
237 foreach ($caseRoles as $id => $value) {
238 if ($id != "client") {
240 $rel['relation'] = $value;
241 $rel['relation_type'] = $id;
242 $rel['name'] = '(not assigned)';
245 $rel['source'] = 'caseRoles';
246 $caseRelationships[] = $rel;
249 foreach ($value as $clientRole) {
250 $relClient = array();
251 $relClient['relation'] = 'Client';
252 $relClient['name'] = $clientRole['sort_name'];
253 $relClient['phone'] = $clientRole['phone'];
254 $relClient['email'] = $clientRole['email'];
255 $relClient['cid'] = $clientRole['contact_id'];
256 $relClient['source'] = 'contact';
257 $caseRelationships[] = $relClient;
262 // sort clientRelationships array using jquery call params
263 foreach ($caseRelationships as $key => $row) {
264 $sortArray[$key] = $row[$sort];
267 $sort_type = "SORT_" . strtoupper($sortOrder);
268 array_multisort($sortArray, constant($sort_type), $caseRelationships);
271 $allCaseRelationships = $caseRelationships;
272 $caseRelationships = array_slice($allCaseRelationships, $offset, $rowCount, TRUE);
274 // set user name, email and edit columns links
275 // idx will count number of current row / needed by edit links
277 foreach ($caseRelationships as &$row) {
278 // Get rid of the "<br />(Case Manager)" from label
279 list($typeLabel) = explode('<', $row['relation']);
281 if (!empty($row['cid'])) {
282 $row['name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System
::url('civicrm/contact/view',
283 'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['name'] . '</a>';
285 // email column links/icon
287 $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>';
290 $row['actions'] = '';
291 if ($hasAccessToAllCases) {
292 $contactType = empty($row['relation_type']) ?
'' : (string) CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', $row['relation_type'], 'contact_type_b');
293 $contactType = $contactType == 'Contact' ?
'' : $contactType;
294 switch ($row['source']) {
297 = '<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') . '">' .
298 '<span class="icon ui-icon-pencil"></span>' .
300 '<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') . '">' .
301 '<span class="icon delete-icon"></span>' .
307 = '<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') . '">' .
308 '<span class="icon ui-icon-pencil"></span>' .
315 $iFilteredTotal = $iTotal = $params['total'] = count($allCaseRelationships);
316 $selectorElements = array('relation', 'name', 'phone', 'email', 'actions');
318 echo CRM_Utils_JSON
::encodeDataTableSelector($caseRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
319 CRM_Utils_System
::civiExit();
322 public static function convertToCaseActivity() {
323 $params = array('caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode');
325 foreach ($params as $param) {
326 $vals[$param] = CRM_Utils_Array
::value($param, $_POST);
329 CRM_Utils_JSON
::output(self
::_convertToCaseActivity($vals));
333 * @param array $params
337 public static function _convertToCaseActivity($params) {
338 if (!$params['activityID'] ||
!$params['caseID']) {
339 return (array('error_msg' => 'required params missing.'));
342 $otherActivity = new CRM_Activity_DAO_Activity();
343 $otherActivity->id
= $params['activityID'];
344 if (!$otherActivity->find(TRUE)) {
345 return (array('error_msg' => 'activity record is missing.'));
347 $actDateTime = CRM_Utils_Date
::isoToMysql($otherActivity->activity_date_time
);
349 //create new activity record.
350 $mainActivity = new CRM_Activity_DAO_Activity();
351 $mainActVals = array();
352 CRM_Core_DAO
::storeValues($otherActivity, $mainActVals);
354 //get new activity subject.
355 if (!empty($params['newSubject'])) {
356 $mainActVals['subject'] = $params['newSubject'];
359 $mainActivity->copyValues($mainActVals);
360 $mainActivity->id
= NULL;
361 $mainActivity->activity_date_time
= $actDateTime;
362 //make sure this is current revision.
363 $mainActivity->is_current_revision
= TRUE;
364 //drop all relations.
365 $mainActivity->parent_id
= $mainActivity->original_id
= NULL;
367 $mainActivity->save();
368 $mainActivityId = $mainActivity->id
;
369 CRM_Activity_BAO_Activity
::logActivityAction($mainActivity);
370 $mainActivity->free();
372 /* Mark previous activity as deleted. If it was a non-case activity
373 * then just change the subject.
376 if (in_array($params['mode'], array(
380 $caseActivity = new CRM_Case_DAO_CaseActivity();
381 $caseActivity->case_id
= $params['caseID'];
382 $caseActivity->activity_id
= $otherActivity->id
;
383 if ($params['mode'] == 'move' ||
$caseActivity->find(TRUE)) {
384 $otherActivity->is_deleted
= 1;
387 $otherActivity->subject
= ts('(Filed on case %1)', array(
388 1 => $params['caseID'],
389 )) . ' ' . $otherActivity->subject
;
391 $otherActivity->activity_date_time
= $actDateTime;
392 $otherActivity->save();
394 $caseActivity->free();
396 $otherActivity->free();
398 $targetContacts = array();
399 if (!empty($params['targetContactIds'])) {
400 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
403 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
404 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
405 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
406 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
408 $sourceContactID = CRM_Activity_BAO_Activity
::getSourceContactID($params['activityID']);
410 'activity_id' => $mainActivityId,
411 'contact_id' => $sourceContactID,
412 'record_type_id' => $sourceID,
414 CRM_Activity_BAO_ActivityContact
::create($src_params);
416 foreach ($targetContacts as $key => $value) {
417 $targ_params = array(
418 'activity_id' => $mainActivityId,
419 'contact_id' => $value,
420 'record_type_id' => $targetID,
422 CRM_Activity_BAO_ActivityContact
::create($targ_params);
425 // typically this will be empty, since assignees on another case may be completely different
426 $assigneeContacts = array();
427 if (!empty($params['assigneeContactIds'])) {
428 $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
430 foreach ($assigneeContacts as $key => $value) {
431 $assigneeParams = array(
432 'activity_id' => $mainActivityId,
433 'contact_id' => $value,
434 'record_type_id' => $assigneeID,
436 CRM_Activity_BAO_ActivityContact
::create($assigneeParams);
439 //attach newly created activity to case.
440 $caseActivity = new CRM_Case_DAO_CaseActivity();
441 $caseActivity->case_id
= $params['caseID'];
442 $caseActivity->activity_id
= $mainActivityId;
443 $caseActivity->save();
444 $error_msg = $caseActivity->_lastError
;
445 $caseActivity->free();
447 $params['mainActivityId'] = $mainActivityId;
448 CRM_Activity_BAO_Activity
::copyExtendedActivityData($params);
450 return (array('error_msg' => $error_msg, 'newId' => $mainActivity->id
));
453 public static function getContactActivity() {
454 $contactID = CRM_Utils_Type
::escape($_POST['contact_id'], 'Integer');
455 $context = CRM_Utils_Type
::escape(CRM_Utils_Array
::value('context', $_GET), 'String');
458 0 => 'activity_type',
460 2 => 'source_contact_name',
463 5 => 'activity_date_time',
467 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
468 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
469 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
470 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
471 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
474 if ($sort && $sortOrder) {
475 $params['sortBy'] = $sort . ' ' . $sortOrder;
478 $params['page'] = ($offset / $rowCount) +
1;
479 $params['rp'] = $rowCount;
481 $params['contact_id'] = $contactID;
482 $params['context'] = $context;
484 // get the contact activities
485 $activities = CRM_Activity_BAO_Activity
::getContactActivitySelector($params);
487 foreach ($activities as $key => $value) {
488 //Check if recurring activity
489 if (!empty($value['is_recurring_activity'])) {
490 if ($key == $value['is_recurring_activity']) {
491 $activities[$key]['activity_type'] = $activities[$key]['activity_type'] . '<br/><span class="bold">Recurring Activity - (Parent)</span>';
494 $activities[$key]['activity_type'] = $activities[$key]['activity_type'] . '<br/><span class="bold">Recurring Activity - (Child)</span>';
499 // store the activity filter preference CRM-11761
500 $session = CRM_Core_Session
::singleton();
501 $userID = $session->get('userID');
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',
515 CRM_Core_BAO_Setting
::flushCache($cacheKey);
518 $activityFilter = array(
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'),
523 CRM_Core_BAO_Setting
::setItem(
525 CRM_Core_BAO_Setting
::PERSONAL_PREFERENCES_NAME
,
526 'activity_tab_filter',
533 $iFilteredTotal = $iTotal = $params['total'];
534 $selectorElements = array(
546 echo CRM_Utils_JSON
::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
547 CRM_Utils_System
::civiExit();