Merge pull request #17859 from civicrm/5.28
[civicrm-core.git] / CRM / Activity / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 *
17 */
18
19/**
20 * This class contains all the function that are called using AJAX (jQuery)
21 */
22class CRM_Activity_Page_AJAX {
62d3ee27 23
00be9182 24 public static function getCaseActivity() {
5d817a13
MM
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');
6a488035 30
be2fb01f 31 $optionalParameters = [
9c3f979f
MM
32 'source_contact_id' => 'Integer',
33 'status_id' => 'Integer',
34 'activity_deleted' => 'Boolean',
35 'activity_type_id' => 'Integer',
ab9eca19
CW
36 // "Date" validation fails because it expects only numbers with no hyphens
37 'activity_date_low' => 'Alphanumeric',
38 'activity_date_high' => 'Alphanumeric',
be2fb01f 39 ];
6a488035 40
9c3f979f 41 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
be2fb01f 42 $params += CRM_Core_Page_AJAX::validateParams([], $optionalParameters);
6a488035
TO
43
44 // get the activities related to given case
45 $activities = CRM_Case_BAO_Case::getCaseActivity($caseID, $params, $contactID, $context, $userID);
46
ad280fb6 47 CRM_Utils_JSON::output($activities);
6a488035
TO
48 }
49
00be9182 50 public static function getCaseGlobalRelationships() {
9c3f979f 51 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
1d85d241 52
6a488035 53 // get the activities related to given case
be2fb01f 54 $globalGroupInfo = [];
1d85d241 55
6a488035 56 // get the total row count
9c3f979f 57 CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
6a488035 58 // limit the rows
1d154485 59 $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, $params['sortBy'] ?? NULL, $showLinks = TRUE, FALSE, $params['offset'], $params['rp']);
1d85d241 60
be2fb01f 61 $relationships = [];
ad280fb6
JL
62 // after sort we can update username fields to be a url
63 foreach ($relGlobal as $key => $value) {
be2fb01f 64 $relationship = [];
ad280fb6
JL
65 $relationship['sort_name'] = $value['sort_name'];
66 $relationship['phone'] = $value['phone'];
67 $relationship['email'] = $value['email'];
1d85d241 68
ad280fb6
JL
69 array_push($relationships, $relationship);
70 }
71
be2fb01f 72 $globalRelationshipsDT = [];
ad280fb6 73 $globalRelationshipsDT['data'] = $relationships;
9c3f979f
MM
74 $globalRelationshipsDT['recordsTotal'] = count($relationships);
75 $globalRelationshipsDT['recordsFiltered'] = count($relationships);
aab589e2 76
ad280fb6 77 CRM_Utils_JSON::output($globalRelationshipsDT);
1d85d241
DL
78 }
79
00be9182 80 public static function getCaseClientRelationships() {
353ffa53 81 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
6a488035 82 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
1d85d241 83
9c3f979f 84 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
6a488035
TO
85
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
90 );
1d85d241 91
6a488035 92 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
1d85d241 93
6a488035
TO
94 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
95 // so they don't show up twice.
be2fb01f 96 $clientRelationships = [];
6a488035
TO
97 foreach ($relClient as $r) {
98 if (!array_key_exists($r['id'], $caseRelationships)) {
99 $clientRelationships[] = $r;
100 }
101 }
1d85d241 102
6a488035 103 // sort clientRelationships array using jquery call params
1d154485
JP
104 $sortArray = [];
105 if (!empty($params['_raw_values']['sort'])) {
106 foreach ($clientRelationships as $key => $row) {
107 $sortArray[$key] = $row[$params['_raw_values']['sort'][0]];
108 }
109 $sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]);
110 array_multisort($sortArray, constant($sort_type), $clientRelationships);
6a488035 111 }
1d85d241 112
be2fb01f 113 $relationships = [];
6a488035 114 // after sort we can update username fields to be a url
22e263ad 115 foreach ($clientRelationships as $key => $value) {
be2fb01f 116 $relationship = [];
ad280fb6
JL
117 $relationship['relation'] = $value['relation'];
118 $relationship['name'] = '<a href=' . CRM_Utils_System::url('civicrm/contact/view',
353ffa53 119 'action=view&reset=1&cid=' . $clientRelationships[$key]['cid']) . '>' . $clientRelationships[$key]['name'] . '</a>';
ad280fb6
JL
120 $relationship['phone'] = $value['phone'];
121 $relationship['email'] = $value['email'];
122
123 array_push($relationships, $relationship);
6a488035 124 }
1d85d241 125
be2fb01f 126 $clientRelationshipsDT = [];
ad280fb6 127 $clientRelationshipsDT['data'] = $relationships;
9c3f979f
MM
128 $clientRelationshipsDT['recordsTotal'] = count($relationships);
129 $clientRelationshipsDT['recordsFiltered'] = count($relationships);
aab589e2 130
ad280fb6 131 CRM_Utils_JSON::output($clientRelationshipsDT);
1d85d241
DL
132 }
133
00be9182 134 public static function getCaseRoles() {
353ffa53 135 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
6a488035 136 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
1d85d241 137
9c3f979f 138 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
6a488035
TO
139
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();
353ffa53 143 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
1d85d241 144
6a488035 145 $hasAccessToAllCases = CRM_Core_Permission::check('access all cases and activities');
1d85d241 146
6a488035 147 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
ad280fb6 148
6a488035 149 foreach ($caseRelationships as $key => $value) {
3b1c37fe 150 // This role has been filled
41cf58d3 151 unset($caseRoles[$value['relation_type'] . '_' . $value['relationship_direction']]);
b44e3f84 152 // mark original case relationships record to use on setting edit links below
6a488035
TO
153 $caseRelationships[$key]['source'] = 'caseRel';
154 }
1d85d241 155
6a488035
TO
156 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
157
158 // move/transform caseRoles array data to caseRelationships
159 // for sorting and display
5b6db2c7 160 // CRM-14466 added cid to the non-client array to avoid php notice
22e263ad 161 foreach ($caseRoles as $id => $value) {
6a488035 162 if ($id != "client") {
be2fb01f 163 $rel = [];
6a488035
TO
164 $rel['relation'] = $value;
165 $rel['relation_type'] = $id;
166 $rel['name'] = '(not assigned)';
167 $rel['phone'] = '';
168 $rel['email'] = '';
169 $rel['source'] = 'caseRoles';
170 $caseRelationships[] = $rel;
0db6c3e1
TO
171 }
172 else {
22e263ad 173 foreach ($value as $clientRole) {
be2fb01f 174 $relClient = [];
5eb4e891 175 $relClient['relation'] = ts('Client');
6a488035
TO
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;
182 }
183 }
184 }
185
186 // sort clientRelationships array using jquery call params
1d154485
JP
187 $sortArray = [];
188 if (!empty($params['_raw_values']['sort'])) {
189 foreach ($caseRelationships as $key => $row) {
190 $sortArray[$key] = $row[$params['_raw_values']['sort'][0]];
191 }
192 $sort_type = "SORT_" . strtoupper($params['_raw_values']['order'][0]);
193 array_multisort($sortArray, constant($sort_type), $caseRelationships);
6a488035 194 }
6a488035 195
be2fb01f 196 $relationships = [];
5b6db2c7 197
6a488035 198 // set user name, email and edit columns links
ad280fb6 199 foreach ($caseRelationships as $key => &$row) {
3b1c37fe
CW
200 $typeLabel = $row['relation'];
201 // Add "<br />(Case Manager)" to label
41cf58d3 202 if (!empty($row['relation_type']) && !empty($row['relationship_direction']) && $row['relation_type'] . '_' . $row['relationship_direction'] == $managerRoleId) {
3b1c37fe
CW
203 $row['relation'] .= '<br />' . '(' . ts('Case Manager') . ')';
204 }
6a488035 205 // view user links
3662628a 206 if (!empty($row['cid'])) {
86bfa4f6 207 $row['name'] = '<a class="view-contact" title="' . ts('View Contact') . '" href=' . CRM_Utils_System::url('civicrm/contact/view',
353ffa53 208 'action=view&reset=1&cid=' . $row['cid']) . '>' . $row['name'] . '</a>';
5b6db2c7 209 }
6a488035 210 // email column links/icon
c91df8b4 211 if ($row['email']) {
13a3d214 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>';
6a488035
TO
213 }
214 // edit links
3662628a 215 $row['actions'] = '';
6a488035 216 if ($hasAccessToAllCases) {
e3756c36
CW
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;
22e263ad 219 switch ($row['source']) {
32864ccf 220 case 'caseRel':
be2fb01f 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') . '">' .
13a3d214 222 '<i class="crm-i fa-pencil" aria-hidden="true"></i>' .
353ffa53 223 '</a>' .
be2fb01f 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') . '">' .
92fcb95f 225 '<span class="icon delete-icon"></span>' .
353ffa53 226 '</a>';
32864ccf 227 break;
1d85d241 228
32864ccf 229 case 'caseRoles':
be2fb01f 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') . '">' .
13a3d214 231 '<i class="crm-i fa-pencil" aria-hidden="true"></i>' .
353ffa53 232 '</a>';
32864ccf 233 break;
6a488035 234 }
6a488035 235 }
ad280fb6
JL
236 unset($row['cid']);
237 unset($row['relation_type']);
238 unset($row['rel_id']);
239 unset($row['client_id']);
240 unset($row['source']);
241 array_push($relationships, $row);
6a488035 242 }
ad280fb6
JL
243 $params['total'] = count($relationships);
244
be2fb01f 245 $caseRelationshipsDT = [];
ad280fb6
JL
246 $caseRelationshipsDT['data'] = $relationships;
247 $caseRelationshipsDT['recordsTotal'] = $params['total'];
248 $caseRelationshipsDT['recordsFiltered'] = $params['total'];
249
250 CRM_Utils_JSON::output($caseRelationshipsDT);
ffd93213 251
1d85d241
DL
252 }
253
00be9182 254 public static function convertToCaseActivity() {
be2fb01f
CW
255 $params = ['caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode'];
256 $vals = [];
6a488035 257 foreach ($params as $param) {
9c1bc317 258 $vals[$param] = $_POST[$param] ?? NULL;
6a488035
TO
259 }
260
ecdef330 261 CRM_Utils_JSON::output(self::_convertToCaseActivity($vals));
6a488035
TO
262 }
263
ffd93213 264 /**
c490a46a 265 * @param array $params
ffd93213
EM
266 *
267 * @return array
268 */
00be9182 269 public static function _convertToCaseActivity($params) {
6a488035 270 if (!$params['activityID'] || !$params['caseID']) {
be2fb01f 271 return (['error_msg' => 'required params missing.']);
6a488035
TO
272 }
273
274 $otherActivity = new CRM_Activity_DAO_Activity();
275 $otherActivity->id = $params['activityID'];
276 if (!$otherActivity->find(TRUE)) {
be2fb01f 277 return (['error_msg' => 'activity record is missing.']);
6a488035
TO
278 }
279 $actDateTime = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time);
280
7808aae6 281 // Create new activity record.
6a488035 282 $mainActivity = new CRM_Activity_DAO_Activity();
be2fb01f 283 $mainActVals = [];
6a488035
TO
284 CRM_Core_DAO::storeValues($otherActivity, $mainActVals);
285
7808aae6 286 // Get new activity subject.
6a488035
TO
287 if (!empty($params['newSubject'])) {
288 $mainActVals['subject'] = $params['newSubject'];
289 }
290
291 $mainActivity->copyValues($mainActVals);
292 $mainActivity->id = NULL;
293 $mainActivity->activity_date_time = $actDateTime;
7808aae6 294 // Make sure this is current revision.
6a488035 295 $mainActivity->is_current_revision = TRUE;
9a30d940
MWMC
296 $mainActivity->original_id = $otherActivity->id;
297 $otherActivity->is_current_revision = FALSE;
6a488035
TO
298
299 $mainActivity->save();
300 $mainActivityId = $mainActivity->id;
301 CRM_Activity_BAO_Activity::logActivityAction($mainActivity);
6a488035 302
7808aae6
SB
303 // Mark previous activity as deleted. If it was a non-case activity
304 // then just change the subject.
be2fb01f 305 if (in_array($params['mode'], [
353ffa53 306 'move',
79d7553f 307 'file',
be2fb01f 308 ])) {
6a488035
TO
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;
314 }
315 else {
be2fb01f 316 $otherActivity->subject = ts('(Filed on case %1)', [
c5c263ca 317 1 => $params['caseID'],
be2fb01f 318 ]) . ' ' . $otherActivity->subject;
6a488035 319 }
6a488035
TO
320 $otherActivity->save();
321
6a488035 322 }
6a488035 323
be2fb01f 324 $targetContacts = [];
6a488035
TO
325 if (!empty($params['targetContactIds'])) {
326 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
327 }
f813f78e 328
44f817d4 329 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
a24b3694 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);
333
7b1ec1c6 334 $sourceContactID = CRM_Activity_BAO_Activity::getSourceContactID($params['activityID']);
be2fb01f 335 $src_params = [
7b1ec1c6 336 'activity_id' => $mainActivityId,
337 'contact_id' => $sourceContactID,
21dfd5f5 338 'record_type_id' => $sourceID,
be2fb01f 339 ];
7b1ec1c6 340 CRM_Activity_BAO_ActivityContact::create($src_params);
341
6a488035 342 foreach ($targetContacts as $key => $value) {
be2fb01f 343 $targ_params = [
6a488035 344 'activity_id' => $mainActivityId,
1d85d241 345 'contact_id' => $value,
21dfd5f5 346 'record_type_id' => $targetID,
be2fb01f 347 ];
1d85d241 348 CRM_Activity_BAO_ActivityContact::create($targ_params);
6a488035
TO
349 }
350
c012436d
BS
351 //CRM-21114 retrieve assignee contacts from original case; allow overriding from params
352 $assigneeContacts = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($params['activityID'], $assigneeID);
6a488035
TO
353 if (!empty($params['assigneeContactIds'])) {
354 $assigneeContacts = array_unique(explode(',', $params['assigneeContactIds']));
355 }
5402bfb8 356 foreach ($assigneeContacts as $value) {
be2fb01f 357 $assigneeParams = [
6a488035 358 'activity_id' => $mainActivityId,
1d85d241 359 'contact_id' => $value,
21dfd5f5 360 'record_type_id' => $assigneeID,
be2fb01f 361 ];
1d85d241 362 CRM_Activity_BAO_ActivityContact::create($assigneeParams);
6a488035
TO
363 }
364
7808aae6 365 // Attach newly created activity to case.
6a488035
TO
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;
6a488035
TO
371
372 $params['mainActivityId'] = $mainActivityId;
373 CRM_Activity_BAO_Activity::copyExtendedActivityData($params);
388995aa 374 CRM_Utils_Hook::post('create', 'CaseActivity', $caseActivity->id, $caseActivity);
6a488035 375
be2fb01f 376 return (['error_msg' => $error_msg, 'newId' => $mainActivity->id]);
6a488035
TO
377 }
378
f2ac86d1 379 /**
380 * Get activities for the contact.
381 *
382 * @return array
383 */
00be9182 384 public static function getContactActivity() {
be2fb01f 385 $requiredParameters = [
9c3f979f 386 'cid' => 'Integer',
be2fb01f 387 ];
6a488035 388
be2fb01f 389 $optionalParameters = [
9c3f979f 390 'context' => 'String',
23289ddd 391 'activity_type_id' => 'Integer',
392 'activity_type_exclude_id' => 'Integer',
49d4d222 393 'activity_status_id' => 'String',
6e793248 394 'activity_date_time_relative' => 'String',
395 'activity_date_time_low' => 'String',
396 'activity_date_time_high' => 'String',
be2fb01f 397 ];
6a488035 398
9c3f979f 399 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
5d817a13 400 $params += CRM_Core_Page_AJAX::validateParams($requiredParameters, $optionalParameters);
6a488035 401
e8691b2c
MM
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']);
406
6a488035
TO
407 // get the contact activities
408 $activities = CRM_Activity_BAO_Activity::getContactActivitySelector($params);
409
c7d77593 410 foreach ($activities['data'] as $key => $value) {
7808aae6 411 // Check if recurring activity.
b53cbfbc 412 if (!empty($value['is_recurring_activity'])) {
04374d9d 413 $repeat = $value['is_recurring_activity'];
be2fb01f 414 $activities['data'][$key]['activity_type'] .= '<br/><span class="bold">' . ts('Repeating (%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]) . '</span>';
97c7504f 415 }
416 }
417
6a488035 418 // store the activity filter preference CRM-11761
a6d192c8 419 if (Civi::settings()->get('preserve_activity_tab_filter') && ($userID = CRM_Core_Session::getLoggedInContactID())) {
420 unset($optionalParameters['context']);
421 foreach ($optionalParameters as $searchField => $dataType) {
c3a9ccbb
JP
422 $formSearchField = $searchField;
423 if ($searchField == 'activity_type_id') {
424 $formSearchField = 'activity_type_filter_id';
425 }
426 elseif ($searchField == 'activity_type_exclude_id') {
427 $formSearchField = 'activity_type_exclude_filter_id';
428 }
a6d192c8 429 if (!empty($params[$searchField])) {
c1d3e301 430 $activityFilter[$formSearchField] = $params[$searchField];
be2fb01f 431 if (in_array($searchField, ['activity_date_time_low', 'activity_date_time_high'])) {
e04e6d91 432 $activityFilter['activity_date_time_relative'] = 0;
a6d192c8 433 }
434 elseif ($searchField == 'activity_status_id') {
435 $activityFilter['status_id'] = explode(',', $activityFilter[$searchField]);
436 }
437 }
a6d192c8 438 }
f3548d18 439
c3c679b0 440 Civi::contactSettings()->set('activity_tab_filter', $activityFilter);
6a488035 441 }
c3a9ccbb 442 if (!empty($_GET['is_unit_test'])) {
be2fb01f 443 return [$activities, $activityFilter];
c3a9ccbb 444 }
1d85d241 445
7d12de7f 446 CRM_Utils_JSON::output($activities);
6a488035 447 }
96025800 448
6a488035 449}