Merge pull request #13596 from jackrabbithanna/726-address-api-states
[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 = array('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 = array('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 array(
198 1 => $this->_activityTypeName,
199 2 => ts("Do you want to <a %1>edit the existing activity</a>?", array(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 = array();
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 = array();
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'] = array('' => '- 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 = array();
344 foreach ($this->_relatedContacts as $id => $row) {
345 foreach ($row as $key => $value) {
346 $checkBoxes[$key] = $this->addElement('checkbox', $key, NULL, NULL, array('class' => 'select-row'));
347 }
348 }
349
350 $this->addGroup($checkBoxes, 'contact_check');
351 $this->addElement('checkbox', 'toggleSelect', NULL, NULL,
352 array('class' => 'select-rows')
353 );
354 $this->assign('searchRows', $this->_relatedContacts);
355 }
356 $this->_relatedContacts = $rgc + $relCon;
357
358 $this->addFormRule(array('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 = array('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 = array(
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 = array('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 $params = $this->controller->exportValues($this->_name);
432
433 //set parent id if its edit mode
434 if ($parentId = CRM_Utils_Array::value('parent_id', $this->_defaults)) {
435 $params['parent_id'] = $parentId;
436 }
437
438 $params['activity_type_id'] = $this->_activityTypeId;
439
440 // format with contact (target contact) values
441 if (isset($params['target_contact_id'])) {
442 $params['target_contact_id'] = explode(',', $params['target_contact_id']);
443 }
444 else {
445 $params['target_contact_id'] = array();
446 }
447
448 // format activity custom data
449 if (!empty($params['hidden_custom'])) {
450 if ($this->_activityId) {
451 // retrieve and include the custom data of old Activity
452 $oldActivity = civicrm_api3('Activity', 'getsingle', array('id' => $this->_activityId));
453 $params = array_merge($oldActivity, $params);
454
455 // unset custom fields-id from params since we want custom
456 // fields to be saved for new activity.
457 foreach ($params as $key => $value) {
458 $match = array();
459 if (preg_match('/^(custom_\d+_)(\d+)$/', $key, $match)) {
460 $params[$match[1] . '-1'] = $params[$key];
461
462 // for autocomplete transfer hidden value instead of label
463 if ($params[$key] && isset($params[$key . '_id'])) {
464 $params[$match[1] . '-1_id'] = $params[$key . '_id'];
465 unset($params[$key . '_id']);
466 }
467 unset($params[$key]);
468 }
469 }
470 }
471
472 // build custom data getFields array
473 $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId);
474 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
475 CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE,
476 NULL, NULL, TRUE
477 )
478 );
479 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
480 $this->_activityId,
481 'Activity'
482 );
483 }
484
485 // assigning formatted value
486 if (!empty($params['assignee_contact_id'])) {
487 $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']);
488 }
489 else {
490 $params['assignee_contact_id'] = array();
491 }
492
493 if (isset($this->_activityId)) {
494 // activity which hasn't been modified by a user yet
495 if ($this->_defaults['is_auto'] == 1) {
496 $params['is_auto'] = 0;
497 }
498
499 // always create a revision of an case activity. CRM-4533
500 $newActParams = $params;
501
502 // add target contact values in update mode
503 if (empty($params['target_contact_id']) && !empty($this->_defaults['target_contact'])) {
504 $newActParams['target_contact_id'] = $this->_defaults['target_contact'];
505 }
506 }
507
508 if (!isset($newActParams)) {
509 // add more attachments if needed for old activity
510 CRM_Core_BAO_File::formatAttachment($params,
511 $params,
512 'civicrm_activity'
513 );
514
515 // call begin post process, before the activity is created/updated.
516 $this->beginPostProcess($params);
517 foreach ($this->_caseId as $key => $val) {
518 $params['case_id'] = $val;
519 // activity create/update
520 $activity = CRM_Activity_BAO_Activity::create($params);
521 $vvalue[] = array('case_id' => $val, 'actId' => $activity->id);
522 // call end post process, after the activity has been created/updated.
523 $this->endPostProcess($params, $activity);
524 }
525 }
526 else {
527 // since the params we need to set are very few, and we don't want rest of the
528 // work done by bao create method , lets use dao object to make the changes
529 $params = array('id' => $this->_activityId);
530 $params['is_current_revision'] = 0;
531 $activity = new CRM_Activity_DAO_Activity();
532 $activity->copyValues($params);
533 $activity->save();
534 }
535
536 // create a new version of activity if activity was found to
537 // have been modified/created by user
538 if (isset($newActParams)) {
539 // set proper original_id
540 if (!empty($this->_defaults['original_id'])) {
541 $newActParams['original_id'] = $this->_defaults['original_id'];
542 }
543 else {
544 $newActParams['original_id'] = $activity->id;
545 }
546
547 //is_current_revision will be set to 1 by default.
548 // add attachments if any
549 CRM_Core_BAO_File::formatAttachment($newActParams,
550 $newActParams,
551 'civicrm_activity'
552 );
553
554 // call begin post process, before the activity is created/updated.
555 $this->beginPostProcess($newActParams);
556 foreach ($this->_caseId as $key => $val) {
557 $newActParams['case_id'] = $val;
558 $activity = CRM_Activity_BAO_Activity::create($newActParams);
559 $vvalue[] = array('case_id' => $val, 'actId' => $activity->id);
560 // call end post process, after the activity has been created/updated.
561 $this->endPostProcess($newActParams, $activity);
562 }
563 // copy files attached to old activity if any, to new one,
564 // as long as users have not selected the 'delete attachment' option.
565 if (empty($newActParams['is_delete_attachment']) && ($this->_activityId != $activity->id)) {
566 CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $this->_activityId,
567 'civicrm_activity', $activity->id
568 );
569 }
570
571 // copy back params to original var
572 $params = $newActParams;
573 }
574
575 foreach ($vvalue as $vkey => $vval) {
576 if ($vval['actId']) {
577 // add tags if exists
578 $tagParams = array();
579 if (!empty($params['tag'])) {
580 if (!is_array($params['tag'])) {
581 $params['tag'] = explode(',', $params['tag']);
582 }
583
584 $tagParams = array_fill_keys($params['tag'], 1);
585 }
586
587 //save static tags
588 CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_activity', $vval['actId']);
589
590 //save free tags
591 if (isset($params['taglist']) && !empty($params['taglist'])) {
592 CRM_Core_Form_Tag::postProcess($params['taglist'], $vval['actId'], 'civicrm_activity', $this);
593 }
594 }
595
596 // update existing case record if needed
597 $caseParams = $params;
598 $caseParams['id'] = $vval['case_id'];
599 if (!empty($caseParams['case_status_id'])) {
600 $caseParams['status_id'] = $caseParams['case_status_id'];
601 }
602
603 // unset params intended for activities only
604 unset($caseParams['subject'], $caseParams['details'],
605 $caseParams['status_id'], $caseParams['custom']
606 );
607 CRM_Case_BAO_Case::create($caseParams);
608 // create case activity record
609 $caseParams = array(
610 'activity_id' => $vval['actId'],
611 'case_id' => $vval['case_id'],
612 );
613 CRM_Case_BAO_Case::processCaseActivity($caseParams);
614 }
615
616 // send copy to selected contacts.
617 $mailStatus = '';
618 $mailToContacts = array();
619
620 //CRM-5695
621 //check for notification settings for assignee contacts
622 $selectedContacts = array('contact_check');
623 if (Civi::settings()->get('activity_assignee_notification')) {
624 $selectedContacts[] = 'assignee_contact_id';
625 }
626
627 foreach ($vvalue as $vkey => $vval) {
628 foreach ($selectedContacts as $dnt => $val) {
629 if (array_key_exists($val, $params) && !CRM_Utils_Array::crmIsEmptyArray($params[$val])) {
630 if ($val == 'contact_check') {
631 $mailStatus = ts("A copy of the activity has also been sent to selected contacts(s).");
632 }
633 else {
634 $this->_relatedContacts = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames(array($vval['actId']), TRUE, FALSE);
635 $mailStatus .= ' ' . ts("A copy of the activity has also been sent to assignee contacts(s).");
636 }
637 //build an associative array with unique email addresses.
638 foreach ($params[$val] as $key => $value) {
639 if ($val == 'contact_check') {
640 $id = $key;
641 }
642 else {
643 $id = $value;
644 }
645
646 if (isset($id) && array_key_exists($id, $this->_relatedContacts) && isset($this->_relatedContacts[$id]['email'])) {
647 //if email already exists in array then append with ', ' another role only otherwise add it to array.
648 if ($contactDetails = CRM_Utils_Array::value($this->_relatedContacts[$id]['email'], $mailToContacts)) {
649 $caseRole = CRM_Utils_Array::value('role', $this->_relatedContacts[$id]);
650 $mailToContacts[$this->_relatedContacts[$id]['email']]['role'] = $contactDetails['role'] . ', ' . $caseRole;
651 }
652 else {
653 $mailToContacts[$this->_relatedContacts[$id]['email']] = $this->_relatedContacts[$id];
654 }
655 }
656 }
657 }
658 }
659
660 $extraParams = array('case_id' => $vval['case_id'], 'client_id' => $this->_currentlyViewedContactId);
661 $result = CRM_Activity_BAO_Activity::sendToAssignee($activity, $mailToContacts, $extraParams);
662 if (empty($result)) {
663 $mailStatus = '';
664 }
665
666 // create follow up activity if needed
667 $followupStatus = '';
668 if (!empty($params['followup_activity_type_id'])) {
669 $followupActivity = CRM_Activity_BAO_Activity::createFollowupActivity($vval['actId'], $params);
670
671 if ($followupActivity) {
672 $caseParams = array(
673 'activity_id' => $followupActivity->id,
674 'case_id' => $vval['case_id'],
675 );
676 CRM_Case_BAO_Case::processCaseActivity($caseParams);
677 $followupStatus = ts("A followup activity has been scheduled.") . '<br /><br />';
678 }
679 }
680 $title = ts("%1 Saved", array(1 => $this->_activityTypeName));
681 CRM_Core_Session::setStatus($followupStatus . $mailStatus, $title, 'success');
682 }
683 }
684
685 /**
686 * Returns the groups that contacts must belong to in order to be assigned
687 * an activity for this case. It returns an empty array if no groups are found for
688 * the case type linked to the caseId.
689 *
690 * @return array
691 */
692 private function getActivityAssignmentGroups() {
693 if (!$this->_caseTypeDefinition) {
694 return [];
695 }
696
697 $assignmentGroups = [];
698 foreach ($this->_caseTypeDefinition as $caseId => $definition) {
699 if (!empty($definition['activityAsgmtGrps'])) {
700 $assignmentGroups = array_merge($assignmentGroups, $definition['activityAsgmtGrps']);
701 }
702 }
703
704 return $assignmentGroups;
705 }
706
707 /**
708 * Returns whether contacts must have a user account in order to be
709 * assigned an activity for this case.
710 *
711 * @return bool
712 */
713 private function restrictAssignmentByUserAccount() {
714 if (!$this->_caseTypeDefinition) {
715 return FALSE;
716 }
717
718 foreach ($this->_caseTypeDefinition as $caseId => $definition) {
719 if (!empty($definition['restrictActivityAsgmtToCmsUser'])) {
720 return TRUE;
721 }
722 }
723
724 return FALSE;
725 }
726
727 /**
728 * Returns the case type definition column value for the case type linked to the caseId.
729 *
730 * @return array
731 */
732 private function getCaseTypeDefinition() {
733 if (!$this->_caseId) {
734 return [];
735 }
736
737 $definitions = civicrm_api3('CaseType', 'get', [
738 'return' => ['name', 'definition'],
739 'name' => ['IN' => array_unique($this->_caseType)],
740 ]);
741
742 return array_column($definitions['values'], 'definition', 'name');
743 }
744
745 }