Merge pull request #16150 from jitendrapurohit/dev-1499
[civicrm-core.git] / CRM / Case / Form / Activity.php
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 * This class create activities for a case.
20 */
21 class CRM_Case_Form_Activity extends CRM_Activity_Form_Activity {
22
23 /**
24 * Cases this activity belongs to.
25 *
26 * @var []int
27 */
28 public $_caseId;
29
30 /**
31 * The default case type variable defined.
32 *
33 * @var []int
34 */
35 public $_caseType;
36
37 /**
38 * The array of releted contact info.
39 *
40 * @var array
41 */
42 public $_relatedContacts;
43
44 /**
45 * The case type definition column info
46 * for the caseId;
47 *
48 * @var array
49 */
50 public $_caseTypeDefinition;
51
52 /**
53 * Build the form object.
54 */
55 public function preProcess() {
56 $caseIds = CRM_Utils_Request::retrieve('caseid', 'CommaSeparatedIntegers', $this);
57 $this->_caseId = $caseIds ? explode(',', $caseIds) : [];
58 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
59 if (!$this->_context) {
60 $this->_context = 'caseActivity';
61 }
62 $this->_crmDir = 'Case';
63 $this->assign('context', $this->_context);
64
65 parent::preProcess();
66
67 $scheduleStatusId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Scheduled');
68 $this->assign('scheduleStatusId', $scheduleStatusId);
69
70 if (!$this->_caseId && $this->_activityId) {
71 $this->_caseId = (array) CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseActivity', $this->_activityId,
72 'case_id', 'activity_id'
73 );
74 }
75 if ($this->_caseId) {
76 $this->assign('caseId', $this->_caseId);
77 $this->assign('countId', count($this->_caseId));
78 $this->assign('caseID', CRM_Utils_Array::first($this->_caseId));
79 }
80
81 if (!$this->_caseId ||
82 (!$this->_activityId && !$this->_activityTypeId)
83 ) {
84 CRM_Core_Error::statusBounce(ts('required params missing.'));
85 }
86
87 //check for case activity access.
88 if (!CRM_Case_BAO_Case::accessCiviCase()) {
89 CRM_Core_Error::statusBounce(ts('You are not authorized to access this page.'));
90 }
91 //validate case id.
92 if ($this->_caseId &&
93 !CRM_Core_Permission::check('access all cases and activities')
94 ) {
95 $params = ['type' => 'any'];
96 $allCases = CRM_Case_BAO_Case::getCases(TRUE, $params);
97 if (count(array_intersect($this->_caseId, array_keys($allCases))) == 0) {
98 CRM_Core_Error::statusBounce(ts('You are not authorized to access this page.'));
99 }
100 }
101
102 //validate case activity id.
103 if ($this->_activityId &&
104 ($this->_action & CRM_Core_Action::UPDATE)
105 ) {
106 $valid = CRM_Case_BAO_Case::checkPermission($this->_activityId, 'edit',
107 $this->_activityTypeId
108 );
109 if (!$valid) {
110 CRM_Core_Error::statusBounce(ts('You are not authorized to access this page.'));
111 }
112 }
113
114 foreach ($this->_caseId as $casePos => $caseId) {
115 $this->_caseType[$casePos] = CRM_Case_BAO_Case::getCaseType($caseId, 'name');
116 }
117 $this->assign('caseType', $this->_caseType);
118
119 $this->_caseTypeDefinition = $this->getCaseTypeDefinition();
120
121 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
122 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
123 $this->assign('multiClient', $isMultiClient);
124
125 foreach ($this->_caseId as $casePos => $caseId) {
126 $clients[] = CRM_Case_BAO_Case::getContactNames($caseId);
127 }
128 $this->assign('client_names', $clients);
129
130 $caseIds = implode(',', $this->_caseId);
131 // set context for pushUserContext and for statusBounce
132 if ($this->_context == 'fulltext') {
133 if ($this->_action == CRM_Core_Action::UPDATE || $this->_action == CRM_Core_Action::DELETE) {
134 $url = CRM_Utils_System::url('civicrm/contact/view/case',
135 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseIds}&show=1&context={$this->_context}"
136 );
137 }
138 else {
139 $url = CRM_Utils_System::url('civicrm/contact/search/custom', 'force=1');
140 }
141 }
142 else {
143 $url = CRM_Utils_System::url('civicrm/contact/view/case',
144 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseIds}&show=1"
145 );
146 }
147 if (!$this->_activityId) {
148 $caseTypes = CRM_Case_PseudoConstant::caseType();
149
150 if (empty($caseTypes) && ($this->_activityTypeName == 'Change Case Type') && !$this->_caseId) {
151 $url = CRM_Utils_System::url('civicrm/contact/view/case',
152 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseIds}&show=1"
153 );
154 $session = CRM_Core_Session::singleton();
155 $session->pushUserContext($url);
156 CRM_Core_Error::statusBounce(ts("You do not have any active Case Types"));
157 }
158
159 // check if activity count is within the limit
160 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
161 foreach ($this->_caseId as $casePos => $caseId) {
162 $caseType = $this->_caseType[$casePos];
163 $activityInst = $xmlProcessor->getMaxInstance($caseType);
164
165 // If not bounce back and also provide activity edit link if only one existing activity
166 if (isset($activityInst[$this->_activityTypeName])) {
167 $activityCount = CRM_Case_BAO_Case::getCaseActivityCount($caseId, $this->_activityTypeId);
168 $editUrl = self::checkMaxInstances(
169 $caseId,
170 $this->_activityTypeId,
171 $activityInst[$this->_activityTypeName],
172 $this->_currentUserId,
173 $this->_currentlyViewedContactId,
174 $activityCount
175 );
176 $bounceMessage = self::getMaxInstancesBounceMessage($editUrl, $this->_activityTypeName, $activityInst[$this->_activityTypeName], $activityCount);
177 if ($bounceMessage) {
178 CRM_Core_Error::statusBounce($bounceMessage, $url);
179 }
180 }
181 }
182 }
183
184 // Turn off the prompt which asks the user if they want to create separate
185 // activities when specifying multiple contacts "with" a new activity.
186 // Instead, always create one activity with all contacts together.
187 $this->supportsActivitySeparation = FALSE;
188
189 $session = CRM_Core_Session::singleton();
190 $session->pushUserContext($url);
191 }
192
193 /**
194 * Set default values for the form.
195 */
196 public function setDefaultValues() {
197 $this->_defaults = parent::setDefaultValues();
198 $targetContactValues = [];
199 foreach ($this->_caseId as $key => $val) {
200 //get all clients.
201 $clients = CRM_Case_BAO_Case::getContactNames($val);
202 if (isset($this->_activityId) && empty($_POST)) {
203 if (!CRM_Utils_Array::crmIsEmptyArray($this->_defaults['target_contact'])) {
204 $targetContactValues = array_combine(array_unique($this->_defaults['target_contact']),
205 explode(';', trim($this->_defaults['target_contact_value']))
206 );
207 //exclude all clients.
208 foreach ($clients as $clientId => $vals) {
209 if (array_key_exists($clientId, $targetContactValues)) {
210 unset($targetContactValues[$clientId]);
211 }
212 }
213 }
214 }
215 $this->assign('targetContactValues', empty($targetContactValues) ? FALSE : $targetContactValues);
216
217 if (isset($this->_encounterMedium)) {
218 $this->_defaults['medium_id'] = $this->_encounterMedium;
219 }
220 elseif (empty($this->_defaults['medium_id'])) {
221 // set default encounter medium CRM-4816
222 $medium = CRM_Core_OptionGroup::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1');
223 if (count($medium) == 1) {
224 $this->_defaults['medium_id'] = key($medium);
225 }
226 }
227
228 return $this->_defaults;
229 }
230 }
231
232 public function buildQuickForm() {
233 $this->_fields['source_contact_id']['label'] = ts('Reported By');
234 unset($this->_fields['status_id']['attributes']['required']);
235
236 if ($this->restrictAssignmentByUserAccount()) {
237 $assigneeParameters['uf_user'] = 1;
238 }
239
240 $activityAssignmentGroups = $this->getActivityAssignmentGroups();
241 if (!empty($activityAssignmentGroups)) {
242 $assigneeParameters['group'] = ['IN' => $activityAssignmentGroups];
243 }
244
245 if (!empty($assigneeParameters)) {
246 $this->_fields['assignee_contact_id']['attributes']['api']['params']
247 = array_merge($this->_fields['assignee_contact_id']['attributes']['api']['params'], $assigneeParameters);
248
249 $this->_fields['followup_assignee_contact_id']['attributes']['api']['params']
250 = array_merge($this->_fields['followup_assignee_contact_id']['attributes']['api']['params'], $assigneeParameters);
251
252 //Disallow creating a contact from the assignee field UI.
253 $this->_fields['assignee_contact_id']['attributes']['create'] = FALSE;
254 $this->_fields['followup_assignee_contact_id']['attributes']['create'] = FALSE;
255 }
256
257 if ($this->_caseType) {
258 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
259 $aTypes = [];
260 foreach (array_unique($this->_caseType) as $val) {
261 $activityTypes = $xmlProcessor->get($val, 'ActivityTypes', TRUE);
262 $aTypes = $aTypes + $activityTypes;
263 }
264
265 // remove Open Case activity type since we're inside an existing case
266 $openCaseID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case');
267 unset($aTypes[$openCaseID]);
268 asort($aTypes);
269 $this->_fields['followup_activity_type_id']['attributes'] = ['' => '- select activity type -'] + $aTypes;
270 }
271
272 parent::buildQuickForm();
273
274 if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::DETACH | CRM_Core_Action::RENEW)) {
275 return;
276 }
277
278 $this->assign('urlPath', 'civicrm/case/activity');
279
280 $encounterMediums = CRM_Case_PseudoConstant::encounterMedium();
281
282 if ($this->_activityTypeFile == 'OpenCase' && $this->_action == CRM_Core_Action::UPDATE) {
283 $this->getElement('activity_date_time')->freeze();
284
285 if ($this->_activityId) {
286 // Fixme: what's the justification for this? It seems like it is just re-adding an option in case it is the default and disabled.
287 // Is that really a big problem?
288 $this->_encounterMedium = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $this->_activityId, 'medium_id');
289 if (!array_key_exists($this->_encounterMedium, $encounterMediums)) {
290 $encounterMediums[$this->_encounterMedium] = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'medium_id', $this->_encounterMedium);
291 }
292 }
293 }
294
295 $this->add('select', 'medium_id', ts('Medium'), $encounterMediums, TRUE);
296 $i = 0;
297 foreach ($this->_caseId as $key => $val) {
298 $this->_relatedContacts[] = $rgc = CRM_Case_BAO_Case::getRelatedAndGlobalContacts($val);
299 $contName = CRM_Case_BAO_Case::getContactNames($val);
300 foreach ($contName as $nkey => $nval) {
301 array_push($this->_relatedContacts[$i][0], $this->_relatedContacts[$i][0]['managerOf'] = $nval['display_name']);
302 }
303 $i++;
304 }
305
306 //add case client in send a copy selector.CRM-4438.
307 foreach ($this->_caseId as $key => $val) {
308 $relatedContacts[] = $relCon = CRM_Case_BAO_Case::getContactNames($val);
309 }
310
311 if (!empty($relatedContacts)) {
312 foreach ($relatedContacts as $relatedContact) {
313 $this->_relatedContacts[] = $relatedContact;
314 }
315 }
316
317 if (!empty($this->_relatedContacts)) {
318 $checkBoxes = [];
319 foreach ($this->_relatedContacts as $id => $row) {
320 foreach ($row as $key => $value) {
321 $checkBoxes[$key] = $this->addElement('checkbox', $key, NULL, NULL, ['class' => 'select-row']);
322 }
323 }
324
325 $this->addGroup($checkBoxes, 'contact_check');
326 $this->addElement('checkbox', 'toggleSelect', NULL, NULL,
327 ['class' => 'select-rows']
328 );
329 $this->assign('searchRows', $this->_relatedContacts);
330 }
331 $this->_relatedContacts = $rgc + $relCon;
332
333 $this->addFormRule(['CRM_Case_Form_Activity', 'formRule'], $this);
334 }
335
336 /**
337 * Global form rule.
338 *
339 * @param array $fields
340 * The input form values.
341 * @param array $files
342 * The uploaded files if any.
343 * @param $self
344 *
345 * @return bool|array
346 * true if no errors, else array of errors
347 */
348 public static function formRule($fields, $files, $self) {
349 // skip form rule if deleting
350 if (CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Delete' || CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Restore') {
351 return TRUE;
352 }
353
354 return parent::formRule($fields, $files, $self);
355 }
356
357 /**
358 * Process the form submission.
359 *
360 * @param array $params
361 */
362 public function postProcess($params = NULL) {
363 $transaction = new CRM_Core_Transaction();
364
365 if ($this->_action & CRM_Core_Action::DELETE) {
366 $statusMsg = NULL;
367
368 //block deleting activities which affects
369 //case attributes.CRM-4543
370 $activityCondition = " AND v.name IN ('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date')";
371 $caseAttributeActivities = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $activityCondition);
372
373 if (!array_key_exists($this->_activityTypeId, $caseAttributeActivities)) {
374 $params = ['id' => $this->_activityId];
375 $activityDelete = CRM_Activity_BAO_Activity::deleteActivity($params, TRUE);
376 if ($activityDelete) {
377 $statusMsg = ts('The selected activity has been moved to the Trash. You can view and / or restore deleted activities by checking "Deleted Activities" from the Case Activities search filter (under Manage Case).<br />');
378 }
379 }
380 else {
381 $statusMsg = ts("Selected Activity cannot be deleted.");
382 }
383
384 $tagParams = [
385 'entity_table' => 'civicrm_activity',
386 'entity_id' => $this->_activityId,
387 ];
388 CRM_Core_BAO_EntityTag::del($tagParams);
389
390 CRM_Core_Session::setStatus('', $statusMsg, 'info');
391 return;
392 }
393
394 if ($this->_action & CRM_Core_Action::RENEW) {
395 $statusMsg = NULL;
396 $params = ['id' => $this->_activityId];
397 $activityRestore = CRM_Activity_BAO_Activity::restoreActivity($params);
398 if ($activityRestore) {
399 $statusMsg = ts('The selected activity has been restored.<br />');
400 }
401 CRM_Core_Session::setStatus('', $statusMsg, 'info');
402 return;
403 }
404
405 // store the submitted values in an array
406 // Explanation for why we only check the is_unittest element: Prior to adding that check, there was no check and so any $params passed in would have been overwritten. Just in case somebody is passing in some non-null params and that broken code would have inadvertently been working, we can maintain backwards compatibility by only checking for the is_unittest parameter, and so that broken code will still work. At the same time this allows unit tests to pass in a $params without it getting overwritten. See also PR #2077 for some discussion of when the $params parameter was added as a passed in variable.
407 if (empty($params['is_unittest'])) {
408 $params = $this->controller->exportValues($this->_name);
409 }
410
411 //set parent id if its edit mode
412 if ($parentId = CRM_Utils_Array::value('parent_id', $this->_defaults)) {
413 $params['parent_id'] = $parentId;
414 }
415
416 $params['activity_type_id'] = $this->_activityTypeId;
417
418 // format with contact (target contact) values
419 if (isset($params['target_contact_id'])) {
420 $params['target_contact_id'] = explode(',', $params['target_contact_id']);
421 }
422 else {
423 $params['target_contact_id'] = [];
424 }
425
426 // format activity custom data
427 if (!empty($params['hidden_custom'])) {
428 if ($this->_activityId) {
429 // retrieve and include the custom data of old Activity
430 $oldActivity = civicrm_api3('Activity', 'getsingle', ['id' => $this->_activityId]);
431 $params = array_merge($oldActivity, $params);
432
433 // unset custom fields-id from params since we want custom
434 // fields to be saved for new activity.
435 foreach ($params as $key => $value) {
436 $match = [];
437 if (preg_match('/^(custom_\d+_)(\d+)$/', $key, $match)) {
438 $params[$match[1] . '-1'] = $params[$key];
439
440 // for autocomplete transfer hidden value instead of label
441 if ($params[$key] && isset($params[$key . '_id'])) {
442 $params[$match[1] . '-1_id'] = $params[$key . '_id'];
443 unset($params[$key . '_id']);
444 }
445 unset($params[$key]);
446 }
447 }
448 }
449
450 // build custom data getFields array
451 $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId);
452 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
453 CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE,
454 NULL, NULL, TRUE
455 )
456 );
457 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
458 $this->_activityId,
459 'Activity'
460 );
461 }
462
463 // assigning formatted value
464 if (!empty($params['assignee_contact_id'])) {
465 $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']);
466 }
467 else {
468 $params['assignee_contact_id'] = [];
469 }
470
471 if (isset($this->_activityId)) {
472 // activity which hasn't been modified by a user yet
473 if ($this->_defaults['is_auto'] == 1) {
474 $params['is_auto'] = 0;
475 }
476
477 // always create a revision of an case activity. CRM-4533
478 $newActParams = $params;
479
480 // add target contact values in update mode
481 if (empty($params['target_contact_id']) && !empty($this->_defaults['target_contact'])) {
482 $newActParams['target_contact_id'] = $this->_defaults['target_contact'];
483 }
484 }
485
486 if (!isset($newActParams)) {
487 // add more attachments if needed for old activity
488 CRM_Core_BAO_File::formatAttachment($params,
489 $params,
490 'civicrm_activity'
491 );
492
493 // call begin post process, before the activity is created/updated.
494 $this->beginPostProcess($params);
495 foreach ($this->_caseId as $key => $val) {
496 $params['case_id'] = $val;
497 // activity create/update
498 $activity = CRM_Activity_BAO_Activity::create($params);
499 $vvalue[] = ['case_id' => $val, 'actId' => $activity->id];
500 // call end post process, after the activity has been created/updated.
501 $this->endPostProcess($params, $activity);
502 }
503 }
504 else {
505 // create a new version of activity if activity was found to
506 // have been modified/created by user
507
508 // since the params we need to set are very few, and we don't want rest of the
509 // work done by bao create method , lets use dao object to make the changes
510 $params = ['id' => $this->_activityId];
511 $params['is_current_revision'] = 0;
512 $activity = new CRM_Activity_DAO_Activity();
513 $activity->copyValues($params);
514 $activity->save();
515
516 // set proper original_id
517 if (!empty($this->_defaults['original_id'])) {
518 $newActParams['original_id'] = $this->_defaults['original_id'];
519 }
520 else {
521 $newActParams['original_id'] = $activity->id;
522 }
523
524 //is_current_revision will be set to 1 by default.
525 // add attachments if any
526 CRM_Core_BAO_File::formatAttachment($newActParams,
527 $newActParams,
528 'civicrm_activity'
529 );
530
531 // call begin post process, before the activity is created/updated.
532 $this->beginPostProcess($newActParams);
533 foreach ($this->_caseId as $key => $val) {
534 $newActParams['case_id'] = $val;
535 $activity = CRM_Activity_BAO_Activity::create($newActParams);
536 $vvalue[] = ['case_id' => $val, 'actId' => $activity->id];
537 // call end post process, after the activity has been created/updated.
538 $this->endPostProcess($newActParams, $activity);
539 }
540 // copy files attached to old activity if any, to new one,
541 // as long as users have not selected the 'delete attachment' option.
542 if (empty($newActParams['is_delete_attachment']) && ($this->_activityId != $activity->id)) {
543 CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $this->_activityId,
544 'civicrm_activity', $activity->id
545 );
546 }
547
548 // copy back params to original var
549 $params = $newActParams;
550 }
551
552 foreach ($vvalue as $vkey => $vval) {
553 if ($vval['actId']) {
554 // add tags if exists
555 $tagParams = [];
556 if (!empty($params['tag'])) {
557 if (!is_array($params['tag'])) {
558 $params['tag'] = explode(',', $params['tag']);
559 }
560
561 $tagParams = array_fill_keys($params['tag'], 1);
562 }
563
564 //save static tags
565 CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $vval['actId']);
566
567 //save free tags
568 if (isset($params['taglist']) && !empty($params['taglist'])) {
569 CRM_Core_Form_Tag::postProcess($params['taglist'], $vval['actId'], 'civicrm_activity', $this);
570 }
571 }
572
573 // update existing case record if needed
574 $caseParams = $params;
575 $caseParams['id'] = $vval['case_id'];
576 if (!empty($caseParams['case_status_id'])) {
577 $caseParams['status_id'] = $caseParams['case_status_id'];
578 }
579
580 // unset params intended for activities only
581 unset($caseParams['subject'], $caseParams['details'],
582 $caseParams['status_id'], $caseParams['custom']
583 );
584 CRM_Case_BAO_Case::create($caseParams);
585 // create case activity record
586 $caseParams = [
587 'activity_id' => $vval['actId'],
588 'case_id' => $vval['case_id'],
589 ];
590 CRM_Case_BAO_Case::processCaseActivity($caseParams);
591 }
592
593 // send copy to selected contacts.
594 $mailStatus = '';
595 $mailToContacts = [];
596
597 //CRM-5695
598 //check for notification settings for assignee contacts
599 $selectedContacts = ['contact_check'];
600 if (Civi::settings()->get('activity_assignee_notification')) {
601 $selectedContacts[] = 'assignee_contact_id';
602 }
603
604 foreach ($vvalue as $vkey => $vval) {
605 foreach ($selectedContacts as $dnt => $val) {
606 if (array_key_exists($val, $params) && !CRM_Utils_Array::crmIsEmptyArray($params[$val])) {
607 if ($val == 'contact_check') {
608 $mailStatus = ts("A copy of the activity has also been sent to selected contacts(s).");
609 }
610 else {
611 $this->_relatedContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames([$vval['actId']], TRUE, FALSE);
612 $mailStatus .= ' ' . ts("A copy of the activity has also been sent to assignee contacts(s).");
613 }
614 //build an associative array with unique email addresses.
615 foreach ($params[$val] as $key => $value) {
616 if ($val == 'contact_check') {
617 $id = $key;
618 }
619 else {
620 $id = $value;
621 }
622
623 if (isset($id) && array_key_exists($id, $this->_relatedContacts) && isset($this->_relatedContacts[$id]['email'])) {
624 //if email already exists in array then append with ', ' another role only otherwise add it to array.
625 if ($contactDetails = CRM_Utils_Array::value($this->_relatedContacts[$id]['email'], $mailToContacts)) {
626 $caseRole = CRM_Utils_Array::value('role', $this->_relatedContacts[$id]);
627 $mailToContacts[$this->_relatedContacts[$id]['email']]['role'] = $contactDetails['role'] . ', ' . $caseRole;
628 }
629 else {
630 $mailToContacts[$this->_relatedContacts[$id]['email']] = $this->_relatedContacts[$id];
631 }
632 }
633 }
634 }
635 }
636
637 $extraParams = ['case_id' => $vval['case_id'], 'client_id' => $this->_currentlyViewedContactId];
638 $result = CRM_Activity_BAO_Activity::sendToAssignee($activity, $mailToContacts, $extraParams);
639 if (empty($result)) {
640 $mailStatus = '';
641 }
642
643 // create follow up activity if needed
644 $followupStatus = '';
645 if (!empty($params['followup_activity_type_id'])) {
646 $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($vval['actId'], $params);
647
648 if ($followupActivity) {
649 $caseParams = [
650 'activity_id' => $followupActivity->id,
651 'case_id' => $vval['case_id'],
652 ];
653 CRM_Case_BAO_Case::processCaseActivity($caseParams);
654 $followupStatus = ts("A followup activity has been scheduled.") . '<br /><br />';
655 }
656 }
657 $title = ts("%1 Saved", [1 => $this->_activityTypeName]);
658 CRM_Core_Session::setStatus($followupStatus . $mailStatus, $title, 'success');
659 }
660 }
661
662 /**
663 * Returns the groups that contacts must belong to in order to be assigned
664 * an activity for this case. It returns an empty array if no groups are found for
665 * the case type linked to the caseId.
666 *
667 * @return array
668 */
669 private function getActivityAssignmentGroups() {
670 if (!$this->_caseTypeDefinition) {
671 return [];
672 }
673
674 $assignmentGroups = [];
675 foreach ($this->_caseTypeDefinition as $caseId => $definition) {
676 if (!empty($definition['activityAsgmtGrps'])) {
677 $assignmentGroups = array_merge($assignmentGroups, $definition['activityAsgmtGrps']);
678 }
679 }
680
681 return $assignmentGroups;
682 }
683
684 /**
685 * Returns whether contacts must have a user account in order to be
686 * assigned an activity for this case.
687 *
688 * @return bool
689 */
690 private function restrictAssignmentByUserAccount() {
691 if (!$this->_caseTypeDefinition) {
692 return FALSE;
693 }
694
695 foreach ($this->_caseTypeDefinition as $caseId => $definition) {
696 if (!empty($definition['restrictActivityAsgmtToCmsUser'])) {
697 return TRUE;
698 }
699 }
700
701 return FALSE;
702 }
703
704 /**
705 * Returns the case type definition column value for the case type linked to the caseId.
706 *
707 * @return array
708 */
709 private function getCaseTypeDefinition() {
710 if (!$this->_caseId) {
711 return [];
712 }
713
714 $definitions = civicrm_api3('CaseType', 'get', [
715 'return' => ['name', 'definition'],
716 'name' => ['IN' => array_unique($this->_caseType)],
717 ]);
718
719 return array_column($definitions['values'], 'definition', 'name');
720 }
721
722 /**
723 * Get the edit link for a case activity
724 *
725 * This isn't here for reusability - it was a pull out
726 * from preProcess to make it easier to test.
727 * There is CRM_Case_Selector_Search::addCaseActivityLinks but it would
728 * need some rejigging, and there's also a FIXME note there already.
729 *
730 * @param int $caseId
731 * @param int $activityTypeId
732 * @param int $currentUserId
733 * @param int $currentlyViewedContactId
734 *
735 * @return string
736 */
737 public static function getCaseActivityEditLink($caseId, $activityTypeId, $currentUserId, $currentlyViewedContactId) {
738 $atArray = ['activity_type_id' => $activityTypeId];
739 $activities = CRM_Case_BAO_Case::getCaseActivity($caseId,
740 $atArray,
741 $currentUserId
742 );
743 $firstActivity = CRM_Utils_Array::first($activities['data']);
744 $activityId = empty($firstActivity['DT_RowId']) ? 0 : $firstActivity['DT_RowId'];
745 return CRM_Utils_System::url('civicrm/case/activity',
746 "reset=1&cid={$currentlyViewedContactId}&caseid={$caseId}&action=update&id={$activityId}"
747 );
748 }
749
750 /**
751 * Check the current activity count against max instances for a given case id and activity type.
752 *
753 * This isn't here for reusability - it was a pull out
754 * from preProcess to make it easier to test.
755 *
756 * @param int $caseId
757 * @param int $activityTypeId
758 * @param int $maxInstances
759 * @param int $currentUserId
760 * @param int $currentlyViewedContactId
761 * @param int $activityCount
762 *
763 * @return string
764 * If there is more than one existing activity of the given type then it's not clear which url to return so return null for the url.
765 */
766 public static function checkMaxInstances($caseId, $activityTypeId, $maxInstances, $currentUserId, $currentlyViewedContactId, $activityCount) {
767 $editUrl = NULL;
768 if ($activityCount >= $maxInstances) {
769 if ($maxInstances == 1) {
770 $editUrl = self::getCaseActivityEditLink($caseId, $activityTypeId, $currentUserId, $currentlyViewedContactId);
771 }
772 }
773 return $editUrl;
774 }
775
776 /**
777 * Compute the message text for the bounce message when max_instances is reached, depending on whether it's one or more than one.
778 *
779 * @param string $editUrl
780 * @param string $activityTypeName
781 * This is actually label!! But we do want label though in this function.
782 * @param int $maxInstances
783 * @param int $activityCount
784 * Count of existing activities of the given type on the case
785 *
786 * @return string
787 */
788 public static function getMaxInstancesBounceMessage($editUrl, $activityTypeName, $maxInstances, $activityCount) {
789 $bounceMessage = NULL;
790 if ($activityCount >= $maxInstances) {
791 if ($maxInstances == 1) {
792 $bounceMessage = ts("You can not add another '%1' activity to this case. %2",
793 [
794 1 => $activityTypeName,
795 2 => ts("Do you want to <a %1>edit the existing activity</a>?", [1 => "href='$editUrl'"]),
796 ]
797 );
798 }
799 else {
800 // More than one instance, so don't provide a link. What would it be a link to anyway?
801 $bounceMessage = ts("You can not add another '%1' activity to this case.",
802 [
803 1 => $activityTypeName,
804 ]
805 );
806 }
807 }
808 return $bounceMessage;
809 }
810
811 }