commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Case / Form / Case.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for case activity
38 *
39 */
40 class 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 * Case type id
86 */
87 public $_caseTypeId = NULL;
88
89 /**
90 * Build the form object.
91 *
92 * @return void
93 */
94 public function preProcess() {
95 $this->_cdType = CRM_Utils_Array::value('type', $_GET);
96 $this->assign('cdType', FALSE);
97 if ($this->_cdType) {
98 $this->assign('cdType', TRUE);
99 return CRM_Custom_Form_CustomData::preProcess($this);
100 }
101
102 $this->_caseId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
103
104 $this->_currentlyViewedContactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
105
106 if ($this->_action & CRM_Core_Action::ADD && !$this->_currentlyViewedContactId) {
107 // check for add contacts permissions
108 if (!CRM_Core_Permission::check('add contacts')) {
109 CRM_Utils_System::permissionDenied();
110 return;
111 }
112 }
113
114 //CRM-4418
115 if (!CRM_Core_Permission::checkActionPermission('CiviCase', $this->_action)) {
116 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
117 }
118
119 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) {
120 return TRUE;
121 }
122
123 if (!$this->_caseId) {
124 $caseAttributes = array(
125 'case_type' => CRM_Case_PseudoConstant::caseType(),
126 'case_status' => CRM_Case_PseudoConstant::caseStatus(),
127 'encounter_medium' => CRM_Case_PseudoConstant::encounterMedium(),
128 );
129
130 foreach ($caseAttributes as $key => $values) {
131 if (empty($values)) {
132 CRM_Core_Error::fatal(ts('You do not have any active %1',
133 array(1 => str_replace('_', ' ', $key))
134 ));
135 break;
136 }
137 }
138 }
139
140 if ($this->_action & CRM_Core_Action::ADD) {
141 $this->_activityTypeId = CRM_Core_OptionGroup::getValue('activity_type',
142 'Open Case',
143 'name'
144 );
145 if (!$this->_activityTypeId) {
146 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.'));
147 }
148 }
149
150 //check for case permissions.
151 if (!CRM_Case_BAO_Case::accessCiviCase()) {
152 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
153 }
154 if (($this->_action & CRM_Core_Action::ADD) &&
155 (!CRM_Core_Permission::check('access all cases and activities') &&
156 !CRM_Core_Permission::check('add cases')
157 )
158 ) {
159 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
160 }
161
162 if ($this->_activityTypeFile = CRM_Activity_BAO_Activity::getFileForActivityTypeId($this->_activityTypeId,
163 'Case'
164 )
165 ) {
166 $this->assign('activityTypeFile', $this->_activityTypeFile);
167 }
168
169 $details = CRM_Case_PseudoConstant::caseActivityType(FALSE);
170
171 CRM_Utils_System::setTitle($details[$this->_activityTypeId]['label']);
172 $this->assign('activityType', $details[$this->_activityTypeId]['label']);
173 $this->assign('activityTypeDescription', $details[$this->_activityTypeId]['description']);
174
175 if (isset($this->_currentlyViewedContactId)) {
176 $contact = new CRM_Contact_DAO_Contact();
177 $contact->id = $this->_currentlyViewedContactId;
178 if (!$contact->find(TRUE)) {
179 CRM_Core_Error::statusBounce(ts('Client contact does not exist: %1', array(1 => $this->_currentlyViewedContactId)));
180 }
181 $this->assign('clientName', $contact->display_name);
182 }
183
184 $session = CRM_Core_Session::singleton();
185 $this->_currentUserId = $session->get('userID');
186
187 //when custom data is included in this page
188 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_activityTypeId, 1, 'Activity');
189 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
190 $className::preProcess($this);
191 $activityGroupTree = $this->_groupTree;
192
193 // for case custom fields to populate with defaults
194 if (!empty($_POST['hidden_custom'])) {
195 CRM_Custom_Form_CustomData::preProcess($this);
196 CRM_Custom_Form_CustomData::buildQuickForm($this);
197 }
198
199 // so that grouptree is not populated with case fields, since the grouptree is used
200 // for populating activity custom fields.
201 $this->_groupTree = $activityGroupTree;
202 }
203
204 /**
205 * Set default values for the form. For edit/view mode
206 * the default values are retrieved from the database
207 *
208 *
209 * @return void
210 */
211 public function setDefaultValues() {
212 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW || $this->_cdType) {
213 return TRUE;
214 }
215 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
216 $defaults = $className::setDefaultValues($this);
217 $defaults = array_merge($defaults, CRM_Custom_Form_CustomData::setDefaultValues($this));
218 return $defaults;
219 }
220
221 public function buildQuickForm() {
222 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
223 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
224 $this->assign('multiClient', $isMultiClient);
225
226 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) {
227 $title = 'Delete';
228 if ($this->_action & CRM_Core_Action::RENEW) {
229 $title = 'Restore';
230 }
231 $this->addButtons(array(
232 array(
233 'type' => 'next',
234 'name' => $title,
235 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
236 'isDefault' => TRUE,
237 ),
238 array(
239 'type' => 'cancel',
240 'name' => ts('Cancel'),
241 ),
242 )
243 );
244 return;
245 }
246
247 if ($this->_cdType) {
248 return CRM_Custom_Form_CustomData::buildQuickForm($this);
249 }
250 //need to assign custom data type and subtype to the template
251 $this->assign('customDataType', 'Case');
252
253 CRM_Custom_Form_CustomData::buildQuickForm($this);
254 // we don't want to show button on top of custom form
255 $this->assign('noPreCustomButton', TRUE);
256
257 $s = CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'subject');
258 if (!is_array($s)) {
259 $s = array();
260 }
261 $this->add('text', 'activity_subject', ts('Subject'),
262 array_merge($s, array(
263 'maxlength' => '128',
264 )), TRUE
265 );
266
267 CRM_Core_BAO_Tag::getTags('civicrm_case', $tags, NULL,
268 '&nbsp;&nbsp;', TRUE);
269
270 if (!empty($tags)) {
271 $this->add('select', 'tag', ts('Select Tags'), $tags, FALSE,
272 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
273 );
274 }
275
276 // build tag widget
277 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_case');
278 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE);
279
280 $this->addButtons(array(
281 array(
282 'type' => 'next',
283 'name' => ts('Save'),
284 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
285 'isDefault' => TRUE,
286 ),
287 array(
288 'type' => 'cancel',
289 'name' => ts('Cancel'),
290 ),
291 )
292 );
293
294 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
295 $className::buildQuickForm($this);
296 }
297
298 /**
299 * Add local and global form rules.
300 *
301 *
302 * @return void
303 */
304 public function addRules() {
305 if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW || $this->_cdType) {
306 return TRUE;
307 }
308 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
309 $this->addFormRule(array($className, 'formRule'), $this);
310 $this->addFormRule(array('CRM_Case_Form_Case', 'formRule'), $this);
311 }
312
313 /**
314 * Global validation rules for the form.
315 *
316 * @param array $values
317 * Posted values of the form.
318 *
319 * @param $files
320 * @param CRM_Core_Form $form
321 *
322 * @return array
323 * list of errors to be posted back to the form
324 */
325 public static function formRule($values, $files, $form) {
326 return TRUE;
327 }
328
329 /**
330 * Process the form submission.
331 *
332 *
333 * @return void
334 */
335 public function postProcess() {
336 $transaction = new CRM_Core_Transaction();
337
338 // check if dedupe button, if so return.
339 $buttonName = $this->controller->getButtonName();
340 if (isset($this->_dedupeButtonName) && $buttonName == $this->_dedupeButtonName) {
341 return;
342 }
343
344 if ($this->_action & CRM_Core_Action::DELETE) {
345 $statusMsg = NULL;
346 $caseDelete = CRM_Case_BAO_Case::deleteCase($this->_caseId, TRUE);
347 if ($caseDelete) {
348 $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 />');
349 }
350 CRM_Core_Session::setStatus($statusMsg, ts('Case Deleted'), 'success');
351 return;
352 }
353
354 if ($this->_action & CRM_Core_Action::RENEW) {
355 $statusMsg = NULL;
356 $caseRestore = CRM_Case_BAO_Case::restoreCase($this->_caseId);
357 if ($caseRestore) {
358 $statusMsg = ts('The selected case has been restored.<br />');
359 }
360 CRM_Core_Session::setStatus($statusMsg, ts('Restored'), 'success');
361 return;
362 }
363 // store the submitted values in an array
364 $params = $this->controller->exportValues($this->_name);
365 $params['now'] = date("Ymd");
366
367 // 1. call begin post process
368 if ($this->_activityTypeFile) {
369 $className = "CRM_Case_Form_Activity_{$this->_activityTypeFile}";
370 $className::beginPostProcess($this, $params);
371 }
372
373 if (!empty($params['hidden_custom']) &&
374 !isset($params['custom'])
375 ) {
376 $customFields = array();
377 $params['custom'] = CRM_Core_BAO_CustomField::postProcess(
378 $params,
379 $customFields,
380 NULL,
381 'Case'
382 );
383 }
384
385 // 2. create/edit case
386 if (!empty($params['case_type_id'])) {
387 $params['case_type'] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $params['case_type_id'], 'name', 'id');
388 $params['subject'] = $params['activity_subject'];
389 }
390 $caseObj = CRM_Case_BAO_Case::create($params);
391 $params['case_id'] = $caseObj->id;
392 // unset any ids, custom data
393 unset($params['id'], $params['custom']);
394
395 // add tags if exists
396 $tagParams = array();
397 if (!empty($params['tag'])) {
398 $tagParams = array();
399 foreach ($params['tag'] as $tag) {
400 $tagParams[$tag] = 1;
401 }
402 }
403 CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_case', $caseObj->id);
404
405 //save free tags
406 if (isset($params['case_taglist']) && !empty($params['case_taglist'])) {
407 CRM_Core_Form_Tag::postProcess($params['case_taglist'], $caseObj->id, 'civicrm_case', $this);
408 }
409
410 // user context
411 $url = CRM_Utils_System::url('civicrm/contact/view/case',
412 "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}"
413 );
414 $session = CRM_Core_Session::singleton();
415 $session->pushUserContext($url);
416
417 // 3. format activity custom data
418 if (!empty($params['hidden_custom'])) {
419 $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $this->_activityTypeId);
420 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
421 CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE,
422 NULL, NULL, TRUE
423 )
424 );
425 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
426 $customFields,
427 $this->_activityId,
428 'Activity'
429 );
430 }
431
432 // 4. call end post process
433 if ($this->_activityTypeFile) {
434 $className::endPostProcess($this, $params);
435 }
436
437 // 5. auto populate activites
438
439 // 6. set status
440 CRM_Core_Session::setStatus($params['statusMsg'], ts('Saved'), 'success');
441 }
442
443 }