Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
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 | */ | |
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 | * 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'); | |
186 | eval("CRM_Case_Form_Activity_{$this->_activityTypeFile}::preProcess( \$this );"); | |
187 | $activityGroupTree = $this->_groupTree; | |
188 | ||
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); | |
193 | } | |
194 | ||
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; | |
198 | } | |
199 | ||
200 | /** | |
201 | * This function sets the default values for the form. For edit/view mode | |
202 | * the default values are retrieved from the database | |
203 | * | |
204 | * @access public | |
205 | * | |
206 | * @return None | |
207 | */ | |
208 | function setDefaultValues() { | |
209 | if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW || $this->_cdType) { | |
210 | return TRUE; | |
211 | } | |
212 | eval('$defaults = CRM_Case_Form_Activity_' . $this->_activityTypeFile . '::setDefaultValues($this);'); | |
213 | $defaults = array_merge($defaults, CRM_Custom_Form_CustomData::setDefaultValues($this)); | |
214 | return $defaults; | |
215 | } | |
216 | ||
217 | public function buildQuickForm() { | |
218 | $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process(); | |
219 | $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients(); | |
220 | $this->assign('multiClient', $isMultiClient); | |
221 | ||
222 | if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW) { | |
223 | $title = 'Delete'; | |
224 | if ($this->_action & CRM_Core_Action::RENEW) { | |
225 | $title = 'Restore'; | |
226 | } | |
227 | $this->addButtons(array( | |
228 | array( | |
229 | 'type' => 'next', | |
230 | 'name' => $title, | |
231 | 'spacing' => ' ', | |
232 | 'isDefault' => TRUE, | |
233 | ), | |
234 | array( | |
235 | 'type' => 'cancel', | |
236 | 'name' => ts('Cancel'), | |
237 | ), | |
238 | ) | |
239 | ); | |
240 | return; | |
241 | } | |
242 | ||
243 | if ($this->_cdType) { | |
244 | return CRM_Custom_Form_CustomData::buildQuickForm($this); | |
245 | } | |
246 | //need to assign custom data type and subtype to the template | |
247 | $this->assign('customDataType', 'Case'); | |
248 | ||
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); | |
252 | ||
253 | $s = CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'subject'); | |
254 | if (!is_array($s)) { | |
255 | $s = array(); | |
256 | } | |
257 | $this->add('text', 'activity_subject', ts('Subject'), | |
258 | array_merge($s, array( | |
259 | 'maxlength' => '128')), TRUE | |
260 | ); | |
261 | ||
262 | $tags = CRM_Core_BAO_Tag::getTags('civicrm_case'); | |
263 | if (!empty($tags)) { | |
264 | $this->add('select', 'tag', ts('Select Tags'), $tags, FALSE, | |
265 | array('id' => 'tags', 'multiple' => 'multiple', 'title' => ts('- select -')) | |
266 | ); | |
267 | } | |
268 | ||
269 | // build tag widget | |
270 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_case'); | |
271 | CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', NULL, FALSE, TRUE); | |
272 | ||
273 | $this->addButtons(array( | |
274 | array( | |
275 | 'type' => 'next', | |
276 | 'name' => ts('Save'), | |
277 | 'spacing' => ' ', | |
278 | 'isDefault' => TRUE, | |
279 | ), | |
280 | array( | |
281 | 'type' => 'cancel', | |
282 | 'name' => ts('Cancel'), | |
283 | ), | |
284 | ) | |
285 | ); | |
286 | ||
287 | eval("CRM_Case_Form_Activity_{$this->_activityTypeFile}::buildQuickForm( \$this );"); | |
288 | } | |
289 | ||
290 | /** | |
291 | * Add local and global form rules | |
292 | * | |
293 | * @access protected | |
294 | * | |
295 | * @return void | |
296 | */ | |
297 | function addRules() { | |
298 | if ($this->_action & CRM_Core_Action::DELETE || $this->_action & CRM_Core_Action::RENEW || $this->_cdType) { | |
299 | return TRUE; | |
300 | } | |
301 | eval('$this->addFormRule' . "(array('CRM_Case_Form_Activity_{$this->_activityTypeFile}', 'formrule'), \$this);"); | |
302 | $this->addFormRule(array('CRM_Case_Form_Case', 'formRule'), $this); | |
303 | } | |
304 | ||
305 | /** | |
306 | * global validation rules for the form | |
307 | * | |
308 | * @param array $values posted values of the form | |
309 | * | |
310 | * @return array list of errors to be posted back to the form | |
311 | * @static | |
312 | * @access public | |
313 | */ | |
314 | static function formRule($values, $files, $form) { | |
315 | return TRUE; | |
316 | } | |
317 | ||
318 | /** | |
319 | * Function to process the form | |
320 | * | |
321 | * @access public | |
322 | * | |
323 | * @return None | |
324 | */ | |
325 | public function postProcess() { | |
326 | $transaction = new CRM_Core_Transaction(); | |
327 | ||
328 | // check if dedupe button, if so return. | |
329 | $buttonName = $this->controller->getButtonName(); | |
330 | if (isset($this->_dedupeButtonName) && $buttonName == $this->_dedupeButtonName) { | |
331 | return; | |
332 | } | |
333 | ||
334 | if ($this->_action & CRM_Core_Action::DELETE) { | |
335 | $statusMsg = NULL; | |
336 | $caseDelete = CRM_Case_BAO_Case::deleteCase($this->_caseId, TRUE); | |
337 | if ($caseDelete) { | |
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 />'); | |
339 | } | |
340 | CRM_Core_Session::setStatus($statusMsg, ts('Case Deleted'), 'success'); | |
341 | return; | |
342 | } | |
343 | ||
344 | if ($this->_action & CRM_Core_Action::RENEW) { | |
345 | $statusMsg = NULL; | |
346 | $caseRestore = CRM_Case_BAO_Case::restoreCase($this->_caseId); | |
347 | if ($caseRestore) { | |
348 | $statusMsg = ts('The selected case has been restored.<br />'); | |
349 | } | |
350 | CRM_Core_Session::setStatus($statusMsg, ts('Restored'), 'success'); | |
351 | return; | |
352 | } | |
353 | // store the submitted values in an array | |
354 | $params = $this->controller->exportValues($this->_name); | |
355 | $params['now'] = date("Ymd"); | |
356 | ||
357 | ||
358 | // 1. call begin post process | |
359 | if ($this->_activityTypeFile) { | |
360 | eval("CRM_Case_Form_Activity_{$this->_activityTypeFile}" . "::beginPostProcess( \$this, \$params );"); | |
361 | } | |
362 | ||
363 | if (CRM_Utils_Array::value('hidden_custom', $params) && | |
364 | !isset($params['custom']) | |
365 | ) { | |
366 | $customFields = array(); | |
367 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
368 | $customFields, | |
369 | NULL, | |
370 | 'Case' | |
371 | ); | |
372 | } | |
373 | ||
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; | |
380 | } | |
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']); | |
385 | ||
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; | |
392 | } | |
393 | } | |
394 | CRM_Core_BAO_EntityTag::create($tagParams, 'civicrm_case', $caseObj->id); | |
395 | ||
396 | //save free tags | |
397 | if (isset($params['case_taglist']) && !empty($params['case_taglist'])) { | |
398 | CRM_Core_Form_Tag::postProcess($params['case_taglist'], $caseObj->id, 'civicrm_case', $this); | |
399 | } | |
400 | ||
401 | // user context | |
402 | $url = CRM_Utils_System::url('civicrm/contact/view/case', | |
403 | "reset=1&action=view&cid={$this->_currentlyViewedContactId}&id={$caseObj->id}" | |
404 | ); | |
405 | $session = CRM_Core_Session::singleton(); | |
406 | $session->pushUserContext($url); | |
407 | ||
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, | |
413 | NULL, NULL, TRUE | |
414 | ) | |
415 | ); | |
416 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
417 | $customFields, | |
418 | $this->_activityId, | |
419 | 'Activity' | |
420 | ); | |
421 | } | |
422 | ||
423 | // 4. call end post process | |
424 | if ($this->_activityTypeFile) { | |
425 | eval("CRM_Case_Form_Activity_{$this->_activityTypeFile}" . "::endPostProcess( \$this, \$params );"); | |
426 | } | |
427 | ||
428 | // 5. auto populate activites | |
429 | ||
430 | // 6. set status | |
79dc2175 | 431 | CRM_Core_Session::setStatus($params['statusMsg'], ts('Saved'), 'success'); |
6a488035 TO |
432 | } |
433 | } | |
434 |