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