3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for case activity.
21 class CRM_Case_Form_Case
extends CRM_Core_Form
{
28 public $_context = 'case';
34 public $_caseId = NULL;
40 public $_currentlyViewedContactId = NULL;
46 public $_activityTypeFile = NULL;
49 * Logged in contact Id
52 public $_currentUserId = NULL;
58 public $_activityTypeId = NULL;
64 public $_activityId = NULL;
76 public $_caseTypeId = NULL;
78 public $submitOnce = TRUE;
81 * Explicitly declare the entity api name.
83 public function getDefaultEntity() {
88 * Get the entity id being edited.
92 public function getEntityId() {
93 return $this->_caseId
;
97 * Get the entity subtype ID being edited
103 public function getEntitySubTypeId($subTypeId) {
107 return $this->_caseTypeId
;
111 * Build the form object.
113 public function preProcess() {
114 if (empty($this->_action
)) {
115 $this->_action
= CRM_Core_Action
::ADD
;
118 $this->_caseId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
120 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
122 if ($this->_action
& CRM_Core_Action
::ADD
&& !$this->_currentlyViewedContactId
) {
123 // check for add contacts permissions
124 if (!CRM_Core_Permission
::check('add contacts')) {
125 CRM_Utils_System
::permissionDenied();
131 if (!CRM_Core_Permission
::checkActionPermission('CiviCase', $this->_action
)) {
132 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this page.'));
135 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
139 if (!$this->_caseId
) {
141 'case_type_id' => ts('Case Type'),
142 'status_id' => ts('Case Status'),
143 'medium_id' => ts('Activity Medium'),
146 foreach ($caseAttributes as $key => $label) {
147 if (!CRM_Case_BAO_Case
::buildOptions($key, 'create')) {
148 CRM_Core_Error
::statusBounce(ts('You do not have any active %1', [1 => $label]));
153 if ($this->_action
& CRM_Core_Action
::ADD
) {
154 $this->_activityTypeId
= CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case');
155 if (!$this->_activityTypeId
) {
156 CRM_Core_Error
::statusBounce(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.'));
160 //check for case permissions.
161 if (!CRM_Case_BAO_Case
::accessCiviCase()) {
162 CRM_Core_Error
::statusBounce(ts('You are not authorized to access this page.'));
164 if (($this->_action
& CRM_Core_Action
::ADD
) &&
165 (!CRM_Core_Permission
::check('access all cases and activities') &&
166 !CRM_Core_Permission
::check('add cases')
169 CRM_Core_Error
::statusBounce(ts('You are not authorized to access this page.'));
172 if ($this->_activityTypeFile
= CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
,
176 $this->assign('activityTypeFile', $this->_activityTypeFile
);
179 $details = CRM_Case_PseudoConstant
::caseActivityType(FALSE);
181 CRM_Utils_System
::setTitle($details[$this->_activityTypeId
]['label']);
182 $this->assign('activityType', $details[$this->_activityTypeId
]['label']);
183 $this->assign('activityTypeDescription', $details[$this->_activityTypeId
]['description']);
185 if (isset($this->_currentlyViewedContactId
)) {
186 $contact = new CRM_Contact_DAO_Contact();
187 $contact->id
= $this->_currentlyViewedContactId
;
188 if (!$contact->find(TRUE)) {
189 CRM_Core_Error
::statusBounce(ts('Client contact does not exist: %1', [1 => $this->_currentlyViewedContactId
]));
191 $this->assign('clientName', $contact->display_name
);
194 $session = CRM_Core_Session
::singleton();
195 $this->_currentUserId
= $session->get('userID');
197 //Add activity custom data is included in this page
198 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_activityTypeId
, 1, 'Activity');
199 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
200 $className::preProcess($this);
201 $activityGroupTree = $this->_groupTree
;
203 // Add case custom data to form
204 $caseTypeId = CRM_Utils_Array
::value('case_type_id', CRM_Utils_Request
::exportValues(), $this->_caseTypeId
);
205 CRM_Custom_Form_CustomData
::addToForm($this, $caseTypeId);
207 // so that grouptree is not populated with case fields, since the grouptree is used
208 // for populating activity custom fields.
209 $this->_groupTree
= $activityGroupTree;
213 * Set default values for the form.
215 public function setDefaultValues() {
216 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
219 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
220 $defaults = $className::setDefaultValues($this);
221 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData
::setDefaultValues($this));
225 public function buildQuickForm() {
226 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
227 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
228 $this->assign('multiClient', $isMultiClient);
230 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
231 $title = ts('Delete');
232 if ($this->_action
& CRM_Core_Action
::RENEW
) {
233 $title = ts('Restore');
239 'spacing' => ' ',
244 'name' => ts('Cancel'),
250 // Add the activity custom data to the form
251 CRM_Custom_Form_CustomData
::buildQuickForm($this);
253 // we don't want to show button on top of custom form
254 $this->assign('noPreCustomButton', TRUE);
256 $s = CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'subject');
260 $this->add('text', 'activity_subject', ts('Subject'),
262 'maxlength' => '128',
266 $tags = CRM_Core_BAO_Tag
::getColorTags('civicrm_case');
269 $this->add('select2', 'tag', ts('Tags'), $tags, FALSE,
270 ['class' => 'huge', 'multiple' => 'multiple']
275 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
276 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
281 'name' => ts('Save'),
282 'spacing' => ' ',
287 'name' => ts('Cancel'),
291 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
292 $className::buildQuickForm($this);
296 * Add local and global form rules.
300 public function addRules() {
301 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
304 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
305 $this->addFormRule([$className, 'formRule'], $this);
306 $this->addFormRule(['CRM_Case_Form_Case', 'formRule'], $this);
310 * Global validation rules for the form.
312 * @param array $values
313 * Posted values of the form.
316 * @param CRM_Core_Form $form
319 * list of errors to be posted back to the form
321 public static function formRule($values, $files, $form) {
326 * Wrapper for unit testing the post process submit function.
329 * @param $activityTypeFile
332 * @return CRM_Case_BAO_Case
334 public function testSubmit($params, $activityTypeFile, $contactId, $context = "case") {
335 $this->controller
= new CRM_Core_Controller();
337 $this->_activityTypeFile
= $activityTypeFile;
338 $this->_currentUserId
= $contactId;
339 $this->_context
= $context;
341 return $this->submit($params);
345 * Submit the form with given params.
349 public function submit(&$params) {
350 $params['now'] = date("Ymd");
352 // 1. call begin post process
353 if ($this->_activityTypeFile
) {
354 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
355 $className::beginPostProcess($this, $params);
358 if (!empty($params['hidden_custom']) &&
359 !isset($params['custom'])
361 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess(
368 // 2. create/edit case
369 if (!empty($params['case_type_id'])) {
370 $params['case_type'] = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $params['case_type_id'], 'name', 'id');
371 $params['subject'] = $params['activity_subject'];
373 $caseObj = CRM_Case_BAO_Case
::create($params);
374 $this->_caseId
= $params['case_id'] = $caseObj->id
;
375 // unset any ids, custom data
376 unset($params['id'], $params['custom']);
378 // add tags if exists
380 if (!empty($params['tag'])) {
382 if (!is_array($params['tag'])) {
383 $params['tag'] = explode(',', $params['tag']);
385 foreach ($params['tag'] as $tag) {
386 $tagParams[$tag] = 1;
389 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_case', $caseObj->id
);
392 if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
393 CRM_Core_Form_Tag
::postProcess($params['case_taglist'], $caseObj->id
, 'civicrm_case', $this);
397 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
398 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}"
400 CRM_Core_Session
::singleton()->pushUserContext($url);
402 // 3. format activity custom data
403 if (!empty($params['hidden_custom'])) {
404 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE, $this->_activityTypeId
);
405 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
406 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
410 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
416 // 4. call end post process
417 if ($this->_activityTypeFile
) {
418 $className::endPostProcess($this, $params);
425 * Process the form submission.
427 public function postProcess() {
428 $transaction = new CRM_Core_Transaction();
430 // check if dedupe button, if so return.
431 $buttonName = $this->controller
->getButtonName();
432 if (isset($this->_dedupeButtonName
) && $buttonName == $this->_dedupeButtonName
) {
436 if ($this->_action
& CRM_Core_Action
::DELETE
) {
437 $caseDelete = CRM_Case_BAO_Case
::deleteCase($this->_caseId
, TRUE);
439 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');
444 if ($this->_action
& CRM_Core_Action
::RENEW
) {
445 $caseRestore = CRM_Case_BAO_Case
::restoreCase($this->_caseId
);
447 CRM_Core_Session
::setStatus(ts('The selected case has been restored.'), ts('Restored'), 'success');
451 // store the submitted values in an array
452 $params = $this->controller
->exportValues($this->_name
);
453 $this->submit($params);
455 CRM_Core_Session
::setStatus($params['statusMsg'], ts('Saved'), 'success');