3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This class generates form components for case activity
40 class CRM_Case_Form_Case
extends CRM_Core_Form
{
47 public $_context = 'case';
52 public $_caseId = NULL;
57 public $_currentlyViewedContactId = NULL;
62 public $_activityTypeFile = NULL;
65 * logged in contact Id
67 public $_currentUserId = NULL;
72 public $_activityTypeId = NULL;
77 public $_activityId = NULL;
85 * Function to build the form
90 function preProcess() {
91 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
92 $this->assign('cdType', FALSE);
94 $this->assign('cdType', TRUE);
95 return CRM_Custom_Form_CustomData
::preProcess($this);
98 $this->_caseId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
100 $this->_currentlyViewedContactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
102 if ($this->_action
& CRM_Core_Action
::ADD
&& !$this->_currentlyViewedContactId
) {
103 // check for add contacts permissions
104 if (!CRM_Core_Permission
::check('add contacts')) {
105 CRM_Utils_System
::permissionDenied();
111 if (!CRM_Core_Permission
::checkActionPermission('CiviCase', $this->_action
)) {
112 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
115 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
119 if (!$this->_caseId
) {
120 $caseAttributes = array('case_type' => CRM_Case_PseudoConstant
::caseType(),
121 'case_status' => CRM_Case_PseudoConstant
::caseStatus(),
122 'encounter_medium' => CRM_Case_PseudoConstant
::encounterMedium(),
125 foreach ($caseAttributes as $key => $values) {
126 if (empty($values)) {
127 CRM_Core_Error
::fatal(ts('You do not have any active %1',
128 array(1 => str_replace('_', ' ', $key))
135 if ($this->_action
& CRM_Core_Action
::ADD
) {
136 $this->_activityTypeId
= CRM_Core_OptionGroup
::getValue('activity_type',
140 if (!$this->_activityTypeId
) {
141 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.'));
145 //check for case permissions.
146 if (!CRM_Case_BAO_Case
::accessCiviCase()) {
147 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
149 if (($this->_action
& CRM_Core_Action
::ADD
) &&
150 (!CRM_Core_Permission
::check('access all cases and activities') &&
151 !CRM_Core_Permission
::check('add cases')
154 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
157 if ($this->_activityTypeFile
=
158 CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
,
162 $this->assign('activityTypeFile', $this->_activityTypeFile
);
165 $details = CRM_Case_PseudoConstant
::caseActivityType(FALSE);
167 CRM_Utils_System
::setTitle($details[$this->_activityTypeId
]['label']);
168 $this->assign('activityType', $details[$this->_activityTypeId
]['label']);
169 $this->assign('activityTypeDescription', $details[$this->_activityTypeId
]['description']);
171 if (isset($this->_currentlyViewedContactId
)) {
172 $contact = new CRM_Contact_DAO_Contact();
173 $contact->id
= $this->_currentlyViewedContactId
;
174 if (!$contact->find(TRUE)) {
175 CRM_Core_Error
::statusBounce(ts('Client contact does not exist: %1', array(1 => $this->_currentlyViewedContactId
)));
177 $this->assign('clientName', $contact->display_name
);
181 $session = CRM_Core_Session
::singleton();
182 $this->_currentUserId
= $session->get('userID');
184 //when custom data is included in this page
185 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_activityTypeId
, 1, 'Activity');
186 eval("CRM_Case_Form_Activity_{$this->_activityTypeFile}::preProcess( \$this );");
187 $activityGroupTree = $this->_groupTree
;
189 // for case custom fields to populate with defaults
190 if (CRM_Utils_Array
::value('hidden_custom', $_POST)) {
191 CRM_Custom_Form_CustomData
::preProcess($this);
192 CRM_Custom_Form_CustomData
::buildQuickForm($this);
195 // so that grouptree is not populated with case fields, since the grouptree is used
196 // for populating activity custom fields.
197 $this->_groupTree
= $activityGroupTree;
201 * This function sets the default values for the form. For edit/view mode
202 * the default values are retrieved from the database
208 function setDefaultValues() {
209 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW ||
$this->_cdType
) {
212 eval('$defaults = CRM_Case_Form_Activity_' . $this->_activityTypeFile
. '::setDefaultValues($this);');
213 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData
::setDefaultValues($this));
217 public function buildQuickForm() {
218 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
219 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
220 $this->assign('multiClient', $isMultiClient);
222 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
224 if ($this->_action
& CRM_Core_Action
::RENEW
) {
227 $this->addButtons(array(
231 'spacing' => ' ',
236 'name' => ts('Cancel'),
243 if ($this->_cdType
) {
244 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
246 //need to assign custom data type and subtype to the template
247 $this->assign('customDataType', 'Case');
249 CRM_Custom_Form_CustomData
::buildQuickForm($this);
250 // we don't want to show button on top of custom form
251 $this->assign('noPreCustomButton', TRUE);
253 $s = CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'subject');
257 $this->add('text', 'activity_subject', ts('Subject'),
258 array_merge($s, array(
259 'maxlength' => '128')), TRUE
262 $tags = CRM_Core_BAO_Tag
::getTags('civicrm_case');
264 $this->add('select', 'tag', ts('Select Tags'), $tags, FALSE,
265 array('id' => 'tags', 'multiple' => 'multiple', 'title' => ts('- select -'))
270 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
271 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
273 $this->addButtons(array(
276 'name' => ts('Save'),
277 'spacing' => ' ',
282 'name' => ts('Cancel'),
287 eval("CRM_Case_Form_Activity_{$this->_activityTypeFile}::buildQuickForm( \$this );");
291 * Add local and global form rules
297 function addRules() {
298 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW ||
$this->_cdType
) {
301 eval('$this->addFormRule' . "(array('CRM_Case_Form_Activity_{$this->_activityTypeFile}', 'formrule'), \$this);");
302 $this->addFormRule(array('CRM_Case_Form_Case', 'formRule'), $this);
306 * global validation rules for the form
308 * @param array $values posted values of the form
310 * @return array list of errors to be posted back to the form
314 static function formRule($values, $files, $form) {
319 * Function to process the form
325 public function postProcess() {
326 $transaction = new CRM_Core_Transaction();
328 // check if dedupe button, if so return.
329 $buttonName = $this->controller
->getButtonName();
330 if (isset($this->_dedupeButtonName
) && $buttonName == $this->_dedupeButtonName
) {
334 if ($this->_action
& CRM_Core_Action
::DELETE
) {
336 $caseDelete = CRM_Case_BAO_Case
::deleteCase($this->_caseId
, TRUE);
338 $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 />');
340 CRM_Core_Session
::setStatus($statusMsg, ts('Case Deleted'), 'success');
344 if ($this->_action
& CRM_Core_Action
::RENEW
) {
346 $caseRestore = CRM_Case_BAO_Case
::restoreCase($this->_caseId
);
348 $statusMsg = ts('The selected case has been restored.<br />');
350 CRM_Core_Session
::setStatus($statusMsg, ts('Restored'), 'success');
353 // store the submitted values in an array
354 $params = $this->controller
->exportValues($this->_name
);
355 $params['now'] = date("Ymd");
358 // 1. call begin post process
359 if ($this->_activityTypeFile
) {
360 eval("CRM_Case_Form_Activity_{$this->_activityTypeFile}" . "::beginPostProcess( \$this, \$params );");
363 if (CRM_Utils_Array
::value('hidden_custom', $params) &&
364 !isset($params['custom'])
366 $customFields = array();
367 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
374 // 2. create/edit case
375 if (CRM_Utils_Array
::value('case_type_id', $params)) {
376 $caseType = CRM_Case_PseudoConstant
::caseType('name');
377 $params['case_type'] = $caseType[$params['case_type_id']];
378 $params['subject'] = $params['activity_subject'];
379 $params['case_type_id'] = CRM_Core_DAO
::VALUE_SEPARATOR
. $params['case_type_id'] . CRM_Core_DAO
::VALUE_SEPARATOR
;
381 $caseObj = CRM_Case_BAO_Case
::create($params);
382 $params['case_id'] = $caseObj->id
;
383 // unset any ids, custom data
384 unset($params['id'], $params['custom']);
386 // add tags if exists
387 $tagParams = array();
388 if (!empty($params['tag'])) {
389 $tagParams = array();
390 foreach ($params['tag'] as $tag) {
391 $tagParams[$tag] = 1;
394 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_case', $caseObj->id
);
397 if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
398 CRM_Core_Form_Tag
::postProcess($params['case_taglist'], $caseObj->id
, 'civicrm_case', $this);
402 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
403 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}"
405 $session = CRM_Core_Session
::singleton();
406 $session->pushUserContext($url);
408 // 3. format activity custom data
409 if (CRM_Utils_Array
::value('hidden_custom', $params)) {
410 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE, $this->_activityTypeId
);
411 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
412 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
416 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
423 // 4. call end post process
424 if ($this->_activityTypeFile
) {
425 eval("CRM_Case_Form_Activity_{$this->_activityTypeFile}" . "::endPostProcess( \$this, \$params );");
428 // 5. auto populate activites
431 CRM_Core_Session
::setStatus($params['statusMsg'], '', 'info');