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 | * | |
31 | * @package CRM | |
32 | * @copyright CiviCRM LLC (c) 2004-2013 | |
33 | * $Id$ | |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * This class generates form components for processing Event | |
39 | * | |
40 | */ | |
41 | class CRM_Event_Form_ManageEvent_Registration extends CRM_Event_Form_ManageEvent { | |
42 | ||
43 | /** | |
44 | * what blocks should we show and hide. | |
45 | * | |
46 | * @var CRM_Core_ShowHideBlocks | |
47 | */ | |
48 | protected $_showHide; | |
49 | ||
50 | protected $_profilePostMultiple = array(); | |
51 | protected $_profilePostMultipleAdd = array(); | |
52 | ||
53 | /** | |
54 | * Function to set variables up before form is built | |
55 | * | |
56 | * @return void | |
57 | * @access public | |
58 | */ | |
59 | function preProcess() { | |
60 | $this->_addProfileBottom = CRM_Utils_Array::value('addProfileBottom', $_GET, FALSE); | |
61 | $this->_profileBottomNum = CRM_Utils_Array::value('addProfileNum', $_GET, 0); | |
62 | $this->_addProfileBottomAdd = CRM_Utils_Array::value('addProfileBottomAdd', $_GET, FALSE); | |
63 | $this->_profileBottomNumAdd = CRM_Utils_Array::value('addProfileNumAdd', $_GET, 0); | |
64 | ||
65 | parent::preProcess(); | |
66 | ||
67 | $this->assign('addProfileBottom', $this->_addProfileBottom); | |
68 | $this->assign('profileBottomNum', $this->_profileBottomNum); | |
69 | ||
70 | $urlParams = "id={$this->_id}&addProfileBottom=1&qfKey={$this->controller->_key}"; | |
71 | $this->assign('addProfileParams', $urlParams); | |
72 | ||
73 | if ($addProfileBottom = CRM_Utils_Array::value('custom_post_id_multiple', $_POST)) { | |
74 | foreach (array_keys($addProfileBottom) as $profileNum) { | |
75 | self::buildMultipleProfileBottom($this, $profileNum); | |
76 | } | |
77 | } | |
78 | ||
79 | $this->assign('addProfileBottomAdd', $this->_addProfileBottomAdd); | |
80 | $this->assign('profileBottomNumAdd', $this->_profileBottomNumAdd); | |
81 | ||
82 | $urlParamsAdd = "id={$this->_id}&addProfileBottomAdd=1&qfKey={$this->controller->_key}"; | |
83 | $this->assign('addProfileParamsAdd', $urlParamsAdd); | |
84 | ||
85 | if ($addProfileBottomAdd = CRM_Utils_Array::value('additional_custom_post_id_multiple', $_POST)) { | |
86 | foreach (array_keys($addProfileBottomAdd) as $profileNum) { | |
87 | self::buildMultipleProfileBottom($this, $profileNum, 'additional_', ts('Profile for Additional Participants')); | |
88 | } | |
89 | } | |
90 | } | |
91 | ||
92 | /** | |
93 | * This function sets the default values for the form. | |
94 | * the default values are retrieved from the database | |
95 | * | |
96 | * @access public | |
97 | * | |
98 | * @return None | |
99 | */ | |
100 | function setDefaultValues() { | |
101 | if ($this->_addProfileBottom || $this->_addProfileBottomAdd) { | |
102 | return; | |
103 | } | |
104 | $eventId = $this->_id; | |
105 | ||
106 | $defaults = parent::setDefaultValues(); | |
107 | ||
108 | $this->setShowHide($defaults); | |
109 | if (isset($eventId)) { | |
110 | $params = array('id' => $eventId); | |
111 | CRM_Event_BAO_Event::retrieve($params, $defaults); | |
112 | ||
113 | $ufJoinParams = array( | |
114 | 'entity_table' => 'civicrm_event', | |
115 | 'module' => 'CiviEvent', | |
116 | 'entity_id' => $eventId, | |
117 | ); | |
118 | ||
119 | list($defaults['custom_pre_id'], | |
120 | $defaults['custom_post'] | |
121 | ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
122 | ||
123 | // Get the id for the event registration profile | |
124 | $eventRegistrationIdParams = $eventRegistrationIdDefaults = array( | |
125 | 'name' => 'event_registration', | |
126 | ); | |
127 | CRM_Core_BAO_UFGroup::retrieve($eventRegistrationIdParams, $eventRegistrationIdDefaults); | |
128 | ||
129 | // Set event registration as the default profile if none selected | |
130 | if (!$defaults['custom_pre_id'] && count($defaults['custom_post']) == 0) { | |
131 | $defaults['custom_pre_id'] = CRM_Utils_Array::value('id', $eventRegistrationIdDefaults); | |
132 | } | |
133 | if (isset($defaults['custom_post']) && is_numeric($defaults['custom_post'])) { | |
134 | $defaults['custom_post_id'] = $defaults['custom_post']; | |
135 | } | |
136 | elseif (!empty($defaults['custom_post'])) { | |
137 | $defaults['custom_post_id'] = $defaults['custom_post'][0]; | |
138 | unset($defaults['custom_post'][0]); | |
139 | $this->_profilePostMultiple = $defaults['custom_post']; | |
140 | foreach ($defaults['custom_post'] as $key => $value) { | |
141 | self::buildMultipleProfileBottom($this, $key); | |
142 | $defaults["custom_post_id_multiple[$key]"] = $value; | |
143 | } | |
144 | } | |
145 | ||
146 | $this->assign('profilePostMultiple', CRM_Utils_Array::value('custom_post', $defaults)); | |
147 | ||
148 | if (CRM_Utils_Array::value('is_multiple_registrations', $defaults)) { | |
149 | // CRM-4377: set additional participants’ profiles – set to ‘none’ if explicitly unset (non-active) | |
150 | ||
151 | $ufJoinAddParams = array( | |
152 | 'entity_table' => 'civicrm_event', | |
153 | 'module' => 'CiviEvent_Additional', | |
154 | 'entity_id' => $eventId, | |
155 | ); | |
156 | ||
157 | list($defaults['additional_custom_pre_id'], | |
158 | $defaults['additional_custom_post'] | |
159 | ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinAddParams); | |
160 | ||
161 | if (isset($defaults['additional_custom_post']) && is_numeric($defaults['additional_custom_post'])) { | |
162 | $defaults['additional_custom_post_id'] = $defaults['additional_custom_post']; | |
163 | } | |
164 | elseif (!empty($defaults['additional_custom_post'])) { | |
165 | $defaults['additional_custom_post_id'] = $defaults['additional_custom_post'][0]; | |
166 | unset($defaults['additional_custom_post'][0]); | |
167 | ||
168 | $this->_profilePostMultipleAdd = $defaults['additional_custom_post']; | |
169 | foreach ($defaults['additional_custom_post'] as $key => $value) { | |
170 | self::buildMultipleProfileBottom($this, $key, 'additional_', ts('Profile for Additional Participants')); | |
171 | $defaults["additional_custom_post_id_multiple[$key]"] = $value; | |
172 | } | |
173 | } | |
174 | $this->assign('profilePostMultipleAdd', CRM_Utils_Array::value('additional_custom_post', $defaults)); | |
175 | } | |
176 | } | |
177 | else { | |
178 | $defaults['is_email_confirm'] = 0; | |
179 | } | |
180 | ||
181 | // provide defaults for required fields if empty (and as a 'hint' for approval message field) | |
182 | $defaults['registration_link_text'] = CRM_Utils_Array::value('registration_link_text', $defaults, ts('Register Now')); | |
183 | $defaults['confirm_title'] = CRM_Utils_Array::value('confirm_title', $defaults, ts('Confirm Your Registration Information')); | |
184 | $defaults['thankyou_title'] = CRM_Utils_Array::value('thankyou_title', $defaults, ts('Thank You for Registering')); | |
185 | $defaults['approval_req_text'] = CRM_Utils_Array::value('approval_req_text', $defaults, ts('Participation in this event requires approval. Submit your registration request here. Once approved, you will receive an email with a link to a web page where you can complete the registration process.')); | |
186 | ||
187 | if (CRM_Utils_Array::value('registration_start_date', $defaults)) { | |
188 | list($defaults['registration_start_date'], | |
189 | $defaults['registration_start_date_time'] | |
190 | ) = CRM_Utils_Date::setDateDefaults($defaults['registration_start_date'], | |
191 | 'activityDateTime' | |
192 | ); | |
193 | } | |
194 | ||
195 | if (CRM_Utils_Array::value('registration_end_date', $defaults)) { | |
196 | list($defaults['registration_end_date'], | |
197 | $defaults['registration_end_date_time'] | |
198 | ) = CRM_Utils_Date::setDateDefaults($defaults['registration_end_date'], | |
199 | 'activityDateTime' | |
200 | ); | |
201 | } | |
202 | ||
203 | return $defaults; | |
204 | } | |
205 | ||
206 | /** | |
207 | * Fix what blocks to show/hide based on the default values set | |
208 | * | |
209 | * @param array $defaults the array of default values | |
210 | * @param boolean $force should we set show hide based on input defaults | |
211 | * | |
212 | * @return void | |
213 | */ | |
214 | function setShowHide($defaults) { | |
215 | $this->_showHide = new CRM_Core_ShowHideBlocks(array('registration' => 1), | |
216 | '' | |
217 | ); | |
218 | if (empty($defaults)) { | |
219 | $this->_showHide->addHide('registration'); | |
220 | $this->_showHide->addHide('additional_profile_pre'); | |
221 | $this->_showHide->addHide('additional_profile_post'); | |
222 | $this->_showHide->addHide('id-approval-text'); | |
223 | } | |
224 | else { | |
225 | if (!CRM_Utils_Array::value('is_multiple_registrations', $defaults)) { | |
226 | $this->_showHide->addHide('additional_profile_pre'); | |
227 | $this->_showHide->addHide('additional_profile_post'); | |
228 | } | |
229 | if (!CRM_Utils_Array::value('requires_approval', $defaults)) { | |
230 | $this->_showHide->addHide('id-approval-text'); | |
231 | } | |
232 | } | |
233 | $this->assign('defaultsEmpty', empty($defaults)); | |
234 | $this->_showHide->addToTemplate(); | |
235 | } | |
236 | ||
237 | /** | |
238 | * Function to build the form | |
239 | * | |
240 | * @return None | |
241 | * @access public | |
242 | */ | |
243 | public function buildQuickForm() { | |
244 | if ($this->_addProfileBottom) { | |
245 | return self::buildMultipleProfileBottom($this, $this->_profileBottomNum); | |
246 | } | |
247 | ||
248 | if ($this->_addProfileBottomAdd) { | |
249 | return self::buildMultipleProfileBottom($this, $this->_profileBottomNumAdd, 'additional_', ts('Profile for Additional Participants')); | |
250 | } | |
251 | ||
252 | $this->applyFilter('__ALL__', 'trim'); | |
253 | $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event'); | |
254 | ||
255 | $this->addElement('checkbox', | |
256 | 'is_online_registration', | |
257 | ts('Allow Online Registration?'), | |
258 | NULL, | |
259 | array( | |
260 | 'onclick' => "return showHideByValue('is_online_registration', | |
261 | '', | |
262 | 'registration_blocks', | |
263 | 'block', | |
264 | 'radio', | |
265 | false );", | |
266 | ) | |
267 | ); | |
268 | ||
269 | $this->add('text', 'registration_link_text', ts('Registration Link Text')); | |
270 | ||
271 | if (!$this->_isTemplate) { | |
272 | $this->addDateTime('registration_start_date', ts('Registration Start Date'), FALSE, array('formatType' => 'activityDateTime')); | |
273 | $this->addDateTime('registration_end_date', ts('Registration End Date'), FALSE, array('formatType' => 'activityDateTime')); | |
274 | } | |
275 | ||
276 | $params = array( | |
277 | 'used' => 'Supervised', | |
278 | 'contact_type' => 'Individual', | |
279 | ); | |
280 | $dedupeRuleFields = CRM_Dedupe_BAO_Rule::dedupeRuleFields($params); | |
281 | ||
282 | foreach ($dedupeRuleFields as $key => $fields) { | |
283 | $ruleFields[$key] = ucwords(str_replace('_', ' ', $fields)); | |
284 | } | |
285 | ||
286 | $this->addElement('checkbox', | |
287 | 'is_multiple_registrations', | |
288 | ts('Register multiple participants?'), | |
289 | NULL, | |
290 | array( | |
291 | 'onclick' => "return (showHideByValue('is_multiple_registrations', '', 'additional_profile_pre|additional_profile_post', 'table-row', 'radio', false) || | |
292 | showRuleFields( " . json_encode($ruleFields) . " ));") | |
293 | ); | |
294 | ||
295 | $this->addElement('checkbox', | |
296 | 'allow_same_participant_emails', | |
297 | ts('Allow multiple registrations from the same email address?'), | |
298 | NULL, | |
299 | array('onclick' => "return showRuleFields( " . json_encode($ruleFields) . " );") | |
300 | ); | |
301 | $this->assign('ruleFields', json_encode($ruleFields)); | |
302 | ||
303 | $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); | |
304 | if (in_array('Awaiting approval', $participantStatuses) and in_array('Pending from approval', $participantStatuses) and in_array('Rejected', $participantStatuses)) { | |
305 | $this->addElement('checkbox', | |
306 | 'requires_approval', | |
307 | ts('Require participant approval?'), | |
308 | NULL, | |
309 | array('onclick' => "return showHideByValue('requires_approval', '', 'id-approval-text', 'table-row', 'radio', false);") | |
310 | ); | |
311 | $this->add('textarea', 'approval_req_text', ts('Approval message'), $attributes['approval_req_text']); | |
312 | } | |
313 | ||
314 | $this->add('text', 'expiration_time', ts('Pending participant expiration (hours)')); | |
315 | $this->addRule('expiration_time', ts('Please enter the number of hours (as an integer).'), 'integer'); | |
316 | ||
317 | self::buildRegistrationBlock($this); | |
318 | self::buildConfirmationBlock($this); | |
319 | self::buildMailBlock($this); | |
320 | self::buildThankYouBlock($this); | |
321 | ||
322 | parent::buildQuickForm(); | |
323 | } | |
324 | ||
325 | /** | |
326 | * Function to build Registration Block | |
327 | * | |
328 | * @param int $pageId | |
329 | * @static | |
330 | */ | |
331 | function buildRegistrationBlock(&$form) { | |
332 | $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event'); | |
333 | $form->addWysiwyg('intro_text', ts('Introductory Text'), $attributes['intro_text']); | |
334 | // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing | |
335 | // explicit height and width. | |
336 | $form->addWysiwyg('footer_text', ts('Footer Text'), array('rows' => 2, 'cols' => 40)); | |
337 | ||
338 | $types = array_merge(array('Contact', 'Individual', 'Participant'), | |
339 | CRM_Contact_BAO_ContactType::subTypes('Individual') | |
340 | ); | |
341 | ||
342 | $profiles = CRM_Core_BAO_UFGroup::getProfiles($types); | |
343 | ||
344 | $mainProfiles = array( | |
345 | '' => ts('- select -')) + $profiles; | |
346 | $addtProfiles = array( | |
347 | '' => ts('- same as for main contact -')) + $profiles; | |
348 | ||
349 | $form->add('select', 'custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $mainProfiles); | |
350 | $form->add('select', 'custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $mainProfiles); | |
351 | ||
352 | $form->add('select', 'additional_custom_pre_id', ts('Profile for Additional Participants') . '<br />' . ts('(top of page)'), $addtProfiles); | |
47ecc69e DG |
353 | // Allow user to NOT provide a bottom profile for Additional Participant registration |
354 | $form->add('select', 'additional_custom_post_id', ts('Profile for Additional Participants') . '<br />' . ts('(bottom of page)'), array('none' => ts('- no profile -')) + $addtProfiles); | |
6a488035 TO |
355 | } |
356 | ||
357 | function buildMultipleProfileBottom(&$form, $count, $prefix = '', $name = 'Include Profile') { | |
358 | $types = array_merge(array('Contact', 'Individual', 'Participant'), | |
359 | CRM_Contact_BAO_ContactType::subTypes('Individual') | |
360 | ); | |
361 | ||
362 | $profiles = CRM_Core_BAO_UFGroup::getProfiles($types); | |
363 | ||
364 | if ($prefix == 'additional_') { | |
365 | $mainProfiles = array( | |
47ecc69e | 366 | '' => ts('- same as for main contact -'), 'none' => ts('- no profile -')) + $profiles; |
6a488035 TO |
367 | } |
368 | else { | |
369 | $mainProfiles = array( | |
370 | '' => ts('- select -')) + $profiles; | |
371 | } | |
372 | ||
373 | $element = $prefix . "custom_post_id_multiple[$count]"; | |
374 | $form->add('select', $element, $name . '<br />' . ts('(bottom of page)'), $mainProfiles); | |
375 | } | |
376 | ||
377 | /** | |
378 | * Function to build Confirmation Block | |
379 | * | |
380 | * @param int $pageId | |
381 | * @static | |
382 | */ | |
383 | function buildConfirmationBlock(&$form) { | |
384 | $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event'); | |
385 | $form->add('text', 'confirm_title', ts('Title'), $attributes['confirm_title']); | |
386 | $form->addWysiwyg('confirm_text', ts('Introductory Text'), $attributes['confirm_text']); | |
387 | // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing | |
388 | // explicit height and width. | |
389 | $form->addWysiwyg('confirm_footer_text', ts('Footer Text'), array('rows' => 2, 'cols' => 40)); | |
390 | } | |
391 | ||
392 | /** | |
393 | * Function to build Email Block | |
394 | * | |
395 | * @param int $pageId | |
396 | * @static | |
397 | */ | |
398 | function buildMailBlock(&$form) { | |
399 | $form->registerRule('emailList', 'callback', 'emailList', 'CRM_Utils_Rule'); | |
400 | $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event'); | |
401 | $form->addYesNo('is_email_confirm', ts('Send Confirmation Email?'), NULL, NULL, array('onclick' => "return showHideByValue('is_email_confirm','','confirmEmail','block','radio',false);")); | |
402 | $form->add('textarea', 'confirm_email_text', ts('Text'), $attributes['confirm_email_text']); | |
403 | $form->add('text', 'cc_confirm', ts('CC Confirmation To'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'cc_confirm')); | |
404 | $form->addRule('cc_confirm', ts('Please enter a valid list of comma delimited email addresses'), 'emailList'); | |
405 | $form->add('text', 'bcc_confirm', ts('BCC Confirmation To'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'bcc_confirm')); | |
406 | $form->addRule('bcc_confirm', ts('Please enter a valid list of comma delimited email addresses'), 'emailList'); | |
407 | $form->add('text', 'confirm_from_name', ts('Confirm From Name')); | |
408 | $form->add('text', 'confirm_from_email', ts('Confirm From Email')); | |
409 | $form->addRule('confirm_from_email', ts('Email is not valid.'), 'email'); | |
410 | } | |
411 | ||
412 | function buildThankYouBlock(&$form) { | |
413 | $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event'); | |
414 | $form->add('text', 'thankyou_title', ts('Title'), $attributes['thankyou_title']); | |
415 | $form->addWysiwyg('thankyou_text', ts('Introductory Text'), $attributes['thankyou_text']); | |
416 | // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing | |
417 | // explicit height and width. | |
418 | $form->addWysiwyg('thankyou_footer_text', ts('Footer Text'), array('rows' => 2, 'cols' => 40)); | |
419 | } | |
420 | ||
421 | /** | |
422 | * Add local and global form rules | |
423 | * | |
424 | * @access protected | |
425 | * | |
426 | * @return void | |
427 | */ | |
428 | function addRules() { | |
429 | if ($this->_addProfileBottom || $this->_addProfileBottomAdd) { | |
430 | return; | |
431 | } | |
432 | $this->addFormRule(array('CRM_Event_Form_ManageEvent_Registration', 'formRule'), $this); | |
433 | } | |
434 | ||
435 | /** | |
436 | * global validation rules for the form | |
437 | * | |
438 | * @param array $fields posted values of the form | |
439 | * | |
440 | * @return array list of errors to be posted back to the form | |
441 | * @static | |
442 | * @access public | |
443 | */ | |
444 | static function formRule($values, $files, $form) { | |
445 | if (CRM_Utils_Array::value('is_online_registration', $values)) { | |
446 | ||
447 | if (!$values['confirm_title']) { | |
448 | $errorMsg['confirm_title'] = ts('Please enter a Title for the registration Confirmation Page'); | |
449 | } | |
450 | if (!$values['thankyou_title']) { | |
451 | $errorMsg['thankyou_title'] = ts('Please enter a Title for the registration Thank-you Page'); | |
452 | } | |
453 | if ($values['is_email_confirm']) { | |
454 | if (!$values['confirm_from_name']) { | |
455 | $errorMsg['confirm_from_name'] = ts('Please enter Confirmation Email FROM Name.'); | |
456 | } | |
457 | ||
458 | if (!$values['confirm_from_email']) { | |
459 | $errorMsg['confirm_from_email'] = ts('Please enter Confirmation Email FROM Email Address.'); | |
460 | } | |
461 | } | |
462 | ||
463 | if ($values['registration_start_date'] && $values['registration_end_date']) { | |
464 | $start = CRM_Utils_Date::processDate($values['registration_start_date']); | |
465 | $end = CRM_Utils_Date::processDate($values['registration_end_date']); | |
466 | if ($end < $start) { | |
467 | $errorMsg['registration_end_date'] = ts('Registration end date should be after Registration start date'); | |
468 | } | |
469 | } | |
470 | ||
471 | //check that the selected profiles have either firstname+lastname or email required | |
472 | $profileIds = array( | |
473 | CRM_Utils_Array::value('custom_pre_id', $values), | |
474 | CRM_Utils_Array::value('custom_post_id', $values), | |
475 | ); | |
476 | $additionalProfileIds = array( | |
477 | CRM_Utils_Array::value('additional_custom_pre_id', $values), | |
478 | CRM_Utils_Array::value('additional_custom_post_id', $values), | |
479 | ); | |
480 | //additional profile fields default to main if not set | |
481 | if (!is_numeric($additionalProfileIds[0])) { | |
482 | $additionalProfileIds[0] = $profileIds[0]; | |
483 | } | |
484 | if (!is_numeric($additionalProfileIds[1])) { | |
485 | $additionalProfileIds[1] = $profileIds[1]; | |
486 | } | |
487 | //add multiple profiles if set | |
488 | self::addMultipleProfiles($profileIds, $values, 'custom_post_id_multiple'); | |
489 | self::addMultipleProfiles($additionalProfileIds, $values, 'additional_custom_post_id_multiple'); | |
490 | $isProfileComplete = self::isProfileComplete($profileIds); | |
491 | $isAdditionalProfileComplete = self::isProfileComplete($additionalProfileIds); | |
492 | //Check main profiles have an email address available if 'send confirmation email' is selected | |
493 | if ($values['is_email_confirm']) { | |
494 | $emailFields = self::getEmailFields($profileIds); | |
495 | if (!count($emailFields)) { | |
496 | $errorMsg['is_email_confirm'] = ts("Please add a profile with an email address if 'Send Confirmation Email?' is selected"); | |
497 | } | |
498 | } | |
499 | $additionalCustomPreId = $additionalCustomPostId = NULL; | |
500 | $isPreError = $isPostError = TRUE; | |
501 | if (CRM_Utils_Array::value('allow_same_participant_emails', $values) && | |
502 | CRM_Utils_Array::value('is_multiple_registrations', $values) | |
503 | ) { | |
504 | $types = array_merge(array('Individual'), CRM_Contact_BAO_ContactType::subTypes('Individual')); | |
505 | $profiles = CRM_Core_BAO_UFGroup::getProfiles($types); | |
506 | ||
507 | //check for additional custom pre profile | |
508 | $additionalCustomPreId = CRM_Utils_Array::value('additional_custom_pre_id', $values); | |
509 | if (!empty($additionalCustomPreId)) { | |
510 | if (!($additionalCustomPreId == 'none')) { | |
511 | $customPreId = $additionalCustomPreId; | |
512 | } | |
513 | else { | |
514 | $isPreError = FALSE; | |
515 | } | |
516 | } | |
517 | else { | |
518 | $customPreId = CRM_Utils_Array::value('custom_pre_id', $values) ? $values['custom_pre_id'] : NULL; | |
519 | } | |
520 | //check whether the additional custom pre profile is of type 'Individual' and its subtypes | |
521 | if (!empty($customPreId)) { | |
522 | $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPreId); | |
523 | foreach ($types as $individualTypes) { | |
524 | if (in_array($individualTypes, $profileTypes)) { | |
525 | $isPreError = FALSE; | |
526 | break; | |
527 | } | |
528 | } | |
529 | } | |
530 | else { | |
531 | $isPreError = FALSE; | |
532 | } | |
dbeb7efb DG |
533 | |
534 | // We don't have required Individual fields in the pre-custom profile, so now check the post-custom profile | |
535 | if ($isPreError) { | |
536 | $additionalCustomPostId = CRM_Utils_Array::value('additional_custom_post_id', $values); | |
537 | if (!empty($additionalCustomPostId)) { | |
538 | if (!($additionalCustomPostId == 'none')) { | |
539 | $customPostId = $additionalCustomPostId; | |
540 | } | |
541 | else { | |
542 | $isPostError = FALSE; | |
543 | } | |
6a488035 TO |
544 | } |
545 | else { | |
dbeb7efb | 546 | $customPostId = CRM_Utils_Array::value('custom_post_id', $values) ? $values['custom_post_id'] : NULL; |
6a488035 | 547 | } |
dbeb7efb DG |
548 | //check whether the additional custom post profile is of type 'Individual' and its subtypes |
549 | if (!empty($customPostId)) { | |
550 | $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPostId); | |
551 | foreach ($types as $individualTypes) { | |
552 | if (in_array($individualTypes, $profileTypes)) { | |
553 | $isPostError = FALSE; | |
554 | break; | |
555 | } | |
6a488035 TO |
556 | } |
557 | } | |
dbeb7efb DG |
558 | else { |
559 | $isPostError = FALSE; | |
560 | } | |
561 | ||
562 | if (empty($customPreId) && empty($customPostId)) { | |
563 | $errorMsg['additional_custom_pre_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'"); | |
564 | } | |
565 | if ($isPostError) { | |
566 | $errorMsg['additional_custom_post_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'"); | |
567 | } | |
6a488035 TO |
568 | } |
569 | } | |
570 | if (!$isProfileComplete) { | |
571 | $errorMsg['custom_pre_id'] = ts("Please include a Profile for online registration that contains an Email Address field and / or First Name + Last Name fields."); | |
572 | } | |
573 | if (!$isAdditionalProfileComplete) { | |
574 | $errorMsg['additional_custom_pre_id'] = ts("Please include a Profile for online registration of additional participants that contains an Email Address field and / or First Name + Last Name fields."); | |
575 | } | |
576 | ||
577 | // // CRM-8485 | |
578 | // $config = CRM_Core_Config::singleton(); | |
579 | // if ( $config->doNotAttachPDFReceipt ) { | |
580 | // if ( CRM_Utils_Array::value('custom_post_id_multiple', $values) ) { | |
581 | // foreach( $values['custom_post_id_multiple'] as $count => $customPostMultiple ) { | |
582 | // if ( $customPostMultiple ) { | |
583 | // $errorMsg["custom_post_id_multiple[{$count}]"] = ts('Please disable PDF receipt as an attachment in <a href="%1">Miscellaneous Settings</a> if you want to add additional profiles.', array( 1 => CRM_Utils_System::url( 'civicrm/admin/setting/misc', 'reset=1' ) ) ); | |
584 | // break; | |
585 | // } | |
586 | // } | |
587 | // } | |
588 | // | |
589 | // if ( CRM_Utils_Array::value('is_multiple_registrations', $values) && | |
590 | // CRM_Utils_Array::value('additional_custom_post_id_multiple', $values) ) { | |
591 | // foreach( $values['additional_custom_post_id_multiple'] as $count => $customPostMultiple ) { | |
592 | // if ( $customPostMultiple ) { | |
593 | // $errorMsg["additional_custom_post_id_multiple[{$count}]"] = ts('Please disable PDF receipt as an attachment in <a href="%1">Miscellaneous Settings</a> if you want to add additional profiles.', array( 1 => CRM_Utils_System::url( 'civicrm/admin/setting/misc', 'reset=1' ) ) ); | |
594 | // break; | |
595 | // } | |
596 | // } | |
597 | // } | |
598 | // } | |
599 | ||
600 | if (!empty($errorMsg)) { | |
601 | if (CRM_Utils_Array::value('custom_post_id_multiple', $values)) { | |
602 | foreach ($values['custom_post_id_multiple'] as $count => $customPostMultiple) { | |
603 | self::buildMultipleProfileBottom($form, $count); | |
604 | } | |
605 | $form->assign('profilePostMultiple', $values['custom_post_id_multiple']); | |
606 | } | |
607 | if (CRM_Utils_Array::value('additional_custom_post_id_multiple', $values)) { | |
608 | foreach ($values['additional_custom_post_id_multiple'] as $count => $customPostMultiple) { | |
609 | self::buildMultipleProfileBottom($form, $count, 'additional_', ts('Profile for Additional Participants')); | |
610 | } | |
611 | $form->assign('profilePostMultipleAdd', $values['additional_custom_post_id_multiple']); | |
612 | } | |
613 | } | |
614 | } | |
615 | ||
616 | if (!empty($errorMsg)) { | |
617 | return $errorMsg; | |
618 | } | |
619 | ||
620 | return TRUE; | |
621 | } | |
622 | ||
623 | /** | |
624 | * Collect all email fields for an array of profile ids | |
625 | * | |
626 | * @return boolean | |
627 | */ | |
628 | static function getEmailFields($profileIds) { | |
629 | $emailFields = array(); | |
630 | foreach ($profileIds as $profileId) { | |
631 | if ($profileId && is_numeric($profileId)) { | |
632 | $fields = CRM_Core_BAO_UFGroup::getFields($profileId); | |
633 | foreach ($fields as $field) { | |
634 | if (substr_count($field['name'], 'email')) { | |
635 | $emailFields[] = $field; | |
636 | } | |
637 | } | |
638 | } | |
639 | } | |
640 | return $emailFields; | |
641 | } | |
642 | ||
643 | /** | |
644 | * Check if a profile contains required fields | |
645 | * | |
646 | * @return boolean | |
647 | */ | |
648 | static function isProfileComplete($profileIds) { | |
649 | $profileReqFields = array(); | |
650 | foreach ($profileIds as $profileId) { | |
651 | if ($profileId && is_numeric($profileId)) { | |
652 | $fields = CRM_Core_BAO_UFGroup::getFields($profileId); | |
653 | foreach ($fields as $field) { | |
654 | switch (TRUE) { | |
655 | case substr_count($field['name'], 'email'): | |
656 | $profileReqFields[] = 'email'; | |
657 | break; | |
658 | ||
659 | case substr_count($field['name'], 'first_name'): | |
660 | $profileReqFields[] = 'first_name'; | |
661 | break; | |
662 | ||
663 | case substr_count($field['name'], 'last_name'): | |
664 | $profileReqFields[] = 'last_name'; | |
665 | break; | |
666 | } | |
667 | } | |
668 | } | |
669 | } | |
670 | $profileComplete = (in_array('email', $profileReqFields) | |
671 | || (in_array('first_name', $profileReqFields) && in_array('last_name', $profileReqFields)) | |
672 | ); | |
673 | return $profileComplete; | |
674 | } | |
675 | ||
676 | /** | |
677 | * Add additional profiles from the form to an array of profile ids. | |
678 | * | |
679 | */ | |
680 | static function addMultipleProfiles(&$profileIds, $values, $field) { | |
681 | if ($multipleProfiles = CRM_Utils_Array::value($field, $values)) { | |
682 | foreach ($multipleProfiles as $profileId) { | |
683 | $profileIds[] = $profileId; | |
684 | } | |
685 | } | |
686 | } | |
687 | ||
688 | /** | |
689 | * Function to process the form | |
690 | * | |
691 | * @access public | |
692 | * | |
693 | * @return None | |
694 | */ | |
695 | public function postProcess() { | |
696 | $params = array(); | |
697 | $params = $this->exportValues(); | |
698 | ||
699 | $params['id'] = $this->_id; | |
700 | ||
701 | //format params | |
702 | $params['is_online_registration'] = CRM_Utils_Array::value('is_online_registration', $params, FALSE); | |
703 | $params['is_multiple_registrations'] = CRM_Utils_Array::value('is_multiple_registrations', $params, FALSE); | |
704 | $params['allow_same_participant_emails'] = CRM_Utils_Array::value('allow_same_participant_emails', $params, FALSE); | |
705 | $params['requires_approval'] = CRM_Utils_Array::value('requires_approval', $params, FALSE); | |
706 | ||
707 | // reset is_email confirm if not online reg | |
708 | if (!$params['is_online_registration']) { | |
709 | $params['is_email_confirm'] = FALSE; | |
710 | } | |
711 | ||
712 | if (!$this->_isTemplate) { | |
713 | $params['registration_start_date'] = CRM_Utils_Date::processDate($params['registration_start_date'], | |
714 | $params['registration_start_date_time'], | |
715 | TRUE | |
716 | ); | |
717 | $params['registration_end_date'] = CRM_Utils_Date::processDate($params['registration_end_date'], | |
718 | $params['registration_end_date_time'], | |
719 | TRUE | |
720 | ); | |
721 | } | |
722 | ||
723 | CRM_Event_BAO_Event::add($params); | |
724 | ||
725 | // also update the ProfileModule tables | |
726 | $ufJoinParams = array( | |
727 | 'is_active' => 1, | |
728 | 'module' => 'CiviEvent', | |
729 | 'entity_table' => 'civicrm_event', | |
730 | 'entity_id' => $this->_id, | |
731 | ); | |
732 | ||
733 | ||
734 | // first delete all past entries | |
735 | CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams); | |
736 | ||
737 | $uf = array(); | |
738 | $wt = 2; | |
739 | if (!empty($params['custom_pre_id'])) { | |
740 | $uf[1] = $params['custom_pre_id']; | |
741 | $wt = 1; | |
742 | } | |
743 | ||
744 | if (!empty($params['custom_post_id'])) { | |
745 | $uf[2] = $params['custom_post_id']; | |
746 | } | |
747 | ||
748 | if (CRM_Utils_Array::value('custom_post_id_multiple', $params)) { | |
749 | $uf = array_merge($uf, $params['custom_post_id_multiple']); | |
750 | } | |
751 | $uf = array_values($uf); | |
752 | if (!empty($uf)) { | |
753 | foreach ($uf as $weight => $ufGroupId) { | |
754 | $ufJoinParams['weight'] = $weight + $wt; | |
755 | $ufJoinParams['uf_group_id'] = $ufGroupId; | |
756 | CRM_Core_BAO_UFJoin::create($ufJoinParams); | |
757 | unset($ufJoinParams['id']); | |
758 | } | |
759 | } | |
760 | // also update the ProfileModule tables | |
761 | $ufJoinParamsAdd = array( | |
762 | 'is_active' => 1, | |
763 | 'module' => 'CiviEvent_Additional', | |
764 | 'entity_table' => 'civicrm_event', | |
765 | 'entity_id' => $this->_id, | |
766 | ); | |
767 | ||
768 | // first delete all past entries | |
769 | CRM_Core_BAO_UFJoin::deleteAll($ufJoinParamsAdd); | |
770 | if (CRM_Utils_Array::value('is_multiple_registrations', $params)) { | |
771 | $ufAdd = array(); | |
772 | $wtAdd = 2; | |
773 | ||
774 | if (array_key_exists('additional_custom_pre_id', $params)) { | |
775 | if (!CRM_Utils_Array::value('additional_custom_pre_id', $params)) { | |
776 | $ufAdd[1] = $params['custom_pre_id']; | |
777 | $wtAdd = 1; | |
778 | } | |
779 | elseif (CRM_Utils_Array::value('additional_custom_pre_id', $params) == 'none') {} | |
780 | else { | |
781 | $ufAdd[1] = $params['additional_custom_pre_id']; | |
782 | $wtAdd = 1; | |
783 | } | |
784 | } | |
785 | ||
786 | if (array_key_exists('additional_custom_post_id', $params)) { | |
787 | if (!CRM_Utils_Array::value('additional_custom_post_id', $params)) { | |
788 | $ufAdd[2] = $params['custom_post_id']; | |
789 | } | |
790 | elseif (CRM_Utils_Array::value('additional_custom_post_id', $params) == 'none') {} | |
791 | else { | |
792 | $ufAdd[2] = $params['additional_custom_post_id']; | |
793 | } | |
794 | } | |
795 | ||
796 | if (CRM_Utils_Array::value('additional_custom_post_id_multiple', $params)) { | |
797 | $additionalPostMultiple = array(); | |
798 | foreach ($params['additional_custom_post_id_multiple'] as $key => $value) { | |
799 | if (!$value && CRM_Utils_Array::value('custom_post_id', $params)) { | |
800 | $additionalPostMultiple[$key] = $params['custom_post_id']; | |
801 | } | |
802 | elseif ($value == 'none') { | |
803 | continue; | |
804 | } | |
805 | elseif ($value) { | |
806 | $additionalPostMultiple[$key] = $value; | |
807 | } | |
808 | } | |
809 | $ufAdd = array_merge($ufAdd, $additionalPostMultiple); | |
810 | } | |
811 | ||
812 | $ufAdd = array_values($ufAdd); | |
813 | if (!empty($ufAdd)) { | |
814 | foreach ($ufAdd as $weightAdd => $ufGroupIdAdd) { | |
815 | ||
816 | $ufJoinParamsAdd['weight'] = $weightAdd + $wtAdd; | |
817 | $ufJoinParamsAdd['uf_group_id'] = $ufGroupIdAdd; | |
818 | ||
819 | CRM_Core_BAO_UFJoin::create($ufJoinParamsAdd); | |
820 | unset($ufJoinParamsAdd['id']); | |
821 | } | |
822 | } | |
823 | } | |
824 | ||
825 | parent::endPostProcess(); | |
826 | } | |
827 | //end of function | |
828 | ||
829 | /** | |
830 | * Return a descriptive name for the page, used in wizard header | |
831 | * | |
832 | * @return string | |
833 | * @access public | |
834 | */ | |
835 | public function getTitle() { | |
836 | return ts('Online Registration'); | |
837 | } | |
838 | } | |
839 |