3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class create activities for a case.
21 class CRM_Case_Form_Activity
extends CRM_Activity_Form_Activity
{
24 * Cases this activity belongs to.
31 * The default case type variable defined.
38 * The array of releted contact info.
42 public $_relatedContacts;
45 * The case type definition column info
50 public $_caseTypeDefinition;
53 * Build the form object.
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';
62 $this->_crmDir
= 'Case';
63 $this->assign('context', $this->_context
);
67 $scheduleStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Scheduled');
68 $this->assign('scheduleStatusId', $scheduleStatusId);
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'
76 $this->assign('caseId', $this->_caseId
);
77 $this->assign('countId', count($this->_caseId
));
78 $this->assign('caseID', CRM_Utils_Array
::first($this->_caseId
));
81 if (!$this->_caseId ||
82 (!$this->_activityId
&& !$this->_activityTypeId
)
84 CRM_Core_Error
::statusBounce(ts('required params missing.'));
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.'));
93 !CRM_Core_Permission
::check('access all cases and activities')
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.'));
102 //validate case activity id.
103 if ($this->_activityId
&&
104 ($this->_action
& CRM_Core_Action
::UPDATE
)
106 $valid = CRM_Case_BAO_Case
::checkPermission($this->_activityId
, 'edit',
107 $this->_activityTypeId
110 CRM_Core_Error
::statusBounce(ts('You are not authorized to access this page.'));
114 foreach ($this->_caseId
as $casePos => $caseId) {
115 $this->_caseType
[$casePos] = CRM_Case_BAO_Case
::getCaseType($caseId, 'name');
117 $this->assign('caseType', $this->_caseType
);
119 $this->_caseTypeDefinition
= $this->getCaseTypeDefinition();
121 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
122 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
123 $this->assign('multiClient', $isMultiClient);
125 foreach ($this->_caseId
as $casePos => $caseId) {
126 $clients[] = CRM_Case_BAO_Case
::getContactNames($caseId);
128 $this->assign('client_names', $clients);
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}"
139 $url = CRM_Utils_System
::url('civicrm/contact/search/custom', 'force=1');
143 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
144 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseIds}&show=1"
147 if (!$this->_activityId
) {
148 // check if activity count is within the limit
149 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
150 foreach ($this->_caseId
as $casePos => $caseId) {
151 $caseType = $this->_caseType
[$casePos];
152 $activityInst = $xmlProcessor->getMaxInstance($caseType);
154 // If not bounce back and also provide activity edit link if only one existing activity
155 if (isset($activityInst[$this->_activityTypeName
])) {
156 $activityCount = CRM_Case_BAO_Case
::getCaseActivityCount($caseId, $this->_activityTypeId
);
157 $editUrl = self
::checkMaxInstances(
159 $this->_activityTypeId
,
160 $activityInst[$this->_activityTypeName
],
161 $this->_currentUserId
,
162 $this->_currentlyViewedContactId
,
165 $bounceMessage = self
::getMaxInstancesBounceMessage($editUrl, $this->_activityTypeName
, $activityInst[$this->_activityTypeName
], $activityCount);
166 if ($bounceMessage) {
167 CRM_Core_Error
::statusBounce($bounceMessage, $url);
173 // Turn off the prompt which asks the user if they want to create separate
174 // activities when specifying multiple contacts "with" a new activity.
175 // Instead, always create one activity with all contacts together.
176 $this->supportsActivitySeparation
= FALSE;
178 $session = CRM_Core_Session
::singleton();
179 $session->pushUserContext($url);
183 * Set default values for the form.
185 public function setDefaultValues() {
186 $this->_defaults
= parent
::setDefaultValues();
187 $targetContactValues = [];
188 foreach ($this->_caseId
as $key => $val) {
190 $clients = CRM_Case_BAO_Case
::getContactNames($val);
191 if (isset($this->_activityId
) && empty($_POST)) {
192 if (!CRM_Utils_Array
::crmIsEmptyArray($this->_defaults
['target_contact'])) {
193 $targetContactValues = array_combine(array_unique($this->_defaults
['target_contact']),
194 explode(';', trim($this->_defaults
['target_contact_value']))
196 //exclude all clients.
197 foreach ($clients as $clientId => $vals) {
198 if (array_key_exists($clientId, $targetContactValues)) {
199 unset($targetContactValues[$clientId]);
204 $this->assign('targetContactValues', empty($targetContactValues) ?
FALSE : $targetContactValues);
206 if (isset($this->_encounterMedium
)) {
207 $this->_defaults
['medium_id'] = $this->_encounterMedium
;
209 elseif (empty($this->_defaults
['medium_id'])) {
210 // set default encounter medium CRM-4816
211 $medium = CRM_Core_OptionGroup
::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1');
212 if (count($medium) == 1) {
213 $this->_defaults
['medium_id'] = key($medium);
217 return $this->_defaults
;
221 public function buildQuickForm() {
222 $this->_fields
['source_contact_id']['label'] = ts('Reported By');
223 unset($this->_fields
['status_id']['attributes']['required']);
225 if ($this->restrictAssignmentByUserAccount()) {
226 $assigneeParameters['uf_user'] = 1;
229 $activityAssignmentGroups = $this->getActivityAssignmentGroups();
230 if (!empty($activityAssignmentGroups)) {
231 $assigneeParameters['group'] = ['IN' => $activityAssignmentGroups];
234 if (!empty($assigneeParameters)) {
235 $this->_fields
['assignee_contact_id']['attributes']['api']['params']
236 = array_merge($this->_fields
['assignee_contact_id']['attributes']['api']['params'], $assigneeParameters);
238 $this->_fields
['followup_assignee_contact_id']['attributes']['api']['params']
239 = array_merge($this->_fields
['followup_assignee_contact_id']['attributes']['api']['params'], $assigneeParameters);
241 //Disallow creating a contact from the assignee field UI.
242 $this->_fields
['assignee_contact_id']['attributes']['create'] = FALSE;
243 $this->_fields
['followup_assignee_contact_id']['attributes']['create'] = FALSE;
246 if ($this->_caseType
) {
247 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
249 foreach (array_unique($this->_caseType
) as $val) {
250 $activityTypes = $xmlProcessor->get($val, 'ActivityTypes', TRUE);
251 $aTypes = $aTypes +
$activityTypes;
254 // remove Open Case activity type since we're inside an existing case
255 $openCaseID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case');
256 unset($aTypes[$openCaseID]);
258 $this->_fields
['followup_activity_type_id']['attributes'] = ['' => '- select activity type -'] +
$aTypes;
261 parent
::buildQuickForm();
263 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::DETACH | CRM_Core_Action
::RENEW
)) {
267 $this->assign('urlPath', 'civicrm/case/activity');
269 $encounterMediums = CRM_Case_PseudoConstant
::encounterMedium();
271 if ($this->_activityTypeFile
== 'OpenCase' && $this->_action
== CRM_Core_Action
::UPDATE
) {
272 $this->getElement('activity_date_time')->freeze();
274 if ($this->_activityId
) {
275 // 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.
276 // Is that really a big problem?
277 $this->_encounterMedium
= CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity', $this->_activityId
, 'medium_id');
278 if (!array_key_exists($this->_encounterMedium
, $encounterMediums)) {
279 $encounterMediums[$this->_encounterMedium
] = CRM_Core_PseudoConstant
::getLabel('CRM_Activity_BAO_Activity', 'medium_id', $this->_encounterMedium
);
284 $this->add('select', 'medium_id', ts('Medium'), $encounterMediums, TRUE);
286 foreach ($this->_caseId
as $key => $val) {
287 $this->_relatedContacts
[] = $rgc = CRM_Case_BAO_Case
::getRelatedAndGlobalContacts($val);
288 $contName = CRM_Case_BAO_Case
::getContactNames($val);
289 foreach ($contName as $nkey => $nval) {
290 array_push($this->_relatedContacts
[$i][0], $this->_relatedContacts
[$i][0]['managerOf'] = $nval['display_name']);
295 //add case client in send a copy selector.CRM-4438.
296 foreach ($this->_caseId
as $key => $val) {
297 $relatedContacts[] = $relCon = CRM_Case_BAO_Case
::getContactNames($val);
300 if (!empty($relatedContacts)) {
301 foreach ($relatedContacts as $relatedContact) {
302 $this->_relatedContacts
[] = $relatedContact;
306 if (!empty($this->_relatedContacts
)) {
308 foreach ($this->_relatedContacts
as $id => $row) {
309 foreach ($row as $key => $value) {
310 $checkBoxes[$key] = $this->addElement('checkbox', $key, NULL, NULL, ['class' => 'select-row']);
314 $this->addGroup($checkBoxes, 'contact_check');
315 $this->addElement('checkbox', 'toggleSelect', NULL, NULL,
316 ['class' => 'select-rows']
318 $this->assign('searchRows', $this->_relatedContacts
);
320 $this->_relatedContacts
= $rgc +
$relCon;
322 $this->addFormRule(['CRM_Case_Form_Activity', 'formRule'], $this);
328 * @param array $fields
329 * The input form values.
330 * @param array $files
331 * The uploaded files if any.
335 * true if no errors, else array of errors
337 public static function formRule($fields, $files, $self) {
338 // skip form rule if deleting
339 if (CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Delete' || CRM_Utils_Array
::value('_qf_Activity_next_', $fields) == 'Restore') {
343 return parent
::formRule($fields, $files, $self);
347 * Process the form submission.
349 * @param array $params
351 public function postProcess($params = NULL) {
352 $transaction = new CRM_Core_Transaction();
354 if ($this->_action
& CRM_Core_Action
::DELETE
) {
357 //block deleting activities which affects
358 //case attributes.CRM-4543
359 $activityCondition = " AND v.name IN ('Open Case', 'Change Case Type', 'Change Case Status', 'Change Case Start Date')";
360 $caseAttributeActivities = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, $activityCondition);
362 if (!array_key_exists($this->_activityTypeId
, $caseAttributeActivities)) {
363 $params = ['id' => $this->_activityId
];
364 $activityDelete = CRM_Activity_BAO_Activity
::deleteActivity($params, TRUE);
365 if ($activityDelete) {
366 $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 />');
370 $statusMsg = ts("Selected Activity cannot be deleted.");
374 'entity_table' => 'civicrm_activity',
375 'entity_id' => $this->_activityId
,
377 CRM_Core_BAO_EntityTag
::del($tagParams);
379 CRM_Core_Session
::setStatus('', $statusMsg, 'info');
383 if ($this->_action
& CRM_Core_Action
::RENEW
) {
385 $params = ['id' => $this->_activityId
];
386 $activityRestore = CRM_Activity_BAO_Activity
::restoreActivity($params);
387 if ($activityRestore) {
388 $statusMsg = ts('The selected activity has been restored.<br />');
390 CRM_Core_Session
::setStatus('', $statusMsg, 'info');
394 // store the submitted values in an array
395 // 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.
396 if (empty($params['is_unittest'])) {
397 $params = $this->controller
->exportValues($this->_name
);
400 //set parent id if its edit mode
401 if ($parentId = CRM_Utils_Array
::value('parent_id', $this->_defaults
)) {
402 $params['parent_id'] = $parentId;
405 $params['activity_type_id'] = $this->_activityTypeId
;
407 // format with contact (target contact) values
408 if (isset($params['target_contact_id'])) {
409 $params['target_contact_id'] = explode(',', $params['target_contact_id']);
412 $params['target_contact_id'] = [];
415 // format activity custom data
416 if (!empty($params['hidden_custom'])) {
417 if ($this->_activityId
) {
418 // retrieve and include the custom data of old Activity
419 $oldActivity = civicrm_api3('Activity', 'getsingle', ['id' => $this->_activityId
]);
420 $params = array_merge($oldActivity, $params);
422 // unset custom fields-id from params since we want custom
423 // fields to be saved for new activity.
424 foreach ($params as $key => $value) {
426 if (preg_match('/^(custom_\d+_)(\d+)$/', $key, $match)) {
427 $params[$match[1] . '-1'] = $params[$key];
429 // for autocomplete transfer hidden value instead of label
430 if ($params[$key] && isset($params[$key . '_id'])) {
431 $params[$match[1] . '-1_id'] = $params[$key . '_id'];
432 unset($params[$key . '_id']);
434 unset($params[$key]);
439 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
445 // assigning formatted value
446 if (!empty($params['assignee_contact_id'])) {
447 $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']);
450 $params['assignee_contact_id'] = [];
453 if (isset($this->_activityId
)) {
454 // activity which hasn't been modified by a user yet
455 if ($this->_defaults
['is_auto'] == 1) {
456 $params['is_auto'] = 0;
459 // always create a revision of an case activity. CRM-4533
460 $newActParams = $params;
462 // add target contact values in update mode
463 if (empty($params['target_contact_id']) && !empty($this->_defaults
['target_contact'])) {
464 $newActParams['target_contact_id'] = $this->_defaults
['target_contact'];
468 if (!isset($newActParams)) {
469 // add more attachments if needed for old activity
470 CRM_Core_BAO_File
::formatAttachment($params,
475 // call begin post process, before the activity is created/updated.
476 $this->beginPostProcess($params);
477 foreach ($this->_caseId
as $key => $val) {
478 $params['case_id'] = $val;
479 // activity create/update
480 $activity = CRM_Activity_BAO_Activity
::create($params);
481 $vvalue[] = ['case_id' => $val, 'actId' => $activity->id
];
482 // call end post process, after the activity has been created/updated.
483 $this->endPostProcess($params, $activity);
487 // create a new version of activity if activity was found to
488 // have been modified/created by user
490 // since the params we need to set are very few, and we don't want rest of the
491 // work done by bao create method , lets use dao object to make the changes
492 $params = ['id' => $this->_activityId
];
493 $params['is_current_revision'] = 0;
494 $activity = new CRM_Activity_DAO_Activity();
495 $activity->copyValues($params);
498 // set proper original_id
499 if (!empty($this->_defaults
['original_id'])) {
500 $newActParams['original_id'] = $this->_defaults
['original_id'];
503 $newActParams['original_id'] = $activity->id
;
506 //is_current_revision will be set to 1 by default.
507 // add attachments if any
508 CRM_Core_BAO_File
::formatAttachment($newActParams,
513 // call begin post process, before the activity is created/updated.
514 $this->beginPostProcess($newActParams);
515 foreach ($this->_caseId
as $key => $val) {
516 $newActParams['case_id'] = $val;
517 $activity = CRM_Activity_BAO_Activity
::create($newActParams);
518 $vvalue[] = ['case_id' => $val, 'actId' => $activity->id
];
519 // call end post process, after the activity has been created/updated.
520 $this->endPostProcess($newActParams, $activity);
522 // copy files attached to old activity if any, to new one,
523 // as long as users have not selected the 'delete attachment' option.
524 if (empty($newActParams['is_delete_attachment']) && ($this->_activityId
!= $activity->id
)) {
525 CRM_Core_BAO_File
::copyEntityFile('civicrm_activity', $this->_activityId
,
526 'civicrm_activity', $activity->id
530 // copy back params to original var
531 $params = $newActParams;
534 foreach ($vvalue as $vkey => $vval) {
535 if ($vval['actId']) {
536 // add tags if exists
538 if (!empty($params['tag'])) {
539 if (!is_array($params['tag'])) {
540 $params['tag'] = explode(',', $params['tag']);
543 $tagParams = array_fill_keys($params['tag'], 1);
547 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_activity', $vval['actId']);
550 if (isset($params['taglist']) && !empty($params['taglist'])) {
551 CRM_Core_Form_Tag
::postProcess($params['taglist'], $vval['actId'], 'civicrm_activity', $this);
555 // update existing case record if needed
556 $caseParams = $params;
557 $caseParams['id'] = $vval['case_id'];
558 if (!empty($caseParams['case_status_id'])) {
559 $caseParams['status_id'] = $caseParams['case_status_id'];
562 // unset params intended for activities only
563 unset($caseParams['subject'], $caseParams['details'],
564 $caseParams['status_id'], $caseParams['custom']
566 CRM_Case_BAO_Case
::create($caseParams);
567 // create case activity record
569 'activity_id' => $vval['actId'],
570 'case_id' => $vval['case_id'],
572 CRM_Case_BAO_Case
::processCaseActivity($caseParams);
575 // send copy to selected contacts.
577 $mailToContacts = [];
580 //check for notification settings for assignee contacts
581 $selectedContacts = ['contact_check'];
582 if (Civi
::settings()->get('activity_assignee_notification')) {
583 $selectedContacts[] = 'assignee_contact_id';
586 $dndActivityTypes = Civi
::settings()->get('do_not_notify_assignees_for') ??
[];
587 foreach ($vvalue as $vkey => $vval) {
588 foreach ($selectedContacts as $dnt => $val) {
589 if (array_key_exists($val, $params) && !CRM_Utils_Array
::crmIsEmptyArray($params[$val])) {
590 if ($val == 'contact_check') {
591 $mailStatus = ts("A copy of the activity has also been sent to selected contact(s).");
594 if (!in_array($this->_activityTypeId
, $dndActivityTypes)) {
595 $this->_relatedContacts
= CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames(
596 [$vval['actId']], TRUE, FALSE
598 $mailStatus .= ' ' . ts("A copy of the activity has also been sent to assignee contact(s).");
601 //build an associative array with unique email addresses.
602 foreach ($params[$val] as $key => $value) {
603 if ($val == 'contact_check') {
610 if (isset($id) && array_key_exists($id, $this->_relatedContacts
) && isset($this->_relatedContacts
[$id]['email'])) {
611 //if email already exists in array then append with ', ' another role only otherwise add it to array.
612 if ($contactDetails = CRM_Utils_Array
::value($this->_relatedContacts
[$id]['email'], $mailToContacts)) {
613 $caseRole = $this->_relatedContacts
[$id]['role'] ??
NULL;
614 $mailToContacts[$this->_relatedContacts
[$id]['email']]['role'] = $contactDetails['role'] . ', ' . $caseRole;
617 $mailToContacts[$this->_relatedContacts
[$id]['email']] = $this->_relatedContacts
[$id];
624 $extraParams = ['case_id' => $vval['case_id'], 'client_id' => $this->_currentlyViewedContactId
];
625 $result = CRM_Activity_BAO_Activity
::sendToAssignee($activity, $mailToContacts, $extraParams);
626 if (empty($result)) {
630 // create follow up activity if needed
631 $followupStatus = '';
632 if (!empty($params['followup_activity_type_id'])) {
633 $followupActivity = CRM_Activity_BAO_Activity
::createFollowupActivity($vval['actId'], $params);
635 if ($followupActivity) {
637 'activity_id' => $followupActivity->id
,
638 'case_id' => $vval['case_id'],
640 CRM_Case_BAO_Case
::processCaseActivity($caseParams);
641 $followupStatus = ts("A followup activity has been scheduled.") . '<br /><br />';
644 if (Civi
::settings()->get('activity_assignee_notification') &&
645 !in_array($followupActivity->activity_type_id
,
646 Civi
::settings()->get('do_not_notify_assignees_for'))
648 $followupActivityIDs = [$followupActivity->id
];
649 $followupAssigneeContacts = CRM_Activity_BAO_ActivityAssignment
::getAssigneeNames($followupActivityIDs, TRUE, FALSE);
651 if (!empty($followupAssigneeContacts)) {
652 $mailToFollowupContacts = [];
653 foreach ($followupAssigneeContacts as $facValues) {
654 $mailToFollowupContacts[$facValues['email']] = $facValues;
657 $facParams['case_id'] = $vval['case_id'];
658 $sentFollowup = CRM_Activity_BAO_Activity
::sendToAssignee($followupActivity, $mailToFollowupContacts, $facParams);
660 $mailStatus .= '<br />' . ts("A copy of the follow-up activity has also been sent to follow-up assignee contacts(s).");
666 $title = ts("%1 Saved", [1 => $this->_activityTypeName
]);
667 CRM_Core_Session
::setStatus($followupStatus . $mailStatus, $title, 'success');
672 * Returns the groups that contacts must belong to in order to be assigned
673 * an activity for this case. It returns an empty array if no groups are found for
674 * the case type linked to the caseId.
678 private function getActivityAssignmentGroups() {
679 if (!$this->_caseTypeDefinition
) {
683 $assignmentGroups = [];
684 foreach ($this->_caseTypeDefinition
as $caseId => $definition) {
685 if (!empty($definition['activityAsgmtGrps'])) {
686 $assignmentGroups = array_merge($assignmentGroups, $definition['activityAsgmtGrps']);
690 return $assignmentGroups;
694 * Returns whether contacts must have a user account in order to be
695 * assigned an activity for this case.
699 private function restrictAssignmentByUserAccount() {
700 if (!$this->_caseTypeDefinition
) {
704 foreach ($this->_caseTypeDefinition
as $caseId => $definition) {
705 if (!empty($definition['restrictActivityAsgmtToCmsUser'])) {
714 * Returns the case type definition column value for the case type linked to the caseId.
718 private function getCaseTypeDefinition() {
719 if (!$this->_caseId
) {
723 $definitions = civicrm_api3('CaseType', 'get', [
724 'return' => ['name', 'definition'],
725 'name' => ['IN' => array_unique($this->_caseType
)],
728 return array_column($definitions['values'], 'definition', 'name');
732 * Get the edit link for a case activity
734 * This isn't here for reusability - it was a pull out
735 * from preProcess to make it easier to test.
736 * There is CRM_Case_Selector_Search::addCaseActivityLinks but it would
737 * need some rejigging, and there's also a FIXME note there already.
740 * @param int $activityTypeId
741 * @param int $currentUserId
742 * @param int $currentlyViewedContactId
746 public static function getCaseActivityEditLink($caseId, $activityTypeId, $currentUserId, $currentlyViewedContactId) {
747 $atArray = ['activity_type_id' => $activityTypeId];
748 $activities = CRM_Case_BAO_Case
::getCaseActivity($caseId,
752 $firstActivity = CRM_Utils_Array
::first($activities['data']);
753 $activityId = empty($firstActivity['DT_RowId']) ?
0 : $firstActivity['DT_RowId'];
754 return CRM_Utils_System
::url('civicrm/case/activity',
755 "reset=1&cid={$currentlyViewedContactId}&caseid={$caseId}&action=update&id={$activityId}"
760 * Check the current activity count against max instances for a given case id and activity type.
762 * This isn't here for reusability - it was a pull out
763 * from preProcess to make it easier to test.
766 * @param int $activityTypeId
767 * @param int $maxInstances
768 * @param int $currentUserId
769 * @param int $currentlyViewedContactId
770 * @param int $activityCount
773 * 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.
775 public static function checkMaxInstances($caseId, $activityTypeId, $maxInstances, $currentUserId, $currentlyViewedContactId, $activityCount) {
777 if ($activityCount >= $maxInstances) {
778 if ($maxInstances == 1) {
779 $editUrl = self
::getCaseActivityEditLink($caseId, $activityTypeId, $currentUserId, $currentlyViewedContactId);
786 * Compute the message text for the bounce message when max_instances is reached, depending on whether it's one or more than one.
788 * @param string $editUrl
789 * @param string $activityTypeName
790 * This is actually label!! But we do want label though in this function.
791 * @param int $maxInstances
792 * @param int $activityCount
793 * Count of existing activities of the given type on the case
797 public static function getMaxInstancesBounceMessage($editUrl, $activityTypeName, $maxInstances, $activityCount) {
798 $bounceMessage = NULL;
799 if ($activityCount >= $maxInstances) {
800 if ($maxInstances == 1) {
801 $bounceMessage = ts("You can not add another '%1' activity to this case. %2",
803 1 => $activityTypeName,
804 2 => ts("Do you want to <a %1>edit the existing activity</a>?", [1 => "href='$editUrl'"]),
809 // More than one instance, so don't provide a link. What would it be a link to anyway?
810 $bounceMessage = ts("You can not add another '%1' activity to this case.",
812 1 => $activityTypeName,
817 return $bounceMessage;