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