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