Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
6a488035 | 12 | /** |
df371444 | 13 | * This class generates form components for OpenCase Activity. |
6a488035 TO |
14 | */ |
15 | class CRM_Case_Form_Activity_OpenCase { | |
16 | ||
17 | /** | |
fe482240 | 18 | * The id of the client associated with this case. |
6a488035 TO |
19 | * |
20 | * @var int | |
6a488035 TO |
21 | */ |
22 | public $_contactID; | |
23 | ||
b1da3571 MW |
24 | /** |
25 | * @var int | |
26 | */ | |
27 | public $_caseStatusId; | |
28 | ||
4c6ce474 | 29 | /** |
99ccd336 MWMC |
30 | * @param CRM_Case_Form_Case $form |
31 | * | |
32 | * @throws \CRM_Core_Exception | |
b1da3571 | 33 | * @throws \CiviCRM_API3_Exception |
4c6ce474 | 34 | */ |
00be9182 | 35 | public static function preProcess(&$form) { |
6a488035 TO |
36 | //get multi client case configuration |
37 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); | |
1b2dc6a6 | 38 | $form->_allowMultiClient = (bool) $xmlProcessorProcess->getAllowMultipleCaseClients(); |
6a488035 TO |
39 | |
40 | if ($form->_context == 'caseActivity') { | |
41 | $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $form); | |
9c248a42 | 42 | $atype = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Case Start Date'); |
73f72709 | 43 | $caseId = CRM_Utils_Array::first($form->_caseId); |
6a488035 | 44 | $form->assign('changeStartURL', CRM_Utils_System::url('civicrm/case/activity', |
73f72709 | 45 | "action=add&reset=1&cid=$contactID&caseid={$caseId}&atype=$atype" |
6a488035 TO |
46 | ) |
47 | ); | |
48 | return; | |
49 | } | |
50 | ||
edc80cda | 51 | $form->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $form); |
6a488035 TO |
52 | $form->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $form); |
53 | $form->assign('context', $form->_context); | |
54 | ||
74fb6e99 PJ |
55 | // check if the case type id passed in url is a valid one |
56 | $caseTypeId = CRM_Utils_Request::retrieve('ctype', 'Positive', $form); | |
7a5c0c6c | 57 | $caseTypes = CRM_Case_BAO_Case::buildOptions('case_type_id', 'create'); |
74fb6e99 PJ |
58 | $form->_caseTypeId = array_key_exists($caseTypeId, $caseTypes) ? $caseTypeId : NULL; |
59 | ||
4d91aee1 | 60 | // check if the case status id passed in url is a valid one |
1156155a | 61 | $caseStatusId = CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form); |
4d91aee1 P |
62 | $caseStatus = CRM_Case_PseudoConstant::caseStatus(); |
63 | $form->_caseStatusId = array_key_exists($caseStatusId, $caseStatus) ? $caseStatusId : NULL; | |
64 | ||
6a488035 | 65 | // Add attachments |
481a74f4 | 66 | CRM_Core_BAO_File::buildAttachment($form, 'civicrm_activity', $form->_activityId); |
227cad77 P |
67 | $session = CRM_Core_Session::singleton(); |
68 | $session->pushUserContext(CRM_Utils_System::url('civicrm/case', 'reset=1')); | |
6a488035 TO |
69 | } |
70 | ||
71 | /** | |
c490a46a | 72 | * Set default values for the form. For edit/view mode |
6a488035 TO |
73 | * the default values are retrieved from the database |
74 | * | |
99ccd336 | 75 | * @param CRM_Case_Form_Case $form |
6a488035 | 76 | * |
99ccd336 | 77 | * @return array $defaults |
b1da3571 | 78 | * @throws \CiviCRM_API3_Exception |
6a488035 | 79 | */ |
00be9182 | 80 | public static function setDefaultValues(&$form) { |
be2fb01f | 81 | $defaults = []; |
6a488035 TO |
82 | if ($form->_context == 'caseActivity') { |
83 | return $defaults; | |
84 | } | |
85 | ||
06f51047 | 86 | $defaults['start_date'] = date('Y-m-d H:i:s'); |
6a488035 TO |
87 | |
88 | // set default case status, case type, encounter medium, location type and phone type defaults are set in DB | |
4d91aee1 P |
89 | if ($form->_caseStatusId) { |
90 | $caseStatus = $form->_caseStatusId; | |
6a488035 | 91 | } |
4d91aee1 P |
92 | else { |
93 | $caseStatus = CRM_Core_OptionGroup::values('case_status', FALSE, FALSE, FALSE, 'AND is_default = 1'); | |
94 | if (count($caseStatus) == 1) { | |
5d4fcf54 TO |
95 | //$defaults['status_id'] = key($caseStatus); |
96 | $caseStatus = key($caseStatus); | |
4d91aee1 P |
97 | } |
98 | } | |
99 | $defaults['status_id'] = $caseStatus; | |
74fb6e99 PJ |
100 | |
101 | // set default case type passed in url | |
102 | if ($form->_caseTypeId) { | |
7ef0d72c CW |
103 | $defaults['case_type_id'] = $form->_caseTypeId; |
104 | } | |
105 | else { | |
106 | // TODO: Not possible yet to set a default case type in the system | |
107 | // For now just add the convenience of auto-selecting if there is only one option | |
108 | $caseTypes = CRM_Case_BAO_Case::buildOptions('case_type_id', 'create'); | |
109 | if (count($caseTypes) == 1) { | |
110 | reset($caseTypes); | |
111 | $defaults['case_type_id'] = key($caseTypes); | |
112 | } | |
74fb6e99 | 113 | } |
74fb6e99 | 114 | |
6a488035 TO |
115 | $medium = CRM_Core_OptionGroup::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1'); |
116 | if (count($medium) == 1) { | |
117 | $defaults['medium_id'] = key($medium); | |
118 | } | |
119 | ||
120 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
121 | if ($defaultLocationType->id) { | |
122 | $defaults['location[1][location_type_id]'] = $defaultLocationType->id; | |
123 | } | |
124 | ||
125 | $phoneType = CRM_Core_OptionGroup::values('phone_type', FALSE, FALSE, FALSE, 'AND is_default = 1'); | |
126 | if (count($phoneType) == 1) { | |
127 | $defaults['location[1][phone][1][phone_type_id]'] = key($phoneType); | |
128 | } | |
129 | ||
130 | return $defaults; | |
131 | } | |
132 | ||
1b2dc6a6 CW |
133 | /** |
134 | * @param CRM_Case_Form_Case $form | |
b1da3571 MW |
135 | * |
136 | * @throws \CiviCRM_API3_Exception | |
137 | * @throws \Exception | |
1b2dc6a6 | 138 | */ |
00be9182 | 139 | public static function buildQuickForm(&$form) { |
6a488035 TO |
140 | if ($form->_context == 'caseActivity') { |
141 | return; | |
142 | } | |
143 | if ($form->_context == 'standalone') { | |
be2fb01f | 144 | $form->addEntityRef('client_id', ts('Client'), [ |
5d4fcf54 TO |
145 | 'create' => TRUE, |
146 | 'multiple' => $form->_allowMultiClient, | |
147 | ], TRUE); | |
6a488035 TO |
148 | } |
149 | ||
be2fb01f | 150 | $element = $form->addField('case_type_id', [ |
7a5c0c6c CW |
151 | 'context' => 'create', |
152 | 'entity' => 'Case', | |
153 | 'onchange' => "CRM.buildCustomData('Case', this.value);", | |
be2fb01f | 154 | ], TRUE); |
74fb6e99 PJ |
155 | if ($form->_caseTypeId) { |
156 | $element->freeze(); | |
157 | } | |
158 | ||
be2fb01f | 159 | $csElement = $form->addField('status_id', [ |
7a5c0c6c CW |
160 | 'context' => 'create', |
161 | 'entity' => 'Case', | |
be2fb01f | 162 | ], TRUE); |
4d91aee1 P |
163 | if ($form->_caseStatusId) { |
164 | $csElement->freeze(); | |
165 | } | |
6a488035 | 166 | |
4b2fda1e | 167 | $form->add('number', 'duration', ts('Activity Duration'), ['class' => 'four', 'min' => 1]); |
6a488035 TO |
168 | $form->addRule('duration', ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'); |
169 | ||
170 | if ($form->_currentlyViewedContactId) { | |
171 | list($displayName) = CRM_Contact_BAO_Contact::getDisplayAndImage($form->_currentlyViewedContactId); | |
172 | $form->assign('clientName', $displayName); | |
173 | } | |
174 | ||
06f51047 | 175 | $form->add('datepicker', 'start_date', ts('Case Start Date'), [], TRUE); |
6a488035 | 176 | |
be2fb01f | 177 | $form->addField('medium_id', ['entity' => 'activity', 'context' => 'create'], TRUE); |
6a488035 TO |
178 | |
179 | // calling this field activity_location to prevent conflict with contact location fields | |
180 | $form->add('text', 'activity_location', ts('Location'), CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'location')); | |
181 | ||
be2fb01f | 182 | $form->add('wysiwyg', 'activity_details', ts('Details'), ['rows' => 4, 'cols' => 60], FALSE); |
6a488035 | 183 | |
be2fb01f | 184 | $form->addButtons([ |
5d4fcf54 TO |
185 | [ |
186 | 'type' => 'upload', | |
187 | 'name' => ts('Save'), | |
188 | 'isDefault' => TRUE, | |
5d4fcf54 TO |
189 | ], |
190 | [ | |
191 | 'type' => 'upload', | |
192 | 'name' => ts('Save and New'), | |
193 | 'subName' => 'new', | |
5d4fcf54 TO |
194 | ], |
195 | [ | |
196 | 'type' => 'cancel', | |
197 | 'name' => ts('Cancel'), | |
198 | ], | |
199 | ]); | |
6a488035 TO |
200 | } |
201 | ||
202 | /** | |
fe482240 | 203 | * Process the form submission. |
6a488035 | 204 | * |
99ccd336 | 205 | * @param CRM_Case_Form_Case $form |
c490a46a | 206 | * @param array $params |
6a488035 | 207 | */ |
00be9182 | 208 | public static function beginPostProcess(&$form, &$params) { |
6a488035 TO |
209 | if ($form->_context == 'caseActivity') { |
210 | return; | |
211 | } | |
212 | ||
1b2dc6a6 CW |
213 | if ($form->_context == 'standalone') { |
214 | $params['client_id'] = explode(',', $params['client_id']); | |
215 | $form->_currentlyViewedContactId = $params['client_id'][0]; | |
6a488035 TO |
216 | } |
217 | ||
6a488035 | 218 | // rename activity_location param to the correct column name for activity DAO |
9c1bc317 | 219 | $params['location'] = $params['activity_location'] ?? NULL; |
6a488035 TO |
220 | |
221 | // Add attachments | |
b1da3571 | 222 | CRM_Core_BAO_File::formatAttachment($params, $params, 'civicrm_activity', $form->_activityId); |
6a488035 TO |
223 | } |
224 | ||
225 | /** | |
fe482240 | 226 | * Global validation rules for the form. |
6a488035 | 227 | * |
b1da3571 MW |
228 | * @param array $fields |
229 | * The input form values. | |
230 | * @param array $files | |
231 | * The uploaded files if any. | |
99ccd336 | 232 | * @param CRM_Case_Form_Case $form |
6a488035 | 233 | * |
b1da3571 | 234 | * @return array|bool |
a6c01b45 | 235 | * list of errors to be posted back to the form |
6a488035 | 236 | */ |
00be9182 | 237 | public static function formRule($fields, $files, $form) { |
6a488035 TO |
238 | if ($form->_context == 'caseActivity') { |
239 | return TRUE; | |
240 | } | |
241 | ||
be2fb01f | 242 | $errors = []; |
6a488035 TO |
243 | return $errors; |
244 | } | |
245 | ||
246 | /** | |
fe482240 | 247 | * Process the form submission. |
6a488035 | 248 | * |
99ccd336 | 249 | * @param CRM_Case_Form_Case $form |
c490a46a | 250 | * @param array $params |
99ccd336 MWMC |
251 | * |
252 | * @throws \Exception | |
6a488035 | 253 | */ |
00be9182 | 254 | public static function endPostProcess(&$form, &$params) { |
6a488035 TO |
255 | if ($form->_context == 'caseActivity') { |
256 | return; | |
257 | } | |
258 | ||
259 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); | |
260 | $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients(); | |
261 | ||
262 | if (!$isMultiClient && !$form->_currentlyViewedContactId) { | |
507bc932 | 263 | CRM_Core_Error::statusBounce(ts('Required parameter missing for OpenCase - end post processing')); |
6a488035 TO |
264 | } |
265 | ||
b1da3571 | 266 | if (!$form->_currentUserId || !$params['case_id'] || !$params['case_type']) { |
507bc932 | 267 | CRM_Core_Error::statusBounce(ts('Required parameter missing for OpenCase - end post processing')); |
6a488035 TO |
268 | } |
269 | ||
270 | // 1. create case-contact | |
1b2dc6a6 CW |
271 | if ($isMultiClient && $form->_context == 'standalone') { |
272 | foreach ($params['client_id'] as $cliId) { | |
6a488035 | 273 | if (empty($cliId)) { |
507bc932 | 274 | CRM_Core_Error::statusBounce(ts('client_id cannot be empty for OpenCase - end post processing')); |
6a488035 | 275 | } |
be2fb01f | 276 | $contactParams = [ |
6a488035 TO |
277 | 'case_id' => $params['case_id'], |
278 | 'contact_id' => $cliId, | |
be2fb01f | 279 | ]; |
ff9340a4 | 280 | CRM_Case_BAO_CaseContact::create($contactParams); |
6a488035 TO |
281 | } |
282 | } | |
283 | else { | |
be2fb01f | 284 | $contactParams = [ |
6a488035 TO |
285 | 'case_id' => $params['case_id'], |
286 | 'contact_id' => $form->_currentlyViewedContactId, | |
be2fb01f | 287 | ]; |
ff9340a4 | 288 | CRM_Case_BAO_CaseContact::create($contactParams); |
6a488035 TO |
289 | } |
290 | ||
291 | // 2. initiate xml processor | |
292 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
293 | ||
be2fb01f | 294 | $xmlProcessorParams = [ |
1b2dc6a6 | 295 | 'clientID' => $form->_currentlyViewedContactId, |
6a488035 TO |
296 | 'creatorID' => $form->_currentUserId, |
297 | 'standardTimeline' => 1, | |
298 | 'activityTypeName' => 'Open Case', | |
299 | 'caseID' => $params['case_id'], | |
300 | 'subject' => $params['activity_subject'], | |
301 | 'location' => $params['location'], | |
302 | 'activity_date_time' => $params['start_date'], | |
6b409353 | 303 | 'duration' => $params['duration'] ?? NULL, |
6a488035 TO |
304 | 'medium_id' => $params['medium_id'], |
305 | 'details' => $params['activity_details'], | |
6b409353 | 306 | 'relationship_end_date' => $params['end_date'] ?? NULL, |
be2fb01f | 307 | ]; |
6a488035 TO |
308 | |
309 | if (array_key_exists('custom', $params) && is_array($params['custom'])) { | |
310 | $xmlProcessorParams['custom'] = $params['custom']; | |
311 | } | |
312 | ||
313 | // Add parameters for attachments | |
aaffa79f | 314 | $numAttachments = Civi::settings()->get('max_attachments'); |
481a74f4 | 315 | for ($i = 1; $i <= $numAttachments; $i++) { |
6a488035 | 316 | $attachName = "attachFile_$i"; |
481a74f4 | 317 | if (isset($params[$attachName]) && !empty($params[$attachName])) { |
6a488035 TO |
318 | $xmlProcessorParams[$attachName] = $params[$attachName]; |
319 | } | |
320 | } | |
321 | ||
322 | $xmlProcessor->run($params['case_type'], $xmlProcessorParams); | |
323 | ||
324 | // status msg | |
325 | $params['statusMsg'] = ts('Case opened successfully.'); | |
326 | ||
327 | $buttonName = $form->controller->getButtonName(); | |
328 | $session = CRM_Core_Session::singleton(); | |
329 | if ($buttonName == $form->getButtonName('upload', 'new')) { | |
330 | if ($form->_context == 'standalone') { | |
331 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/case/add', | |
353ffa53 TO |
332 | 'reset=1&action=add&context=standalone' |
333 | )); | |
6a488035 TO |
334 | } |
335 | else { | |
336 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/case', | |
353ffa53 TO |
337 | "reset=1&action=add&context=case&cid={$form->_contactID}" |
338 | )); | |
6a488035 TO |
339 | } |
340 | } | |
341 | } | |
96025800 | 342 | |
6a488035 | 343 | } |