Merge pull request #17859 from civicrm/5.28
[civicrm-core.git] / CRM / Activity / Page / AJAX.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 *
17 */
18
19/**
20 * This class contains all the function that are called using AJAX (jQuery)
21 */
22class CRM_Activity_Page_AJAX {
23
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');
30
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',
39 ];
40
41 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
42 $params += CRM_Core_Page_AJAX::validateParams([], $optionalParameters);
43
44 // get the activities related to given case
45 $activities = CRM_Case_BAO_Case::getCaseActivity($caseID, $params, $contactID, $context, $userID);
46
47 CRM_Utils_JSON::output($activities);
48 }
49
50 public static function getCaseGlobalRelationships() {
51 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
52
53 // get the activities related to given case
54 $globalGroupInfo = [];
55
56 // get the total row count
57 CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, NULL, FALSE, TRUE, NULL, NULL);
58 // limit the rows
59 $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo, $params['sortBy'] ?? NULL, $showLinks = TRUE, FALSE, $params['offset'], $params['rp']);
60
61 $relationships = [];
62 // after sort we can update username fields to be a url
63 foreach ($relGlobal as $key => $value) {
64 $relationship = [];
65 $relationship['sort_name'] = $value['sort_name'];
66 $relationship['phone'] = $value['phone'];
67 $relationship['email'] = $value['email'];
68
69 array_push($relationships, $relationship);
70 }
71
72 $globalRelationshipsDT = [];
73 $globalRelationshipsDT['data'] = $relationships;
74 $globalRelationshipsDT['recordsTotal'] = count($relationships);
75 $globalRelationshipsDT['recordsFiltered'] = count($relationships);
76
77 CRM_Utils_JSON::output($globalRelationshipsDT);
78 }
79
80 public static function getCaseClientRelationships() {
81 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
82 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
83
84 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
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 );
91
92 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($contactID, $caseID);
93
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;
100 }
101 }
102
103 // sort clientRelationships array using jquery call params
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);
111 }
112
113 $relationships = [];
114 // after sort we can update username fields to be a url
115 foreach ($clientRelationships as $key => $value) {
116 $relationship = [];
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'];
122
123 array_push($relationships, $relationship);
124 }
125
126 $clientRelationshipsDT = [];
127 $clientRelationshipsDT['data'] = $relationships;
128 $clientRelationshipsDT['recordsTotal'] = count($relationships);
129 $clientRelationshipsDT['recordsFiltered'] = count($relationships);
130
131 CRM_Utils_JSON::output($clientRelationshipsDT);
132 }
133
134 public static function getCaseRoles() {
135 $caseID = CRM_Utils_Type::escape($_GET['caseID'], 'Integer');
136 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
137
138 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
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();
143 $caseRoles = $xmlProcessor->get($caseTypeName, 'CaseRoles');
144
145 $hasAccessToAllCases = CRM_Core_Permission::check('access all cases and activities');
146
147 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($caseTypeName);
148
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';
154 }
155
156 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($caseID);
157
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") {
163 $rel = [];
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;
171 }
172 else {
173 foreach ($value as $clientRole) {
174 $relClient = [];
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;
182 }
183 }
184 }
185
186 // sort clientRelationships array using jquery call params
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);
194 }
195
196 $relationships = [];
197
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') . ')';
204 }
205 // view user links
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>';
209 }
210 // email column links/icon
211 if ($row['email']) {
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>';
213 }
214 // edit links
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']) {
220 case 'caseRel':
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>' .
223 '</a>' .
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>' .
226 '</a>';
227 break;
228
229 case 'caseRoles':
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>' .
232 '</a>';
233 break;
234 }
235 }
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);
242 }
243 $params['total'] = count($relationships);
244
245 $caseRelationshipsDT = [];
246 $caseRelationshipsDT['data'] = $relationships;
247 $caseRelationshipsDT['recordsTotal'] = $params['total'];
248 $caseRelationshipsDT['recordsFiltered'] = $params['total'];
249
250 CRM_Utils_JSON::output($caseRelationshipsDT);
251
252 }
253
254 public static function convertToCaseActivity() {
255 $params = ['caseID', 'activityID', 'contactID', 'newSubject', 'targetContactIds', 'mode'];
256 $vals = [];
257 foreach ($params as $param) {
258 $vals[$param] = $_POST[$param] ?? NULL;
259 }
260
261 CRM_Utils_JSON::output(self::_convertToCaseActivity($vals));
262 }
263
264 /**
265 * @param array $params
266 *
267 * @return array
268 */
269 public static function _convertToCaseActivity($params) {
270 if (!$params['activityID'] || !$params['caseID']) {
271 return (['error_msg' => 'required params missing.']);
272 }
273
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.']);
278 }
279 $actDateTime = CRM_Utils_Date::isoToMysql($otherActivity->activity_date_time);
280
281 // Create new activity record.
282 $mainActivity = new CRM_Activity_DAO_Activity();
283 $mainActVals = [];
284 CRM_Core_DAO::storeValues($otherActivity, $mainActVals);
285
286 // Get new activity subject.
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;
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;
298
299 $mainActivity->save();
300 $mainActivityId = $mainActivity->id;
301 CRM_Activity_BAO_Activity::logActivityAction($mainActivity);
302
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'], [
306 'move',
307 'file',
308 ])) {
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 {
316 $otherActivity->subject = ts('(Filed on case %1)', [
317 1 => $params['caseID'],
318 ]) . ' ' . $otherActivity->subject;
319 }
320 $otherActivity->save();
321
322 }
323
324 $targetContacts = [];
325 if (!empty($params['targetContactIds'])) {
326 $targetContacts = array_unique(explode(',', $params['targetContactIds']));
327 }
328
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);
333
334 $sourceContactID = CRM_Activity_BAO_Activity::getSourceContactID($params['activityID']);
335 $src_params = [
336 'activity_id' => $mainActivityId,
337 'contact_id' => $sourceContactID,
338 'record_type_id' => $sourceID,
339 ];
340 CRM_Activity_BAO_ActivityContact::create($src_params);
341
342 foreach ($targetContacts as $key => $value) {
343 $targ_params = [
344 'activity_id' => $mainActivityId,
345 'contact_id' => $value,
346 'record_type_id' => $targetID,
347 ];
348 CRM_Activity_BAO_ActivityContact::create($targ_params);
349 }
350
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']));
355 }
356 foreach ($assigneeContacts as $value) {
357 $assigneeParams = [
358 'activity_id' => $mainActivityId,
359 'contact_id' => $value,
360 'record_type_id' => $assigneeID,
361 ];
362 CRM_Activity_BAO_ActivityContact::create($assigneeParams);
363 }
364
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;
371
372 $params['mainActivityId'] = $mainActivityId;
373 CRM_Activity_BAO_Activity::copyExtendedActivityData($params);
374 CRM_Utils_Hook::post('create', 'CaseActivity', $caseActivity->id, $caseActivity);
375
376 return (['error_msg' => $error_msg, 'newId' => $mainActivity->id]);
377 }
378
379 /**
380 * Get activities for the contact.
381 *
382 * @return array
383 */
384 public static function getContactActivity() {
385 $requiredParameters = [
386 'cid' => 'Integer',
387 ];
388
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',
397 ];
398
399 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
400 $params += CRM_Core_Page_AJAX::validateParams($requiredParameters, $optionalParameters);
401
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
407 // get the contact activities
408 $activities = CRM_Activity_BAO_Activity::getContactActivitySelector($params);
409
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>';
415 }
416 }
417
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';
425 }
426 elseif ($searchField == 'activity_type_exclude_id') {
427 $formSearchField = 'activity_type_exclude_filter_id';
428 }
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;
433 }
434 elseif ($searchField == 'activity_status_id') {
435 $activityFilter['status_id'] = explode(',', $activityFilter[$searchField]);
436 }
437 }
438 }
439
440 Civi::contactSettings()->set('activity_tab_filter', $activityFilter);
441 }
442 if (!empty($_GET['is_unit_test'])) {
443 return [$activities, $activityFilter];
444 }
445
446 CRM_Utils_JSON::output($activities);
447 }
448
449}