Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
3819f101 | 35 | * This class generates form components for OpenCase Activity. |
6a488035 TO |
36 | */ |
37 | class CRM_Case_Form_Activity_ChangeCaseStatus { | |
38 | ||
4c6ce474 | 39 | /** |
c490a46a | 40 | * @param CRM_Core_Form $form |
4c6ce474 EM |
41 | * |
42 | * @throws Exception | |
43 | */ | |
00be9182 | 44 | public static function preProcess(&$form) { |
6a488035 TO |
45 | if (!isset($form->_caseId)) { |
46 | CRM_Core_Error::fatal(ts('Case Id not found.')); | |
47 | } | |
74b15fae CR |
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); | |
6a488035 TO |
54 | } |
55 | ||
56 | /** | |
3819f101 | 57 | * Set default values for the form. |
58 | * | |
59 | * For edit/view mode the default values are retrieved from the database. | |
6a488035 | 60 | * |
6a488035 | 61 | * |
c490a46a | 62 | * @param CRM_Core_Form $form |
77b97be7 | 63 | * |
3819f101 | 64 | * @return array |
6a488035 | 65 | */ |
00be9182 | 66 | public static function setDefaultValues(&$form) { |
6a488035 TO |
67 | $defaults = array(); |
68 | // Retrieve current case status | |
69 | $defaults['case_status_id'] = $form->_defaultCaseStatus; | |
70 | ||
71 | return $defaults; | |
72 | } | |
73 | ||
4c6ce474 | 74 | /** |
c490a46a | 75 | * @param CRM_Core_Form $form |
4c6ce474 | 76 | */ |
00be9182 | 77 | public static function buildQuickForm(&$form) { |
6a488035 TO |
78 | $form->removeElement('status_id'); |
79 | $form->removeElement('priority_id'); | |
80 | ||
4de6aa45 CW |
81 | $caseTypes = array(); |
82 | ||
6a488035 | 83 | $form->_caseStatus = CRM_Case_PseudoConstant::caseStatus(); |
4de6aa45 CW |
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 | } | |
6a488035 | 101 | |
725fd9d9 N |
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 | } | |
6a488035 | 113 | } |
1156155a | 114 | $element = $form->add('select', 'case_status_id', ts('Case Status'), |
6a488035 TO |
115 | $form->_caseStatus, TRUE |
116 | ); | |
1156155a P |
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 | } | |
6a488035 TO |
124 | } |
125 | ||
126 | /** | |
d2e5d2ce | 127 | * Global validation rules for the form. |
6a488035 | 128 | * |
64bd5a0e TO |
129 | * @param array $values |
130 | * Posted values of the form. | |
6a488035 | 131 | * |
77b97be7 | 132 | * @param $files |
c490a46a | 133 | * @param CRM_Core_Form $form |
77b97be7 | 134 | * |
a6c01b45 CW |
135 | * @return array |
136 | * list of errors to be posted back to the form | |
6a488035 | 137 | */ |
00be9182 | 138 | public static function formRule($values, $files, $form) { |
6a488035 TO |
139 | return TRUE; |
140 | } | |
141 | ||
142 | /** | |
d2e5d2ce | 143 | * Process the form submission. |
6a488035 | 144 | * |
c490a46a CW |
145 | * @param CRM_Core_Form $form |
146 | * @param array $params | |
6a488035 | 147 | */ |
00be9182 | 148 | public static function beginPostProcess(&$form, &$params) { |
6a488035 | 149 | $params['id'] = CRM_Utils_Array::value('case_id', $params); |
74b15fae CR |
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 | } | |
6a488035 TO |
161 | } |
162 | ||
163 | /** | |
d2e5d2ce | 164 | * Process the form submission. |
6a488035 | 165 | * |
c490a46a CW |
166 | * @param CRM_Core_Form $form |
167 | * @param array $params | |
3819f101 | 168 | * @param CRM_Activity_BAO_Activity $activity |
6a488035 | 169 | */ |
00be9182 | 170 | public static function endPostProcess(&$form, &$params, $activity) { |
74b15fae | 171 | |
6a488035 TO |
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. | |
8cc574cf | 175 | if (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) { |
6a488035 TO |
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) { | |
e547f744 | 184 | $relParams = array( |
353ffa53 | 185 | 1 => array($relId, 'Integer'), |
6a488035 TO |
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) { | |
10a5be27 | 201 | $relParams = array(1 => array($relId, 'Integer')); |
6a488035 TO |
202 | CRM_Core_DAO::executeQuery($query, $relParams); |
203 | } | |
204 | } | |
205 | } | |
9c248a42 | 206 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); |
6a488035 TO |
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 | ||
481a74f4 | 211 | foreach ($form->_oldCaseStatus as $statuskey => $statusval) { |
725fd9d9 N |
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), | |
21dfd5f5 | 215 | 2 => CRM_Utils_Array::value($params['case_status_id'], $form->_caseStatus), |
725fd9d9 N |
216 | ) |
217 | ); | |
218 | $activity->save(); | |
219 | } | |
6a488035 | 220 | } |
6a488035 | 221 | } |
96025800 | 222 | |
6a488035 | 223 | } |