Merge pull request #2390 from dlobo/CRM-14085
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Registration.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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 */
41class 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
f55dc004 58 */
6a488035
TO
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 *
355ba699 98 * @return void
6a488035
TO
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 *
355ba699 240 * @return void
6a488035
TO
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(
f55dc004 260 'onclick' => "return showHideByValue('is_online_registration',
261 '',
262 'registration_blocks',
263 'block',
264 'radio',
6a488035
TO
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(
f55dc004 291 'onclick' => "return (showHideByValue('is_multiple_registrations', '', 'additional_profile_pre|additional_profile_post', 'table-row', 'radio', false) ||
6a488035
TO
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);
f55dc004 353 // Allow user to NOT provide a bottom profile for Additional Participant registration
47ecc69e 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 }
f55dc004 462
ef597b45
DL
463 if (
464 isset($values['registration_start_date']) &&
465 isset($values['registration_end_date'])
466 ) {
6a488035
TO
467 $start = CRM_Utils_Date::processDate($values['registration_start_date']);
468 $end = CRM_Utils_Date::processDate($values['registration_end_date']);
469 if ($end < $start) {
470 $errorMsg['registration_end_date'] = ts('Registration end date should be after Registration start date');
471 }
472 }
473
474 //check that the selected profiles have either firstname+lastname or email required
475 $profileIds = array(
476 CRM_Utils_Array::value('custom_pre_id', $values),
477 CRM_Utils_Array::value('custom_post_id', $values),
478 );
479 $additionalProfileIds = array(
480 CRM_Utils_Array::value('additional_custom_pre_id', $values),
481 CRM_Utils_Array::value('additional_custom_post_id', $values),
482 );
483 //additional profile fields default to main if not set
484 if (!is_numeric($additionalProfileIds[0])) {
485 $additionalProfileIds[0] = $profileIds[0];
486 }
487 if (!is_numeric($additionalProfileIds[1])) {
488 $additionalProfileIds[1] = $profileIds[1];
489 }
490 //add multiple profiles if set
491 self::addMultipleProfiles($profileIds, $values, 'custom_post_id_multiple');
492 self::addMultipleProfiles($additionalProfileIds, $values, 'additional_custom_post_id_multiple');
493 $isProfileComplete = self::isProfileComplete($profileIds);
494 $isAdditionalProfileComplete = self::isProfileComplete($additionalProfileIds);
495 //Check main profiles have an email address available if 'send confirmation email' is selected
496 if ($values['is_email_confirm']) {
497 $emailFields = self::getEmailFields($profileIds);
498 if (!count($emailFields)) {
499 $errorMsg['is_email_confirm'] = ts("Please add a profile with an email address if 'Send Confirmation Email?' is selected");
500 }
501 }
502 $additionalCustomPreId = $additionalCustomPostId = NULL;
503 $isPreError = $isPostError = TRUE;
504 if (CRM_Utils_Array::value('allow_same_participant_emails', $values) &&
505 CRM_Utils_Array::value('is_multiple_registrations', $values)
506 ) {
507 $types = array_merge(array('Individual'), CRM_Contact_BAO_ContactType::subTypes('Individual'));
508 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types);
509
510 //check for additional custom pre profile
511 $additionalCustomPreId = CRM_Utils_Array::value('additional_custom_pre_id', $values);
512 if (!empty($additionalCustomPreId)) {
513 if (!($additionalCustomPreId == 'none')) {
514 $customPreId = $additionalCustomPreId;
515 }
516 else {
517 $isPreError = FALSE;
518 }
519 }
520 else {
521 $customPreId = CRM_Utils_Array::value('custom_pre_id', $values) ? $values['custom_pre_id'] : NULL;
522 }
523 //check whether the additional custom pre profile is of type 'Individual' and its subtypes
524 if (!empty($customPreId)) {
525 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPreId);
526 foreach ($types as $individualTypes) {
527 if (in_array($individualTypes, $profileTypes)) {
528 $isPreError = FALSE;
529 break;
530 }
531 }
532 }
533 else {
534 $isPreError = FALSE;
535 }
f55dc004 536
dbeb7efb
DG
537 // We don't have required Individual fields in the pre-custom profile, so now check the post-custom profile
538 if ($isPreError) {
539 $additionalCustomPostId = CRM_Utils_Array::value('additional_custom_post_id', $values);
540 if (!empty($additionalCustomPostId)) {
541 if (!($additionalCustomPostId == 'none')) {
542 $customPostId = $additionalCustomPostId;
543 }
544 else {
545 $isPostError = FALSE;
546 }
6a488035
TO
547 }
548 else {
dbeb7efb 549 $customPostId = CRM_Utils_Array::value('custom_post_id', $values) ? $values['custom_post_id'] : NULL;
6a488035 550 }
dbeb7efb
DG
551 //check whether the additional custom post profile is of type 'Individual' and its subtypes
552 if (!empty($customPostId)) {
553 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPostId);
554 foreach ($types as $individualTypes) {
555 if (in_array($individualTypes, $profileTypes)) {
556 $isPostError = FALSE;
557 break;
558 }
6a488035
TO
559 }
560 }
dbeb7efb
DG
561 else {
562 $isPostError = FALSE;
563 }
564
565 if (empty($customPreId) && empty($customPostId)) {
566 $errorMsg['additional_custom_pre_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
567 }
568 if ($isPostError) {
569 $errorMsg['additional_custom_post_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
570 }
6a488035
TO
571 }
572 }
573 if (!$isProfileComplete) {
574 $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.");
575 }
576 if (!$isAdditionalProfileComplete) {
577 $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.");
578 }
579
580 // // CRM-8485
581 // $config = CRM_Core_Config::singleton();
582 // if ( $config->doNotAttachPDFReceipt ) {
583 // if ( CRM_Utils_Array::value('custom_post_id_multiple', $values) ) {
584 // foreach( $values['custom_post_id_multiple'] as $count => $customPostMultiple ) {
585 // if ( $customPostMultiple ) {
586 // $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' ) ) );
587 // break;
588 // }
589 // }
590 // }
591 //
592 // if ( CRM_Utils_Array::value('is_multiple_registrations', $values) &&
593 // CRM_Utils_Array::value('additional_custom_post_id_multiple', $values) ) {
594 // foreach( $values['additional_custom_post_id_multiple'] as $count => $customPostMultiple ) {
595 // if ( $customPostMultiple ) {
596 // $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' ) ) );
597 // break;
598 // }
599 // }
600 // }
601 // }
602
603 if (!empty($errorMsg)) {
604 if (CRM_Utils_Array::value('custom_post_id_multiple', $values)) {
605 foreach ($values['custom_post_id_multiple'] as $count => $customPostMultiple) {
606 self::buildMultipleProfileBottom($form, $count);
607 }
608 $form->assign('profilePostMultiple', $values['custom_post_id_multiple']);
609 }
610 if (CRM_Utils_Array::value('additional_custom_post_id_multiple', $values)) {
611 foreach ($values['additional_custom_post_id_multiple'] as $count => $customPostMultiple) {
612 self::buildMultipleProfileBottom($form, $count, 'additional_', ts('Profile for Additional Participants'));
613 }
614 $form->assign('profilePostMultipleAdd', $values['additional_custom_post_id_multiple']);
615 }
616 }
617 }
618
619 if (!empty($errorMsg)) {
620 return $errorMsg;
621 }
622
623 return TRUE;
624 }
625
626 /**
627 * Collect all email fields for an array of profile ids
628 *
629 * @return boolean
630 */
631 static function getEmailFields($profileIds) {
632 $emailFields = array();
633 foreach ($profileIds as $profileId) {
634 if ($profileId && is_numeric($profileId)) {
635 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
636 foreach ($fields as $field) {
637 if (substr_count($field['name'], 'email')) {
638 $emailFields[] = $field;
639 }
640 }
641 }
642 }
643 return $emailFields;
644 }
645
646 /**
647 * Check if a profile contains required fields
648 *
649 * @return boolean
650 */
651 static function isProfileComplete($profileIds) {
652 $profileReqFields = array();
653 foreach ($profileIds as $profileId) {
654 if ($profileId && is_numeric($profileId)) {
655 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
656 foreach ($fields as $field) {
657 switch (TRUE) {
658 case substr_count($field['name'], 'email'):
659 $profileReqFields[] = 'email';
660 break;
661
662 case substr_count($field['name'], 'first_name'):
663 $profileReqFields[] = 'first_name';
664 break;
665
666 case substr_count($field['name'], 'last_name'):
667 $profileReqFields[] = 'last_name';
668 break;
669 }
670 }
671 }
672 }
673 $profileComplete = (in_array('email', $profileReqFields)
674 || (in_array('first_name', $profileReqFields) && in_array('last_name', $profileReqFields))
675 );
676 return $profileComplete;
677 }
678
679 /**
680 * Add additional profiles from the form to an array of profile ids.
681 *
682 */
683 static function addMultipleProfiles(&$profileIds, $values, $field) {
684 if ($multipleProfiles = CRM_Utils_Array::value($field, $values)) {
685 foreach ($multipleProfiles as $profileId) {
686 $profileIds[] = $profileId;
687 }
688 }
689 }
690
691 /**
692 * Function to process the form
693 *
694 * @access public
695 *
355ba699 696 * @return void
6a488035
TO
697 */
698 public function postProcess() {
6a488035
TO
699 $params = $this->exportValues();
700
701 $params['id'] = $this->_id;
702
703 //format params
704 $params['is_online_registration'] = CRM_Utils_Array::value('is_online_registration', $params, FALSE);
705 $params['is_multiple_registrations'] = CRM_Utils_Array::value('is_multiple_registrations', $params, FALSE);
706 $params['allow_same_participant_emails'] = CRM_Utils_Array::value('allow_same_participant_emails', $params, FALSE);
707 $params['requires_approval'] = CRM_Utils_Array::value('requires_approval', $params, FALSE);
708
709 // reset is_email confirm if not online reg
710 if (!$params['is_online_registration']) {
711 $params['is_email_confirm'] = FALSE;
712 }
713
714 if (!$this->_isTemplate) {
715 $params['registration_start_date'] = CRM_Utils_Date::processDate($params['registration_start_date'],
716 $params['registration_start_date_time'],
717 TRUE
718 );
719 $params['registration_end_date'] = CRM_Utils_Date::processDate($params['registration_end_date'],
720 $params['registration_end_date_time'],
721 TRUE
722 );
723 }
724
725 CRM_Event_BAO_Event::add($params);
726
727 // also update the ProfileModule tables
728 $ufJoinParams = array(
729 'is_active' => 1,
730 'module' => 'CiviEvent',
731 'entity_table' => 'civicrm_event',
732 'entity_id' => $this->_id,
733 );
734
735
736 // first delete all past entries
737 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
738
739 $uf = array();
740 $wt = 2;
741 if (!empty($params['custom_pre_id'])) {
742 $uf[1] = $params['custom_pre_id'];
743 $wt = 1;
744 }
745
746 if (!empty($params['custom_post_id'])) {
747 $uf[2] = $params['custom_post_id'];
748 }
749
750 if (CRM_Utils_Array::value('custom_post_id_multiple', $params)) {
751 $uf = array_merge($uf, $params['custom_post_id_multiple']);
752 }
753 $uf = array_values($uf);
754 if (!empty($uf)) {
755 foreach ($uf as $weight => $ufGroupId) {
756 $ufJoinParams['weight'] = $weight + $wt;
757 $ufJoinParams['uf_group_id'] = $ufGroupId;
758 CRM_Core_BAO_UFJoin::create($ufJoinParams);
759 unset($ufJoinParams['id']);
760 }
761 }
762 // also update the ProfileModule tables
763 $ufJoinParamsAdd = array(
764 'is_active' => 1,
765 'module' => 'CiviEvent_Additional',
766 'entity_table' => 'civicrm_event',
767 'entity_id' => $this->_id,
768 );
769
770 // first delete all past entries
771 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParamsAdd);
772 if (CRM_Utils_Array::value('is_multiple_registrations', $params)) {
773 $ufAdd = array();
774 $wtAdd = 2;
775
776 if (array_key_exists('additional_custom_pre_id', $params)) {
777 if (!CRM_Utils_Array::value('additional_custom_pre_id', $params)) {
778 $ufAdd[1] = $params['custom_pre_id'];
779 $wtAdd = 1;
780 }
781 elseif (CRM_Utils_Array::value('additional_custom_pre_id', $params) == 'none') {}
782 else {
783 $ufAdd[1] = $params['additional_custom_pre_id'];
784 $wtAdd = 1;
785 }
786 }
787
788 if (array_key_exists('additional_custom_post_id', $params)) {
789 if (!CRM_Utils_Array::value('additional_custom_post_id', $params)) {
790 $ufAdd[2] = $params['custom_post_id'];
791 }
792 elseif (CRM_Utils_Array::value('additional_custom_post_id', $params) == 'none') {}
793 else {
794 $ufAdd[2] = $params['additional_custom_post_id'];
795 }
796 }
797
798 if (CRM_Utils_Array::value('additional_custom_post_id_multiple', $params)) {
799 $additionalPostMultiple = array();
800 foreach ($params['additional_custom_post_id_multiple'] as $key => $value) {
801 if (!$value && CRM_Utils_Array::value('custom_post_id', $params)) {
802 $additionalPostMultiple[$key] = $params['custom_post_id'];
803 }
804 elseif ($value == 'none') {
805 continue;
806 }
807 elseif ($value) {
808 $additionalPostMultiple[$key] = $value;
809 }
810 }
811 $ufAdd = array_merge($ufAdd, $additionalPostMultiple);
812 }
813
814 $ufAdd = array_values($ufAdd);
815 if (!empty($ufAdd)) {
816 foreach ($ufAdd as $weightAdd => $ufGroupIdAdd) {
817
818 $ufJoinParamsAdd['weight'] = $weightAdd + $wtAdd;
819 $ufJoinParamsAdd['uf_group_id'] = $ufGroupIdAdd;
820
821 CRM_Core_BAO_UFJoin::create($ufJoinParamsAdd);
822 unset($ufJoinParamsAdd['id']);
823 }
824 }
825 }
826
5d92a7e7
CW
827 // Update tab "disabled" css class
828 $this->ajaxResponse['tabValid'] = !empty($params['is_online_registration']);
829
6a488035
TO
830 parent::endPostProcess();
831 }
832 //end of function
833
834 /**
835 * Return a descriptive name for the page, used in wizard header
836 *
837 * @return string
838 * @access public
839 */
840 public function getTitle() {
841 return ts('Online Registration');
842 }
843}
844