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 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
187 $className::preProcess($this);
188 $activityGroupTree = $this->_groupTree
;
190 // for case custom fields to populate with defaults
191 if (CRM_Utils_Array
::value('hidden_custom', $_POST)) {
192 CRM_Custom_Form_CustomData
::preProcess($this);
193 CRM_Custom_Form_CustomData
::buildQuickForm($this);
196 // so that grouptree is not populated with case fields, since the grouptree is used
197 // for populating activity custom fields.
198 $this->_groupTree
= $activityGroupTree;
202 * This function sets the default values for the form. For edit/view mode
203 * the default values are retrieved from the database
209 function setDefaultValues() {
210 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW ||
$this->_cdType
) {
213 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
214 $defaults = $className::setDefaultValues($this);
215 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData
::setDefaultValues($this));
219 public function buildQuickForm() {
220 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
221 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
222 $this->assign('multiClient', $isMultiClient);
224 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
226 if ($this->_action
& CRM_Core_Action
::RENEW
) {
229 $this->addButtons(array(
233 'spacing' => ' ',
238 'name' => ts('Cancel'),
245 if ($this->_cdType
) {
246 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
248 //need to assign custom data type and subtype to the template
249 $this->assign('customDataType', 'Case');
251 CRM_Custom_Form_CustomData
::buildQuickForm($this);
252 // we don't want to show button on top of custom form
253 $this->assign('noPreCustomButton', TRUE);
255 $s = CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'subject');
259 $this->add('text', 'activity_subject', ts('Subject'),
260 array_merge($s, array(
261 'maxlength' => '128')), TRUE
264 $tags = CRM_Core_BAO_Tag
::getTags('civicrm_case');
266 $this->add('select', 'tag', ts('Select Tags'), $tags, FALSE,
267 array('id' => 'tags', 'multiple' => 'multiple', 'title' => ts('- select -'))
272 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
273 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
275 $this->addButtons(array(
278 'name' => ts('Save'),
279 'spacing' => ' ',
284 'name' => ts('Cancel'),
289 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
290 $className::buildQuickForm($this);
294 * Add local and global form rules
300 function addRules() {
301 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW ||
$this->_cdType
) {
304 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
305 $this->addFormRule(array($className, 'formRule'), $this);
306 $this->addFormRule(array('CRM_Case_Form_Case', 'formRule'), $this);
310 * global validation rules for the form
312 * @param array $values posted values of the form
314 * @return array list of errors to be posted back to the form
318 static function formRule($values, $files, $form) {
323 * Function to process the form
329 public function postProcess() {
330 $transaction = new CRM_Core_Transaction();
332 // check if dedupe button, if so return.
333 $buttonName = $this->controller
->getButtonName();
334 if (isset($this->_dedupeButtonName
) && $buttonName == $this->_dedupeButtonName
) {
338 if ($this->_action
& CRM_Core_Action
::DELETE
) {
340 $caseDelete = CRM_Case_BAO_Case
::deleteCase($this->_caseId
, TRUE);
342 $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 />');
344 CRM_Core_Session
::setStatus($statusMsg, ts('Case Deleted'), 'success');
348 if ($this->_action
& CRM_Core_Action
::RENEW
) {
350 $caseRestore = CRM_Case_BAO_Case
::restoreCase($this->_caseId
);
352 $statusMsg = ts('The selected case has been restored.<br />');
354 CRM_Core_Session
::setStatus($statusMsg, ts('Restored'), 'success');
357 // store the submitted values in an array
358 $params = $this->controller
->exportValues($this->_name
);
359 $params['now'] = date("Ymd");
362 // 1. call begin post process
363 if ($this->_activityTypeFile
) {
364 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
365 $className::beginPostProcess($this, $params );
369 CRM_Utils_Array
::value('hidden_custom', $params) &&
370 !isset($params['custom'])
372 $customFields = array();
373 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess(
381 // 2. create/edit case
382 if (CRM_Utils_Array
::value('case_type_id', $params)) {
383 $caseType = CRM_Case_PseudoConstant
::caseType('name');
384 $params['case_type'] = $caseType[$params['case_type_id']];
385 $params['subject'] = $params['activity_subject'];
386 $params['case_type_id'] = CRM_Core_DAO
::VALUE_SEPARATOR
. $params['case_type_id'] . CRM_Core_DAO
::VALUE_SEPARATOR
;
388 $caseObj = CRM_Case_BAO_Case
::create($params);
389 $params['case_id'] = $caseObj->id
;
390 // unset any ids, custom data
391 unset($params['id'], $params['custom']);
393 // add tags if exists
394 $tagParams = array();
395 if (!empty($params['tag'])) {
396 $tagParams = array();
397 foreach ($params['tag'] as $tag) {
398 $tagParams[$tag] = 1;
401 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_case', $caseObj->id
);
404 if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
405 CRM_Core_Form_Tag
::postProcess($params['case_taglist'], $caseObj->id
, 'civicrm_case', $this);
409 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
410 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}"
412 $session = CRM_Core_Session
::singleton();
413 $session->pushUserContext($url);
415 // 3. format activity custom data
416 if (CRM_Utils_Array
::value('hidden_custom', $params)) {
417 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE, $this->_activityTypeId
);
418 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
419 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
423 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
430 // 4. call end post process
431 if ($this->_activityTypeFile
) {
432 $className::endPostProcess($this, $params );
435 // 5. auto populate activites
438 CRM_Core_Session
::setStatus($params['statusMsg'], ts('Saved'), 'success');