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