Merge pull request #11396 from mukeshcompucorp/fix-page-structure
[civicrm-core.git] / CRM / Case / Form / Activity / OpenCase.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
df371444 35 * This class generates form components for OpenCase Activity.
6a488035
TO
36 */
37class 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);
7a5c0c6c 74 $caseTypes = CRM_Case_BAO_Case::buildOptions('case_type_id', 'create');
74fb6e99
PJ
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) {
7ef0d72c
CW
117 $defaults['case_type_id'] = $form->_caseTypeId;
118 }
119 else {
120 // TODO: Not possible yet to set a default case type in the system
121 // For now just add the convenience of auto-selecting if there is only one option
122 $caseTypes = CRM_Case_BAO_Case::buildOptions('case_type_id', 'create');
123 if (count($caseTypes) == 1) {
124 reset($caseTypes);
125 $defaults['case_type_id'] = key($caseTypes);
126 }
74fb6e99 127 }
74fb6e99 128
6a488035
TO
129 $medium = CRM_Core_OptionGroup::values('encounter_medium', FALSE, FALSE, FALSE, 'AND is_default = 1');
130 if (count($medium) == 1) {
131 $defaults['medium_id'] = key($medium);
132 }
133
134 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
135 if ($defaultLocationType->id) {
136 $defaults['location[1][location_type_id]'] = $defaultLocationType->id;
137 }
138
139 $phoneType = CRM_Core_OptionGroup::values('phone_type', FALSE, FALSE, FALSE, 'AND is_default = 1');
140 if (count($phoneType) == 1) {
141 $defaults['location[1][phone][1][phone_type_id]'] = key($phoneType);
142 }
143
144 return $defaults;
145 }
146
1b2dc6a6
CW
147 /**
148 * @param CRM_Case_Form_Case $form
149 */
00be9182 150 public static function buildQuickForm(&$form) {
6a488035
TO
151 if ($form->_context == 'caseActivity') {
152 return;
153 }
154 if ($form->_context == 'standalone') {
353ffa53
TO
155 $form->addEntityRef('client_id', ts('Client'), array(
156 'create' => TRUE,
af9b09df 157 'multiple' => $form->_allowMultiClient,
353ffa53 158 ), TRUE);
6a488035
TO
159 }
160
7a5c0c6c
CW
161 $element = $form->addField('case_type_id', array(
162 'context' => 'create',
163 'entity' => 'Case',
164 'onchange' => "CRM.buildCustomData('Case', this.value);",
165 ), TRUE);
74fb6e99
PJ
166 if ($form->_caseTypeId) {
167 $element->freeze();
168 }
169
7a5c0c6c
CW
170 $csElement = $form->addField('status_id', array(
171 'context' => 'create',
172 'entity' => 'Case',
173 ), TRUE);
4d91aee1
P
174 if ($form->_caseStatusId) {
175 $csElement->freeze();
176 }
6a488035 177
1b2dc6a6 178 $form->add('text', 'duration', ts('Activity Duration'), array('size' => 4, 'maxlength' => 8));
6a488035
TO
179 $form->addRule('duration', ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger');
180
181 if ($form->_currentlyViewedContactId) {
182 list($displayName) = CRM_Contact_BAO_Contact::getDisplayAndImage($form->_currentlyViewedContactId);
183 $form->assign('clientName', $displayName);
184 }
185
186 $form->addDate('start_date', ts('Case Start Date'), TRUE, array('formatType' => 'activityDateTime'));
187
7a5c0c6c 188 $form->addField('medium_id', array('entity' => 'activity', 'context' => 'create'), TRUE);
6a488035
TO
189
190 // calling this field activity_location to prevent conflict with contact location fields
191 $form->add('text', 'activity_location', ts('Location'), CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'location'));
192
5d51a2f9 193 $form->add('wysiwyg', 'activity_details', ts('Details'), array('rows' => 4, 'cols' => 60), FALSE);
6a488035
TO
194
195 $form->addButtons(array(
196 array(
197 'type' => 'upload',
198 'name' => ts('Save'),
199 'isDefault' => TRUE,
200 ),
201 array(
202 'type' => 'upload',
203 'name' => ts('Save and New'),
204 'subName' => 'new',
205 ),
206 array(
207 'type' => 'cancel',
208 'name' => ts('Cancel'),
209 ),
210 )
211 );
212 }
213
214 /**
fe482240 215 * Process the form submission.
6a488035 216 *
6a488035 217 *
c490a46a
CW
218 * @param CRM_Core_Form $form
219 * @param array $params
6a488035 220 */
00be9182 221 public static function beginPostProcess(&$form, &$params) {
6a488035
TO
222 if ($form->_context == 'caseActivity') {
223 return;
224 }
225
1b2dc6a6
CW
226 if ($form->_context == 'standalone') {
227 $params['client_id'] = explode(',', $params['client_id']);
228 $form->_currentlyViewedContactId = $params['client_id'][0];
6a488035
TO
229 }
230
231 // for open case start date should be set to current date
232 $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time']);
6a488035
TO
233
234 // rename activity_location param to the correct column name for activity DAO
235 $params['location'] = CRM_Utils_Array::value('activity_location', $params);
236
237 // Add attachments
238 CRM_Core_BAO_File::formatAttachment(
239 $params,
240 $params,
241 'civicrm_activity',
242 $form->_activityId
243 );
244
245 }
246
247 /**
fe482240 248 * Global validation rules for the form.
6a488035 249 *
fd31fa4c
EM
250 * @param $fields
251 * @param $files
c490a46a 252 * @param CRM_Core_Form $form
6a488035 253 *
a6c01b45
CW
254 * @return array
255 * list of errors to be posted back to the form
6a488035 256 */
00be9182 257 public static function formRule($fields, $files, $form) {
6a488035
TO
258 if ($form->_context == 'caseActivity') {
259 return TRUE;
260 }
261
262 $errors = array();
6a488035
TO
263 return $errors;
264 }
265
266 /**
fe482240 267 * Process the form submission.
6a488035 268 *
df371444 269 * @param CRM_Core_Form $form
c490a46a 270 * @param array $params
6a488035 271 */
00be9182 272 public static function endPostProcess(&$form, &$params) {
6a488035
TO
273 if ($form->_context == 'caseActivity') {
274 return;
275 }
276
277 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
278 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
279
280 if (!$isMultiClient && !$form->_currentlyViewedContactId) {
281 CRM_Core_Error::fatal('Required parameter missing for OpenCase - end post processing');
282 }
283
284 if (!$form->_currentUserId ||
285 !$params['case_id'] ||
286 !$params['case_type']
287 ) {
288 CRM_Core_Error::fatal('Required parameter missing for OpenCase - end post processing');
289 }
290
291 // 1. create case-contact
1b2dc6a6
CW
292 if ($isMultiClient && $form->_context == 'standalone') {
293 foreach ($params['client_id'] as $cliId) {
6a488035 294 if (empty($cliId)) {
1b2dc6a6 295 CRM_Core_Error::fatal('client_id cannot be empty');
6a488035
TO
296 }
297 $contactParams = array(
298 'case_id' => $params['case_id'],
299 'contact_id' => $cliId,
300 );
ff9340a4 301 CRM_Case_BAO_CaseContact::create($contactParams);
6a488035
TO
302 }
303 }
304 else {
305 $contactParams = array(
306 'case_id' => $params['case_id'],
307 'contact_id' => $form->_currentlyViewedContactId,
308 );
ff9340a4 309 CRM_Case_BAO_CaseContact::create($contactParams);
6a488035
TO
310 }
311
312 // 2. initiate xml processor
313 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
314
315 $xmlProcessorParams = array(
1b2dc6a6 316 'clientID' => $form->_currentlyViewedContactId,
6a488035
TO
317 'creatorID' => $form->_currentUserId,
318 'standardTimeline' => 1,
319 'activityTypeName' => 'Open Case',
320 'caseID' => $params['case_id'],
321 'subject' => $params['activity_subject'],
322 'location' => $params['location'],
323 'activity_date_time' => $params['start_date'],
324 'duration' => CRM_Utils_Array::value('duration', $params),
325 'medium_id' => $params['medium_id'],
326 'details' => $params['activity_details'],
3717c347 327 'relationship_end_date' => CRM_Utils_Array::value('end_date', $params),
6a488035
TO
328 );
329
330 if (array_key_exists('custom', $params) && is_array($params['custom'])) {
331 $xmlProcessorParams['custom'] = $params['custom'];
332 }
333
334 // Add parameters for attachments
aaffa79f 335 $numAttachments = Civi::settings()->get('max_attachments');
481a74f4 336 for ($i = 1; $i <= $numAttachments; $i++) {
6a488035 337 $attachName = "attachFile_$i";
481a74f4 338 if (isset($params[$attachName]) && !empty($params[$attachName])) {
6a488035
TO
339 $xmlProcessorParams[$attachName] = $params[$attachName];
340 }
341 }
342
343 $xmlProcessor->run($params['case_type'], $xmlProcessorParams);
344
345 // status msg
346 $params['statusMsg'] = ts('Case opened successfully.');
347
348 $buttonName = $form->controller->getButtonName();
349 $session = CRM_Core_Session::singleton();
350 if ($buttonName == $form->getButtonName('upload', 'new')) {
351 if ($form->_context == 'standalone') {
352 $session->replaceUserContext(CRM_Utils_System::url('civicrm/case/add',
353ffa53
TO
353 'reset=1&action=add&context=standalone'
354 ));
6a488035
TO
355 }
356 else {
357 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/case',
353ffa53
TO
358 "reset=1&action=add&context=case&cid={$form->_contactID}"
359 ));
6a488035
TO
360 }
361 }
362 }
96025800 363
6a488035 364}