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 generates form components for LinkCase Activity.
21 class CRM_Case_Form_Activity_LinkCases
{
24 * @param CRM_Core_Form $form
28 public static function preProcess(&$form) {
29 if (empty($form->_caseId
)) {
30 CRM_Core_Error
::statusBounce(ts('Case Id not found.'));
32 if (count($form->_caseId
) != 1) {
33 CRM_Core_Error
::statusBounce(ts('Expected one case-type'));
36 $caseId = CRM_Utils_Array
::first($form->_caseId
);
38 $form->assign('clientID', $form->_currentlyViewedContactId
);
39 $form->assign('sortName', CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $form->_currentlyViewedContactId
, 'sort_name'));
40 $form->assign('caseTypeLabel', CRM_Case_BAO_Case
::getCaseType($caseId));
42 // get the related cases for given case.
43 $relatedCases = $form->get('relatedCases');
44 if (!isset($relatedCases)) {
45 $relatedCases = CRM_Case_BAO_Case
::getRelatedCases($caseId);
46 $form->set('relatedCases', empty($relatedCases) ?
FALSE : $relatedCases);
51 * Set default values for the form.
53 * @param CRM_Core_Form $form
57 public static function setDefaultValues(&$form) {
59 if (!empty($_GET['link_to_case_id']) && CRM_Utils_Rule
::positiveInteger($_GET['link_to_case_id'])) {
60 $defaults['link_to_case_id'] = $_GET['link_to_case_id'];
66 * @param CRM_Core_Form $form
68 public static function buildQuickForm(&$form) {
69 $excludeCaseIds = (array) $form->_caseId
;
70 $relatedCases = $form->get('relatedCases');
71 if (is_array($relatedCases) && !empty($relatedCases)) {
72 $excludeCaseIds = array_merge($excludeCaseIds, array_keys($relatedCases));
74 $form->addEntityRef('link_to_case_id', ts('Link To Case'), [
77 'extra' => ['case_id.case_type_id.title', 'contact_id.sort_name'],
79 'case_id' => ['NOT IN' => $excludeCaseIds],
80 'case_id.is_deleted' => 0,
87 * Global validation rules for the form.
89 * @param array $values
90 * Posted values of the form.
93 * @param CRM_Core_Form $form
96 * list of errors to be posted back to the form
98 public static function formRule($values, $files, $form) {
101 $linkCaseId = $values['link_to_case_id'] ??
NULL;
103 if (!CRM_Utils_Rule
::positiveInteger($linkCaseId) ||
$linkCaseId == 0) {
104 // We can't just return $errors because when the page reloads the
105 // entityref widget throws an error before the page can display the error.
106 // It seems ok with other invalid values, just not 0, but both are equally invalid.
107 CRM_Core_Error
::statusBounce(ts('The linked case ID is invalid.'));
110 if ($linkCaseId == CRM_Utils_Array
::first($form->_caseId
)) {
111 $errors['link_to_case_id'] = ts('Please select some other case to link.');
114 // do check for existing related cases.
115 $relatedCases = $form->get('relatedCases');
116 if (is_array($relatedCases) && array_key_exists($linkCaseId, $relatedCases)) {
117 $errors['link_to_case_id'] = ts('Selected case is already linked.');
120 return empty($errors) ?
TRUE : $errors;
124 * Process the form submission.
127 * @param CRM_Core_Form $form
128 * @param array $params
130 public static function beginPostProcess(&$form, &$params) {
134 * Process the form submission.
137 * @param CRM_Core_Form $form
138 * @param array $params
139 * @param CRM_Activity_BAO_Activity $activity
141 public static function endPostProcess($form, $params, $activity) {
142 $activityId = $activity->id
;
143 $linkCaseID = $params['link_to_case_id'] ??
NULL;
145 //create a link between two cases.
146 if ($activityId && $linkCaseID) {
148 'case_id' => $linkCaseID,
149 'activity_id' => $activityId,
151 CRM_Case_BAO_Case
::processCaseActivity($caseParams);