Cleanup deprecated CRM_Core_BAO_Settings calls CRM-17507
[civicrm-core.git] / CRM / Case / Form / Activity / OpenCase.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33
34 /**
35 * This class generates form components for OpenCase Activity.
36 */
37 class CRM_Case_Form_Activity_OpenCase {
38
39 /**
40 * The id of the client associated with this case.
41 *
42 * @var int
43 */
44 public $_contactID;
45
46 /**
47 * @param CRM_Core_Form $form
48 */
49 public static function preProcess(&$form) {
50 //get multi client case configuration
51 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
52 $form->_allowMultiClient = (bool) $xmlProcessorProcess->getAllowMultipleCaseClients();
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 );
60 $caseId = CRM_Utils_Array::first($form->_caseId);
61 $form->assign('changeStartURL', CRM_Utils_System::url('civicrm/case/activity',
62 "action=add&reset=1&cid=$contactID&caseid={$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
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
77 // check if the case status id passed in url is a valid one
78 $caseStatusId = CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form);
79 $caseStatus = CRM_Case_PseudoConstant::caseStatus();
80 $form->_caseStatusId = array_key_exists($caseStatusId, $caseStatus) ? $caseStatusId : NULL;
81
82 // Add attachments
83 CRM_Core_BAO_File::buildAttachment($form, 'civicrm_activity', $form->_activityId);
84 $session = CRM_Core_Session::singleton();
85 $session->pushUserContext(CRM_Utils_System::url('civicrm/case', 'reset=1'));
86 }
87
88 /**
89 * Set default values for the form. For edit/view mode
90 * the default values are retrieved from the database
91 *
92 *
93 * @param CRM_Core_Form $form
94 */
95 public static function setDefaultValues(&$form) {
96 $defaults = array();
97 if ($form->_context == 'caseActivity') {
98 return $defaults;
99 }
100
101 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
102
103 // set default case status, case type, encounter medium, location type and phone type defaults are set in DB
104 if ($form->_caseStatusId) {
105 $caseStatus = $form->_caseStatusId;
106 }
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;
114
115 // set default case type passed in url
116 if ($form->_caseTypeId) {
117 $caseType = $form->_caseTypeId;
118 $defaults['case_type_id'] = $caseType;
119 }
120
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
139 /**
140 * @param CRM_Case_Form_Case $form
141 */
142 public static function buildQuickForm(&$form) {
143 if ($form->_context == 'caseActivity') {
144 return;
145 }
146 if ($form->_context == 'standalone') {
147 $form->addEntityRef('client_id', ts('Client'), array(
148 'create' => TRUE,
149 'multiple' => $form->_allowMultiClient,
150 ), TRUE);
151 }
152
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);")
157 );
158
159 if ($form->_caseTypeId) {
160 $element->freeze();
161 }
162
163 $csElement = $form->add('select', 'status_id', ts('Case Status'),
164 CRM_Case_PseudoConstant::caseStatus(),
165 FALSE
166 );
167
168 if ($form->_caseStatusId) {
169 $csElement->freeze();
170 }
171
172 $form->add('text', 'duration', ts('Activity Duration'), array('size' => 4, 'maxlength' => 8));
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
182 $form->addSelect('medium_id', array('entity' => 'activity'), TRUE);
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
187 $form->add('wysiwyg', 'activity_details', ts('Details'), array('rows' => 4, 'cols' => 60), FALSE);
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 /**
209 * Process the form submission.
210 *
211 *
212 * @param CRM_Core_Form $form
213 * @param array $params
214 */
215 public static function beginPostProcess(&$form, &$params) {
216 if ($form->_context == 'caseActivity') {
217 return;
218 }
219
220 if ($form->_context == 'standalone') {
221 $params['client_id'] = explode(',', $params['client_id']);
222 $form->_currentlyViewedContactId = $params['client_id'][0];
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 /**
247 * Global validation rules for the form.
248 *
249 * @param $fields
250 * @param $files
251 * @param CRM_Core_Form $form
252 *
253 * @return array
254 * list of errors to be posted back to the form
255 */
256 public static function formRule($fields, $files, $form) {
257 if ($form->_context == 'caseActivity') {
258 return TRUE;
259 }
260
261 $errors = array();
262 return $errors;
263 }
264
265 /**
266 * Process the form submission.
267 *
268 * @param CRM_Core_Form $form
269 * @param array $params
270 */
271 public static function endPostProcess(&$form, &$params) {
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
291 if ($isMultiClient && $form->_context == 'standalone') {
292 foreach ($params['client_id'] as $cliId) {
293 if (empty($cliId)) {
294 CRM_Core_Error::fatal('client_id cannot be empty');
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);
309 }
310
311 // 2. initiate xml processor
312 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
313
314 $xmlProcessorParams = array(
315 'clientID' => $form->_currentlyViewedContactId,
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 = Civi::settings()->get('max_attachments');
334 for ($i = 1; $i <= $numAttachments; $i++) {
335 $attachName = "attachFile_$i";
336 if (isset($params[$attachName]) && !empty($params[$attachName])) {
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',
351 'reset=1&action=add&context=standalone'
352 ));
353 }
354 else {
355 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/case',
356 "reset=1&action=add&context=case&cid={$form->_contactID}"
357 ));
358 }
359 }
360 }
361
362 }