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