Merge branch 'crm-13261-api-hook' of git://github.com/tttp/civicrm-core into pull...
[civicrm-core.git] / CRM / Case / Form / Case.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for case activity
38 *
39 */
40class CRM_Case_Form_Case extends CRM_Core_Form {
41
42 /**
43 * The context
44 *
45 * @var string
46 */
47 public $_context = 'case';
48
49 /**
50 * Case Id
51 */
52 public $_caseId = NULL;
53
54 /**
55 * Client Id
56 */
57 public $_currentlyViewedContactId = NULL;
58
59 /**
60 * Activity Type File
61 */
62 public $_activityTypeFile = NULL;
63
64 /**
65 * logged in contact Id
66 */
67 public $_currentUserId = NULL;
68
69 /**
70 * activity type Id
71 */
72 public $_activityTypeId = NULL;
73
74 /**
75 * activity type Id
76 */
77 public $_activityId = NULL;
78
79 /**
80 * action
81 */
82 public $_action;
83
84 /**
85 * Function to build the form
86 *
87 * @return None
88 * @access public
89 */
90 function preProcess() {
91 $this->_cdType = CRM_Utils_Array::value('type', $_GET);
92 $this->assign('cdType', FALSE);
93 if ($this->_cdType) {
94 $this->assign('cdType', TRUE);
95 return CRM_Custom_Form_CustomData::preProcess($this);
96 }
97
98 $this->_caseId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
99
100 $this->_currentlyViewedContactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
101
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();
106 return;
107 }
108 }
109
110 //CRM-4418
111 if (!CRM_Core_Permission::checkActionPermission('CiviCase', $this->_action)) {
112 CRM_Core_Error::fatal(ts('You do not have permission to access this page'));
113 }
114
115 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) {
116 return TRUE;
117 }
118
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(),
123 );
124
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))
129 ));
130 break;
131 }
132 }
133 }
134
135 if ($this->_action & CRM_Core_Action::ADD) {
136 $this->_activityTypeId = CRM_Core_OptionGroup::getValue('activity_type',
137 'Open Case',
138 'name'
139 );
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.'));
142 }
143 }
144
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.'));
148 }
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')
152 )
153 ) {
154 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
155 }
156
157 if ($this->_activityTypeFile =
158 CRM_Activity_BAO_Activity::getFileForActivityTypeId($this->_activityTypeId,
159 'Case'
160 )
161 ) {
162 $this->assign('activityTypeFile', $this->_activityTypeFile);
163 }
164
165 $details = CRM_Case_PseudoConstant::caseActivityType(FALSE);
166
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']);
170
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)));
176 }
177 $this->assign('clientName', $contact->display_name);
178 }
179
180
181 $session = CRM_Core_Session::singleton();
182 $this->_currentUserId = $session->get('userID');
183
184 //when custom data is included in this page
185 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_activityTypeId, 1, 'Activity');
0e6e8724
DL
186 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
187 $className::preProcess($this);
6a488035
TO
188 $activityGroupTree = $this->_groupTree;
189
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);
194 }
195
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;
199 }
200
201 /**
202 * This function sets the default values for the form. For edit/view mode
203 * the default values are retrieved from the database
204 *
205 * @access public
206 *
207 * @return None
208 */
209 function setDefaultValues() {
210 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW || $this->_cdType) {
211 return TRUE;
212 }
0e6e8724
DL
213 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
214 $defaults = $className::setDefaultValues($this);
6a488035
TO
215 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData::setDefaultValues($this));
216 return $defaults;
217 }
218
219 public function buildQuickForm() {
220 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
221 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
222 $this->assign('multiClient', $isMultiClient);
223
224 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) {
225 $title = 'Delete';
226 if ($this->_action & CRM_Core_Action::RENEW) {
227 $title = 'Restore';
228 }
229 $this->addButtons(array(
230 array(
231 'type' => 'next',
232 'name' => $title,
233 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
234 'isDefault' => TRUE,
235 ),
236 array(
237 'type' => 'cancel',
238 'name' => ts('Cancel'),
239 ),
240 )
241 );
242 return;
243 }
244
245 if ($this->_cdType) {
246 return CRM_Custom_Form_CustomData::buildQuickForm($this);
247 }
248 //need to assign custom data type and subtype to the template
249 $this->assign('customDataType', 'Case');
250
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);
254
255 $s = CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'subject');
256 if (!is_array($s)) {
257 $s = array();
258 }
259 $this->add('text', 'activity_subject', ts('Subject'),
260 array_merge($s, array(
261 'maxlength' => '128')), TRUE
262 );
263
264 $tags = CRM_Core_BAO_Tag::getTags('civicrm_case');
265 if (!empty($tags)) {
266 $this->add('select', 'tag', ts('Select Tags'), $tags, FALSE,
267 array('id' => 'tags', 'multiple' => 'multiple', 'title' => ts('- select -'))
268 );
269 }
270
271 // build tag widget
272 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_case');
273 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
274
275 $this->addButtons(array(
276 array(
277 'type' => 'next',
278 'name' => ts('Save'),
279 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
280 'isDefault' => TRUE,
281 ),
282 array(
283 'type' => 'cancel',
284 'name' => ts('Cancel'),
285 ),
286 )
287 );
288
0e6e8724
DL
289 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
290 $className::buildQuickForm($this);
6a488035
TO
291 }
292
293 /**
294 * Add local and global form rules
295 *
296 * @access protected
297 *
298 * @return void
299 */
300 function addRules() {
301 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW || $this->_cdType) {
302 return TRUE;
303 }
0e6e8724
DL
304 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
305 $this->addFormRule(array($className, 'formRule'), $this);
6a488035
TO
306 $this->addFormRule(array('CRM_Case_Form_Case', 'formRule'), $this);
307 }
308
309 /**
310 * global validation rules for the form
311 *
312 * @param array $values posted values of the form
313 *
314 * @return array list of errors to be posted back to the form
315 * @static
316 * @access public
317 */
318 static function formRule($values, $files, $form) {
319 return TRUE;
320 }
321
322 /**
323 * Function to process the form
324 *
325 * @access public
326 *
327 * @return None
328 */
329 public function postProcess() {
330 $transaction = new CRM_Core_Transaction();
331
332 // check if dedupe button, if so return.
333 $buttonName = $this->controller->getButtonName();
334 if (isset($this->_dedupeButtonName) && $buttonName == $this->_dedupeButtonName) {
335 return;
336 }
337
338 if ($this->_action & CRM_Core_Action::DELETE) {
339 $statusMsg = NULL;
340 $caseDelete = CRM_Case_BAO_Case::deleteCase($this->_caseId, TRUE);
341 if ($caseDelete) {
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 />');
343 }
344 CRM_Core_Session::setStatus($statusMsg, ts('Case Deleted'), 'success');
345 return;
346 }
347
348 if ($this->_action & CRM_Core_Action::RENEW) {
349 $statusMsg = NULL;
350 $caseRestore = CRM_Case_BAO_Case::restoreCase($this->_caseId);
351 if ($caseRestore) {
352 $statusMsg = ts('The selected case has been restored.<br />');
353 }
354 CRM_Core_Session::setStatus($statusMsg, ts('Restored'), 'success');
355 return;
356 }
357 // store the submitted values in an array
358 $params = $this->controller->exportValues($this->_name);
359 $params['now'] = date("Ymd");
360
361
362 // 1. call begin post process
363 if ($this->_activityTypeFile) {
0e6e8724
DL
364 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
365 $className::beginPostProcess($this, $params );
6a488035
TO
366 }
367
0e6e8724
DL
368 if (
369 CRM_Utils_Array::value('hidden_custom', $params) &&
6a488035
TO
370 !isset($params['custom'])
371 ) {
372 $customFields = array();
0e6e8724
DL
373 $params['custom'] = CRM_Core_BAO_CustomField::postProcess(
374 $params,
6a488035
TO
375 $customFields,
376 NULL,
377 'Case'
378 );
379 }
380
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;
387 }
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']);
392
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;
399 }
400 }
401 CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_case', $caseObj->id);
402
403 //save free tags
404 if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
405 CRM_Core_Form_Tag::postProcess($params['case_taglist'], $caseObj->id, 'civicrm_case', $this);
406 }
407
408 // user context
409 $url = CRM_Utils_System::url('civicrm/contact/view/case',
410 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}"
411 );
412 $session = CRM_Core_Session::singleton();
413 $session->pushUserContext($url);
414
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,
420 NULL, NULL, TRUE
421 )
422 );
423 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
424 $customFields,
425 $this->_activityId,
426 'Activity'
427 );
428 }
429
430 // 4. call end post process
431 if ($this->_activityTypeFile) {
0e6e8724 432 $className::endPostProcess($this, $params );
6a488035
TO
433 }
434
435 // 5. auto populate activites
436
437 // 6. set status
79dc2175 438 CRM_Core_Session::setStatus($params['statusMsg'], ts('Saved'), 'success');
6a488035
TO
439 }
440}
441