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