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
::fatal(ts('Case Id not found.'));
32 if (count($form->_caseId
) != 1) {
33 CRM_Core_Resources
::fatal(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 = CRM_Utils_Array
::value('link_to_case_id', $values);
102 assert('is_numeric($linkCaseId)');
103 if ($linkCaseId == CRM_Utils_Array
::first($form->_caseId
)) {
104 $errors['link_to_case'] = ts('Please select some other case to link.');
107 // do check for existing related cases.
108 $relatedCases = $form->get('relatedCases');
109 if (is_array($relatedCases) && array_key_exists($linkCaseId, $relatedCases)) {
110 $errors['link_to_case'] = ts('Selected case is already linked.');
113 return empty($errors) ?
TRUE : $errors;
117 * Process the form submission.
120 * @param CRM_Core_Form $form
121 * @param array $params
123 public static function beginPostProcess(&$form, &$params) {
127 * Process the form submission.
130 * @param CRM_Core_Form $form
131 * @param array $params
132 * @param CRM_Activity_BAO_Activity $activity
134 public static function endPostProcess(&$form, &$params, &$activity) {
135 $activityId = $activity->id
;
136 $linkCaseID = CRM_Utils_Array
::value('link_to_case_id', $params);
138 //create a link between two cases.
139 if ($activityId && $linkCaseID) {
141 'case_id' => $linkCaseID,
142 'activity_id' => $activityId,
144 CRM_Case_BAO_Case
::processCaseActivity($caseParams);