Merge pull request #15326 from totten/master-headfoot-2
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseStatus.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 OpenCase Activity.
20 */
21 class CRM_Case_Form_Activity_ChangeCaseStatus {
22
23 /**
24 * @param CRM_Core_Form $form
25 *
26 * @throws Exception
27 */
28 public static function preProcess(&$form) {
29 if (!isset($form->_caseId)) {
30 CRM_Core_Error::fatal(ts('Case Id not found.'));
31 }
32
33 $form->addElement('checkbox', 'updateLinkedCases', NULL, NULL, ['class' => 'select-row']);
34
35 $caseID = CRM_Utils_Array::first($form->_caseId);
36 $cases = CRM_Case_BAO_Case::getRelatedCases($caseID);
37 $form->assign('linkedCases', $cases);
38 }
39
40 /**
41 * Set default values for the form.
42 *
43 * For edit/view mode the default values are retrieved from the database.
44 *
45 *
46 * @param CRM_Core_Form $form
47 *
48 * @return array
49 */
50 public static function setDefaultValues(&$form) {
51 $defaults = [];
52 // Retrieve current case status
53 $defaults['case_status_id'] = $form->_defaultCaseStatus;
54
55 return $defaults;
56 }
57
58 /**
59 * @param CRM_Core_Form $form
60 */
61 public static function buildQuickForm(&$form) {
62 $form->removeElement('status_id');
63 $form->removeElement('priority_id');
64
65 $caseTypes = [];
66
67 $form->_caseStatus = CRM_Case_PseudoConstant::caseStatus();
68 $statusNames = CRM_Case_PseudoConstant::caseStatus('name');
69
70 // Limit case statuses to allowed types for these case(s)
71 $allCases = civicrm_api3('Case', 'get', ['return' => 'case_type_id', 'id' => ['IN' => (array) $form->_caseId]]);
72 foreach ($allCases['values'] as $case) {
73 $caseTypes[$case['case_type_id']] = $case['case_type_id'];
74 }
75 $caseTypes = civicrm_api3('CaseType', 'get', ['id' => ['IN' => $caseTypes]]);
76 foreach ($caseTypes['values'] as $ct) {
77 if (!empty($ct['definition']['statuses'])) {
78 foreach ($form->_caseStatus as $id => $label) {
79 if (!in_array($statusNames[$id], $ct['definition']['statuses'])) {
80 unset($form->_caseStatus[$id]);
81 }
82 }
83 }
84 }
85
86 foreach ($form->_caseId as $key => $val) {
87 $form->_oldCaseStatus[] = $form->_defaultCaseStatus[] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $val, 'status_id');
88 }
89
90 foreach ($form->_defaultCaseStatus as $keydefault => $valdefault) {
91 if (!array_key_exists($valdefault, $form->_caseStatus)) {
92 $form->_caseStatus[$valdefault] = CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'status_id', $valdefault);
93 }
94 }
95 $element = $form->add('select', 'case_status_id', ts('Case Status'),
96 $form->_caseStatus, TRUE
97 );
98 // check if the case status id passed in url is a valid one, set as default and freeze
99 if (CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form)) {
100 $caseStatusId = CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form);
101 $caseStatus = CRM_Case_PseudoConstant::caseStatus();
102 $form->_defaultCaseStatus = array_key_exists($caseStatusId, $caseStatus) ? $caseStatusId : NULL;
103 $element->freeze();
104 }
105 }
106
107 /**
108 * Global validation rules for the form.
109 *
110 * @param array $values
111 * Posted values of the form.
112 *
113 * @param $files
114 * @param CRM_Core_Form $form
115 *
116 * @return array
117 * list of errors to be posted back to the form
118 */
119 public static function formRule($values, $files, $form) {
120 return TRUE;
121 }
122
123 /**
124 * Process the form submission.
125 *
126 * @param CRM_Core_Form $form
127 * @param array $params
128 */
129 public static function beginPostProcess(&$form, &$params) {
130 $params['id'] = CRM_Utils_Array::value('case_id', $params);
131
132 if (CRM_Utils_Array::value('updateLinkedCases', $params) === '1') {
133 $caseID = CRM_Utils_Array::first($form->_caseId);
134 $cases = CRM_Case_BAO_Case::getRelatedCases($caseID);
135
136 foreach ($cases as $currentCase) {
137 if ($currentCase['status_id'] != $params['case_status_id']) {
138 $form->_caseId[] = $currentCase['case_id'];
139 }
140 }
141 }
142 }
143
144 /**
145 * Process the form submission.
146 *
147 * @param CRM_Core_Form $form
148 * @param array $params
149 * @param CRM_Activity_BAO_Activity $activity
150 */
151 public static function endPostProcess(&$form, &$params, $activity) {
152 $groupingValues = CRM_Core_OptionGroup::values('case_status', FALSE, TRUE, FALSE, NULL, 'value');
153
154 // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it.
155 if (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) {
156 $params['end_date'] = CRM_Utils_Date::isoToMysql($params['activity_date_time']);
157
158 // End case-specific relationships (roles)
159 foreach ($params['target_contact_id'] as $cid) {
160 $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']);
161 foreach ($rels as $relId => $relData) {
162 $relationshipParams = [
163 'id' => $relId,
164 'end_date' => $params['end_date'],
165 ];
166 // @todo we can't switch directly to api because there is too much business logic and it breaks closing cases with organisations as client relationships
167 //civicrm_api3('Relationship', 'create', $relationshipParams);
168 CRM_Contact_BAO_Relationship::add($relationshipParams);
169 }
170 }
171 }
172 elseif (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Opened') {
173 $params['end_date'] = "null";
174
175 // Reopen case-specific relationships (roles)
176 foreach ($params['target_contact_id'] as $cid) {
177 $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id'], NULL, FALSE);
178 foreach ($rels as $relId => $relData) {
179 $relationshipParams = [
180 'id' => $relId,
181 'end_date' => 'null',
182 ];
183 // @todo we can't switch directly to api because there is too much business logic and it breaks closing cases with organisations as client relationships
184 //civicrm_api3('Relationship', 'create', $relationshipParams);
185 CRM_Contact_BAO_Relationship::add($relationshipParams);
186 }
187 }
188 }
189 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
190 $activity->status_id = $params['status_id'];
191 $params['priority_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal');
192 $activity->priority_id = $params['priority_id'];
193
194 foreach ($form->_oldCaseStatus as $statuskey => $statusval) {
195 if ($activity->subject == 'null') {
196 $activity->subject = ts('Case status changed from %1 to %2', [
197 1 => CRM_Utils_Array::value($statusval, $form->_caseStatus),
198 2 => CRM_Utils_Array::value($params['case_status_id'], $form->_caseStatus),
199 ]);
200 $activity->save();
201 }
202 }
203 }
204
205 }