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