3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 // Should those params be passed through the validateParams method?
41 $caseID = CRM_Utils_Type
::validate($_GET['caseID'], 'Integer');
42 $contactID = CRM_Utils_Type
::validate($_GET['cid'], 'Integer');
43 $userID = CRM_Utils_Type
::validate($_GET['userID'], 'Integer');
44 $context = CRM_Utils_Type
::validate(CRM_Utils_Array
::value('context', $_GET), 'String');
46 $optionalParameters = array(
47 'source_contact_id' => 'Integer',
48 'status_id' => 'Integer',
49 'activity_deleted' => 'Boolean',
50 'activity_type_id' => 'Integer',
51 // "Date" validation fails because it expects only numbers with no hyphens
52 'activity_date_low' => 'Alphanumeric',
53 'activity_date_high' => 'Alphanumeric',
56 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
57 $params +
= CRM_Core_Page_AJAX
::validateParams(array(), $optionalParameters);
59 // get the activities related to given case
60 $activities = CRM_Case_BAO_Case
::getCaseActivity($caseID, $params, $contactID, $context, $userID);
62 CRM_Utils_JSON
::output($activities);
65 public static function getCaseGlobalRelationships() {
66 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
68 // get the activities related to given case
69 $globalGroupInfo = array();
71 // get the total row count
72 CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
74 $relGlobal = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, $params['sortBy'], $showLinks = TRUE, FALSE, $params['offset'], $params['rp']);
76 $relationships = array();
77 // after sort we can update username fields to be a url
78 foreach ($relGlobal as $key => $value) {
79 $relationship = array();
80 $relationship['sort_name'] = $value['sort_name'];
81 $relationship['phone'] = $value['phone'];
82 $relationship['email'] = $value['email'];
84 array_push($relationships, $relationship);
87 $globalRelationshipsDT = array();
88 $globalRelationshipsDT['data'] = $relationships;
89 $globalRelationshipsDT['recordsTotal'] = count($relationships);
90 $globalRelationshipsDT['recordsFiltered'] = count($relationships);
92 CRM_Utils_JSON
::output($globalRelationshipsDT);
95 public static function getCaseClientRelationships() {
96 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
97 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
99 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
101 // Retrieve ALL client relationships
102 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($contactID,
103 CRM_Contact_BAO_Relationship
::CURRENT
,
104 0, 0, 0, NULL, NULL, FALSE
107 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID);
109 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
110 // so they don't show up twice.
111 $clientRelationships = array();
112 foreach ($relClient as $r) {
113 if (!array_key_exists($r['id'], $caseRelationships)) {
114 $clientRelationships[] = $r;
118 // sort clientRelationships array using jquery call params
119 foreach ($clientRelationships as $key => $row) {
120 $sortArray[$key] = $row[$params['_raw_values']['sort'][0]];
122 $sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]);
123 array_multisort($sortArray, constant($sort_type), $clientRelationships);
125 $relationships = array();
126 // after sort we can update username fields to be a url
127 foreach ($clientRelationships as $key => $value) {
128 $relationship = array();
129 $relationship['relation'] = $value['relation'];
130 $relationship['name'] = '<a href=' . CRM_Utils_System
::url('civicrm/contact/view',
131 'action=view&reset=1&cid=' . $clientRelationships[$key]['cid']) . '>' . $clientRelationships[$key]['name'] . '</a>';
132 $relationship['phone'] = $value['phone'];
133 $relationship['email'] = $value['email'];
135 array_push($relationships, $relationship);
138 $clientRelationshipsDT = array();
139 $clientRelationshipsDT['data'] = $relationships;
140 $clientRelationshipsDT['recordsTotal'] = count($relationships);
141 $clientRelationshipsDT['recordsFiltered'] = count($relationships);
143 CRM_Utils_JSON
::output($clientRelationshipsDT);
147 public static function getCaseRoles() {
148 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
149 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
151 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
153 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID);
154 $caseTypeName = CRM_Case_BAO_Case
::getCaseType($caseID, 'name');
155 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
156 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
158 $hasAccessToAllCases = CRM_Core_Permission
::check('access all cases and activities');
160 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
162 foreach ($caseRelationships as $key => $value) {
163 // This role has been filled
164 unset($caseRoles[$value['relation_type']]);
165 // mark original case relationships record to use on setting edit links below
166 $caseRelationships[$key]['source'] = 'caseRel';
169 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($caseID);
171 // move/transform caseRoles array data to caseRelationships
172 // for sorting and display
173 // CRM-14466 added cid to the non-client array to avoid php notice
174 foreach ($caseRoles as $id => $value) {
175 if ($id != "client") {
177 $rel['relation'] = $value;
178 $rel['relation_type'] = $id;
179 $rel['name'] = '(not assigned)';
182 $rel['source'] = 'caseRoles';
183 $caseRelationships[] = $rel;
186 foreach ($value as $clientRole) {
187 $relClient = array();
188 $relClient['relation'] = 'Client';
189 $relClient['name'] = $clientRole['sort_name'];
190 $relClient['phone'] = $clientRole['phone'];
191 $relClient['email'] = $clientRole['email'];
192 $relClient['cid'] = $clientRole['contact_id'];
193 $relClient['source'] = 'contact';
194 $caseRelationships[] = $relClient;
199 // sort clientRelationships array using jquery call params
200 foreach ($caseRelationships as $key => $row) {
201 $sortArray[$key] = $row[$params['_raw_values']['sort'][0]];
203 $sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]);
204 array_multisort($sortArray, constant($sort_type), $caseRelationships);
206 $relationships = array();
208 // set user name, email and edit columns links
209 foreach ($caseRelationships as $key => &$row) {
210 $typeLabel = $row['relation'];
211 // Add "<br />(Case Manager)" to label
212 if (!empty($row['relation_type']) && $row['relation_type'] == $managerRoleId) {
213 $row['relation'] .= '<br />' . '(' . ts('Case Manager') . ')';
216 if (!empty($row['cid'])) {
217 $row['name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System
::url('civicrm/contact/view',
218 'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['name'] . '</a>';
220 // email column links/icon
222 $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') . '"><i class="crm-i fa-envelope"></i></a>';
225 $row['actions'] = '';
226 if ($hasAccessToAllCases) {
227 $contactType = empty($row['relation_type']) ?
'' : (string) CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', $row['relation_type'], 'contact_type_b');
228 $contactType = $contactType == 'Contact' ?
'' : $contactType;
229 switch ($row['source']) {
231 $row['actions'] = '<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'] . '_' . $row['relationship_direction'] . '" data-cid="' . $row['cid'] . '" data-rel_id="' . $row['rel_id'] . '"data-key="' . CRM_Core_Key
::get('civicrm/ajax/relation') . '">' .
232 '<i class="crm-i fa-pencil"></i>' .
234 '<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'] . '_' . $row['relationship_direction'] . '" data-cid="' . $row['cid'] . '" data-key="' . CRM_Core_Key
::get('civicrm/ajax/delcaserole') . '">' .
235 '<span class="icon delete-icon"></span>' .
240 $row['actions'] = '<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'] . '_a_b" data-key="' . CRM_Core_Key
::get('civicrm/ajax/relation') . '">' .
241 '<i class="crm-i fa-pencil"></i>' .
247 unset($row['relation_type']);
248 unset($row['rel_id']);
249 unset($row['client_id']);
250 unset($row['source']);
251 array_push($relationships, $row);
253 $params['total'] = count($relationships);
255 $caseRelationshipsDT = array();
256 $caseRelationshipsDT['data'] = $relationships;
257 $caseRelationshipsDT['recordsTotal'] = $params['total'];
258 $caseRelationshipsDT['recordsFiltered'] = $params['total'];
260 CRM_Utils_JSON
::output($caseRelationshipsDT);
264 public static function convertToCaseActivity() {
265 $params = array('caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode');
267 foreach ($params as $param) {
268 $vals[$param] = CRM_Utils_Array
::value($param, $_POST);
271 CRM_Utils_JSON
::output(self
::_convertToCaseActivity($vals));
275 * @param array $params
279 public static function _convertToCaseActivity($params) {
280 if (!$params['activityID'] ||
!$params['caseID']) {
281 return (array('error_msg' => 'required params missing.'));
284 $otherActivity = new CRM_Activity_DAO_Activity();
285 $otherActivity->id
= $params['activityID'];
286 if (!$otherActivity->find(TRUE)) {
287 return (array('error_msg' => 'activity record is missing.'));
289 $actDateTime = CRM_Utils_Date
::isoToMysql($otherActivity->activity_date_time
);
291 // Create new activity record.
292 $mainActivity = new CRM_Activity_DAO_Activity();
293 $mainActVals = array();
294 CRM_Core_DAO
::storeValues($otherActivity, $mainActVals);
296 // Get new activity subject.
297 if (!empty($params['newSubject'])) {
298 $mainActVals['subject'] = $params['newSubject'];
301 $mainActivity->copyValues($mainActVals);
302 $mainActivity->id
= NULL;
303 $mainActivity->activity_date_time
= $actDateTime;
304 // Make sure this is current revision.
305 $mainActivity->is_current_revision
= TRUE;
306 $mainActivity->original_id
= $otherActivity->id
;
307 $otherActivity->is_current_revision
= FALSE;
309 $mainActivity->save();
310 $mainActivityId = $mainActivity->id
;
311 CRM_Activity_BAO_Activity
::logActivityAction($mainActivity);
313 // Mark previous activity as deleted. If it was a non-case activity
314 // then just change the subject.
315 if (in_array($params['mode'], array(
319 $caseActivity = new CRM_Case_DAO_CaseActivity();
320 $caseActivity->case_id
= $params['caseID'];
321 $caseActivity->activity_id
= $otherActivity->id
;
322 if ($params['mode'] == 'move' ||
$caseActivity->find(TRUE)) {
323 $otherActivity->is_deleted
= 1;
326 $otherActivity->subject
= ts('(Filed on case %1)', array(
327 1 => $params['caseID'],
328 )) . ' ' . $otherActivity->subject
;
330 $otherActivity->save();
334 $targetContacts = array();
335 if (!empty($params['targetContactIds'])) {
336 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
339 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
340 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
341 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
342 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
344 $sourceContactID = CRM_Activity_BAO_Activity
::getSourceContactID($params['activityID']);
346 'activity_id' => $mainActivityId,
347 'contact_id' => $sourceContactID,
348 'record_type_id' => $sourceID,
350 CRM_Activity_BAO_ActivityContact
::create($src_params);
352 foreach ($targetContacts as $key => $value) {
353 $targ_params = array(
354 'activity_id' => $mainActivityId,
355 'contact_id' => $value,
356 'record_type_id' => $targetID,
358 CRM_Activity_BAO_ActivityContact
::create($targ_params);
361 //CRM-21114 retrieve assignee contacts from original case; allow overriding from params
362 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::retrieveContactIdsByActivityId($params['activityID'], $assigneeID);
363 if (!empty($params['assigneeContactIds'])) {
364 $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
366 foreach ($assigneeContacts as $key => $value) {
367 $assigneeParams = array(
368 'activity_id' => $mainActivityId,
369 'contact_id' => $value,
370 'record_type_id' => $assigneeID,
372 CRM_Activity_BAO_ActivityContact
::create($assigneeParams);
375 // Attach newly created activity to case.
376 $caseActivity = new CRM_Case_DAO_CaseActivity();
377 $caseActivity->case_id
= $params['caseID'];
378 $caseActivity->activity_id
= $mainActivityId;
379 $caseActivity->save();
380 $error_msg = $caseActivity->_lastError
;
382 $params['mainActivityId'] = $mainActivityId;
383 CRM_Activity_BAO_Activity
::copyExtendedActivityData($params);
384 CRM_Utils_Hook
::post('create', 'CaseActivity', $caseActivity->id
, $caseActivity);
386 return (array('error_msg' => $error_msg, 'newId' => $mainActivity->id
));
390 * Get activities for the contact.
394 public static function getContactActivity() {
395 $requiredParameters = array(
399 $optionalParameters = array(
400 'context' => 'String',
401 'activity_type_id' => 'Integer',
402 'activity_type_exclude_id' => 'Integer',
403 'activity_status_id' => 'String',
404 'activity_date_time_relative' => 'String',
405 'activity_date_time_low' => 'String',
406 'activity_date_time_high' => 'String',
409 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
410 $params +
= CRM_Core_Page_AJAX
::validateParams($requiredParameters, $optionalParameters);
412 // To be consistent, the cid parameter should be renamed to contact_id in
413 // the template file, see templates/CRM/Activity/Selector/Selector.tpl
414 $params['contact_id'] = $params['cid'];
415 unset($params['cid']);
417 // get the contact activities
418 $activities = CRM_Activity_BAO_Activity
::getContactActivitySelector($params);
420 foreach ($activities['data'] as $key => $value) {
421 // Check if recurring activity.
422 if (!empty($value['is_recurring_activity'])) {
423 $repeat = $value['is_recurring_activity'];
424 $activities['data'][$key]['activity_type'] .= '<br/><span class="bold">' . ts('Repeating (%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1])) . '</span>';
428 // store the activity filter preference CRM-11761
429 if (Civi
::settings()->get('preserve_activity_tab_filter') && ($userID = CRM_Core_Session
::getLoggedInContactID())) {
430 unset($optionalParameters['context']);
431 foreach ($optionalParameters as $searchField => $dataType) {
432 $formSearchField = $searchField;
433 if ($searchField == 'activity_type_id') {
434 $formSearchField = 'activity_type_filter_id';
436 elseif ($searchField == 'activity_type_exclude_id') {
437 $formSearchField = 'activity_type_exclude_filter_id';
439 if (!empty($params[$searchField])) {
440 $activityFilter[$formSearchField] = $params[$searchField];
441 if (in_array($searchField, array('activity_date_time_low', 'activity_date_time_high'))) {
442 $activityFilter['activity_date_time_relative'] = 0;
444 elseif ($searchField == 'activity_status_id') {
445 $activityFilter['status_id'] = explode(',', $activityFilter[$searchField]);
450 Civi
::contactSettings()->set('activity_tab_filter', $activityFilter);
452 if (!empty($_GET['is_unit_test'])) {
453 return array($activities, $activityFilter);
456 CRM_Utils_JSON
::output($activities);