3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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_id' => ts('Case Type'),
115 'status_id' => ts('Case Status'),
116 'medium_id' => ts('Activity Medium'),
119 foreach ($caseAttributes as $key => $label) {
120 if (!CRM_Case_BAO_Case
::buildOptions($key, 'create')) {
121 CRM_Core_Error
::fatal(ts('You do not have any active %1', array(1 => $label)));
126 if ($this->_action
& CRM_Core_Action
::ADD
) {
127 $this->_activityTypeId
= CRM_Core_OptionGroup
::getValue('activity_type',
131 if (!$this->_activityTypeId
) {
132 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.'));
136 //check for case permissions.
137 if (!CRM_Case_BAO_Case
::accessCiviCase()) {
138 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
140 if (($this->_action
& CRM_Core_Action
::ADD
) &&
141 (!CRM_Core_Permission
::check('access all cases and activities') &&
142 !CRM_Core_Permission
::check('add cases')
145 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
148 if ($this->_activityTypeFile
= CRM_Activity_BAO_Activity
::getFileForActivityTypeId($this->_activityTypeId
,
152 $this->assign('activityTypeFile', $this->_activityTypeFile
);
155 $details = CRM_Case_PseudoConstant
::caseActivityType(FALSE);
157 CRM_Utils_System
::setTitle($details[$this->_activityTypeId
]['label']);
158 $this->assign('activityType', $details[$this->_activityTypeId
]['label']);
159 $this->assign('activityTypeDescription', $details[$this->_activityTypeId
]['description']);
161 if (isset($this->_currentlyViewedContactId
)) {
162 $contact = new CRM_Contact_DAO_Contact();
163 $contact->id
= $this->_currentlyViewedContactId
;
164 if (!$contact->find(TRUE)) {
165 CRM_Core_Error
::statusBounce(ts('Client contact does not exist: %1', array(1 => $this->_currentlyViewedContactId
)));
167 $this->assign('clientName', $contact->display_name
);
170 $session = CRM_Core_Session
::singleton();
171 $this->_currentUserId
= $session->get('userID');
173 //when custom data is included in this page
174 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_activityTypeId
, 1, 'Activity');
175 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
176 $className::preProcess($this);
177 $activityGroupTree = $this->_groupTree
;
179 // for case custom fields to populate with defaults
180 if (!empty($_POST['hidden_custom'])) {
181 $params = CRM_Utils_Request
::exportValues();
182 CRM_Custom_Form_CustomData
::preProcess($this, NULL, CRM_Utils_Array
::value('case_type_id', $params, $this->_caseTypeId
), 1, 'Case', $this->_caseId
);
183 CRM_Custom_Form_CustomData
::buildQuickForm($this);
186 // so that grouptree is not populated with case fields, since the grouptree is used
187 // for populating activity custom fields.
188 $this->_groupTree
= $activityGroupTree;
192 * Set default values for the form.
194 public function setDefaultValues() {
195 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
198 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
199 $defaults = $className::setDefaultValues($this);
200 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData
::setDefaultValues($this));
204 public function buildQuickForm() {
205 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
206 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
207 $this->assign('multiClient', $isMultiClient);
209 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
211 if ($this->_action
& CRM_Core_Action
::RENEW
) {
214 $this->addButtons(array(
218 'spacing' => ' ',
223 'name' => ts('Cancel'),
230 //need to assign custom data type and subtype to the template
231 $this->assign('customDataType', 'Case');
233 CRM_Custom_Form_CustomData
::buildQuickForm($this);
234 // we don't want to show button on top of custom form
235 $this->assign('noPreCustomButton', TRUE);
237 $s = CRM_Core_DAO
::getAttribute('CRM_Activity_DAO_Activity', 'subject');
241 $this->add('text', 'activity_subject', ts('Subject'),
242 array_merge($s, array(
243 'maxlength' => '128',
247 CRM_Core_BAO_Tag
::getTags('civicrm_case', $tags, NULL,
248 ' ', TRUE);
251 $this->add('select', 'tag', ts('Select Tags'), $tags, FALSE,
252 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
257 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
258 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
260 $this->addButtons(array(
263 'name' => ts('Save'),
264 'spacing' => ' ',
269 'name' => ts('Cancel'),
274 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
275 $className::buildQuickForm($this);
279 * Add local and global form rules.
283 public function addRules() {
284 if ($this->_action
& CRM_Core_Action
::DELETE ||
$this->_action
& CRM_Core_Action
::RENEW
) {
287 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
288 $this->addFormRule(array($className, 'formRule'), $this);
289 $this->addFormRule(array('CRM_Case_Form_Case', 'formRule'), $this);
293 * Global validation rules for the form.
295 * @param array $values
296 * Posted values of the form.
299 * @param CRM_Core_Form $form
302 * list of errors to be posted back to the form
304 public static function formRule($values, $files, $form) {
309 * Process the form submission.
311 public function postProcess() {
312 $transaction = new CRM_Core_Transaction();
314 // check if dedupe button, if so return.
315 $buttonName = $this->controller
->getButtonName();
316 if (isset($this->_dedupeButtonName
) && $buttonName == $this->_dedupeButtonName
) {
320 if ($this->_action
& CRM_Core_Action
::DELETE
) {
322 $caseDelete = CRM_Case_BAO_Case
::deleteCase($this->_caseId
, TRUE);
324 $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 />');
326 CRM_Core_Session
::setStatus($statusMsg, ts('Case Deleted'), 'success');
330 if ($this->_action
& CRM_Core_Action
::RENEW
) {
332 $caseRestore = CRM_Case_BAO_Case
::restoreCase($this->_caseId
);
334 $statusMsg = ts('The selected case has been restored.<br />');
336 CRM_Core_Session
::setStatus($statusMsg, ts('Restored'), 'success');
339 // store the submitted values in an array
340 $params = $this->controller
->exportValues($this->_name
);
341 $params['now'] = date("Ymd");
343 // 1. call begin post process
344 if ($this->_activityTypeFile
) {
345 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
346 $className::beginPostProcess($this, $params);
349 if (!empty($params['hidden_custom']) &&
350 !isset($params['custom'])
352 $customFields = array();
353 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess(
360 // 2. create/edit case
361 if (!empty($params['case_type_id'])) {
362 $params['case_type'] = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $params['case_type_id'], 'name', 'id');
363 $params['subject'] = $params['activity_subject'];
365 $caseObj = CRM_Case_BAO_Case
::create($params);
366 $params['case_id'] = $caseObj->id
;
367 // unset any ids, custom data
368 unset($params['id'], $params['custom']);
370 // add tags if exists
371 $tagParams = array();
372 if (!empty($params['tag'])) {
373 $tagParams = array();
374 foreach ($params['tag'] as $tag) {
375 $tagParams[$tag] = 1;
378 CRM_Core_BAO_EntityTag
::create($tagParams, 'civicrm_case', $caseObj->id
);
381 if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
382 CRM_Core_Form_Tag
::postProcess($params['case_taglist'], $caseObj->id
, 'civicrm_case', $this);
386 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
387 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}"
389 $session = CRM_Core_Session
::singleton();
390 $session->pushUserContext($url);
392 // 3. format activity custom data
393 if (!empty($params['hidden_custom'])) {
394 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE, $this->_activityTypeId
);
395 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
396 CRM_Core_BAO_CustomField
::getFields('Activity', FALSE, FALSE,
400 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
406 // 4. call end post process
407 if ($this->_activityTypeFile
) {
408 $className::endPostProcess($this, $params);
411 // 5. auto populate activities
414 CRM_Core_Session
::setStatus($params['statusMsg'], ts('Saved'), 'success');