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