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 $caseTypes = CRM_Case_PseudoConstant
::caseType();
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"
154 $session = CRM_Core_Session
::singleton();
155 $session->pushUserContext($url);
156 CRM_Core_Error
::statusBounce(ts("You do not have any active Case Types"));
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);
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(
170 $this->_activityTypeId
,
171 $activityInst[$this->_activityTypeName
],
172 $this->_currentUserId
,
173 $this->_currentlyViewedContactId
,
176 $bounceMessage = self
::getMaxInstancesBounceMessage($editUrl, $this->_activityTypeName
, $activityInst[$this->_activityTypeName
], $activityCount);
177 if ($bounceMessage) {
178 CRM_Core_Error
::statusBounce($bounceMessage, $url);
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;
189 $session = CRM_Core_Session
::singleton();
190 $session->pushUserContext($url);
194 * Set default values for the form.
196 public function setDefaultValues() {
197 $this->_defaults
= parent
::setDefaultValues();
198 $targetContactValues = [];
199 foreach ($this->_caseId
as $key => $val) {
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']))
207 //exclude all clients.
208 foreach ($clients as $clientId => $vals) {
209 if (array_key_exists($clientId, $targetContactValues)) {
210 unset($targetContactValues[$clientId]);
215 $this->assign('targetContactValues', empty($targetContactValues) ?
FALSE : $targetContactValues);
217 if (isset($this->_encounterMedium
)) {
218 $this->_defaults
['medium_id'] = $this->_encounterMedium
;
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);
228 return $this->_defaults
;
232 public function buildQuickForm() {
233 $this->_fields
['source_contact_id']['label'] = ts('Reported By');
234 unset($this->_fields
['status_id']['attributes']['required']);
236 if ($this->restrictAssignmentByUserAccount()) {
237 $assigneeParameters['uf_user'] = 1;
240 $activityAssignmentGroups = $this->getActivityAssignmentGroups();
241 if (!empty($activityAssignmentGroups)) {
242 $assigneeParameters['group'] = ['IN' => $activityAssignmentGroups];
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);
249 $this->_fields
['followup_assignee_contact_id']['attributes']['api']['params']
250 = array_merge($this->_fields
['followup_assignee_contact_id']['attributes']['api']['params'], $assigneeParameters);
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;
257 if ($this->_caseType
) {
258 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
260 foreach (array_unique($this->_caseType
) as $val) {
261 $activityTypes = $xmlProcessor->get($val, 'ActivityTypes', TRUE);
262 $aTypes = $aTypes +
$activityTypes;
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]);
269 $this->_fields
['followup_activity_type_id']['attributes'] = ['' => '- select activity type -'] +
$aTypes;
272 parent
::buildQuickForm();
274 if ($this->_action
& (CRM_Core_Action
::DELETE | CRM_Core_Action
::DETACH | CRM_Core_Action
::RENEW
)) {
278 $this->assign('urlPath', 'civicrm/case/activity');
280 $encounterMediums = CRM_Case_PseudoConstant
::encounterMedium();
282 if ($this->_activityTypeFile
== 'OpenCase' && $this->_action
== CRM_Core_Action
::UPDATE
) {
283 $this->getElement('activity_date_time')->freeze();
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
);
295 $this->add('select', 'medium_id', ts('Medium'), $encounterMediums, TRUE);
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']);
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);
311 if (!empty($relatedContacts)) {
312 foreach ($relatedContacts as $relatedContact) {
313 $this->_relatedContacts
[] = $relatedContact;
317 if (!empty($this->_relatedContacts
)) {
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']);
325 $this->addGroup($checkBoxes, 'contact_check');
326 $this->addElement('checkbox', 'toggleSelect', NULL, NULL,
327 ['class' => 'select-rows']
329 $this->assign('searchRows', $this->_relatedContacts
);
331 $this->_relatedContacts
= $rgc +
$relCon;
333 $this->addFormRule(['CRM_Case_Form_Activity', 'formRule'], $this);
339 * @param array $fields
340 * The input form values.
341 * @param array $files
342 * The uploaded files if any.
346 * true if no errors, else array of errors
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') {
354 return parent
::formRule($fields, $files, $self);
358 * Process the form submission.
360 * @param array $params
362 public function postProcess($params = NULL) {
363 $transaction = new CRM_Core_Transaction();
365 if ($this->_action
& CRM_Core_Action
::DELETE
) {
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);
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 />');
381 $statusMsg = ts("Selected Activity cannot be deleted.");
385 'entity_table' => 'civicrm_activity',
386 'entity_id' => $this->_activityId
,
388 CRM_Core_BAO_EntityTag
::del($tagParams);
390 CRM_Core_Session
::setStatus('', $statusMsg, 'info');
394 if ($this->_action
& CRM_Core_Action
::RENEW
) {
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 />');
401 CRM_Core_Session
::setStatus('', $statusMsg, 'info');
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
);
411 //set parent id if its edit mode
412 if ($parentId = CRM_Utils_Array
::value('parent_id', $this->_defaults
)) {
413 $params['parent_id'] = $parentId;
416 $params['activity_type_id'] = $this->_activityTypeId
;
418 // format with contact (target contact) values
419 if (isset($params['target_contact_id'])) {
420 $params['target_contact_id'] = explode(',', $params['target_contact_id']);
423 $params['target_contact_id'] = [];
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);
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) {
437 if (preg_match('/^(custom_\d+_)(\d+)$/', $key, $match)) {
438 $params[$match[1] . '-1'] = $params[$key];
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']);
445 unset($params[$key]);
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,
457 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
463 // assigning formatted value
464 if (!empty($params['assignee_contact_id'])) {
465 $params['assignee_contact_id'] = explode(',', $params['assignee_contact_id']);
468 $params['assignee_contact_id'] = [];
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;
477 // always create a revision of an case activity. CRM-4533
478 $newActParams = $params;
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'];
486 if (!isset($newActParams)) {
487 // add more attachments if needed for old activity
488 CRM_Core_BAO_File
::formatAttachment($params,
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);
505 // create a new version of activity if activity was found to
506 // have been modified/created by user
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);
516 // set proper original_id
517 if (!empty($this->_defaults
['original_id'])) {
518 $newActParams['original_id'] = $this->_defaults
['original_id'];
521 $newActParams['original_id'] = $activity->id
;
524 //is_current_revision will be set to 1 by default.
525 // add attachments if any
526 CRM_Core_BAO_File
::formatAttachment($newActParams,
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);
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
548 // copy back params to original var
549 $params = $newActParams;
552 foreach ($vvalue as $vkey => $vval) {
553 if ($vval['actId']) {
554 // add tags if exists
556 if (!empty($params['tag'])) {
557 if (!is_array($params['tag'])) {
558 $params['tag'] = explode(',', $params['tag']);
561 $tagParams = array_fill_keys($params['tag'], 1);
565 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_activity', $vval['actId']);
568 if (isset($params['taglist']) && !empty($params['taglist'])) {
569 CRM_Core_Form_Tag
::postProcess($params['taglist'], $vval['actId'], 'civicrm_activity', $this);
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'];
580 // unset params intended for activities only
581 unset($caseParams['subject'], $caseParams['details'],
582 $caseParams['status_id'], $caseParams['custom']
584 CRM_Case_BAO_Case
::create($caseParams);
585 // create case activity record
587 'activity_id' => $vval['actId'],
588 'case_id' => $vval['case_id'],
590 CRM_Case_BAO_Case
::processCaseActivity($caseParams);
593 // send copy to selected contacts.
595 $mailToContacts = [];
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';
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).");
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).");
614 //build an associative array with unique email addresses.
615 foreach ($params[$val] as $key => $value) {
616 if ($val == 'contact_check') {
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;
630 $mailToContacts[$this->_relatedContacts
[$id]['email']] = $this->_relatedContacts
[$id];
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)) {
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);
648 if ($followupActivity) {
650 'activity_id' => $followupActivity->id
,
651 'case_id' => $vval['case_id'],
653 CRM_Case_BAO_Case
::processCaseActivity($caseParams);
654 $followupStatus = ts("A followup activity has been scheduled.") . '<br /><br />';
657 $title = ts("%1 Saved", [1 => $this->_activityTypeName
]);
658 CRM_Core_Session
::setStatus($followupStatus . $mailStatus, $title, 'success');
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.
669 private function getActivityAssignmentGroups() {
670 if (!$this->_caseTypeDefinition
) {
674 $assignmentGroups = [];
675 foreach ($this->_caseTypeDefinition
as $caseId => $definition) {
676 if (!empty($definition['activityAsgmtGrps'])) {
677 $assignmentGroups = array_merge($assignmentGroups, $definition['activityAsgmtGrps']);
681 return $assignmentGroups;
685 * Returns whether contacts must have a user account in order to be
686 * assigned an activity for this case.
690 private function restrictAssignmentByUserAccount() {
691 if (!$this->_caseTypeDefinition
) {
695 foreach ($this->_caseTypeDefinition
as $caseId => $definition) {
696 if (!empty($definition['restrictActivityAsgmtToCmsUser'])) {
705 * Returns the case type definition column value for the case type linked to the caseId.
709 private function getCaseTypeDefinition() {
710 if (!$this->_caseId
) {
714 $definitions = civicrm_api3('CaseType', 'get', [
715 'return' => ['name', 'definition'],
716 'name' => ['IN' => array_unique($this->_caseType
)],
719 return array_column($definitions['values'], 'definition', 'name');
723 * Get the edit link for a case activity
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.
731 * @param int $activityTypeId
732 * @param int $currentUserId
733 * @param int $currentlyViewedContactId
737 public static function getCaseActivityEditLink($caseId, $activityTypeId, $currentUserId, $currentlyViewedContactId) {
738 $atArray = ['activity_type_id' => $activityTypeId];
739 $activities = CRM_Case_BAO_Case
::getCaseActivity($caseId,
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}"
751 * Check the current activity count against max instances for a given case id and activity type.
753 * This isn't here for reusability - it was a pull out
754 * from preProcess to make it easier to test.
757 * @param int $activityTypeId
758 * @param int $maxInstances
759 * @param int $currentUserId
760 * @param int $currentlyViewedContactId
761 * @param int $activityCount
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.
766 public static function checkMaxInstances($caseId, $activityTypeId, $maxInstances, $currentUserId, $currentlyViewedContactId, $activityCount) {
768 if ($activityCount >= $maxInstances) {
769 if ($maxInstances == 1) {
770 $editUrl = self
::getCaseActivityEditLink($caseId, $activityTypeId, $currentUserId, $currentlyViewedContactId);
777 * Compute the message text for the bounce message when max_instances is reached, depending on whether it's one or more than one.
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
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",
794 1 => $activityTypeName,
795 2 => ts("Do you want to <a %1>edit the existing activity</a>?", [1 => "href='$editUrl'"]),
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.",
803 1 => $activityTypeName,
808 return $bounceMessage;