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