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 | } | |
48 | } | |
49 | ||
50 | /** | |
3819f101 | 51 | * Set default values for the form. |
52 | * | |
53 | * For edit/view mode the default values are retrieved from the database. | |
6a488035 | 54 | * |
6a488035 | 55 | * |
c490a46a | 56 | * @param CRM_Core_Form $form |
77b97be7 | 57 | * |
3819f101 | 58 | * @return array |
6a488035 | 59 | */ |
00be9182 | 60 | public static function setDefaultValues(&$form) { |
6a488035 TO |
61 | $defaults = array(); |
62 | // Retrieve current case status | |
63 | $defaults['case_status_id'] = $form->_defaultCaseStatus; | |
64 | ||
65 | return $defaults; | |
66 | } | |
67 | ||
4c6ce474 | 68 | /** |
c490a46a | 69 | * @param CRM_Core_Form $form |
4c6ce474 | 70 | */ |
00be9182 | 71 | public static function buildQuickForm(&$form) { |
6a488035 TO |
72 | $form->removeElement('status_id'); |
73 | $form->removeElement('priority_id'); | |
74 | ||
4de6aa45 CW |
75 | $caseTypes = array(); |
76 | ||
6a488035 | 77 | $form->_caseStatus = CRM_Case_PseudoConstant::caseStatus(); |
4de6aa45 CW |
78 | $statusNames = CRM_Case_PseudoConstant::caseStatus('name'); |
79 | ||
80 | // Limit case statuses to allowed types for these case(s) | |
81 | $allCases = civicrm_api3('Case', 'get', array('return' => 'case_type_id', 'id' => array('IN' => (array) $form->_caseId))); | |
82 | foreach ($allCases['values'] as $case) { | |
83 | $caseTypes[$case['case_type_id']] = $case['case_type_id']; | |
84 | } | |
85 | $caseTypes = civicrm_api3('CaseType', 'get', array('id' => array('IN' => $caseTypes))); | |
86 | foreach ($caseTypes['values'] as $ct) { | |
87 | if (!empty($ct['definition']['statuses'])) { | |
88 | foreach ($form->_caseStatus as $id => $label) { | |
89 | if (!in_array($statusNames[$id], $ct['definition']['statuses'])) { | |
90 | unset($form->_caseStatus[$id]); | |
91 | } | |
92 | } | |
93 | } | |
94 | } | |
6a488035 | 95 | |
725fd9d9 N |
96 | foreach ($form->_caseId as $key => $val) { |
97 | $form->_oldCaseStatus[] = $form->_defaultCaseStatus[] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $val, 'status_id'); | |
98 | } | |
99 | ||
100 | foreach ($form->_defaultCaseStatus as $keydefault => $valdefault) { | |
101 | if (!array_key_exists($valdefault, $form->_caseStatus)) { | |
102 | $form->_caseStatus[$valdefault] = CRM_Core_OptionGroup::getLabel('case_status', | |
103 | $valdefault, | |
104 | FALSE | |
105 | ); | |
106 | } | |
6a488035 | 107 | } |
1156155a | 108 | $element = $form->add('select', 'case_status_id', ts('Case Status'), |
6a488035 TO |
109 | $form->_caseStatus, TRUE |
110 | ); | |
1156155a P |
111 | // check if the case status id passed in url is a valid one, set as default and freeze |
112 | if (CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form)) { | |
113 | $caseStatusId = CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form); | |
114 | $caseStatus = CRM_Case_PseudoConstant::caseStatus(); | |
115 | $form->_defaultCaseStatus = array_key_exists($caseStatusId, $caseStatus) ? $caseStatusId : NULL; | |
116 | $element->freeze(); | |
117 | } | |
6a488035 TO |
118 | } |
119 | ||
120 | /** | |
d2e5d2ce | 121 | * Global validation rules for the form. |
6a488035 | 122 | * |
64bd5a0e TO |
123 | * @param array $values |
124 | * Posted values of the form. | |
6a488035 | 125 | * |
77b97be7 | 126 | * @param $files |
c490a46a | 127 | * @param CRM_Core_Form $form |
77b97be7 | 128 | * |
a6c01b45 CW |
129 | * @return array |
130 | * list of errors to be posted back to the form | |
6a488035 | 131 | */ |
00be9182 | 132 | public static function formRule($values, $files, $form) { |
6a488035 TO |
133 | return TRUE; |
134 | } | |
135 | ||
136 | /** | |
d2e5d2ce | 137 | * Process the form submission. |
6a488035 | 138 | * |
6a488035 | 139 | * |
c490a46a CW |
140 | * @param CRM_Core_Form $form |
141 | * @param array $params | |
6a488035 | 142 | */ |
00be9182 | 143 | public static function beginPostProcess(&$form, &$params) { |
6a488035 TO |
144 | $params['id'] = CRM_Utils_Array::value('case_id', $params); |
145 | } | |
146 | ||
147 | /** | |
d2e5d2ce | 148 | * Process the form submission. |
6a488035 | 149 | * |
6a488035 | 150 | * |
c490a46a CW |
151 | * @param CRM_Core_Form $form |
152 | * @param array $params | |
3819f101 | 153 | * @param CRM_Activity_BAO_Activity $activity |
6a488035 | 154 | */ |
00be9182 | 155 | public static function endPostProcess(&$form, &$params, $activity) { |
6a488035 TO |
156 | $groupingValues = CRM_Core_OptionGroup::values('case_status', FALSE, TRUE, FALSE, NULL, 'value'); |
157 | ||
158 | // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it. | |
8cc574cf | 159 | if (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) { |
6a488035 TO |
160 | $params['end_date'] = $params['activity_date_time']; |
161 | ||
162 | // End case-specific relationships (roles) | |
163 | foreach ($params['target_contact_id'] as $cid) { | |
164 | $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']); | |
165 | // FIXME: Is there an existing function to close a relationship? | |
166 | $query = 'UPDATE civicrm_relationship SET end_date=%2 WHERE id=%1'; | |
167 | foreach ($rels as $relId => $relData) { | |
e547f744 | 168 | $relParams = array( |
353ffa53 | 169 | 1 => array($relId, 'Integer'), |
6a488035 TO |
170 | 2 => array($params['end_date'], 'Timestamp'), |
171 | ); | |
172 | CRM_Core_DAO::executeQuery($query, $relParams); | |
173 | } | |
174 | } | |
175 | } | |
176 | elseif (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Opened') { | |
177 | $params['end_date'] = "null"; | |
178 | ||
179 | // Reopen case-specific relationships (roles) | |
180 | foreach ($params['target_contact_id'] as $cid) { | |
181 | $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']); | |
182 | // FIXME: Is there an existing function? | |
183 | $query = 'UPDATE civicrm_relationship SET end_date=NULL WHERE id=%1'; | |
184 | foreach ($rels as $relId => $relData) { | |
10a5be27 | 185 | $relParams = array(1 => array($relId, 'Integer')); |
6a488035 TO |
186 | CRM_Core_DAO::executeQuery($query, $relParams); |
187 | } | |
188 | } | |
189 | } | |
9c248a42 | 190 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); |
6a488035 TO |
191 | $activity->status_id = $params['status_id']; |
192 | $params['priority_id'] = CRM_Core_OptionGroup::getValue('priority', 'Normal', 'name'); | |
193 | $activity->priority_id = $params['priority_id']; | |
194 | ||
481a74f4 | 195 | foreach ($form->_oldCaseStatus as $statuskey => $statusval) { |
725fd9d9 N |
196 | if ($activity->subject == 'null') { |
197 | $activity->subject = ts('Case status changed from %1 to %2', array( | |
198 | 1 => CRM_Utils_Array::value($statusval, $form->_caseStatus), | |
21dfd5f5 | 199 | 2 => CRM_Utils_Array::value($params['case_status_id'], $form->_caseStatus), |
725fd9d9 N |
200 | ) |
201 | ); | |
202 | $activity->save(); | |
203 | } | |
6a488035 | 204 | } |
6a488035 | 205 | } |
96025800 | 206 | |
6a488035 | 207 | } |