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