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
20 * This class contains all the function that are called using AJAX (jQuery)
22 class CRM_Activity_Page_AJAX
{
24 public static function getCaseActivity() {
25 // Should those params be passed through the validateParams method?
26 $caseID = CRM_Utils_Type
::validate($_GET['caseID'], 'Integer');
27 $contactID = CRM_Utils_Type
::validate($_GET['cid'], 'Integer');
28 $userID = CRM_Utils_Type
::validate($_GET['userID'], 'Integer');
29 $context = CRM_Utils_Type
::validate(CRM_Utils_Array
::value('context', $_GET), 'String');
31 $optionalParameters = [
32 'source_contact_id' => 'Integer',
33 'status_id' => 'Integer',
34 'activity_deleted' => 'Boolean',
35 'activity_type_id' => 'Integer',
36 // "Date" validation fails because it expects only numbers with no hyphens
37 'activity_date_low' => 'Alphanumeric',
38 'activity_date_high' => 'Alphanumeric',
41 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
42 $params +
= CRM_Core_Page_AJAX
::validateParams([], $optionalParameters);
44 // get the activities related to given case
45 $activities = CRM_Case_BAO_Case
::getCaseActivity($caseID, $params, $contactID, $context, $userID);
47 CRM_Utils_JSON
::output($activities);
50 public static function getCaseGlobalRelationships() {
51 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
53 // get the activities related to given case
54 $globalGroupInfo = [];
56 // get the total row count
57 CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
59 $relGlobal = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo, $params['sortBy'] ??
NULL, $showLinks = TRUE, FALSE, $params['offset'], $params['rp']);
62 // after sort we can update username fields to be a url
63 foreach ($relGlobal as $key => $value) {
65 $relationship['sort_name'] = $value['sort_name'];
66 $relationship['phone'] = $value['phone'];
67 $relationship['email'] = $value['email'];
69 array_push($relationships, $relationship);
72 $globalRelationshipsDT = [];
73 $globalRelationshipsDT['data'] = $relationships;
74 $globalRelationshipsDT['recordsTotal'] = count($relationships);
75 $globalRelationshipsDT['recordsFiltered'] = count($relationships);
77 CRM_Utils_JSON
::output($globalRelationshipsDT);
80 public static function getCaseClientRelationships() {
81 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
82 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
84 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
86 // Retrieve ALL client relationships
87 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($contactID,
88 CRM_Contact_BAO_Relationship
::CURRENT
,
89 0, 0, 0, NULL, NULL, FALSE
92 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID);
94 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
95 // so they don't show up twice.
96 $clientRelationships = [];
97 foreach ($relClient as $r) {
98 if (!array_key_exists($r['id'], $caseRelationships)) {
99 $clientRelationships[] = $r;
103 // sort clientRelationships array using jquery call params
105 if (!empty($params['_raw_values']['sort'])) {
106 foreach ($clientRelationships as $key => $row) {
107 $sortArray[$key] = $row[$params['_raw_values']['sort'][0]];
109 $sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]);
110 array_multisort($sortArray, constant($sort_type), $clientRelationships);
114 // after sort we can update username fields to be a url
115 foreach ($clientRelationships as $key => $value) {
117 $relationship['relation'] = $value['relation'];
118 $relationship['name'] = '<a href=' . CRM_Utils_System
::url('civicrm/contact/view',
119 'action=view&reset=1&cid=' . $clientRelationships[$key]['cid']) . '>' . $clientRelationships[$key]['name'] . '</a>';
120 $relationship['phone'] = $value['phone'];
121 $relationship['email'] = $value['email'];
123 array_push($relationships, $relationship);
126 $clientRelationshipsDT = [];
127 $clientRelationshipsDT['data'] = $relationships;
128 $clientRelationshipsDT['recordsTotal'] = count($relationships);
129 $clientRelationshipsDT['recordsFiltered'] = count($relationships);
131 CRM_Utils_JSON
::output($clientRelationshipsDT);
134 public static function getCaseRoles() {
135 $caseID = CRM_Utils_Type
::escape($_GET['caseID'], 'Integer');
136 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
138 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
140 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($contactID, $caseID);
141 $caseTypeName = CRM_Case_BAO_Case
::getCaseType($caseID, 'name');
142 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
143 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
145 $hasAccessToAllCases = CRM_Core_Permission
::check('access all cases and activities');
147 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
149 foreach ($caseRelationships as $key => $value) {
150 // This role has been filled
151 unset($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']]);
152 // mark original case relationships record to use on setting edit links below
153 $caseRelationships[$key]['source'] = 'caseRel';
156 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($caseID);
158 // move/transform caseRoles array data to caseRelationships
159 // for sorting and display
160 // CRM-14466 added cid to the non-client array to avoid php notice
161 foreach ($caseRoles as $id => $value) {
162 if ($id != "client") {
164 $rel['relation'] = $value;
165 $rel['relation_type'] = $id;
166 $rel['name'] = '(not assigned)';
169 $rel['source'] = 'caseRoles';
170 $caseRelationships[] = $rel;
173 foreach ($value as $clientRole) {
175 $relClient['relation'] = ts('Client');
176 $relClient['name'] = $clientRole['sort_name'];
177 $relClient['phone'] = $clientRole['phone'];
178 $relClient['email'] = $clientRole['email'];
179 $relClient['cid'] = $clientRole['contact_id'];
180 $relClient['source'] = 'contact';
181 $caseRelationships[] = $relClient;
186 // sort clientRelationships array using jquery call params
188 if (!empty($params['_raw_values']['sort'])) {
189 foreach ($caseRelationships as $key => $row) {
190 $sortArray[$key] = $row[$params['_raw_values']['sort'][0]];
192 $sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]);
193 array_multisort($sortArray, constant($sort_type), $caseRelationships);
198 // set user name, email and edit columns links
199 foreach ($caseRelationships as $key => &$row) {
200 $typeLabel = $row['relation'];
201 // Add "<br />(Case Manager)" to label
202 if (!empty($row['relation_type']) && !empty($row['relationship_direction']) && $row['relation_type'] . '_' . $row['relationship_direction'] == $managerRoleId) {
203 $row['relation'] .= '<br />' . '(' . ts('Case Manager') . ')';
206 if (!empty($row['cid'])) {
207 $row['name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System
::url('civicrm/contact/view',
208 'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['name'] . '</a>';
210 // email column links/icon
212 $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" aria-hidden="true"></i></a>';
215 $row['actions'] = '';
216 if ($hasAccessToAllCases) {
217 $contactType = empty($row['relation_type']) ?
'' : (string) CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', $row['relation_type'], 'contact_type_b');
218 $contactType = $contactType == 'Contact' ?
'' : $contactType;
219 switch ($row['source']) {
221 $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') . '">' .
222 '<i class="crm-i fa-pencil" aria-hidden="true"></i>' .
224 '<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') . '">' .
225 '<span class="icon delete-icon"></span>' .
230 $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') . '">' .
231 '<i class="crm-i fa-pencil" aria-hidden="true"></i>' .
237 unset($row['relation_type']);
238 unset($row['rel_id']);
239 unset($row['client_id']);
240 unset($row['source']);
241 array_push($relationships, $row);
243 $params['total'] = count($relationships);
245 $caseRelationshipsDT = [];
246 $caseRelationshipsDT['data'] = $relationships;
247 $caseRelationshipsDT['recordsTotal'] = $params['total'];
248 $caseRelationshipsDT['recordsFiltered'] = $params['total'];
250 CRM_Utils_JSON
::output($caseRelationshipsDT);
254 public static function convertToCaseActivity() {
255 $params = ['caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode'];
257 foreach ($params as $param) {
258 $vals[$param] = $_POST[$param] ??
NULL;
261 CRM_Utils_JSON
::output(self
::_convertToCaseActivity($vals));
265 * @param array $params
269 public static function _convertToCaseActivity($params) {
270 if (!$params['activityID'] ||
!$params['caseID']) {
271 return (['error_msg' => 'required params missing.']);
274 $otherActivity = new CRM_Activity_DAO_Activity();
275 $otherActivity->id
= $params['activityID'];
276 if (!$otherActivity->find(TRUE)) {
277 return (['error_msg' => 'activity record is missing.']);
279 $actDateTime = CRM_Utils_Date
::isoToMysql($otherActivity->activity_date_time
);
281 // Create new activity record.
282 $mainActivity = new CRM_Activity_DAO_Activity();
284 CRM_Core_DAO
::storeValues($otherActivity, $mainActVals);
286 // Get new activity subject.
287 if (!empty($params['newSubject'])) {
288 $mainActVals['subject'] = $params['newSubject'];
291 $mainActivity->copyValues($mainActVals);
292 $mainActivity->id
= NULL;
293 $mainActivity->activity_date_time
= $actDateTime;
294 // Make sure this is current revision.
295 $mainActivity->is_current_revision
= TRUE;
296 $mainActivity->original_id
= $otherActivity->id
;
297 $otherActivity->is_current_revision
= FALSE;
299 $mainActivity->save();
300 $mainActivityId = $mainActivity->id
;
301 CRM_Activity_BAO_Activity
::logActivityAction($mainActivity);
303 // Mark previous activity as deleted. If it was a non-case activity
304 // then just change the subject.
305 if (in_array($params['mode'], [
309 $caseActivity = new CRM_Case_DAO_CaseActivity();
310 $caseActivity->case_id
= $params['caseID'];
311 $caseActivity->activity_id
= $otherActivity->id
;
312 if ($params['mode'] == 'move' ||
$caseActivity->find(TRUE)) {
313 $otherActivity->is_deleted
= 1;
316 $otherActivity->subject
= ts('(Filed on case %1)', [
317 1 => $params['caseID'],
318 ]) . ' ' . $otherActivity->subject
;
320 $otherActivity->save();
324 $targetContacts = [];
325 if (!empty($params['targetContactIds'])) {
326 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
329 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
330 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
331 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
332 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
334 $sourceContactID = CRM_Activity_BAO_Activity
::getSourceContactID($params['activityID']);
336 'activity_id' => $mainActivityId,
337 'contact_id' => $sourceContactID,
338 'record_type_id' => $sourceID,
340 CRM_Activity_BAO_ActivityContact
::create($src_params);
342 foreach ($targetContacts as $key => $value) {
344 'activity_id' => $mainActivityId,
345 'contact_id' => $value,
346 'record_type_id' => $targetID,
348 CRM_Activity_BAO_ActivityContact
::create($targ_params);
351 //CRM-21114 retrieve assignee contacts from original case; allow overriding from params
352 $assigneeContacts = CRM_Activity_BAO_ActivityContact
::retrieveContactIdsByActivityId($params['activityID'], $assigneeID);
353 if (!empty($params['assigneeContactIds'])) {
354 $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
356 foreach ($assigneeContacts as $value) {
358 'activity_id' => $mainActivityId,
359 'contact_id' => $value,
360 'record_type_id' => $assigneeID,
362 CRM_Activity_BAO_ActivityContact
::create($assigneeParams);
365 // Attach newly created activity to case.
366 $caseActivity = new CRM_Case_DAO_CaseActivity();
367 $caseActivity->case_id
= $params['caseID'];
368 $caseActivity->activity_id
= $mainActivityId;
369 $caseActivity->save();
370 $error_msg = $caseActivity->_lastError
;
372 $params['mainActivityId'] = $mainActivityId;
373 CRM_Activity_BAO_Activity
::copyExtendedActivityData($params);
374 CRM_Utils_Hook
::post('create', 'CaseActivity', $caseActivity->id
, $caseActivity);
376 return (['error_msg' => $error_msg, 'newId' => $mainActivity->id
]);
380 * Get activities for the contact.
382 * @throws \CRM_Core_Exception
384 public static function getContactActivity() {
385 $requiredParameters = [
389 $optionalParameters = [
390 'context' => 'String',
391 'activity_type_id' => 'Integer',
392 'activity_type_exclude_id' => 'Integer',
393 'activity_status_id' => 'String',
394 'activity_date_time_relative' => 'String',
395 'activity_date_time_low' => 'String',
396 'activity_date_time_high' => 'String',
399 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
400 $params +
= CRM_Core_Page_AJAX
::validateParams($requiredParameters, $optionalParameters);
402 // To be consistent, the cid parameter should be renamed to contact_id in
403 // the template file, see templates/CRM/Activity/Selector/Selector.tpl
404 $params['contact_id'] = $params['cid'];
405 unset($params['cid']);
407 // get the contact activities
408 $activities = CRM_Activity_BAO_Activity
::getContactActivitySelector($params);
410 foreach ($activities['data'] as $key => $value) {
411 // Check if recurring activity.
412 if (!empty($value['is_recurring_activity'])) {
413 $repeat = $value['is_recurring_activity'];
414 $activities['data'][$key]['activity_type'] .= '<br/><span class="bold">' . ts('Repeating (%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]) . '</span>';
418 // store the activity filter preference CRM-11761
419 if (Civi
::settings()->get('preserve_activity_tab_filter') && ($userID = CRM_Core_Session
::getLoggedInContactID())) {
420 unset($optionalParameters['context']);
421 foreach ($optionalParameters as $searchField => $dataType) {
422 $formSearchField = $searchField;
423 if ($searchField === 'activity_type_id') {
424 $formSearchField = 'activity_type_filter_id';
426 elseif ($searchField === 'activity_type_exclude_id') {
427 $formSearchField = 'activity_type_exclude_filter_id';
429 if (!empty($params[$searchField])) {
430 $activityFilter[$formSearchField] = $params[$searchField];
431 if (in_array($searchField, ['activity_date_time_low', 'activity_date_time_high'])) {
432 $activityFilter['activity_date_time_relative'] = 0;
434 elseif ($searchField === 'activity_status_id') {
435 $activityFilter['status_id'] = explode(',', $activityFilter[$searchField]);
440 Civi
::contactSettings()->set('activity_tab_filter', $activityFilter);
443 CRM_Utils_JSON
::output($activities);