3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class generates form components for case activity.
37 class CRM_Case_Form_Case
extends CRM_Core_Form
{
44 public $_context = 'case';
50 public $_caseId = NULL;
56 public $_currentlyViewedContactId = NULL;
62 public $_activityTypeFile = NULL;
65 * Logged in contact Id
68 public $_currentUserId = NULL;
74 public $_activityTypeId = NULL;
80 public $_activityId = NULL;
92 public $_caseTypeId = NULL;
94 public $submitOnce = TRUE;
97 * Explicitly declare the entity api name.
99 public function getDefaultEntity() {
104 * Get the entity id being edited.
108 public function getEntityId() {
109 return $this->_caseId
;
113 * Get the entity subtype ID being edited
119 public function getEntitySubTypeId($subTypeId) {
123 return $this->_caseTypeId
;
127 * Build the form object.
129 public function preProcess() {
130 if (empty($this->_action
)) {
131 $this->_action
= CRM_Core_Action
::ADD
;
134 $this->_caseId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
136 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
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();
147 if (!CRM_Core_Permission
::checkActionPermission('CiviCase', $this->_action
)) {
148 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
151 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
155 if (!$this->_caseId
) {
157 'case_type_id' => ts('Case Type'),
158 'status_id' => ts('Case Status'),
159 'medium_id' => ts('Activity Medium'),
162 foreach ($caseAttributes as $key => $label) {
163 if (!CRM_Case_BAO_Case
::buildOptions($key, 'create')) {
164 CRM_Core_Error
::fatal(ts('You do not have any active %1', [1 => $label]));
169 if ($this->_action
& CRM_Core_Action
::ADD
) {
170 $this->_activityTypeId
= CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case');
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.'));
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.'));
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')
185 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
188 if ($this->_activityTypeFile
= CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
,
192 $this->assign('activityTypeFile', $this->_activityTypeFile
);
195 $details = CRM_Case_PseudoConstant
::caseActivityType(FALSE);
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']);
201 if (isset($this->_currentlyViewedContactId
)) {
202 $contact = new CRM_Contact_DAO_Contact();
203 $contact->id
= $this->_currentlyViewedContactId
;
204 if (!$contact->find(TRUE)) {
205 CRM_Core_Error
::statusBounce(ts('Client contact does not exist: %1', [1 => $this->_currentlyViewedContactId
]));
207 $this->assign('clientName', $contact->display_name
);
210 $session = CRM_Core_Session
::singleton();
211 $this->_currentUserId
= $session->get('userID');
213 //Add activity custom data is included in this page
214 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_activityTypeId
, 1, 'Activity');
215 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
216 $className::preProcess($this);
217 $activityGroupTree = $this->_groupTree
;
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);
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;
229 * Set default values for the form.
231 public function setDefaultValues() {
232 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
235 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
236 $defaults = $className::setDefaultValues($this);
237 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData
::setDefaultValues($this));
241 public function buildQuickForm() {
242 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
243 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
244 $this->assign('multiClient', $isMultiClient);
246 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
247 $title = ts('Delete');
248 if ($this->_action
& CRM_Core_Action
::RENEW
) {
249 $title = ts('Restore');
255 'spacing' => ' ',
260 'name' => ts('Cancel'),
266 // Add the activity custom data to the form
267 CRM_Custom_Form_CustomData
::buildQuickForm($this);
269 // we don't want to show button on top of custom form
270 $this->assign('noPreCustomButton', TRUE);
272 $s = CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'subject');
276 $this->add('text', 'activity_subject', ts('Subject'),
278 'maxlength' => '128',
282 $tags = CRM_Core_BAO_Tag
::getColorTags('civicrm_case');
285 $this->add('select2', 'tag', ts('Tags'), $tags, FALSE,
286 ['class' => 'huge', 'multiple' => 'multiple']
291 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
292 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
297 'name' => ts('Save'),
298 'spacing' => ' ',
303 'name' => ts('Cancel'),
307 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
308 $className::buildQuickForm($this);
312 * Add local and global form rules.
316 public function addRules() {
317 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
320 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
321 $this->addFormRule([$className, 'formRule'], $this);
322 $this->addFormRule(['CRM_Case_Form_Case', 'formRule'], $this);
326 * Global validation rules for the form.
328 * @param array $values
329 * Posted values of the form.
332 * @param CRM_Core_Form $form
335 * list of errors to be posted back to the form
337 public static function formRule($values, $files, $form) {
342 * Wrapper for unit testing the post process submit function.
345 * @param $activityTypeFile
348 * @return CRM_Case_BAO_Case
350 public function testSubmit($params, $activityTypeFile, $contactId, $context = "case") {
351 $this->controller
= new CRM_Core_Controller();
353 $this->_activityTypeFile
= $activityTypeFile;
354 $this->_currentUserId
= $contactId;
355 $this->_context
= $context;
357 return $this->submit($params);
361 * Submit the form with given params.
365 public function submit(&$params) {
366 $params['now'] = date("Ymd");
368 // 1. call begin post process
369 if ($this->_activityTypeFile
) {
370 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
371 $className::beginPostProcess($this, $params);
374 if (!empty($params['hidden_custom']) &&
375 !isset($params['custom'])
377 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess(
384 // 2. create/edit case
385 if (!empty($params['case_type_id'])) {
386 $params['case_type'] = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $params['case_type_id'], 'name', 'id');
387 $params['subject'] = $params['activity_subject'];
389 $caseObj = CRM_Case_BAO_Case
::create($params);
390 $this->_caseId
= $params['case_id'] = $caseObj->id
;
391 // unset any ids, custom data
392 unset($params['id'], $params['custom']);
394 // add tags if exists
396 if (!empty($params['tag'])) {
398 if (!is_array($params['tag'])) {
399 $params['tag'] = explode(',', $params['tag']);
401 foreach ($params['tag'] as $tag) {
402 $tagParams[$tag] = 1;
405 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_case', $caseObj->id
);
408 if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
409 CRM_Core_Form_Tag
::postProcess($params['case_taglist'], $caseObj->id
, 'civicrm_case', $this);
413 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
414 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}"
416 CRM_Core_Session
::singleton()->pushUserContext($url);
418 // 3. format activity custom data
419 if (!empty($params['hidden_custom'])) {
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,
426 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
432 // 4. call end post process
433 if ($this->_activityTypeFile
) {
434 $className::endPostProcess($this, $params);
441 * Process the form submission.
443 public function postProcess() {
444 $transaction = new CRM_Core_Transaction();
446 // check if dedupe button, if so return.
447 $buttonName = $this->controller
->getButtonName();
448 if (isset($this->_dedupeButtonName
) && $buttonName == $this->_dedupeButtonName
) {
452 if ($this->_action
& CRM_Core_Action
::DELETE
) {
453 $caseDelete = CRM_Case_BAO_Case
::deleteCase($this->_caseId
, TRUE);
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');
460 if ($this->_action
& CRM_Core_Action
::RENEW
) {
461 $caseRestore = CRM_Case_BAO_Case
::restoreCase($this->_caseId
);
463 CRM_Core_Session
::setStatus(ts('The selected case has been restored.'), ts('Restored'), 'success');
467 // store the submitted values in an array
468 $params = $this->controller
->exportValues($this->_name
);
469 $this->submit($params);
471 CRM_Core_Session
::setStatus($params['statusMsg'], ts('Saved'), 'success');