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