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