3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class contains all the function that are called using AJAX (jQuery)
21 class CRM_Activity_Page_AJAX
{
23 public static function getCaseActivity() {
24 // Should those params be passed through the validateParams method?
25 $caseID = CRM_Utils_Type
::validate($_GET['caseID'], 'Integer');
26 $contactID = CRM_Utils_Type
::validate($_GET['cid'], 'Integer');
27 $userID = CRM_Utils_Type
::validate($_GET['userID'], 'Integer');
28 $context = CRM_Utils_Type
::validate(CRM_Utils_Array
::value('context', $_GET), 'String');
30 $optionalParameters = [
31 'source_contact_id' => 'Integer',
32 'status_id' => 'Integer',
33 'activity_deleted' => 'Boolean',
34 'activity_type_id' => 'Integer',
35 // "Date" validation fails because it expects only numbers with no hyphens
36 'activity_date_low' => 'Alphanumeric',
37 'activity_date_high' => 'Alphanumeric',
40 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
41 $params +
= CRM_Core_Page_AJAX
::validateParams([], $optionalParameters);
43 // get the activities related to given case
44 $activities = CRM_Case_BAO_Case
::getCaseActivity($caseID, $params, $contactID, $context, $userID);
46 CRM_Utils_JSON
::output($activities);
49 public static function getCaseGlobalRelationships() {
50 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
52 // get the activities related to given case
53 $globalGroupInfo = [];
55 // get the total row count
56 CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
58 $relGlobal = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, $params['sortBy'] ??
NULL, $showLinks = TRUE, FALSE, $params['offset'], $params['rp']);
61 // after sort we can update username fields to be a url
62 foreach ($relGlobal as $key => $value) {
64 $relationship['sort_name'] = $value['sort_name'];
65 $relationship['phone'] = $value['phone'];
66 $relationship['email'] = $value['email'];
68 array_push($relationships, $relationship);
71 $globalRelationshipsDT = [];
72 $globalRelationshipsDT['data'] = $relationships;
73 $globalRelationshipsDT['recordsTotal'] = count($relationships);
74 $globalRelationshipsDT['recordsFiltered'] = count($relationships);
76 CRM_Utils_JSON
::output($globalRelationshipsDT);
79 public static function getCaseClientRelationships() {
80 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
81 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
83 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
85 // Retrieve ALL client relationships
86 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($contactID,
87 CRM_Contact_BAO_Relationship
::CURRENT
,
88 0, 0, 0, NULL, NULL, FALSE
91 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID, NULL, FALSE);
93 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
94 // so they don't show up twice.
95 $clientRelationships = [];
96 foreach ($relClient as $r) {
97 if (!array_key_exists($r['id'], $caseRelationships)) {
98 $clientRelationships[] = $r;
102 // sort clientRelationships array using jquery call params
104 if (!empty($params['_raw_values']['sort'])) {
105 foreach ($clientRelationships as $key => $row) {
106 $sortArray[$key] = $row[$params['_raw_values']['sort'][0]];
108 $sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]);
109 array_multisort($sortArray, constant($sort_type), $clientRelationships);
113 // after sort we can update username fields to be a url
114 foreach ($clientRelationships as $key => $value) {
116 $relationship['relation'] = $value['relation'];
117 $relationship['name'] = '<a href=' . CRM_Utils_System
::url('civicrm/contact/view',
118 'action=view&reset=1&cid=' . $clientRelationships[$key]['cid']) . '>' . $clientRelationships[$key]['name'] . '</a>';
119 $relationship['phone'] = $value['phone'];
120 $relationship['email'] = $value['email'];
122 array_push($relationships, $relationship);
125 $clientRelationshipsDT = [];
126 $clientRelationshipsDT['data'] = $relationships;
127 $clientRelationshipsDT['recordsTotal'] = count($relationships);
128 $clientRelationshipsDT['recordsFiltered'] = count($relationships);
130 CRM_Utils_JSON
::output($clientRelationshipsDT);
133 public static function getCaseRoles() {
134 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
135 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
137 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
139 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID, NULL, FALSE);
140 $caseTypeName = CRM_Case_BAO_Case
::getCaseType($caseID, 'name');
141 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
142 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
144 $hasAccessToAllCases = CRM_Core_Permission
::check('access all cases and activities');
146 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
148 foreach ($caseRelationships as $key => $value) {
149 // This role has been filled
150 unset($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']]);
151 // mark original case relationships record to use on setting edit links below
152 $caseRelationships[$key]['source'] = 'caseRel';
155 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($caseID);
157 // move/transform caseRoles array data to caseRelationships
158 // for sorting and display
159 // CRM-14466 added cid to the non-client array to avoid php notice
160 foreach ($caseRoles as $id => $value) {
161 if ($id != "client") {
163 $rel['relation'] = $value;
164 $rel['relation_type'] = $id;
165 $rel['sort_name'] = '(not assigned)';
168 $rel['is_active'] = '';
169 $rel['end_date'] = '';
170 $rel['source'] = 'caseRoles';
171 $caseRelationships[] = $rel;
174 foreach ($value as $clientRole) {
176 $relClient['relation'] = ts('Client');
177 $relClient['sort_name'] = $clientRole['sort_name'];
178 $relClient['phone'] = $clientRole['phone'];
179 $relClient['email'] = $clientRole['email'];
180 $relClient['cid'] = $clientRole['contact_id'];
181 $relClient['end_date'] = '';
182 $relClient['source'] = 'contact';
183 $caseRelationships[] = $relClient;
188 // sort clientRelationships array using jquery call params
190 if (!empty($params['_raw_values']['sort'])) {
191 foreach ($caseRelationships as $key => $row) {
192 $sortArray[$key] = $row[$params['_raw_values']['sort'][0]];
194 $sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]);
195 array_multisort($sortArray, constant($sort_type), $caseRelationships);
200 // set user name, email and edit columns links
201 foreach ($caseRelationships as $key => &$row) {
202 // add disabled class if role is inactive
203 if (isset($row['is_active'])) {
204 if ($row['is_active'] == '0') {
205 $row['DT_RowClass'] = 'disabled';
208 $typeLabel = $row['relation'];
209 // Add "<br />(Case Manager)" to label
210 if (!empty($row['relation_type']) && !empty($row['relationship_direction']) && $row['relation_type'] . '_' . $row['relationship_direction'] == $managerRoleId) {
211 $row['relation'] .= '<br />' . '(' . ts('Case Manager') . ')';
214 if (!empty($row['cid'])) {
215 $row['sort_name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System
::url('civicrm/contact/view',
216 'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['sort_name'] . '</a>';
218 // email column links/icon
220 $row['email'] = '<a class="crm-hover-button crm-popup" href="' . CRM_Utils_System
::url('civicrm/case/email/add', 'reset=1&action=add&atype=3&cid=' . $row['cid']) . '&caseid=' . $caseID . '" title="' . ts('Send an Email') . '"><i class="crm-i fa-envelope" aria-hidden="true"></i></a>';
223 // view end date if set
224 if (!empty($row['end_date'])) {
225 $row['end_date'] = CRM_Utils_Date
::customFormat($row['end_date']);
226 // add disabled class if end date is less than equal to current date.
227 if (CRM_Utils_Date
::overdue($row['end_date'])) {
228 $row['DT_RowClass'] = 'disabled';
232 $row['end_date'] = '';
236 $row['actions'] = '';
237 if ($hasAccessToAllCases) {
238 $contactType = empty($row['relation_type']) ?
'' : (string) CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', $row['relation_type'], 'contact_type_b');
239 $contactType = $contactType == 'Contact' ?
'' : $contactType;
240 switch ($row['source']) {
242 if (empty($row['end_date'])) {
243 $row['actions'] = '<a href="#editCaseRoleDialog" title="' . ts('Reassign %1', [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') . '">' .
244 '<i class="crm-i fa-pencil" aria-hidden="true"></i>' .
246 '<a href="#deleteCaseRoleDialog" title="' . ts('Remove %1', [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') . '">' .
247 '<span class="icon delete-icon"></span>' .
253 $row['actions'] = '<a href="#editCaseRoleDialog" title="' . ts('Assign %1', [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') . '">' .
254 '<i class="crm-i fa-pencil" aria-hidden="true"></i>' .
260 unset($row['relation_type']);
261 unset($row['rel_id']);
262 unset($row['client_id']);
263 unset($row['source']);
264 array_push($relationships, $row);
266 $params['total'] = count($relationships);
268 $caseRelationshipsDT = [];
269 $caseRelationshipsDT['data'] = $relationships;
270 $caseRelationshipsDT['recordsTotal'] = $params['total'];
271 $caseRelationshipsDT['recordsFiltered'] = $params['total'];
273 CRM_Utils_JSON
::output($caseRelationshipsDT);
277 public static function convertToCaseActivity() {
278 $params = ['caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode'];
280 foreach ($params as $param) {
281 $vals[$param] = $_POST[$param] ??
NULL;
284 CRM_Utils_JSON
::output(self
::_convertToCaseActivity($vals));
288 * @param array $params
292 public static function _convertToCaseActivity($params) {
293 if (!$params['activityID'] ||
!$params['caseID']) {
294 return (['error_msg' => 'required params missing.']);
297 $otherActivity = new CRM_Activity_DAO_Activity();
298 $otherActivity->id
= $params['activityID'];
299 if (!$otherActivity->find(TRUE)) {
300 return (['error_msg' => 'activity record is missing.']);
302 $actDateTime = $otherActivity->activity_date_time
;
304 // Create new activity record.
305 $mainActivity = new CRM_Activity_DAO_Activity();
307 CRM_Core_DAO
::storeValues($otherActivity, $mainActVals);
309 // Get new activity subject.
310 if (!empty($params['newSubject'])) {
311 $mainActVals['subject'] = $params['newSubject'];
314 $mainActivity->copyValues($mainActVals);
315 $mainActivity->id
= NULL;
316 $mainActivity->activity_date_time
= $actDateTime;
317 // Make sure this is current revision.
318 $mainActivity->is_current_revision
= TRUE;
319 $mainActivity->original_id
= $otherActivity->id
;
320 $otherActivity->is_current_revision
= FALSE;
322 $mainActivity->save();
323 $mainActivityId = $mainActivity->id
;
324 CRM_Activity_BAO_Activity
::logActivityAction($mainActivity);
326 // Mark previous activity as deleted. If it was a non-case activity
327 // then just change the subject.
328 if (in_array($params['mode'], [
332 $caseActivity = new CRM_Case_DAO_CaseActivity();
333 $caseActivity->case_id
= $params['caseID'];
334 $caseActivity->activity_id
= $otherActivity->id
;
335 if ($params['mode'] == 'move' ||
$caseActivity->find(TRUE)) {
336 $otherActivity->is_deleted
= 1;
339 $otherActivity->subject
= ts('(Filed on case %1)', [
340 1 => $params['caseID'],
341 ]) . ' ' . $otherActivity->subject
;
343 $otherActivity->save();
347 $targetContacts = [];
348 if (!empty($params['targetContactIds'])) {
349 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
352 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
353 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
354 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
355 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
357 $sourceContactID = CRM_Activity_BAO_Activity
::getSourceContactID($params['activityID']);
359 'activity_id' => $mainActivityId,
360 'contact_id' => $sourceContactID,
361 'record_type_id' => $sourceID,
363 CRM_Activity_BAO_ActivityContact
::create($src_params);
365 foreach ($targetContacts as $key => $value) {
367 'activity_id' => $mainActivityId,
368 'contact_id' => $value,
369 'record_type_id' => $targetID,
371 CRM_Activity_BAO_ActivityContact
::create($targ_params);
374 //CRM-21114 retrieve assignee contacts from original case; allow overriding from params
375 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::retrieveContactIdsByActivityId($params['activityID'], $assigneeID);
376 if (!empty($params['assigneeContactIds'])) {
377 $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
379 foreach ($assigneeContacts as $value) {
381 'activity_id' => $mainActivityId,
382 'contact_id' => $value,
383 'record_type_id' => $assigneeID,
385 CRM_Activity_BAO_ActivityContact
::create($assigneeParams);
388 // Attach newly created activity to case.
389 $caseActivity = new CRM_Case_DAO_CaseActivity();
390 $caseActivity->case_id
= $params['caseID'];
391 $caseActivity->activity_id
= $mainActivityId;
392 $caseActivity->save();
393 $error_msg = $caseActivity->_lastError
;
395 $params['mainActivityId'] = $mainActivityId;
396 CRM_Activity_BAO_Activity
::copyExtendedActivityData($params);
397 CRM_Utils_Hook
::post('create', 'CaseActivity', $caseActivity->id
, $caseActivity);
399 return (['error_msg' => $error_msg, 'newId' => $mainActivity->id
]);
403 * Get activities for the contact.
405 * @throws \CRM_Core_Exception
407 public static function getContactActivity() {
408 $requiredParameters = [
412 $optionalParameters = [
413 'context' => 'String',
414 'activity_type_id' => 'Integer',
415 'activity_type_exclude_id' => 'Integer',
416 'activity_status_id' => 'String',
417 'activity_date_time_relative' => 'String',
418 'activity_date_time_low' => 'String',
419 'activity_date_time_high' => 'String',
422 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
423 $params +
= CRM_Core_Page_AJAX
::validateParams($requiredParameters, $optionalParameters);
424 // $params will be modified later on, need to save original filters
425 $filterParams = $params;
427 // To be consistent, the cid parameter should be renamed to contact_id in
428 // the template file, see templates/CRM/Activity/Selector/Selector.tpl
429 $params['contact_id'] = $params['cid'];
430 unset($params['cid']);
432 // get the contact activities
433 $activities = CRM_Activity_BAO_Activity
::getContactActivitySelector($params);
435 foreach ($activities['data'] as $key => $value) {
436 // Check if recurring activity.
437 if (!empty($value['is_recurring_activity'])) {
438 $repeat = $value['is_recurring_activity'];
439 $activities['data'][$key]['activity_type'] .= '<br/><span class="bold">' . ts('Repeating (%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]) . '</span>';
443 // store the activity filter preference CRM-11761
444 if (Civi
::settings()->get('preserve_activity_tab_filter') && ($userID = CRM_Core_Session
::getLoggedInContactID())) {
445 $activityFilter = [];
446 unset($optionalParameters['context']);
447 foreach ($optionalParameters as $searchField => $dataType) {
448 $formSearchField = $searchField;
449 if ($searchField === 'activity_type_id') {
450 $formSearchField = 'activity_type_filter_id';
452 elseif ($searchField === 'activity_type_exclude_id') {
453 $formSearchField = 'activity_type_exclude_filter_id';
455 if (!empty($filterParams[$searchField])) {
456 $activityFilter[$formSearchField] = $filterParams[$searchField];
457 if (in_array($searchField, ['activity_date_time_low', 'activity_date_time_high'])) {
458 $activityFilter['activity_date_time_relative'] = 0;
460 elseif ($searchField === 'activity_status_id') {
461 $activityFilter['status_id'] = explode(',', $activityFilter[$searchField]);
466 Civi
::contactSettings()->set('activity_tab_filter', $activityFilter);
469 CRM_Utils_JSON
::output($activities);