Merge pull request #16004 from civicrm/5.20
[civicrm-core.git] / CRM / Case / Form / Activity / LinkCases.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for LinkCase Activity.
20 */
21 class CRM_Case_Form_Activity_LinkCases {
22
23 /**
24 * @param CRM_Core_Form $form
25 *
26 * @throws Exception
27 */
28 public static function preProcess(&$form) {
29 if (empty($form->_caseId)) {
30 CRM_Core_Error::fatal(ts('Case Id not found.'));
31 }
32 if (count($form->_caseId) != 1) {
33 CRM_Core_Resources::fatal(ts('Expected one case-type'));
34 }
35
36 $caseId = CRM_Utils_Array::first($form->_caseId);
37
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));
41
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);
47 }
48 }
49
50 /**
51 * Set default values for the form.
52 *
53 * @param CRM_Core_Form $form
54 *
55 * @return array
56 */
57 public static function setDefaultValues(&$form) {
58 $defaults = [];
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'];
61 }
62 return $defaults;
63 }
64
65 /**
66 * @param CRM_Core_Form $form
67 */
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));
73 }
74 $form->addEntityRef('link_to_case_id', ts('Link To Case'), [
75 'entity' => 'Case',
76 'api' => [
77 'extra' => ['case_id.case_type_id.title', 'contact_id.sort_name'],
78 'params' => [
79 'case_id' => ['NOT IN' => $excludeCaseIds],
80 'case_id.is_deleted' => 0,
81 ],
82 ],
83 ], TRUE);
84 }
85
86 /**
87 * Global validation rules for the form.
88 *
89 * @param array $values
90 * Posted values of the form.
91 *
92 * @param $files
93 * @param CRM_Core_Form $form
94 *
95 * @return array
96 * list of errors to be posted back to the form
97 */
98 public static function formRule($values, $files, $form) {
99 $errors = [];
100
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.');
105 }
106
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.');
111 }
112
113 return empty($errors) ? TRUE : $errors;
114 }
115
116 /**
117 * Process the form submission.
118 *
119 *
120 * @param CRM_Core_Form $form
121 * @param array $params
122 */
123 public static function beginPostProcess(&$form, &$params) {
124 }
125
126 /**
127 * Process the form submission.
128 *
129 *
130 * @param CRM_Core_Form $form
131 * @param array $params
132 * @param CRM_Activity_BAO_Activity $activity
133 */
134 public static function endPostProcess(&$form, &$params, &$activity) {
135 $activityId = $activity->id;
136 $linkCaseID = CRM_Utils_Array::value('link_to_case_id', $params);
137
138 //create a link between two cases.
139 if ($activityId && $linkCaseID) {
140 $caseParams = [
141 'case_id' => $linkCaseID,
142 'activity_id' => $activityId,
143 ];
144 CRM_Case_BAO_Case::processCaseActivity($caseParams);
145 }
146 }
147
148 }