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;
95 * Explicitly declare the entity api name.
97 public function getDefaultEntity() {
102 * Get the entity id being edited.
106 public function getEntityId() {
107 return $this->_caseId
;
111 * Get the entity subtype ID being edited
117 public function getEntitySubTypeId($subTypeId) {
121 return $this->_caseTypeId
;
125 * Build the form object.
127 public function preProcess() {
128 if (empty($this->_action
)) {
129 $this->_action
= CRM_Core_Action
::ADD
;
132 $this->_caseId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
134 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
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();
145 if (!CRM_Core_Permission
::checkActionPermission('CiviCase', $this->_action
)) {
146 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
149 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
153 if (!$this->_caseId
) {
155 'case_type_id' => ts('Case Type'),
156 'status_id' => ts('Case Status'),
157 'medium_id' => ts('Activity Medium'),
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]));
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.'));
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.'));
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')
183 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
186 if ($this->_activityTypeFile
= CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
,
190 $this->assign('activityTypeFile', $this->_activityTypeFile
);
193 $details = CRM_Case_PseudoConstant
::caseActivityType(FALSE);
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']);
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
]));
205 $this->assign('clientName', $contact->display_name
);
208 $session = CRM_Core_Session
::singleton();
209 $this->_currentUserId
= $session->get('userID');
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
;
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);
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;
227 * Set default values for the form.
229 public function setDefaultValues() {
230 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
233 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
234 $defaults = $className::setDefaultValues($this);
235 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData
::setDefaultValues($this));
239 public function buildQuickForm() {
240 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
241 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
242 $this->assign('multiClient', $isMultiClient);
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');
253 'spacing' => ' ',
258 'name' => ts('Cancel'),
264 // Add the activity custom data to the form
265 CRM_Custom_Form_CustomData
::buildQuickForm($this);
267 // we don't want to show button on top of custom form
268 $this->assign('noPreCustomButton', TRUE);
270 $s = CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'subject');
274 $this->add('text', 'activity_subject', ts('Subject'),
276 'maxlength' => '128',
280 $tags = CRM_Core_BAO_Tag
::getColorTags('civicrm_case');
283 $this->add('select2', 'tag', ts('Tags'), $tags, FALSE,
284 ['class' => 'huge', 'multiple' => 'multiple']
289 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
290 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
295 'name' => ts('Save'),
296 'spacing' => ' ',
301 'name' => ts('Cancel'),
305 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
306 $className::buildQuickForm($this);
310 * Add local and global form rules.
314 public function addRules() {
315 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
318 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
319 $this->addFormRule([$className, 'formRule'], $this);
320 $this->addFormRule(['CRM_Case_Form_Case', 'formRule'], $this);
324 * Global validation rules for the form.
326 * @param array $values
327 * Posted values of the form.
330 * @param CRM_Core_Form $form
333 * list of errors to be posted back to the form
335 public static function formRule($values, $files, $form) {
340 * Wrapper for unit testing the post process submit function.
343 * @param $activityTypeFile
346 * @return CRM_Case_BAO_Case
348 public function testSubmit($params, $activityTypeFile, $contactId, $context = "case") {
349 $this->controller
= new CRM_Core_Controller();
351 $this->_activityTypeFile
= $activityTypeFile;
352 $this->_currentUserId
= $contactId;
353 $this->_context
= $context;
355 return $this->submit($params);
359 * Submit the form with given params.
363 public function submit(&$params) {
364 $params['now'] = date("Ymd");
366 // 1. call begin post process
367 if ($this->_activityTypeFile
) {
368 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
369 $className::beginPostProcess($this, $params);
372 if (!empty($params['hidden_custom']) &&
373 !isset($params['custom'])
375 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess(
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'];
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']);
392 // add tags if exists
394 if (!empty($params['tag'])) {
396 if (!is_array($params['tag'])) {
397 $params['tag'] = explode(',', $params['tag']);
399 foreach ($params['tag'] as $tag) {
400 $tagParams[$tag] = 1;
403 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_case', $caseObj->id
);
406 if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
407 CRM_Core_Form_Tag
::postProcess($params['case_taglist'], $caseObj->id
, 'civicrm_case', $this);
411 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
412 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}"
414 CRM_Core_Session
::singleton()->pushUserContext($url);
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,
424 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
430 // 4. call end post process
431 if ($this->_activityTypeFile
) {
432 $className::endPostProcess($this, $params);
439 * Process the form submission.
441 public function postProcess() {
442 $transaction = new CRM_Core_Transaction();
444 // check if dedupe button, if so return.
445 $buttonName = $this->controller
->getButtonName();
446 if (isset($this->_dedupeButtonName
) && $buttonName == $this->_dedupeButtonName
) {
450 if ($this->_action
& CRM_Core_Action
::DELETE
) {
451 $caseDelete = CRM_Case_BAO_Case
::deleteCase($this->_caseId
, TRUE);
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');
458 if ($this->_action
& CRM_Core_Action
::RENEW
) {
459 $caseRestore = CRM_Case_BAO_Case
::restoreCase($this->_caseId
);
461 CRM_Core_Session
::setStatus(ts('The selected case has been restored.'), ts('Restored'), 'success');
465 // store the submitted values in an array
466 $params = $this->controller
->exportValues($this->_name
);
467 $this->submit($params);
469 CRM_Core_Session
::setStatus($params['statusMsg'], ts('Saved'), 'success');