Merge pull request #11642 from JKingsnorth/CRM-21739
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseStatus.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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, array('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 = array();
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 = array();
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', array('return' => 'case_type_id', 'id' => array('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', array('id' => array('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_OptionGroup::getLabel('case_status',
109 $valdefault,
110 FALSE
111 );
112 }
113 }
114 $element = $form->add('select', 'case_status_id', ts('Case Status'),
115 $form->_caseStatus, TRUE
116 );
117 // check if the case status id passed in url is a valid one, set as default and freeze
118 if (CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form)) {
119 $caseStatusId = CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form);
120 $caseStatus = CRM_Case_PseudoConstant::caseStatus();
121 $form->_defaultCaseStatus = array_key_exists($caseStatusId, $caseStatus) ? $caseStatusId : NULL;
122 $element->freeze();
123 }
124 }
125
126 /**
127 * Global validation rules for the form.
128 *
129 * @param array $values
130 * Posted values of the form.
131 *
132 * @param $files
133 * @param CRM_Core_Form $form
134 *
135 * @return array
136 * list of errors to be posted back to the form
137 */
138 public static function formRule($values, $files, $form) {
139 return TRUE;
140 }
141
142 /**
143 * Process the form submission.
144 *
145 * @param CRM_Core_Form $form
146 * @param array $params
147 */
148 public static function beginPostProcess(&$form, &$params) {
149 $params['id'] = CRM_Utils_Array::value('case_id', $params);
150
151 if ($params['updateLinkedCases'] === '1') {
152 $caseID = CRM_Utils_Array::first($form->_caseId);
153 $cases = CRM_Case_BAO_Case::getRelatedCases($caseID);
154
155 foreach ($cases as $currentCase) {
156 if ($currentCase['status_id'] != $params['case_status_id']) {
157 $form->_caseId[] = $currentCase['case_id'];
158 }
159 }
160 }
161 }
162
163 /**
164 * Process the form submission.
165 *
166 * @param CRM_Core_Form $form
167 * @param array $params
168 * @param CRM_Activity_BAO_Activity $activity
169 */
170 public static function endPostProcess(&$form, &$params, $activity) {
171
172 $groupingValues = CRM_Core_OptionGroup::values('case_status', FALSE, TRUE, FALSE, NULL, 'value');
173
174 // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it.
175 if (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) {
176 $params['end_date'] = $params['activity_date_time'];
177
178 // End case-specific relationships (roles)
179 foreach ($params['target_contact_id'] as $cid) {
180 $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']);
181 // FIXME: Is there an existing function to close a relationship?
182 $query = 'UPDATE civicrm_relationship SET end_date=%2 WHERE id=%1';
183 foreach ($rels as $relId => $relData) {
184 $relParams = array(
185 1 => array($relId, 'Integer'),
186 2 => array($params['end_date'], 'Timestamp'),
187 );
188 CRM_Core_DAO::executeQuery($query, $relParams);
189 }
190 }
191 }
192 elseif (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Opened') {
193 $params['end_date'] = "null";
194
195 // Reopen case-specific relationships (roles)
196 foreach ($params['target_contact_id'] as $cid) {
197 $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']);
198 // FIXME: Is there an existing function?
199 $query = 'UPDATE civicrm_relationship SET end_date=NULL WHERE id=%1';
200 foreach ($rels as $relId => $relData) {
201 $relParams = array(1 => array($relId, 'Integer'));
202 CRM_Core_DAO::executeQuery($query, $relParams);
203 }
204 }
205 }
206 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
207 $activity->status_id = $params['status_id'];
208 $params['priority_id'] = CRM_Core_OptionGroup::getValue('priority', 'Normal', 'name');
209 $activity->priority_id = $params['priority_id'];
210
211 foreach ($form->_oldCaseStatus as $statuskey => $statusval) {
212 if ($activity->subject == 'null') {
213 $activity->subject = ts('Case status changed from %1 to %2', array(
214 1 => CRM_Utils_Array::value($statusval, $form->_caseStatus),
215 2 => CRM_Utils_Array::value($params['case_status_id'], $form->_caseStatus),
216 )
217 );
218 $activity->save();
219 }
220 }
221 }
222
223 }