CRM-16990: Custom fields are NOT being saved for some component custom data (activiti...
[civicrm-core.git] / CRM / Case / Form / Case.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 case activity.
36 */
37 class CRM_Case_Form_Case extends CRM_Core_Form {
38
39 /**
40 * The context
41 *
42 * @var string
43 */
44 public $_context = 'case';
45
46 /**
47 * Case Id
48 */
49 public $_caseId = NULL;
50
51 /**
52 * Client Id
53 */
54 public $_currentlyViewedContactId = NULL;
55
56 /**
57 * Activity Type File
58 */
59 public $_activityTypeFile = NULL;
60
61 /**
62 * Logged in contact Id
63 */
64 public $_currentUserId = NULL;
65
66 /**
67 * Activity type Id
68 */
69 public $_activityTypeId = NULL;
70
71 /**
72 * Activity type Id
73 */
74 public $_activityId = NULL;
75
76 /**
77 * Action
78 */
79 public $_action;
80
81 /**
82 * Case type id
83 */
84 public $_caseTypeId = NULL;
85
86 /**
87 * Build the form object.
88 */
89 public function preProcess() {
90
91 $this->_caseId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
92
93 $this->_currentlyViewedContactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
94
95 if ($this->_action & CRM_Core_Action::ADD && !$this->_currentlyViewedContactId) {
96 // check for add contacts permissions
97 if (!CRM_Core_Permission::check('add contacts')) {
98 CRM_Utils_System::permissionDenied();
99 return;
100 }
101 }
102
103 //CRM-4418
104 if (!CRM_Core_Permission::checkActionPermission('CiviCase', $this->_action)) {
105 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
106 }
107
108 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) {
109 return TRUE;
110 }
111
112 if (!$this->_caseId) {
113 $caseAttributes = array(
114 'case_type' => CRM_Case_PseudoConstant::caseType(),
115 'case_status' => CRM_Case_PseudoConstant::caseStatus(),
116 'encounter_medium' => CRM_Case_PseudoConstant::encounterMedium(),
117 );
118
119 foreach ($caseAttributes as $key => $values) {
120 if (empty($values)) {
121 CRM_Core_Error::fatal(ts('You do not have any active %1',
122 array(1 => str_replace('_', ' ', $key))
123 ));
124 break;
125 }
126 }
127 }
128
129 if ($this->_action & CRM_Core_Action::ADD) {
130 $this->_activityTypeId = CRM_Core_OptionGroup::getValue('activity_type',
131 'Open Case',
132 'name'
133 );
134 if (!$this->_activityTypeId) {
135 CRM_Core_Error::fatal(ts('The Open Case activity type is missing or disabled. Please have your site administrator check Administer > Option Lists > Activity Types for the CiviCase component.'));
136 }
137 }
138
139 //check for case permissions.
140 if (!CRM_Case_BAO_Case::accessCiviCase()) {
141 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
142 }
143 if (($this->_action & CRM_Core_Action::ADD) &&
144 (!CRM_Core_Permission::check('access all cases and activities') &&
145 !CRM_Core_Permission::check('add cases')
146 )
147 ) {
148 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
149 }
150
151 if ($this->_activityTypeFile = CRM_Activity_BAO_Activity::getFileForActivityTypeId($this->_activityTypeId,
152 'Case'
153 )
154 ) {
155 $this->assign('activityTypeFile', $this->_activityTypeFile);
156 }
157
158 $details = CRM_Case_PseudoConstant::caseActivityType(FALSE);
159
160 CRM_Utils_System::setTitle($details[$this->_activityTypeId]['label']);
161 $this->assign('activityType', $details[$this->_activityTypeId]['label']);
162 $this->assign('activityTypeDescription', $details[$this->_activityTypeId]['description']);
163
164 if (isset($this->_currentlyViewedContactId)) {
165 $contact = new CRM_Contact_DAO_Contact();
166 $contact->id = $this->_currentlyViewedContactId;
167 if (!$contact->find(TRUE)) {
168 CRM_Core_Error::statusBounce(ts('Client contact does not exist: %1', array(1 => $this->_currentlyViewedContactId)));
169 }
170 $this->assign('clientName', $contact->display_name);
171 }
172
173 $session = CRM_Core_Session::singleton();
174 $this->_currentUserId = $session->get('userID');
175
176 //when custom data is included in this page
177 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_activityTypeId, 1, 'Activity');
178 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
179 $className::preProcess($this);
180 $activityGroupTree = $this->_groupTree;
181
182 if (!$this->_caseTypeId) {
183 $params = CRM_Utils_Request::exportValues();
184 $this->_caseTypeId = $params['case_type_id'];
185 }
186 // for case custom fields to populate with defaults
187 if (!empty($_POST['hidden_custom'])) {
188 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_caseTypeId, 1, 'Case', $this->_caseId);
189 CRM_Custom_Form_CustomData::buildQuickForm($this);
190 }
191
192 // so that grouptree is not populated with case fields, since the grouptree is used
193 // for populating activity custom fields.
194 $this->_groupTree = $activityGroupTree;
195 }
196
197 /**
198 * Set default values for the form.
199 */
200 public function setDefaultValues() {
201 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) {
202 return TRUE;
203 }
204 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
205 $defaults = $className::setDefaultValues($this);
206 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData::setDefaultValues($this));
207 return $defaults;
208 }
209
210 public function buildQuickForm() {
211 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
212 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
213 $this->assign('multiClient', $isMultiClient);
214
215 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) {
216 $title = 'Delete';
217 if ($this->_action & CRM_Core_Action::RENEW) {
218 $title = 'Restore';
219 }
220 $this->addButtons(array(
221 array(
222 'type' => 'next',
223 'name' => $title,
224 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
225 'isDefault' => TRUE,
226 ),
227 array(
228 'type' => 'cancel',
229 'name' => ts('Cancel'),
230 ),
231 )
232 );
233 return;
234 }
235
236 //need to assign custom data type and subtype to the template
237 $this->assign('customDataType', 'Case');
238
239 CRM_Custom_Form_CustomData::buildQuickForm($this);
240 // we don't want to show button on top of custom form
241 $this->assign('noPreCustomButton', TRUE);
242
243 $s = CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'subject');
244 if (!is_array($s)) {
245 $s = array();
246 }
247 $this->add('text', 'activity_subject', ts('Subject'),
248 array_merge($s, array(
249 'maxlength' => '128',
250 )), TRUE
251 );
252
253 CRM_Core_BAO_Tag::getTags('civicrm_case', $tags, NULL,
254 '&nbsp;&nbsp;', TRUE);
255
256 if (!empty($tags)) {
257 $this->add('select', 'tag', ts('Select Tags'), $tags, FALSE,
258 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
259 );
260 }
261
262 // build tag widget
263 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_case');
264 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
265
266 $this->addButtons(array(
267 array(
268 'type' => 'next',
269 'name' => ts('Save'),
270 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
271 'isDefault' => TRUE,
272 ),
273 array(
274 'type' => 'cancel',
275 'name' => ts('Cancel'),
276 ),
277 )
278 );
279
280 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
281 $className::buildQuickForm($this);
282 }
283
284 /**
285 * Add local and global form rules.
286 *
287 * @return bool
288 */
289 public function addRules() {
290 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) {
291 return TRUE;
292 }
293 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
294 $this->addFormRule(array($className, 'formRule'), $this);
295 $this->addFormRule(array('CRM_Case_Form_Case', 'formRule'), $this);
296 }
297
298 /**
299 * Global validation rules for the form.
300 *
301 * @param array $values
302 * Posted values of the form.
303 *
304 * @param $files
305 * @param CRM_Core_Form $form
306 *
307 * @return array
308 * list of errors to be posted back to the form
309 */
310 public static function formRule($values, $files, $form) {
311 return TRUE;
312 }
313
314 /**
315 * Process the form submission.
316 */
317 public function postProcess() {
318 $transaction = new CRM_Core_Transaction();
319
320 // check if dedupe button, if so return.
321 $buttonName = $this->controller->getButtonName();
322 if (isset($this->_dedupeButtonName) && $buttonName == $this->_dedupeButtonName) {
323 return;
324 }
325
326 if ($this->_action & CRM_Core_Action::DELETE) {
327 $statusMsg = NULL;
328 $caseDelete = CRM_Case_BAO_Case::deleteCase($this->_caseId, TRUE);
329 if ($caseDelete) {
330 $statusMsg = ts('The selected case has been moved to the Trash. You can view and / or restore deleted cases by checking the "Deleted Cases" option under Find Cases.<br />');
331 }
332 CRM_Core_Session::setStatus($statusMsg, ts('Case Deleted'), 'success');
333 return;
334 }
335
336 if ($this->_action & CRM_Core_Action::RENEW) {
337 $statusMsg = NULL;
338 $caseRestore = CRM_Case_BAO_Case::restoreCase($this->_caseId);
339 if ($caseRestore) {
340 $statusMsg = ts('The selected case has been restored.<br />');
341 }
342 CRM_Core_Session::setStatus($statusMsg, ts('Restored'), 'success');
343 return;
344 }
345 // store the submitted values in an array
346 $params = $this->controller->exportValues($this->_name);
347 $params['now'] = date("Ymd");
348
349 // 1. call begin post process
350 if ($this->_activityTypeFile) {
351 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
352 $className::beginPostProcess($this, $params);
353 }
354
355 if (!empty($params['hidden_custom']) &&
356 !isset($params['custom'])
357 ) {
358 $customFields = array();
359 $params['custom'] = CRM_Core_BAO_CustomField::postProcess(
360 $params,
361 NULL,
362 'Case'
363 );
364 }
365
366 // 2. create/edit case
367 if (!empty($params['case_type_id'])) {
368 $params['case_type'] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $params['case_type_id'], 'name', 'id');
369 $params['subject'] = $params['activity_subject'];
370 }
371 $caseObj = CRM_Case_BAO_Case::create($params);
372 $params['case_id'] = $caseObj->id;
373 // unset any ids, custom data
374 unset($params['id'], $params['custom']);
375
376 // add tags if exists
377 $tagParams = array();
378 if (!empty($params['tag'])) {
379 $tagParams = array();
380 foreach ($params['tag'] as $tag) {
381 $tagParams[$tag] = 1;
382 }
383 }
384 CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_case', $caseObj->id);
385
386 //save free tags
387 if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
388 CRM_Core_Form_Tag::postProcess($params['case_taglist'], $caseObj->id, 'civicrm_case', $this);
389 }
390
391 // user context
392 $url = CRM_Utils_System::url('civicrm/contact/view/case',
393 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}"
394 );
395 $session = CRM_Core_Session::singleton();
396 $session->pushUserContext($url);
397
398 // 3. format activity custom data
399 if (!empty($params['hidden_custom'])) {
400 $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId);
401 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
402 CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE,
403 NULL, NULL, TRUE
404 )
405 );
406 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
407 $this->_activityId,
408 'Activity'
409 );
410 }
411
412 // 4. call end post process
413 if ($this->_activityTypeFile) {
414 $className::endPostProcess($this, $params);
415 }
416
417 // 5. auto populate activities
418
419 // 6. set status
420 CRM_Core_Session::setStatus($params['statusMsg'], ts('Saved'), 'success');
421 }
422
423 }