Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for OpenCase Activity | |
38 | * | |
39 | */ | |
40 | class CRM_Case_Form_Activity_OpenCase { | |
41 | ||
42 | /** | |
43 | * the id of the client associated with this case | |
44 | * | |
45 | * @var int | |
46 | * @public | |
47 | */ | |
48 | public $_contactID; | |
49 | ||
4c6ce474 EM |
50 | /** |
51 | * @param $form | |
52 | */ | |
6a488035 TO |
53 | static function preProcess(&$form) { |
54 | //get multi client case configuration | |
55 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); | |
1b2dc6a6 | 56 | $form->_allowMultiClient = (bool) $xmlProcessorProcess->getAllowMultipleCaseClients(); |
6a488035 TO |
57 | |
58 | if ($form->_context == 'caseActivity') { | |
59 | $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $form); | |
60 | $atype = CRM_Core_OptionGroup::getValue('activity_type', | |
61 | 'Change Case Start Date', | |
62 | 'name' | |
63 | ); | |
64 | $form->assign('changeStartURL', CRM_Utils_System::url('civicrm/case/activity', | |
65 | "action=add&reset=1&cid=$contactID&caseid={$form->_caseId}&atype=$atype" | |
66 | ) | |
67 | ); | |
68 | return; | |
69 | } | |
70 | ||
71 | $form->_context = CRM_Utils_Request::retrieve('context', 'String', $form); | |
72 | $form->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $form); | |
73 | $form->assign('context', $form->_context); | |
74 | ||
74fb6e99 PJ |
75 | // check if the case type id passed in url is a valid one |
76 | $caseTypeId = CRM_Utils_Request::retrieve('ctype', 'Positive', $form); | |
77 | $caseTypes = CRM_Case_PseudoConstant::caseType(); | |
78 | $form->_caseTypeId = array_key_exists($caseTypeId, $caseTypes) ? $caseTypeId : NULL; | |
79 | ||
4d91aee1 | 80 | // check if the case status id passed in url is a valid one |
1156155a | 81 | $caseStatusId = CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form); |
4d91aee1 P |
82 | $caseStatus = CRM_Case_PseudoConstant::caseStatus(); |
83 | $form->_caseStatusId = array_key_exists($caseStatusId, $caseStatus) ? $caseStatusId : NULL; | |
84 | ||
6a488035 TO |
85 | // Add attachments |
86 | CRM_Core_BAO_File::buildAttachment( $form, 'civicrm_activity', $form->_activityId ); | |
227cad77 P |
87 | $session = CRM_Core_Session::singleton(); |
88 | $session->pushUserContext(CRM_Utils_System::url('civicrm/case', 'reset=1')); | |
6a488035 TO |
89 | } |
90 | ||
91 | /** | |
92 | * This function sets the default values for the form. For edit/view mode | |
93 | * the default values are retrieved from the database | |
94 | * | |
95 | * @access public | |
96 | * | |
dd244018 EM |
97 | * @param $form |
98 | * | |
355ba699 | 99 | * @return void |
6a488035 TO |
100 | */ |
101 | static function setDefaultValues(&$form) { | |
102 | $defaults = array(); | |
103 | if ($form->_context == 'caseActivity') { | |
104 | return $defaults; | |
105 | } | |
106 | ||
1da41104 | 107 | list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime'); |
6a488035 TO |
108 | |
109 | // set default case status, case type, encounter medium, location type and phone type defaults are set in DB | |
4d91aee1 P |
110 | if ($form->_caseStatusId) { |
111 | $caseStatus = $form->_caseStatusId; | |
6a488035 | 112 | } |
4d91aee1 P |
113 | else { |
114 | $caseStatus = CRM_Core_OptionGroup::values('case_status', FALSE, FALSE, FALSE, 'AND is_default = 1'); | |
115 | if (count($caseStatus) == 1) { | |
116 | $caseStatus = key($caseStatus); //$defaults['status_id'] = key($caseStatus); | |
117 | } | |
118 | } | |
119 | $defaults['status_id'] = $caseStatus; | |
74fb6e99 PJ |
120 | |
121 | // set default case type passed in url | |
122 | if ($form->_caseTypeId) { | |
123 | $caseType = $form->_caseTypeId; | |
d14b35a4 | 124 | $defaults['case_type_id'] = $caseType; |
74fb6e99 | 125 | } |
74fb6e99 | 126 | |
6a488035 TO |
127 | $medium = CRM_Core_OptionGroup::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1'); |
128 | if (count($medium) == 1) { | |
129 | $defaults['medium_id'] = key($medium); | |
130 | } | |
131 | ||
132 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
133 | if ($defaultLocationType->id) { | |
134 | $defaults['location[1][location_type_id]'] = $defaultLocationType->id; | |
135 | } | |
136 | ||
137 | $phoneType = CRM_Core_OptionGroup::values('phone_type', FALSE, FALSE, FALSE, 'AND is_default = 1'); | |
138 | if (count($phoneType) == 1) { | |
139 | $defaults['location[1][phone][1][phone_type_id]'] = key($phoneType); | |
140 | } | |
141 | ||
142 | return $defaults; | |
143 | } | |
144 | ||
1b2dc6a6 CW |
145 | /** |
146 | * @param CRM_Case_Form_Case $form | |
147 | */ | |
6a488035 TO |
148 | static function buildQuickForm(&$form) { |
149 | if ($form->_context == 'caseActivity') { | |
150 | return; | |
151 | } | |
152 | if ($form->_context == 'standalone') { | |
1b2dc6a6 | 153 | $form->addEntityRef('client_id', ts('Client'), array('create' => TRUE, 'multiple' => $form->_allowMultiClient), TRUE); |
6a488035 TO |
154 | } |
155 | ||
d14b35a4 N |
156 | $caseTypes = CRM_Case_PseudoConstant::caseType(); |
157 | $element = $form->add('select', | |
158 | 'case_type_id', ts('Case Type'), $caseTypes, | |
159 | TRUE, array('onchange' => "CRM.buildCustomData('Case', this.value);") | |
6a488035 TO |
160 | ); |
161 | ||
74fb6e99 PJ |
162 | if ($form->_caseTypeId) { |
163 | $element->freeze(); | |
164 | } | |
165 | ||
1957e5cb | 166 | $csElement = $form->add('select', 'status_id', ts('Case Status'), |
167 | CRM_Case_PseudoConstant::caseStatus(), | |
168 | FALSE | |
169 | ); | |
170 | ||
4d91aee1 P |
171 | if ($form->_caseStatusId) { |
172 | $csElement->freeze(); | |
173 | } | |
6a488035 | 174 | |
1b2dc6a6 | 175 | $form->add('text', 'duration', ts('Activity Duration'), array('size' => 4, 'maxlength' => 8)); |
6a488035 TO |
176 | $form->addRule('duration', ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger'); |
177 | ||
178 | if ($form->_currentlyViewedContactId) { | |
179 | list($displayName) = CRM_Contact_BAO_Contact::getDisplayAndImage($form->_currentlyViewedContactId); | |
180 | $form->assign('clientName', $displayName); | |
181 | } | |
182 | ||
183 | $form->addDate('start_date', ts('Case Start Date'), TRUE, array('formatType' => 'activityDateTime')); | |
184 | ||
1b2dc6a6 | 185 | $form->addSelect('medium_id', array('entity' => 'activity'), TRUE); |
6a488035 TO |
186 | |
187 | // calling this field activity_location to prevent conflict with contact location fields | |
188 | $form->add('text', 'activity_location', ts('Location'), CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'location')); | |
189 | ||
190 | $form->addWysiwyg('activity_details', ts('Details'), array('rows' => 4, 'cols' => 60), FALSE); | |
191 | ||
192 | $form->addButtons(array( | |
193 | array( | |
194 | 'type' => 'upload', | |
195 | 'name' => ts('Save'), | |
196 | 'isDefault' => TRUE, | |
197 | ), | |
198 | array( | |
199 | 'type' => 'upload', | |
200 | 'name' => ts('Save and New'), | |
201 | 'subName' => 'new', | |
202 | ), | |
203 | array( | |
204 | 'type' => 'cancel', | |
205 | 'name' => ts('Cancel'), | |
206 | ), | |
207 | ) | |
208 | ); | |
209 | } | |
210 | ||
211 | /** | |
212 | * Function to process the form | |
213 | * | |
214 | * @access public | |
215 | * | |
77b97be7 EM |
216 | * @param $form |
217 | * @param $params | |
218 | * | |
355ba699 | 219 | * @return void |
6a488035 TO |
220 | */ |
221 | static function beginPostProcess(&$form, &$params) { | |
222 | if ($form->_context == 'caseActivity') { | |
223 | return; | |
224 | } | |
225 | ||
1b2dc6a6 CW |
226 | if ($form->_context == 'standalone') { |
227 | $params['client_id'] = explode(',', $params['client_id']); | |
228 | $form->_currentlyViewedContactId = $params['client_id'][0]; | |
6a488035 TO |
229 | } |
230 | ||
231 | // for open case start date should be set to current date | |
232 | $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time']); | |
233 | $caseStatus = CRM_Case_PseudoConstant::caseStatus('name'); | |
234 | // for resolved case the end date should set to now | |
235 | if ($params['status_id'] == array_search('Closed', $caseStatus)) { | |
236 | $params['end_date'] = $params['now']; | |
237 | } | |
238 | ||
239 | // rename activity_location param to the correct column name for activity DAO | |
240 | $params['location'] = CRM_Utils_Array::value('activity_location', $params); | |
241 | ||
242 | // Add attachments | |
243 | CRM_Core_BAO_File::formatAttachment( | |
244 | $params, | |
245 | $params, | |
246 | 'civicrm_activity', | |
247 | $form->_activityId | |
248 | ); | |
249 | ||
250 | } | |
251 | ||
252 | /** | |
253 | * global validation rules for the form | |
254 | * | |
fd31fa4c EM |
255 | * @param $fields |
256 | * @param $files | |
257 | * @param $form | |
258 | * | |
259 | * @internal param array $values posted values of the form | |
6a488035 TO |
260 | * |
261 | * @return array list of errors to be posted back to the form | |
262 | * @static | |
263 | * @access public | |
264 | */ | |
265 | static function formRule($fields, $files, $form) { | |
266 | if ($form->_context == 'caseActivity') { | |
267 | return TRUE; | |
268 | } | |
269 | ||
270 | $errors = array(); | |
6a488035 TO |
271 | return $errors; |
272 | } | |
273 | ||
274 | /** | |
275 | * Function to process the form | |
276 | * | |
277 | * @access public | |
278 | * | |
da6b46f4 EM |
279 | * @param $form |
280 | * @param $params | |
281 | * | |
355ba699 | 282 | * @return void |
6a488035 TO |
283 | */ |
284 | static function endPostProcess(&$form, &$params) { | |
285 | if ($form->_context == 'caseActivity') { | |
286 | return; | |
287 | } | |
288 | ||
289 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); | |
290 | $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients(); | |
291 | ||
292 | if (!$isMultiClient && !$form->_currentlyViewedContactId) { | |
293 | CRM_Core_Error::fatal('Required parameter missing for OpenCase - end post processing'); | |
294 | } | |
295 | ||
296 | if (!$form->_currentUserId || | |
297 | !$params['case_id'] || | |
298 | !$params['case_type'] | |
299 | ) { | |
300 | CRM_Core_Error::fatal('Required parameter missing for OpenCase - end post processing'); | |
301 | } | |
302 | ||
303 | // 1. create case-contact | |
1b2dc6a6 CW |
304 | if ($isMultiClient && $form->_context == 'standalone') { |
305 | foreach ($params['client_id'] as $cliId) { | |
6a488035 | 306 | if (empty($cliId)) { |
1b2dc6a6 | 307 | CRM_Core_Error::fatal('client_id cannot be empty'); |
6a488035 TO |
308 | } |
309 | $contactParams = array( | |
310 | 'case_id' => $params['case_id'], | |
311 | 'contact_id' => $cliId, | |
312 | ); | |
313 | CRM_Case_BAO_Case::addCaseToContact($contactParams); | |
314 | } | |
315 | } | |
316 | else { | |
317 | $contactParams = array( | |
318 | 'case_id' => $params['case_id'], | |
319 | 'contact_id' => $form->_currentlyViewedContactId, | |
320 | ); | |
321 | CRM_Case_BAO_Case::addCaseToContact($contactParams); | |
6a488035 TO |
322 | } |
323 | ||
324 | // 2. initiate xml processor | |
325 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
326 | ||
327 | $xmlProcessorParams = array( | |
1b2dc6a6 | 328 | 'clientID' => $form->_currentlyViewedContactId, |
6a488035 TO |
329 | 'creatorID' => $form->_currentUserId, |
330 | 'standardTimeline' => 1, | |
331 | 'activityTypeName' => 'Open Case', | |
332 | 'caseID' => $params['case_id'], | |
333 | 'subject' => $params['activity_subject'], | |
334 | 'location' => $params['location'], | |
335 | 'activity_date_time' => $params['start_date'], | |
336 | 'duration' => CRM_Utils_Array::value('duration', $params), | |
337 | 'medium_id' => $params['medium_id'], | |
338 | 'details' => $params['activity_details'], | |
339 | ); | |
340 | ||
341 | if (array_key_exists('custom', $params) && is_array($params['custom'])) { | |
342 | $xmlProcessorParams['custom'] = $params['custom']; | |
343 | } | |
344 | ||
345 | // Add parameters for attachments | |
346 | $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments'); | |
347 | for ( $i = 1; $i <= $numAttachments; $i++ ) { | |
348 | $attachName = "attachFile_$i"; | |
349 | if ( isset( $params[$attachName] ) && !empty( $params[$attachName] ) ) { | |
350 | $xmlProcessorParams[$attachName] = $params[$attachName]; | |
351 | } | |
352 | } | |
353 | ||
354 | $xmlProcessor->run($params['case_type'], $xmlProcessorParams); | |
355 | ||
356 | // status msg | |
357 | $params['statusMsg'] = ts('Case opened successfully.'); | |
358 | ||
359 | $buttonName = $form->controller->getButtonName(); | |
360 | $session = CRM_Core_Session::singleton(); | |
361 | if ($buttonName == $form->getButtonName('upload', 'new')) { | |
362 | if ($form->_context == 'standalone') { | |
363 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/case/add', | |
364 | 'reset=1&action=add&context=standalone' | |
365 | )); | |
366 | } | |
367 | else { | |
368 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/case', | |
369 | "reset=1&action=add&context=case&cid={$form->_contactID}" | |
370 | )); | |
371 | } | |
372 | } | |
373 | } | |
374 | } | |
375 |