ef56b06b77d90a1578407fa6675563bc664d3583
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Registration.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 void
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 (!empty($defaults['is_multiple_registrations'])) {
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 (!empty($defaults['registration_start_date'])) {
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 (!empty($defaults['registration_end_date'])) {
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 *
211 * @internal param bool $force should we set show hide based on input defaults
212 *
213 * @return void
214 */
215 function setShowHide($defaults) {
216 $this->_showHide = new CRM_Core_ShowHideBlocks(array('registration' => 1),
217 ''
218 );
219 if (empty($defaults)) {
220 $this->_showHide->addHide('registration');
221 $this->_showHide->addHide('id-approval-text');
222 }
223 else {
224 if (empty($defaults['requires_approval'])) {
225 $this->_showHide->addHide('id-approval-text');
226 }
227 }
228 $this->assign('defaultsEmpty', empty($defaults));
229 $this->_showHide->addToTemplate();
230 }
231
232 /**
233 * Function to build the form
234 *
235 * @return void
236 * @access public
237 */
238 public function buildQuickForm() {
239 if ($this->_addProfileBottom) {
240 return self::buildMultipleProfileBottom($this, $this->_profileBottomNum);
241 }
242
243 if ($this->_addProfileBottomAdd) {
244 return self::buildMultipleProfileBottom($this, $this->_profileBottomNumAdd, 'additional_', ts('Profile for Additional Participants'));
245 }
246
247 $this->applyFilter('__ALL__', 'trim');
248 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
249
250 $this->addElement('checkbox',
251 'is_online_registration',
252 ts('Allow Online Registration?'),
253 NULL,
254 array(
255 'onclick' => "return showHideByValue('is_online_registration',
256 '',
257 'registration_blocks',
258 'block',
259 'radio',
260 false );",
261 )
262 );
263
264 $this->add('text', 'registration_link_text', ts('Registration Link Text'));
265
266 if (!$this->_isTemplate) {
267 $this->addDateTime('registration_start_date', ts('Registration Start Date'), FALSE, array('formatType' => 'activityDateTime'));
268 $this->addDateTime('registration_end_date', ts('Registration End Date'), FALSE, array('formatType' => 'activityDateTime'));
269 }
270
271 $params = array(
272 'used' => 'Supervised',
273 'contact_type' => 'Individual',
274 );
275 $dedupeRuleFields = CRM_Dedupe_BAO_Rule::dedupeRuleFields($params);
276
277 foreach ($dedupeRuleFields as $key => $fields) {
278 $ruleFields[$key] = ucwords(str_replace('_', ' ', $fields));
279 }
280
281 $this->addElement('checkbox',
282 'is_multiple_registrations',
283 ts('Register multiple participants?')
284 );
285
286 $this->addElement('checkbox',
287 'allow_same_participant_emails',
288 ts('Same email address?')
289 );
290 $this->assign('ruleFields', json_encode($ruleFields));
291
292 $dedupeRules = array(
293 '' => '- Unsupervised rule -',
294 );
295 $dedupeRules += CRM_Dedupe_BAO_RuleGroup::getByType('Individual');
296 $this->add('select', 'dedupe_rule_group_id', ts('Duplicate matching rule'), $dedupeRules);
297
298 $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
299 if (in_array('Awaiting approval', $participantStatuses) and in_array('Pending from approval', $participantStatuses) and in_array('Rejected', $participantStatuses)) {
300 $this->addElement('checkbox',
301 'requires_approval',
302 ts('Require participant approval?'),
303 NULL,
304 array('onclick' => "return showHideByValue('requires_approval', '', 'id-approval-text', 'table-row', 'radio', false);")
305 );
306 $this->add('textarea', 'approval_req_text', ts('Approval message'), $attributes['approval_req_text']);
307 }
308
309 $this->add('text', 'expiration_time', ts('Pending participant expiration (hours)'));
310 $this->addRule('expiration_time', ts('Please enter the number of hours (as an integer).'), 'integer');
311
312 self::buildRegistrationBlock($this);
313 self::buildConfirmationBlock($this);
314 self::buildMailBlock($this);
315 self::buildThankYouBlock($this);
316
317 parent::buildQuickForm();
318 }
319
320 /**
321 * Function to build Registration Block
322 *
323 * @param $form
324 *
325 * @internal param int $pageId
326 * @static
327 */
328 function buildRegistrationBlock(&$form) {
329 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
330 $form->addWysiwyg('intro_text', ts('Introductory Text'), $attributes['intro_text']);
331 // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing
332 // explicit height and width.
333 $form->addWysiwyg('footer_text', ts('Footer Text'), array('rows' => 2, 'cols' => 40));
334
335 extract( self::getProfileSelectorTypes() );
336
337 $form->addProfileSelector( 'custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities);
338 $form->addProfileSelector( 'custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities);
339
340 $form->addProfileSelector( 'additional_custom_pre_id', ts('Profile for Additional Participants') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities);
341 $form->addProfileSelector( 'additional_custom_post_id', ts('Profile for Additional Participants') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities);
342 }
343
344 /**
345 * Subroutine to insert a Profile Editor widget
346 * depends on getProfileSelectorTypes
347 *
348 * @param array &$form
349 * @param int $count unique index
350 * @param string $prefix dom element ID prefix
351 * @param string $label Label
352 * @param array $configs Optional, for addProfileSelector(), defaults to using getProfileSelectorTypes()
353 **/
354 function buildMultipleProfileBottom(&$form, $count, $prefix = '', $label = 'Include Profile', $configs = null) {
355 extract( ( is_null($configs) ) ? self::getProfileSelectorTypes() : $configs );
356 $element = $prefix . "custom_post_id_multiple[$count]";
357 $label .= '<br />'.ts('(bottom of page)');
358 $form->addProfileSelector( $element, $label, $allowCoreTypes, $allowSubTypes, $profileEntitites);
359 }
360
361 /**
362 * Create initializers for addprofileSelector
363 *
364 * @return array( 'allowCoreTypes' => array(), 'allowSubTypes' => array(), 'profileEntities' => array() )
365 **/
366 static function getProfileSelectorTypes() {
367 $configs = array(
368 'allowCoreTypes' => array(),
369 'allowSubTypes' => array(),
370 'profileEntities' => array(),
371 );
372
373 $configs['allowCoreTypes'][] = 'Contact';
374 $configs['allowCoreTypes'][] = 'Individual';
375 $configs['allowCoreTypes'][] = 'Participant';
376
377 $configs['profileEntities'][] = array('entity_name' => 'individual_entity', 'entity_type' => 'IndividualModel');
378 $configs['profileEntities'][] = array('entity_name' => 'participant_entity', 'entity_type' => 'ParticipantModel');
379
380 return $configs;
381 }
382
383 /**
384 * Function to build Confirmation Block
385 *
386 * @param int $pageId
387 * @static
388 */
389 function buildConfirmationBlock(&$form) {
390 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
391
392 // CRM-11182 - Optional confirmation page for free events
393 $is_monetary = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $form->_id, 'is_monetary');
394 $form->assign('is_monetary', $is_monetary);
395 if ($is_monetary == "0") {
396 $form->addYesNo('is_confirm_enabled', ts('Use a confirmation screen?'), NULL, NULL, array('onclick' => "return showHideByValue('is_confirm_enabled','','confirm_screen_settings','block','radio',false);"));
397 }
398 $form->add('text', 'confirm_title', ts('Title'), $attributes['confirm_title']);
399 $form->addWysiwyg('confirm_text', ts('Introductory Text'), $attributes['confirm_text']);
400 // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing
401 // explicit height and width.
402 $form->addWysiwyg('confirm_footer_text', ts('Footer Text'), array('rows' => 2, 'cols' => 40));
403 }
404
405 /**
406 * Function to build Email Block
407 *
408 * @param int $pageId
409 * @static
410 */
411 function buildMailBlock(&$form) {
412 $form->registerRule('emailList', 'callback', 'emailList', 'CRM_Utils_Rule');
413 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
414 $form->addYesNo('is_email_confirm', ts('Send Confirmation Email?'), NULL, NULL, array('onclick' => "return showHideByValue('is_email_confirm','','confirmEmail','block','radio',false);"));
415 $form->add('textarea', 'confirm_email_text', ts('Text'), $attributes['confirm_email_text']);
416 $form->add('text', 'cc_confirm', ts('CC Confirmation To'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'cc_confirm'));
417 $form->addRule('cc_confirm', ts('Please enter a valid list of comma delimited email addresses'), 'emailList');
418 $form->add('text', 'bcc_confirm', ts('BCC Confirmation To'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'bcc_confirm'));
419 $form->addRule('bcc_confirm', ts('Please enter a valid list of comma delimited email addresses'), 'emailList');
420 $form->add('text', 'confirm_from_name', ts('Confirm From Name'));
421 $form->add('text', 'confirm_from_email', ts('Confirm From Email'));
422 $form->addRule('confirm_from_email', ts('Email is not valid.'), 'email');
423 }
424
425 function buildThankYouBlock(&$form) {
426 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
427 $form->add('text', 'thankyou_title', ts('Title'), $attributes['thankyou_title']);
428 $form->addWysiwyg('thankyou_text', ts('Introductory Text'), $attributes['thankyou_text']);
429 // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing
430 // explicit height and width.
431 $form->addWysiwyg('thankyou_footer_text', ts('Footer Text'), array('rows' => 2, 'cols' => 40));
432 }
433
434 /**
435 * Add local and global form rules
436 *
437 * @access protected
438 *
439 * @return void
440 */
441 function addRules() {
442 if ($this->_addProfileBottom || $this->_addProfileBottomAdd) {
443 return;
444 }
445 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Registration', 'formRule'), $this);
446 }
447
448 /**
449 * global validation rules for the form
450 *
451 * @param array $fields posted values of the form
452 *
453 * @return array list of errors to be posted back to the form
454 * @static
455 * @access public
456 */
457 static function formRule($values, $files, $form) {
458 if (!empty($values['is_online_registration'])) {
459
460 if (!$values['confirm_title']) {
461 $errorMsg['confirm_title'] = ts('Please enter a Title for the registration Confirmation Page');
462 }
463 if (!$values['thankyou_title']) {
464 $errorMsg['thankyou_title'] = ts('Please enter a Title for the registration Thank-you Page');
465 }
466 if ($values['is_email_confirm']) {
467 if (!$values['confirm_from_name']) {
468 $errorMsg['confirm_from_name'] = ts('Please enter Confirmation Email FROM Name.');
469 }
470
471 if (!$values['confirm_from_email']) {
472 $errorMsg['confirm_from_email'] = ts('Please enter Confirmation Email FROM Email Address.');
473 }
474 }
475
476 if (
477 isset($values['registration_start_date']) &&
478 isset($values['registration_end_date'])
479 ) {
480 $start = CRM_Utils_Date::processDate($values['registration_start_date']);
481 $end = CRM_Utils_Date::processDate($values['registration_end_date']);
482 if ($end < $start) {
483 $errorMsg['registration_end_date'] = ts('Registration end date should be after Registration start date');
484 }
485 }
486
487 //check that the selected profiles have either firstname+lastname or email required
488 $profileIds = array(
489 CRM_Utils_Array::value('custom_pre_id', $values),
490 CRM_Utils_Array::value('custom_post_id', $values),
491 );
492 $additionalProfileIds = array(
493 CRM_Utils_Array::value('additional_custom_pre_id', $values),
494 CRM_Utils_Array::value('additional_custom_post_id', $values),
495 );
496 //additional profile fields default to main if not set
497 if (!is_numeric($additionalProfileIds[0])) {
498 $additionalProfileIds[0] = $profileIds[0];
499 }
500 if (!is_numeric($additionalProfileIds[1])) {
501 $additionalProfileIds[1] = $profileIds[1];
502 }
503 //add multiple profiles if set
504 self::addMultipleProfiles($profileIds, $values, 'custom_post_id_multiple');
505 self::addMultipleProfiles($additionalProfileIds, $values, 'additional_custom_post_id_multiple');
506 $isProfileComplete = self::isProfileComplete($profileIds);
507 $isAdditionalProfileComplete = self::isProfileComplete($additionalProfileIds);
508
509 //Check main profiles have an email address available if 'send confirmation email' is selected
510 if ($values['is_email_confirm']) {
511 $emailFields = self::getEmailFields($profileIds);
512 if (!count($emailFields)) {
513 $errorMsg['is_email_confirm'] = ts("Please add a profile with an email address if 'Send Confirmation Email?' is selected");
514 }
515 }
516 $additionalCustomPreId = $additionalCustomPostId = NULL;
517 $isPreError = $isPostError = TRUE;
518 if (!empty($values['allow_same_participant_emails']) && !empty($values['is_multiple_registrations'])) {
519 $types = array_merge(array('Individual'), CRM_Contact_BAO_ContactType::subTypes('Individual'));
520 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types);
521
522 //check for additional custom pre profile
523 $additionalCustomPreId = CRM_Utils_Array::value('additional_custom_pre_id', $values);
524 if (!empty($additionalCustomPreId)) {
525 if (!($additionalCustomPreId == 'none')) {
526 $customPreId = $additionalCustomPreId;
527 }
528 else {
529 $isPreError = FALSE;
530 }
531 }
532 else {
533 $customPreId = !empty($values['custom_pre_id']) ? $values['custom_pre_id'] : NULL;
534 }
535 //check whether the additional custom pre profile is of type 'Individual' and its subtypes
536 if (!empty($customPreId)) {
537 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPreId);
538 foreach ($types as $individualTypes) {
539 if (in_array($individualTypes, $profileTypes)) {
540 $isPreError = FALSE;
541 break;
542 }
543 }
544 }
545 else {
546 $isPreError = FALSE;
547 }
548
549 // We don't have required Individual fields in the pre-custom profile, so now check the post-custom profile
550 if ($isPreError) {
551 $additionalCustomPostId = CRM_Utils_Array::value('additional_custom_post_id', $values);
552 if (!empty($additionalCustomPostId)) {
553 if (!($additionalCustomPostId == 'none')) {
554 $customPostId = $additionalCustomPostId;
555 }
556 else {
557 $isPostError = FALSE;
558 }
559 }
560 else {
561 $customPostId = !empty($values['custom_post_id']) ? $values['custom_post_id'] : NULL;
562 }
563 //check whether the additional custom post profile is of type 'Individual' and its subtypes
564 if (!empty($customPostId)) {
565 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPostId);
566 foreach ($types as $individualTypes) {
567 if (in_array($individualTypes, $profileTypes)) {
568 $isPostError = FALSE;
569 break;
570 }
571 }
572 }
573 else {
574 $isPostError = FALSE;
575 }
576
577 if (empty($customPreId) && empty($customPostId)) {
578 $errorMsg['additional_custom_pre_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
579 }
580 if ($isPostError) {
581 $errorMsg['additional_custom_post_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
582 }
583 }
584 }
585 if (!$isProfileComplete) {
586 $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.");
587 }
588 if (!$isAdditionalProfileComplete) {
589 $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.");
590 }
591
592 // // CRM-8485
593 // $config = CRM_Core_Config::singleton();
594 // if ( $config->doNotAttachPDFReceipt ) {
595 // if (!empty($values['custom_post_id_multiple'])) {
596 // foreach( $values['custom_post_id_multiple'] as $count => $customPostMultiple ) {
597 // if ( $customPostMultiple ) {
598 // $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' ) ) );
599 // break;
600 // }
601 // }
602 // }
603 //
604 // if (!empty($values['is_multiple_registrations']) &&
605 // CRM_Utils_Array::value('additional_custom_post_id_multiple', $values) ) {
606 // foreach( $values['additional_custom_post_id_multiple'] as $count => $customPostMultiple ) {
607 // if ( $customPostMultiple ) {
608 // $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' ) ) );
609 // break;
610 // }
611 // }
612 // }
613 // }
614
615 if (!empty($errorMsg)) {
616 if (!empty($values['custom_post_id_multiple'])) {
617 foreach ($values['custom_post_id_multiple'] as $count => $customPostMultiple) {
618 self::buildMultipleProfileBottom($form, $count);
619 }
620 $form->assign('profilePostMultiple', $values['custom_post_id_multiple']);
621 }
622 if (!empty($values['additional_custom_post_id_multiple'])) {
623 foreach ($values['additional_custom_post_id_multiple'] as $count => $customPostMultiple) {
624 self::buildMultipleProfileBottom($form, $count, 'additional_', ts('Profile for Additional Participants'));
625 }
626 $form->assign('profilePostMultipleAdd', $values['additional_custom_post_id_multiple']);
627 }
628 }
629 }
630
631 if (!empty($errorMsg)) {
632 return $errorMsg;
633 }
634
635 return TRUE;
636 }
637
638 /**
639 * Collect all email fields for an array of profile ids
640 *
641 * @return boolean
642 */
643 static function getEmailFields($profileIds) {
644 $emailFields = array();
645 foreach ($profileIds as $profileId) {
646 if ($profileId && is_numeric($profileId)) {
647 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
648 foreach ($fields as $field) {
649 if (substr_count($field['name'], 'email')) {
650 $emailFields[] = $field;
651 }
652 }
653 }
654 }
655 return $emailFields;
656 }
657
658 /**
659 * Check if a profile contains required fields
660 *
661 * @return boolean
662 */
663 static function isProfileComplete($profileIds) {
664 $profileReqFields = array();
665 foreach ($profileIds as $profileId) {
666 if ($profileId && is_numeric($profileId)) {
667 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
668 foreach ($fields as $field) {
669 switch (TRUE) {
670 case substr_count($field['name'], 'email'):
671 $profileReqFields[] = 'email';
672 break;
673
674 case substr_count($field['name'], 'first_name'):
675 $profileReqFields[] = 'first_name';
676 break;
677
678 case substr_count($field['name'], 'last_name'):
679 $profileReqFields[] = 'last_name';
680 break;
681 }
682 }
683 }
684 }
685 $profileComplete = (in_array('email', $profileReqFields)
686 || (in_array('first_name', $profileReqFields) && in_array('last_name', $profileReqFields))
687 );
688 return $profileComplete;
689 }
690
691 /**
692 * Check if the profiles collect enough information to dedupe
693 *
694 * @return boolean
695 */
696
697 function canProfilesDedupe($profileIds, $rgId = 0) {
698
699 // find the unsupervised rule
700
701 $rgParams = array(
702 'used' => 'Unsupervised',
703 'contact_type' => 'Individual',
704 );
705 if ($rgId > 0) {
706 $rgParams['id'] = $rgId;
707 }
708 $activeRg = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($rgParams);
709
710 // get the combinations that could be a match for the rule
711 $okCombos = $combos = array();
712 CRM_Dedupe_BAO_RuleGroup::combos($activeRg[0], $activeRg[1], $combos);
713
714 // create an index of what combinations involve each field
715 $index = array();
716 foreach ($combos as $comboid => $combo) {
717 foreach ($combo as $cfield) {
718 $index[$cfield][$comboid] = true;
719 }
720 $combos[$comboid] = array_fill_keys($combo, 0);
721 $okCombos[$comboid] = array_fill_keys($combo, 2);
722 }
723
724 // get profiles and see if they have the necessary combos
725 $profileReqFields = array();
726 foreach ($profileIds as $profileId) {
727 if ($profileId && is_numeric($profileId)) {
728 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
729
730 // walk through the fields in the profile
731 foreach ($fields as $field) {
732
733 // check each of the fields in the index against the profile field
734 foreach ($index as $ifield => $icombos) {
735 if(strpos($field['name'], $ifield) !== false) {
736
737 // we found the field in the profile, now record it in the index
738 foreach ($icombos as $icombo => $dontcare) {
739 $combos[$icombo][$ifield] = ($combos[$icombo][$ifield] != 2 && !$field['is_required']) ? 1 : 2;
740
741 if ($combos[$icombo] == $okCombos[$icombo]) {
742 // if any combo is complete with 2s (all fields are present and required), we can go home
743 return 2;
744 }
745 }
746 }
747 }
748 }
749 }
750 }
751
752 // check the combos to see if everything is > 0
753 foreach ($combos as $comboid => $combo) {
754 $complete = false;
755 foreach ($combo as $cfield) {
756 if ($cfield > 0) {
757 $complete = true;
758 }
759 else {
760 // this combo isn't complete--skip to the next combo
761 continue 2;
762 }
763 }
764 if ($complete) { return 1; }
765 }
766
767 // no combo succeeded
768 return 0;
769 }
770
771 /**
772 * Add additional profiles from the form to an array of profile ids.
773 *
774 */
775 static function addMultipleProfiles(&$profileIds, $values, $field) {
776 if ($multipleProfiles = CRM_Utils_Array::value($field, $values)) {
777 foreach ($multipleProfiles as $profileId) {
778 $profileIds[] = $profileId;
779 }
780 }
781 }
782
783 /**
784 * Function to process the form
785 *
786 * @access public
787 *
788 * @return void
789 */
790 public function postProcess() {
791 $params = $this->exportValues();
792
793 $params['id'] = $this->_id;
794
795 // format params
796 $params['is_online_registration'] = CRM_Utils_Array::value('is_online_registration', $params, FALSE);
797 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE); // CRM-11182
798 $params['is_multiple_registrations'] = CRM_Utils_Array::value('is_multiple_registrations', $params, FALSE);
799 $params['allow_same_participant_emails'] = CRM_Utils_Array::value('allow_same_participant_emails', $params, FALSE);
800 $params['requires_approval'] = CRM_Utils_Array::value('requires_approval', $params, FALSE);
801
802 // reset is_email confirm if not online reg
803 if (!$params['is_online_registration']) {
804 $params['is_email_confirm'] = FALSE;
805 }
806
807 if (!$this->_isTemplate) {
808 $params['registration_start_date'] = CRM_Utils_Date::processDate($params['registration_start_date'],
809 $params['registration_start_date_time'],
810 TRUE
811 );
812 $params['registration_end_date'] = CRM_Utils_Date::processDate($params['registration_end_date'],
813 $params['registration_end_date_time'],
814 TRUE
815 );
816 }
817
818 CRM_Event_BAO_Event::add($params);
819
820 // also update the ProfileModule tables
821 $ufJoinParams = array(
822 'is_active' => 1,
823 'module' => 'CiviEvent',
824 'entity_table' => 'civicrm_event',
825 'entity_id' => $this->_id,
826 );
827
828
829 // first delete all past entries
830 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
831
832 $uf = array();
833 $wt = 2;
834 if (!empty($params['custom_pre_id'])) {
835 $uf[1] = $params['custom_pre_id'];
836 $wt = 1;
837 }
838
839 if (!empty($params['custom_post_id'])) {
840 $uf[2] = $params['custom_post_id'];
841 }
842
843 if (!empty($params['custom_post_id_multiple'])) {
844 $uf = array_merge($uf, $params['custom_post_id_multiple']);
845 }
846 $uf = array_values($uf);
847 if (!empty($uf)) {
848 foreach ($uf as $weight => $ufGroupId) {
849 $ufJoinParams['weight'] = $weight + $wt;
850 $ufJoinParams['uf_group_id'] = $ufGroupId;
851 CRM_Core_BAO_UFJoin::create($ufJoinParams);
852 unset($ufJoinParams['id']);
853 }
854 }
855 // also update the ProfileModule tables
856 $ufJoinParamsAdd = array(
857 'is_active' => 1,
858 'module' => 'CiviEvent_Additional',
859 'entity_table' => 'civicrm_event',
860 'entity_id' => $this->_id,
861 );
862
863 // first delete all past entries
864 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParamsAdd);
865 if (!empty($params['is_multiple_registrations'])) {
866 $ufAdd = array();
867 $wtAdd = 2;
868
869 if (array_key_exists('additional_custom_pre_id', $params)) {
870 if (empty($params['additional_custom_pre_id'])) {
871 $ufAdd[1] = $params['custom_pre_id'];
872 $wtAdd = 1;
873 }
874 elseif (CRM_Utils_Array::value('additional_custom_pre_id', $params) == 'none') {}
875 else {
876 $ufAdd[1] = $params['additional_custom_pre_id'];
877 $wtAdd = 1;
878 }
879 }
880
881 if (array_key_exists('additional_custom_post_id', $params)) {
882 if (empty($params['additional_custom_post_id'])) {
883 $ufAdd[2] = $params['custom_post_id'];
884 }
885 elseif (CRM_Utils_Array::value('additional_custom_post_id', $params) == 'none') {}
886 else {
887 $ufAdd[2] = $params['additional_custom_post_id'];
888 }
889 }
890
891 if (!empty($params['additional_custom_post_id_multiple'])) {
892 $additionalPostMultiple = array();
893 foreach ($params['additional_custom_post_id_multiple'] as $key => $value) {
894 if (!$value && !empty($params['custom_post_id'])) {
895 $additionalPostMultiple[$key] = $params['custom_post_id'];
896 }
897 elseif ($value == 'none') {
898 continue;
899 }
900 elseif ($value) {
901 $additionalPostMultiple[$key] = $value;
902 }
903 }
904 $ufAdd = array_merge($ufAdd, $additionalPostMultiple);
905 }
906
907 $ufAdd = array_values($ufAdd);
908 if (!empty($ufAdd)) {
909 foreach ($ufAdd as $weightAdd => $ufGroupIdAdd) {
910
911 $ufJoinParamsAdd['weight'] = $weightAdd + $wtAdd;
912 $ufJoinParamsAdd['uf_group_id'] = $ufGroupIdAdd;
913
914 CRM_Core_BAO_UFJoin::create($ufJoinParamsAdd);
915 unset($ufJoinParamsAdd['id']);
916 }
917 }
918 }
919
920 // get the profiles to evaluate what they collect
921 $profileIds = array(
922 CRM_Utils_Array::value('custom_pre_id', $params),
923 CRM_Utils_Array::value('custom_post_id', $params),
924 );
925 $additionalProfileIds = array(
926 CRM_Utils_Array::value('additional_custom_pre_id', $params),
927 CRM_Utils_Array::value('additional_custom_post_id', $params),
928 );
929 // additional profile fields default to main if not set
930 if (!is_numeric($additionalProfileIds[0])) {
931 $additionalProfileIds[0] = $profileIds[0];
932 }
933 if (!is_numeric($additionalProfileIds[1])) {
934 $additionalProfileIds[1] = $profileIds[1];
935 }
936 //add multiple profiles if set
937 self::addMultipleProfiles($profileIds, $params, 'custom_post_id_multiple');
938 self::addMultipleProfiles($additionalProfileIds, $params, 'additional_custom_post_id_multiple');
939
940 $cantDedupe = false;
941 $rgId = CRM_Utils_Array::value('dedupe_rule_group_id', $params, 0);
942
943 switch (self::canProfilesDedupe($profileIds, $rgId)) {
944 case 0:
945 $dedupeTitle = 'Duplicate Matching Impossible';
946 $cantDedupe = ts("The selected profiles do not contain the fields necessary to match registrations with existing contacts. This means all anonymous registrations will result in a new contact.");
947 break;
948 case 1:
949 $dedupeTitle = 'Duplicate Contacts Possible';
950 $cantDedupe = ts("The selected profiles can collect enough information to match registrations with existing contacts, but not all of the relevant fields are required. Anonymous registrations may result in duplicate contacts.");
951 }
952 if (!empty($params['is_multiple_registrations'])) {
953 switch(self::canProfilesDedupe($additionalProfileIds, $rgId)) {
954 case 0:
955 $dedupeTitle = 'Duplicate Matching Impossible';
956 if ($cantDedupe) {
957 $cantDedupe = ts("The selected profiles do not contain the fields necessary to match registrations with existing contacts. This means all anonymous registrations will result in a new contact.");
958 }
959 else {
960 $cantDedupe = ts("The selected profiles do not contain the fields necessary to match additional participants with existing contacts. This means all additional participants will result in a new contact.");
961 }
962 break;
963 case 1:
964 if (!$cantDedupe) {
965 $dedupeTitle = 'Duplicate Contacts Possible';
966 $cantDedupe = ts("The selected profiles can collect enough information to match additional participants with existing contacts, but not all of the relevant fields are required. This may result in duplicate contacts.");
967 }
968 }
969 }
970 if ($cantDedupe) {
971 CRM_Core_Session::setStatus($cantDedupe, $dedupeTitle, 'alert dedupenotify', array('expires' => 0));
972 }
973
974 // Update tab "disabled" css class
975 $this->ajaxResponse['tabValid'] = !empty($params['is_online_registration']);
976
977 parent::endPostProcess();
978 }
979 //end of function
980
981 /**
982 * Return a descriptive name for the page, used in wizard header
983 *
984 * @return string
985 * @access public
986 */
987 public function getTitle() {
988 return ts('Online Registration');
989 }
990 }
991