3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 * This class generates form components for case activity.
37 class CRM_Case_Form_Case
extends CRM_Core_Form
{
44 public $_context = 'case';
49 public $_caseId = NULL;
54 public $_currentlyViewedContactId = NULL;
59 public $_activityTypeFile = NULL;
62 * Logged in contact Id
64 public $_currentUserId = NULL;
69 public $_activityTypeId = NULL;
74 public $_activityId = NULL;
84 public $_caseTypeId = NULL;
87 * Build the form object.
89 public function preProcess() {
91 $this->_caseId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
93 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
95 if ($this->_action
& CRM_Core_Action
::ADD
&& !$this->_currentlyViewedContactId
) {
96 // check for add contacts permissions
97 if (!CRM_Core_Permission
::check('add contacts')) {
98 CRM_Utils_System
::permissionDenied();
104 if (!CRM_Core_Permission
::checkActionPermission('CiviCase', $this->_action
)) {
105 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
108 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
112 if (!$this->_caseId
) {
113 $caseAttributes = array(
114 'case_type' => CRM_Case_PseudoConstant
::caseType(),
115 'case_status' => CRM_Case_PseudoConstant
::caseStatus(),
116 'encounter_medium' => CRM_Case_PseudoConstant
::encounterMedium(),
119 foreach ($caseAttributes as $key => $values) {
120 if (empty($values)) {
121 CRM_Core_Error
::fatal(ts('You do not have any active %1',
122 array(1 => str_replace('_', ' ', $key))
129 if ($this->_action
& CRM_Core_Action
::ADD
) {
130 $this->_activityTypeId
= CRM_Core_OptionGroup
::getValue('activity_type',
134 if (!$this->_activityTypeId
) {
135 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.'));
139 //check for case permissions.
140 if (!CRM_Case_BAO_Case
::accessCiviCase()) {
141 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
143 if (($this->_action
& CRM_Core_Action
::ADD
) &&
144 (!CRM_Core_Permission
::check('access all cases and activities') &&
145 !CRM_Core_Permission
::check('add cases')
148 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
151 if ($this->_activityTypeFile
= CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
,
155 $this->assign('activityTypeFile', $this->_activityTypeFile
);
158 $details = CRM_Case_PseudoConstant
::caseActivityType(FALSE);
160 CRM_Utils_System
::setTitle($details[$this->_activityTypeId
]['label']);
161 $this->assign('activityType', $details[$this->_activityTypeId
]['label']);
162 $this->assign('activityTypeDescription', $details[$this->_activityTypeId
]['description']);
164 if (isset($this->_currentlyViewedContactId
)) {
165 $contact = new CRM_Contact_DAO_Contact();
166 $contact->id
= $this->_currentlyViewedContactId
;
167 if (!$contact->find(TRUE)) {
168 CRM_Core_Error
::statusBounce(ts('Client contact does not exist: %1', array(1 => $this->_currentlyViewedContactId
)));
170 $this->assign('clientName', $contact->display_name
);
173 $session = CRM_Core_Session
::singleton();
174 $this->_currentUserId
= $session->get('userID');
176 //when custom data is included in this page
177 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_activityTypeId
, 1, 'Activity');
178 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
179 $className::preProcess($this);
180 $activityGroupTree = $this->_groupTree
;
182 // for case custom fields to populate with defaults
183 if (!empty($_POST['hidden_custom'])) {
184 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_caseTypeId
, 1, 'Case', $this->_caseId
);
185 CRM_Custom_Form_CustomData
::buildQuickForm($this);
188 // so that grouptree is not populated with case fields, since the grouptree is used
189 // for populating activity custom fields.
190 $this->_groupTree
= $activityGroupTree;
194 * Set default values for the form.
196 public function setDefaultValues() {
197 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
200 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
201 $defaults = $className::setDefaultValues($this);
202 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData
::setDefaultValues($this));
206 public function buildQuickForm() {
207 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
208 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
209 $this->assign('multiClient', $isMultiClient);
211 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
213 if ($this->_action
& CRM_Core_Action
::RENEW
) {
216 $this->addButtons(array(
220 'spacing' => ' ',
225 'name' => ts('Cancel'),
232 //need to assign custom data type and subtype to the template
233 $this->assign('customDataType', 'Case');
235 CRM_Custom_Form_CustomData
::buildQuickForm($this);
236 // we don't want to show button on top of custom form
237 $this->assign('noPreCustomButton', TRUE);
239 $s = CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'subject');
243 $this->add('text', 'activity_subject', ts('Subject'),
244 array_merge($s, array(
245 'maxlength' => '128',
249 CRM_Core_BAO_Tag
::getTags('civicrm_case', $tags, NULL,
250 ' ', TRUE);
253 $this->add('select', 'tag', ts('Select Tags'), $tags, FALSE,
254 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
259 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
260 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
262 $this->addButtons(array(
265 'name' => ts('Save'),
266 'spacing' => ' ',
271 'name' => ts('Cancel'),
276 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
277 $className::buildQuickForm($this);
281 * Add local and global form rules.
285 public function addRules() {
286 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
289 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
290 $this->addFormRule(array($className, 'formRule'), $this);
291 $this->addFormRule(array('CRM_Case_Form_Case', 'formRule'), $this);
295 * Global validation rules for the form.
297 * @param array $values
298 * Posted values of the form.
301 * @param CRM_Core_Form $form
304 * list of errors to be posted back to the form
306 public static function formRule($values, $files, $form) {
311 * Process the form submission.
313 public function postProcess() {
314 $transaction = new CRM_Core_Transaction();
316 // check if dedupe button, if so return.
317 $buttonName = $this->controller
->getButtonName();
318 if (isset($this->_dedupeButtonName
) && $buttonName == $this->_dedupeButtonName
) {
322 if ($this->_action
& CRM_Core_Action
::DELETE
) {
324 $caseDelete = CRM_Case_BAO_Case
::deleteCase($this->_caseId
, TRUE);
326 $statusMsg = ts('The selected case has been moved to the Trash. You can view and / or restore deleted cases by checking the "Deleted Cases" option under Find Cases.<br />');
328 CRM_Core_Session
::setStatus($statusMsg, ts('Case Deleted'), 'success');
332 if ($this->_action
& CRM_Core_Action
::RENEW
) {
334 $caseRestore = CRM_Case_BAO_Case
::restoreCase($this->_caseId
);
336 $statusMsg = ts('The selected case has been restored.<br />');
338 CRM_Core_Session
::setStatus($statusMsg, ts('Restored'), 'success');
341 // store the submitted values in an array
342 $params = $this->controller
->exportValues($this->_name
);
343 $params['now'] = date("Ymd");
345 // 1. call begin post process
346 if ($this->_activityTypeFile
) {
347 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
348 $className::beginPostProcess($this, $params);
351 if (!empty($params['hidden_custom']) &&
352 !isset($params['custom'])
354 $customFields = array();
355 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess(
362 // 2. create/edit case
363 if (!empty($params['case_type_id'])) {
364 $params['case_type'] = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $params['case_type_id'], 'name', 'id');
365 $params['subject'] = $params['activity_subject'];
367 $caseObj = CRM_Case_BAO_Case
::create($params);
368 $params['case_id'] = $caseObj->id
;
369 // unset any ids, custom data
370 unset($params['id'], $params['custom']);
372 // add tags if exists
373 $tagParams = array();
374 if (!empty($params['tag'])) {
375 $tagParams = array();
376 foreach ($params['tag'] as $tag) {
377 $tagParams[$tag] = 1;
380 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_case', $caseObj->id
);
383 if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
384 CRM_Core_Form_Tag
::postProcess($params['case_taglist'], $caseObj->id
, 'civicrm_case', $this);
388 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
389 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}"
391 $session = CRM_Core_Session
::singleton();
392 $session->pushUserContext($url);
394 // 3. format activity custom data
395 if (!empty($params['hidden_custom'])) {
396 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE, $this->_activityTypeId
);
397 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
398 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
402 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
408 // 4. call end post process
409 if ($this->_activityTypeFile
) {
410 $className::endPostProcess($this, $params);
413 // 5. auto populate activities
416 CRM_Core_Session
::setStatus($params['statusMsg'], ts('Saved'), 'success');