Merge pull request #15208 from civicrm/5.17
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseStatus.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class generates form components for OpenCase Activity.
36 */
37 class CRM_Case_Form_Activity_ChangeCaseStatus {
38
39 /**
40 * @param CRM_Core_Form $form
41 *
42 * @throws Exception
43 */
44 public static function preProcess(&$form) {
45 if (!isset($form->_caseId)) {
46 CRM_Core_Error::fatal(ts('Case Id not found.'));
47 }
48
49 $form->addElement('checkbox', 'updateLinkedCases', NULL, NULL, ['class' => 'select-row']);
50
51 $caseID = CRM_Utils_Array::first($form->_caseId);
52 $cases = CRM_Case_BAO_Case::getRelatedCases($caseID);
53 $form->assign('linkedCases', $cases);
54 }
55
56 /**
57 * Set default values for the form.
58 *
59 * For edit/view mode the default values are retrieved from the database.
60 *
61 *
62 * @param CRM_Core_Form $form
63 *
64 * @return array
65 */
66 public static function setDefaultValues(&$form) {
67 $defaults = [];
68 // Retrieve current case status
69 $defaults['case_status_id'] = $form->_defaultCaseStatus;
70
71 return $defaults;
72 }
73
74 /**
75 * @param CRM_Core_Form $form
76 */
77 public static function buildQuickForm(&$form) {
78 $form->removeElement('status_id');
79 $form->removeElement('priority_id');
80
81 $caseTypes = [];
82
83 $form->_caseStatus = CRM_Case_PseudoConstant::caseStatus();
84 $statusNames = CRM_Case_PseudoConstant::caseStatus('name');
85
86 // Limit case statuses to allowed types for these case(s)
87 $allCases = civicrm_api3('Case', 'get', ['return' => 'case_type_id', 'id' => ['IN' => (array) $form->_caseId]]);
88 foreach ($allCases['values'] as $case) {
89 $caseTypes[$case['case_type_id']] = $case['case_type_id'];
90 }
91 $caseTypes = civicrm_api3('CaseType', 'get', ['id' => ['IN' => $caseTypes]]);
92 foreach ($caseTypes['values'] as $ct) {
93 if (!empty($ct['definition']['statuses'])) {
94 foreach ($form->_caseStatus as $id => $label) {
95 if (!in_array($statusNames[$id], $ct['definition']['statuses'])) {
96 unset($form->_caseStatus[$id]);
97 }
98 }
99 }
100 }
101
102 foreach ($form->_caseId as $key => $val) {
103 $form->_oldCaseStatus[] = $form->_defaultCaseStatus[] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $val, 'status_id');
104 }
105
106 foreach ($form->_defaultCaseStatus as $keydefault => $valdefault) {
107 if (!array_key_exists($valdefault, $form->_caseStatus)) {
108 $form->_caseStatus[$valdefault] = CRM_Core_PseudoConstant::getLabel('CRM_Case_BAO_Case', 'status_id', $valdefault);
109 }
110 }
111 $element = $form->add('select', 'case_status_id', ts('Case Status'),
112 $form->_caseStatus, TRUE
113 );
114 // check if the case status id passed in url is a valid one, set as default and freeze
115 if (CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form)) {
116 $caseStatusId = CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form);
117 $caseStatus = CRM_Case_PseudoConstant::caseStatus();
118 $form->_defaultCaseStatus = array_key_exists($caseStatusId, $caseStatus) ? $caseStatusId : NULL;
119 $element->freeze();
120 }
121 }
122
123 /**
124 * Global validation rules for the form.
125 *
126 * @param array $values
127 * Posted values of the form.
128 *
129 * @param $files
130 * @param CRM_Core_Form $form
131 *
132 * @return array
133 * list of errors to be posted back to the form
134 */
135 public static function formRule($values, $files, $form) {
136 return TRUE;
137 }
138
139 /**
140 * Process the form submission.
141 *
142 * @param CRM_Core_Form $form
143 * @param array $params
144 */
145 public static function beginPostProcess(&$form, &$params) {
146 $params['id'] = CRM_Utils_Array::value('case_id', $params);
147
148 if (CRM_Utils_Array::value('updateLinkedCases', $params) === '1') {
149 $caseID = CRM_Utils_Array::first($form->_caseId);
150 $cases = CRM_Case_BAO_Case::getRelatedCases($caseID);
151
152 foreach ($cases as $currentCase) {
153 if ($currentCase['status_id'] != $params['case_status_id']) {
154 $form->_caseId[] = $currentCase['case_id'];
155 }
156 }
157 }
158 }
159
160 /**
161 * Process the form submission.
162 *
163 * @param CRM_Core_Form $form
164 * @param array $params
165 * @param CRM_Activity_BAO_Activity $activity
166 */
167 public static function endPostProcess(&$form, &$params, $activity) {
168 $groupingValues = CRM_Core_OptionGroup::values('case_status', FALSE, TRUE, FALSE, NULL, 'value');
169
170 // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it.
171 if (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) {
172 $params['end_date'] = CRM_Utils_Date::isoToMysql($params['activity_date_time']);
173
174 // End case-specific relationships (roles)
175 foreach ($params['target_contact_id'] as $cid) {
176 $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']);
177 foreach ($rels as $relId => $relData) {
178 $relationshipParams = [
179 'id' => $relId,
180 'end_date' => $params['end_date'],
181 ];
182 // @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
183 //civicrm_api3('Relationship', 'create', $relationshipParams);
184 CRM_Contact_BAO_Relationship::add($relationshipParams);
185 }
186 }
187 }
188 elseif (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Opened') {
189 $params['end_date'] = "null";
190
191 // Reopen case-specific relationships (roles)
192 foreach ($params['target_contact_id'] as $cid) {
193 $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id'], NULL, FALSE);
194 foreach ($rels as $relId => $relData) {
195 $relationshipParams = [
196 'id' => $relId,
197 'end_date' => 'null',
198 ];
199 // @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
200 //civicrm_api3('Relationship', 'create', $relationshipParams);
201 CRM_Contact_BAO_Relationship::add($relationshipParams);
202 }
203 }
204 }
205 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
206 $activity->status_id = $params['status_id'];
207 $params['priority_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal');
208 $activity->priority_id = $params['priority_id'];
209
210 foreach ($form->_oldCaseStatus as $statuskey => $statusval) {
211 if ($activity->subject == 'null') {
212 $activity->subject = ts('Case status changed from %1 to %2', [
213 1 => CRM_Utils_Array::value($statusval, $form->_caseStatus),
214 2 => CRM_Utils_Array::value($params['case_status_id'], $form->_caseStatus),
215 ]);
216 $activity->save();
217 }
218 }
219 }
220
221 }