3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 header('Content-Type: application/json');
84 echo CRM_Utils_JSON
::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
85 CRM_Utils_System
::civiExit();
88 public static function getCaseGlobalRelationships() {
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;
99 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
102 //CRM-14466 initialize variable to avoid php notice
104 if ($sort && $sortOrder) {
105 $sortSQL = $sort . ' ' . $sortOrder;
108 // get the activities related to given case
109 $globalGroupInfo = array();
111 // get the total row count
112 $relGlobalTotalCount = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
114 $relGlobal = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, $sortSQL, $showLinks = TRUE, FALSE, $offset, $rowCount);
116 $iFilteredTotal = $iTotal = $relGlobalTotalCount;
117 $selectorElements = array('sort_name', 'phone', 'email');
119 header('Content-Type: application/json');
120 echo CRM_Utils_JSON
::encodeDataTableSelector($relGlobal, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
121 CRM_Utils_System
::civiExit();
124 public static function getCaseClientRelationships() {
125 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
126 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
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';
139 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
142 if ($sort && $sortOrder) {
143 $sortSQL = $sort . ' ' . $sortOrder;
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
152 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID);
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;
163 // sort clientRelationships array using jquery call params
164 foreach ($clientRelationships as $key => $row) {
165 $sortArray[$key] = $row[$sort];
167 $sort_type = "SORT_" . strtoupper($sortOrder);
168 array_multisort($sortArray, constant($sort_type), $clientRelationships);
171 $allClientRelationships = $clientRelationships;
172 $clientRelationships = array_slice($allClientRelationships, $offset, $rowCount, TRUE);
174 // after sort we can update username fields to be a url
175 foreach ($clientRelationships as $key => $value) {
176 $clientRelationships[$key]['name'] = '<a href=' . CRM_Utils_System
::url('civicrm/contact/view',
177 'action=view&reset=1&cid=' . $clientRelationships[$key]['cid']) . '>' . $clientRelationships[$key]['name'] . '</a>';
180 $iFilteredTotal = $iTotal = $params['total'] = count($allClientRelationships);
181 $selectorElements = array('relation', 'name', 'phone', 'email');
183 header('Content-Type: application/json');
184 echo CRM_Utils_JSON
::encodeDataTableSelector($clientRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
185 CRM_Utils_System
::civiExit();
189 public static function getCaseRoles() {
190 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
191 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
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';
205 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
208 if ($sort && $sortOrder) {
209 $sortSQL = $sort . ' ' . $sortOrder;
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();
215 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
217 $hasAccessToAllCases = CRM_Core_Permission
::check('access all cases and activities');
219 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
220 if (!empty($managerRoleId)) {
221 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
224 foreach ($caseRelationships as $key => $value) {
225 //calculate roles that don't have relationships
226 if (!empty($caseRoles[$value['relation_type']])) {
227 //keep naming from careRoles array
228 $caseRelationships[$key]['relation'] = $caseRoles[$value['relation_type']];
229 unset($caseRoles[$value['relation_type']]);
231 // mark orginal case relationships record to use on setting edit links below
232 $caseRelationships[$key]['source'] = 'caseRel';
235 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($caseID);
237 // move/transform caseRoles array data to caseRelationships
238 // for sorting and display
239 // CRM-14466 added cid to the non-client array to avoid php notice
240 foreach ($caseRoles as $id => $value) {
241 if ($id != "client") {
243 $rel['relation'] = $value;
244 $rel['relation_type'] = $id;
245 $rel['name'] = '(not assigned)';
248 $rel['source'] = 'caseRoles';
249 $caseRelationships[] = $rel;
252 foreach ($value as $clientRole) {
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;
265 // sort clientRelationships array using jquery call params
266 foreach ($caseRelationships as $key => $row) {
267 $sortArray[$key] = $row[$sort];
270 $sort_type = "SORT_" . strtoupper($sortOrder);
271 array_multisort($sortArray, constant($sort_type), $caseRelationships);
274 $allCaseRelationships = $caseRelationships;
275 $caseRelationships = array_slice($allCaseRelationships, $offset, $rowCount, TRUE);
277 // set user name, email and edit columns links
278 // idx will count number of current row / needed by edit links
280 foreach ($caseRelationships as &$row) {
281 // Get rid of the "<br />(Case Manager)" from label
282 list($typeLabel) = explode('<', $row['relation']);
284 if (!empty($row['cid'])) {
285 $row['name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System
::url('civicrm/contact/view',
286 'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['name'] . '</a>';
288 // email column links/icon
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>';
293 $row['actions'] = '';
294 if ($hasAccessToAllCases) {
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;
297 switch ($row['source']) {
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') . '">' .
301 '<span class="icon ui-icon-pencil"></span>' .
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') . '">' .
304 '<span class="icon delete-icon"></span>' .
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') . '">' .
311 '<span class="icon ui-icon-pencil"></span>' .
318 $iFilteredTotal = $iTotal = $params['total'] = count($allCaseRelationships);
319 $selectorElements = array('relation', 'name', 'phone', 'email', 'actions');
321 header('Content-Type: application/json');
322 echo CRM_Utils_JSON
::encodeDataTableSelector($caseRelationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
323 CRM_Utils_System
::civiExit();
326 public static function convertToCaseActivity() {
327 $params = array('caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode');
329 foreach ($params as $param) {
330 $vals[$param] = CRM_Utils_Array
::value($param, $_POST);
333 CRM_Utils_JSON
::output(self
::_convertToCaseActivity($vals));
337 * @param array $params
341 public static function _convertToCaseActivity($params) {
342 if (!$params['activityID'] ||
!$params['caseID']) {
343 return (array('error_msg' => 'required params missing.'));
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.'));
351 $actDateTime = CRM_Utils_Date
::isoToMysql($otherActivity->activity_date_time
);
353 //create new activity record.
354 $mainActivity = new CRM_Activity_DAO_Activity();
355 $mainActVals = array();
356 CRM_Core_DAO
::storeValues($otherActivity, $mainActVals);
358 //get new activity subject.
359 if (!empty($params['newSubject'])) {
360 $mainActVals['subject'] = $params['newSubject'];
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;
371 $mainActivity->save();
372 $mainActivityId = $mainActivity->id
;
373 CRM_Activity_BAO_Activity
::logActivityAction($mainActivity);
374 $mainActivity->free();
376 /* Mark previous activity as deleted. If it was a non-case activity
377 * then just change the subject.
380 if (in_array($params['mode'], array(
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;
391 $otherActivity->subject
= ts('(Filed on case %1)', array(
392 1 => $params['caseID'],
393 )) . ' ' . $otherActivity->subject
;
395 $otherActivity->activity_date_time
= $actDateTime;
396 $otherActivity->save();
398 $caseActivity->free();
400 $otherActivity->free();
402 $targetContacts = array();
403 if (!empty($params['targetContactIds'])) {
404 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
407 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
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);
412 $sourceContactID = CRM_Activity_BAO_Activity
::getSourceContactID($params['activityID']);
414 'activity_id' => $mainActivityId,
415 'contact_id' => $sourceContactID,
416 'record_type_id' => $sourceID,
418 CRM_Activity_BAO_ActivityContact
::create($src_params);
420 foreach ($targetContacts as $key => $value) {
421 $targ_params = array(
422 'activity_id' => $mainActivityId,
423 'contact_id' => $value,
424 'record_type_id' => $targetID,
426 CRM_Activity_BAO_ActivityContact
::create($targ_params);
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']));
434 foreach ($assigneeContacts as $key => $value) {
435 $assigneeParams = array(
436 'activity_id' => $mainActivityId,
437 'contact_id' => $value,
438 'record_type_id' => $assigneeID,
440 CRM_Activity_BAO_ActivityContact
::create($assigneeParams);
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();
451 $params['mainActivityId'] = $mainActivityId;
452 CRM_Activity_BAO_Activity
::copyExtendedActivityData($params);
454 return (array('error_msg' => $error_msg, 'newId' => $mainActivity->id
));
457 public static function getContactActivity() {
458 $contactID = CRM_Utils_Type
::escape($_POST['contact_id'], 'Integer');
459 $context = CRM_Utils_Type
::escape(CRM_Utils_Array
::value('context', $_GET), 'String');
462 0 => 'activity_type',
464 2 => 'source_contact_name',
467 5 => 'activity_date_time',
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;
475 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
478 if ($sort && $sortOrder) {
479 $params['sortBy'] = $sort . ' ' . $sortOrder;
482 $params['page'] = ($offset / $rowCount) +
1;
483 $params['rp'] = $rowCount;
485 $params['contact_id'] = $contactID;
486 $params['context'] = $context;
488 // get the contact activities
489 $activities = CRM_Activity_BAO_Activity
::getContactActivitySelector($params);
491 foreach ($activities as $key => $value) {
492 //Check if recurring activity
493 if (!empty($value['is_recurring_activity'])) {
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>';
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 header('Content-Type: application/json');
547 echo CRM_Utils_JSON
::encodeDataTableSelector($activities, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
548 CRM_Utils_System
::civiExit();