Cleanup references to old dedupe class names
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Registration.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * This class generates form components for processing Event.
19 */
20 class CRM_Event_Form_ManageEvent_Registration extends CRM_Event_Form_ManageEvent {
21
22 /**
23 * What blocks should we show and hide.
24 *
25 * @var CRM_Core_ShowHideBlocks
26 */
27 protected $_showHide;
28
29 protected $_profilePostMultiple = [];
30 protected $_profilePostMultipleAdd = [];
31
32 /**
33 * Set variables up before form is built.
34 */
35 public function preProcess() {
36 $this->_addProfileBottom = CRM_Utils_Array::value('addProfileBottom', $_GET, FALSE);
37 $this->_profileBottomNum = CRM_Utils_Array::value('addProfileNum', $_GET, 0);
38 $this->_addProfileBottomAdd = CRM_Utils_Array::value('addProfileBottomAdd', $_GET, FALSE);
39 $this->_profileBottomNumAdd = CRM_Utils_Array::value('addProfileNumAdd', $_GET, 0);
40
41 parent::preProcess();
42 $this->setSelectedChild('registration');
43
44 $this->assign('addProfileBottom', $this->_addProfileBottom);
45 $this->assign('profileBottomNum', $this->_profileBottomNum);
46
47 $urlParams = "id={$this->_id}&addProfileBottom=1&qfKey={$this->controller->_key}";
48 $this->assign('addProfileParams', $urlParams);
49
50 if ($addProfileBottom = CRM_Utils_Array::value('custom_post_id_multiple', $_POST)) {
51 foreach (array_keys($addProfileBottom) as $profileNum) {
52 self::buildMultipleProfileBottom($this, $profileNum);
53 }
54 }
55
56 $this->assign('addProfileBottomAdd', $this->_addProfileBottomAdd);
57 $this->assign('profileBottomNumAdd', $this->_profileBottomNumAdd);
58
59 $urlParamsAdd = "id={$this->_id}&addProfileBottomAdd=1&qfKey={$this->controller->_key}";
60 $this->assign('addProfileParamsAdd', $urlParamsAdd);
61
62 if ($addProfileBottomAdd = CRM_Utils_Array::value('additional_custom_post_id_multiple', $_POST)) {
63 foreach (array_keys($addProfileBottomAdd) as $profileNum) {
64 self::buildMultipleProfileBottom($this, $profileNum, 'additional_', ts('Profile for Additional Participants'));
65 }
66 }
67 }
68
69 /**
70 * Set default values for the form.
71 *
72 * The default values are retrieved from the database.
73 */
74 public function setDefaultValues() {
75 if ($this->_addProfileBottom || $this->_addProfileBottomAdd) {
76 return;
77 }
78 $eventId = $this->_id;
79
80 $defaults = parent::setDefaultValues();
81
82 $this->setShowHide($defaults);
83 if (isset($eventId)) {
84 $params = ['id' => $eventId];
85 CRM_Event_BAO_Event::retrieve($params, $defaults);
86
87 $ufJoinParams = [
88 'entity_table' => 'civicrm_event',
89 'module' => 'CiviEvent',
90 'entity_id' => $eventId,
91 ];
92
93 list($defaults['custom_pre_id'],
94 $defaults['custom_post']
95 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
96
97 // Get the id for the event registration profile
98 $eventRegistrationIdParams = $eventRegistrationIdDefaults = [
99 'name' => 'event_registration',
100 ];
101 CRM_Core_BAO_UFGroup::retrieve($eventRegistrationIdParams, $eventRegistrationIdDefaults);
102
103 // Set event registration as the default profile if none selected
104 if (!$defaults['custom_pre_id'] && count($defaults['custom_post']) == 0) {
105 $defaults['custom_pre_id'] = $eventRegistrationIdDefaults['id'] ?? NULL;
106 }
107 if (isset($defaults['custom_post']) && is_numeric($defaults['custom_post'])) {
108 $defaults['custom_post_id'] = $defaults['custom_post'];
109 }
110 elseif (!empty($defaults['custom_post'])) {
111 $defaults['custom_post_id'] = $defaults['custom_post'][0];
112 unset($defaults['custom_post'][0]);
113 $this->_profilePostMultiple = $defaults['custom_post'];
114 foreach ($defaults['custom_post'] as $key => $value) {
115 self::buildMultipleProfileBottom($this, $key);
116 $defaults["custom_post_id_multiple[$key]"] = $value;
117 }
118 }
119
120 $this->assign('profilePostMultiple', CRM_Utils_Array::value('custom_post', $defaults));
121
122 // CRM-17745: Make max additional participants configurable
123 if (empty($defaults['max_additional_participants'])) {
124 $defaults['max_additional_participants'] = 9;
125 }
126
127 if (!empty($defaults['is_multiple_registrations'])) {
128 // CRM-4377: set additional participants’ profiles – set to ‘none’ if explicitly unset (non-active)
129 $ufJoinAddParams = [
130 'entity_table' => 'civicrm_event',
131 'module' => 'CiviEvent_Additional',
132 'entity_id' => $eventId,
133 ];
134
135 list($defaults['additional_custom_pre_id'],
136 $defaults['additional_custom_post']
137 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinAddParams);
138
139 if (isset($defaults['additional_custom_post']) && is_numeric($defaults['additional_custom_post'])) {
140 $defaults['additional_custom_post_id'] = $defaults['additional_custom_post'];
141 }
142 elseif (!empty($defaults['additional_custom_post'])) {
143 $defaults['additional_custom_post_id'] = $defaults['additional_custom_post'][0];
144 unset($defaults['additional_custom_post'][0]);
145
146 $this->_profilePostMultipleAdd = $defaults['additional_custom_post'];
147 foreach ($defaults['additional_custom_post'] as $key => $value) {
148 self::buildMultipleProfileBottom($this, $key, 'additional_', ts('Profile for Additional Participants'));
149 $defaults["additional_custom_post_id_multiple[$key]"] = $value;
150 }
151 }
152 $this->assign('profilePostMultipleAdd', CRM_Utils_Array::value('additional_custom_post', $defaults, []));
153 }
154 else {
155 // Avoid PHP notices in the template
156 $this->assign('profilePostMultipleAdd', []);
157 }
158 }
159 else {
160 $defaults['is_email_confirm'] = 0;
161 }
162
163 // provide defaults for required fields if empty (and as a 'hint' for approval message field)
164 $defaults['registration_link_text'] = CRM_Utils_Array::value('registration_link_text', $defaults, ts('Register Now'));
165 $defaults['confirm_title'] = CRM_Utils_Array::value('confirm_title', $defaults, ts('Confirm Your Registration Information'));
166 $defaults['thankyou_title'] = CRM_Utils_Array::value('thankyou_title', $defaults, ts('Thank You for Registering'));
167 $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.'));
168
169 return $defaults;
170 }
171
172 /**
173 * Fix what blocks to show/hide based on the default values set
174 *
175 * @param array $defaults
176 * The array of default values.
177 *
178 * @return void
179 */
180 public function setShowHide($defaults) {
181 $this->_showHide = new CRM_Core_ShowHideBlocks(['registration' => 1],
182 ''
183 );
184 if (empty($defaults)) {
185 $this->_showHide->addHide('registration');
186 $this->_showHide->addHide('id-approval-text');
187 }
188 else {
189 if (empty($defaults['requires_approval'])) {
190 $this->_showHide->addHide('id-approval-text');
191 }
192 }
193 $this->assign('defaultsEmpty', empty($defaults));
194 $this->_showHide->addToTemplate();
195 }
196
197 /**
198 * Build the form object.
199 *
200 * @return void
201 */
202 public function buildQuickForm() {
203 if ($this->_addProfileBottom) {
204 return self::buildMultipleProfileBottom($this, $this->_profileBottomNum);
205 }
206
207 if ($this->_addProfileBottomAdd) {
208 return self::buildMultipleProfileBottom($this, $this->_profileBottomNumAdd, 'additional_', ts('Profile for Additional Participants'));
209 }
210
211 $this->applyFilter('__ALL__', 'trim');
212 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
213
214 $this->addElement('checkbox',
215 'is_online_registration',
216 ts('Allow Online Registration'),
217 NULL,
218 [
219 'onclick' => "return showHideByValue('is_online_registration'," .
220 "''," .
221 "'registration_blocks'," .
222 "'block'," .
223 "'radio'," .
224 "false );",
225 ]
226 );
227
228 $this->add('text', 'registration_link_text', ts('Registration Link Text'));
229
230 if (!$this->_isTemplate) {
231 $this->add('datepicker', 'registration_start_date', ts('Registration Start Date'), [], FALSE, ['time' => TRUE]);
232 $this->add('datepicker', 'registration_end_date', ts('Registration End Date'), [], FALSE, ['time' => TRUE]);
233 }
234
235 $params = [
236 'used' => 'Supervised',
237 'contact_type' => 'Individual',
238 ];
239 $dedupeRuleFields = CRM_Dedupe_BAO_DedupeRule::dedupeRuleFields($params);
240
241 foreach ($dedupeRuleFields as $key => $fields) {
242 $ruleFields[$key] = ucwords(str_replace('_', ' ', $fields));
243 }
244
245 $this->addElement('checkbox',
246 'is_multiple_registrations',
247 ts('Register multiple participants?')
248 );
249
250 // CRM-17745: Make maximum additional participants configurable
251 $numericOptions = CRM_Core_SelectValues::getNumericOptions(1, 9);
252 $this->add('select', 'max_additional_participants', ts('Maximum additional participants'), $numericOptions, FALSE, ['class' => 'required']);
253
254 $this->addElement('checkbox',
255 'allow_same_participant_emails',
256 ts('Same email address?')
257 );
258 $this->assign('ruleFields', json_encode($ruleFields));
259
260 $dedupeRules = [
261 '' => '- Unsupervised rule -',
262 ];
263 $dedupeRules += CRM_Dedupe_BAO_DedupeRuleGroup::getByType('Individual');
264 $this->add('select', 'dedupe_rule_group_id', ts('Duplicate matching rule'), $dedupeRules);
265
266 $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
267 if (in_array('Awaiting approval', $participantStatuses) and in_array('Pending from approval', $participantStatuses) and in_array('Rejected', $participantStatuses)) {
268 $this->addElement('checkbox',
269 'requires_approval',
270 ts('Require participant approval?'),
271 NULL,
272 ['onclick' => "return showHideByValue('requires_approval', '', 'id-approval-text', 'table-row', 'radio', false);"]
273 );
274 $this->add('textarea', 'approval_req_text', ts('Approval message'), $attributes['approval_req_text']);
275 }
276
277 $this->add('text', 'expiration_time', ts('Pending participant expiration (hours)'));
278 $this->addRule('expiration_time', ts('Please enter the number of hours (as an integer).'), 'integer');
279 $this->addField('allow_selfcancelxfer', ['label' => ts('Allow self-service cancellation or transfer?'), 'type' => 'advcheckbox']);
280 $this->add('text', 'selfcancelxfer_time', ts('Cancellation or transfer time limit (hours)'));
281 $this->addRule('selfcancelxfer_time', ts('Please enter the number of hours (as an integer).'), 'integer');
282 self::buildRegistrationBlock($this);
283 self::buildConfirmationBlock($this);
284 self::buildMailBlock($this);
285 self::buildThankYouBlock($this);
286
287 parent::buildQuickForm();
288 }
289
290 /**
291 * Build Registration Block.
292 *
293 * @param CRM_Core_Form $form
294 *
295 */
296 public function buildRegistrationBlock(&$form) {
297 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'intro_text') + ['class' => 'collapsed', 'preset' => 'civievent'];
298 $form->add('wysiwyg', 'intro_text', ts('Introductory Text'), $attributes);
299 $form->add('wysiwyg', 'footer_text', ts('Footer Text'), $attributes);
300
301 extract(self::getProfileSelectorTypes());
302 //CRM-15427
303 $form->addProfileSelector('custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities, TRUE, $usedFor);
304 $form->addProfileSelector('custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities, TRUE, $usedFor);
305
306 $form->addProfileSelector('additional_custom_pre_id', ts('Profile for Additional Participants') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities, TRUE, $usedFor);
307 $form->addProfileSelector('additional_custom_post_id', ts('Profile for Additional Participants') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities, TRUE, $usedFor);
308 }
309
310 /**
311 * Subroutine to insert a Profile Editor widget.
312 * depends on getProfileSelectorTypes
313 *
314 * @param array &$form
315 * @param int $count
316 * Unique index.
317 * @param string $prefix
318 * Dom element ID prefix.
319 * @param string $label
320 * Label.
321 * @param array $configs
322 * Optional, for addProfileSelector(), defaults to using getProfileSelectorTypes().
323 */
324 public function buildMultipleProfileBottom(&$form, $count, $prefix = '', $label = 'Include Profile', $configs = NULL) {
325 extract((is_null($configs)) ? self::getProfileSelectorTypes() : $configs);
326 $element = $prefix . "custom_post_id_multiple[$count]";
327 $label .= '<br />' . ts('(bottom of page)');
328 $form->addProfileSelector($element, $label, $allowCoreTypes, $allowSubTypes, $profileEntities, TRUE, $usedFor);
329 }
330
331 /**
332 * Create initializers for addprofileSelector.
333 *
334 * @return array
335 * ['allowCoreTypes' => array, 'allowSubTypes' => array, 'profileEntities' => array]
336 */
337 public static function getProfileSelectorTypes() {
338 $configs = [
339 'allowCoreTypes' => [],
340 'allowSubTypes' => [],
341 'profileEntities' => [],
342 'usedFor' => [],
343 ];
344
345 $configs['allowCoreTypes'] = array_merge([
346 'Contact',
347 'Individual',
348 ], CRM_Contact_BAO_ContactType::subTypes('Individual'));
349 $configs['allowCoreTypes'][] = 'Participant';
350 if (CRM_Core_Permission::check('manage event profiles') && !CRM_Core_Permission::check('administer CiviCRM')) {
351 $configs['usedFor'][] = 'CiviEvent';
352 }
353 //CRM-15427
354 $id = CRM_Utils_Request::retrieve('id', 'Integer');
355 if ($id) {
356 $participantEventType = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $id, 'event_type_id', 'id');
357 $participantRole = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $id, 'default_role_id');
358 $configs['allowSubTypes']['ParticipantEventName'] = [$id];
359 $configs['allowSubTypes']['ParticipantEventType'] = [$participantEventType];
360 $configs['allowSubTypes']['ParticipantRole'] = [$participantRole];
361 }
362 $configs['profileEntities'][] = ['entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'];
363 $configs['profileEntities'][] = [
364 'entity_name' => 'participant_1',
365 'entity_type' => 'ParticipantModel',
366 'entity_sub_type' => '*',
367 ];
368
369 return $configs;
370 }
371
372 /**
373 * Build Confirmation Block.
374 *
375 * @param CRM_Core_Form $form
376 *
377 */
378 public function buildConfirmationBlock(&$form) {
379 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
380 // CRM-11182 - Optional confirmation page for free events
381 $is_monetary = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $form->_id, 'is_monetary');
382 $form->assign('is_monetary', $is_monetary);
383 if ($is_monetary == "0") {
384 $form->addYesNo('is_confirm_enabled', ts('Use a confirmation screen?'), NULL, NULL, ['onclick' => "return showHideByValue('is_confirm_enabled','','confirm_screen_settings','block','radio',false);"]);
385 }
386 $form->add('text', 'confirm_title', ts('Title'), $attributes['confirm_title']);
387 $form->add('wysiwyg', 'confirm_text', ts('Introductory Text'), $attributes['confirm_text'] + ['class' => 'collapsed', 'preset' => 'civievent']);
388 $form->add('wysiwyg', 'confirm_footer_text', ts('Footer Text'), $attributes['confirm_text'] + ['class' => 'collapsed', 'preset' => 'civievent']);
389 }
390
391 /**
392 * Build Email Block.
393 *
394 * @param CRM_Core_Form $form
395 *
396 */
397 public function buildMailBlock(&$form) {
398 $form->registerRule('emailList', 'callback', 'emailList', 'CRM_Utils_Rule');
399 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
400 $form->addYesNo('is_email_confirm', ts('Send Confirmation Email?'), NULL, NULL, ['onclick' => "return showHideByValue('is_email_confirm','','confirmEmail','block','radio',false);"]);
401 $form->add('textarea', 'confirm_email_text', ts('Text'), $attributes['confirm_email_text']);
402 $form->add('text', 'cc_confirm', ts('CC Confirmation To'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'cc_confirm'));
403 $form->addRule('cc_confirm', ts('Please enter a valid list of comma delimited email addresses'), 'emailList');
404 $form->add('text', 'bcc_confirm', ts('BCC Confirmation To'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'bcc_confirm'));
405 $form->addRule('bcc_confirm', ts('Please enter a valid list of comma delimited email addresses'), 'emailList');
406 $form->add('text', 'confirm_from_name', ts('Confirm From Name'));
407 $form->add('text', 'confirm_from_email', ts('Confirm From Email'));
408 $form->addRule('confirm_from_email', ts('Email is not valid.'), 'email');
409 }
410
411 /**
412 * @param CRM_Core_Form $form
413 */
414 public function buildThankYouBlock(&$form) {
415 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
416 $form->add('text', 'thankyou_title', ts('Title'), $attributes['thankyou_title']);
417 $form->add('wysiwyg', 'thankyou_text', ts('Introductory Text'), $attributes['thankyou_text'] + ['class' => 'collapsed', 'preset' => 'civievent']);
418 $form->add('wysiwyg', 'thankyou_footer_text', ts('Footer Text'), $attributes['thankyou_text'] + ['class' => 'collapsed', 'preset' => 'civievent']);
419 }
420
421 /**
422 * Add local and global form rules.
423 *
424 *
425 * @return void
426 */
427 public function addRules() {
428 if ($this->_addProfileBottom || $this->_addProfileBottomAdd) {
429 return;
430 }
431 $this->addFormRule(['CRM_Event_Form_ManageEvent_Registration', 'formRule'], $this);
432 }
433
434 /**
435 * Global validation rules for the form.
436 *
437 * @param array $values
438 * @param $files
439 * @param CRM_Core_Form $form
440 *
441 * @return array
442 * list of errors to be posted back to the form
443 */
444 public static function formRule($values, $files, $form) {
445 if (!empty($values['is_online_registration'])) {
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 (isset($values['registration_start_date']) && isset($values['registration_end_date'])) {
464 if ($values['registration_end_date'] < $values['registration_start_date']) {
465 $errorMsg['registration_end_date'] = ts('Registration end date should be after Registration start date');
466 }
467 }
468
469 //check that the selected profiles have either firstname+lastname or email required
470 $profileIds = [
471 CRM_Utils_Array::value('custom_pre_id', $values),
472 CRM_Utils_Array::value('custom_post_id', $values),
473 ];
474 $additionalProfileIds = [
475 CRM_Utils_Array::value('additional_custom_pre_id', $values),
476 CRM_Utils_Array::value('additional_custom_post_id', $values),
477 ];
478 //additional profile fields default to main if not set
479 if (!is_numeric($additionalProfileIds[0])) {
480 $additionalProfileIds[0] = $profileIds[0];
481 }
482 if (!is_numeric($additionalProfileIds[1])) {
483 $additionalProfileIds[1] = $profileIds[1];
484 }
485 //add multiple profiles if set
486 self::addMultipleProfiles($profileIds, $values, 'custom_post_id_multiple');
487 self::addMultipleProfiles($additionalProfileIds, $values, 'additional_custom_post_id_multiple');
488 $isProfileComplete = self::isProfileComplete($profileIds);
489 $isAdditionalProfileComplete = self::isProfileComplete($additionalProfileIds);
490
491 //Check main profiles have an email address available if 'send confirmation email' is selected
492 if ($values['is_email_confirm']) {
493 $emailFields = self::getEmailFields($profileIds);
494 if (!count($emailFields)) {
495 $errorMsg['is_email_confirm'] = ts("Please add a profile with an email address if 'Send Confirmation Email?' is selected");
496 }
497 }
498 $additionalCustomPreId = $additionalCustomPostId = NULL;
499 $isPreError = $isPostError = TRUE;
500 if (!empty($values['allow_same_participant_emails']) && !empty($values['is_multiple_registrations'])) {
501 $types = array_merge(['Individual'], CRM_Contact_BAO_ContactType::subTypes('Individual'));
502 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types);
503
504 //check for additional custom pre profile
505 $additionalCustomPreId = $values['additional_custom_pre_id'] ?? NULL;
506 if (!empty($additionalCustomPreId)) {
507 if (!($additionalCustomPreId == 'none')) {
508 $customPreId = $additionalCustomPreId;
509 }
510 else {
511 $isPreError = FALSE;
512 }
513 }
514 else {
515 $customPreId = !empty($values['custom_pre_id']) ? $values['custom_pre_id'] : NULL;
516 }
517 //check whether the additional custom pre profile is of type 'Individual' and its subtypes
518 if (!empty($customPreId)) {
519 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPreId);
520 foreach ($types as $individualTypes) {
521 if (in_array($individualTypes, $profileTypes)) {
522 $isPreError = FALSE;
523 break;
524 }
525 }
526 }
527 else {
528 $isPreError = FALSE;
529 }
530
531 // We don't have required Individual fields in the pre-custom profile, so now check the post-custom profile
532 if ($isPreError) {
533 $additionalCustomPostId = $values['additional_custom_post_id'] ?? NULL;
534 if (!empty($additionalCustomPostId)) {
535 if (!($additionalCustomPostId == 'none')) {
536 $customPostId = $additionalCustomPostId;
537 }
538 else {
539 $isPostError = FALSE;
540 }
541 }
542 else {
543 $customPostId = !empty($values['custom_post_id']) ? $values['custom_post_id'] : NULL;
544 }
545 //check whether the additional custom post profile is of type 'Individual' and its subtypes
546 if (!empty($customPostId)) {
547 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPostId);
548 foreach ($types as $individualTypes) {
549 if (in_array($individualTypes, $profileTypes)) {
550 $isPostError = FALSE;
551 break;
552 }
553 }
554 }
555 else {
556 $isPostError = FALSE;
557 }
558
559 if (empty($customPreId) && empty($customPostId)) {
560 $errorMsg['additional_custom_pre_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
561 }
562 if ($isPostError) {
563 $errorMsg['additional_custom_post_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
564 }
565 }
566 }
567 if (!$isProfileComplete) {
568 $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.");
569 }
570 if (!$isAdditionalProfileComplete) {
571 $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.");
572 }
573
574 // // CRM-8485
575 // $config = CRM_Core_Config::singleton();
576 // if ( $config->doNotAttachPDFReceipt ) {
577 // if (!empty($values['custom_post_id_multiple'])) {
578 // foreach( $values['custom_post_id_multiple'] as $count => $customPostMultiple ) {
579 // if ( $customPostMultiple ) {
580 // $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' ) ) );
581 // break;
582 // }
583 // }
584 // }
585 //
586 // if (!empty($values['is_multiple_registrations']) &&
587 // CRM_Utils_Array::value('additional_custom_post_id_multiple', $values) ) {
588 // foreach( $values['additional_custom_post_id_multiple'] as $count => $customPostMultiple ) {
589 // if ( $customPostMultiple ) {
590 // $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' ) ) );
591 // break;
592 // }
593 // }
594 // }
595 // }
596
597 if (!empty($errorMsg)) {
598 if (!empty($values['custom_post_id_multiple'])) {
599 foreach ($values['custom_post_id_multiple'] as $count => $customPostMultiple) {
600 self::buildMultipleProfileBottom($form, $count);
601 }
602 $form->assign('profilePostMultiple', $values['custom_post_id_multiple']);
603 }
604 if (!empty($values['additional_custom_post_id_multiple'])) {
605 foreach ($values['additional_custom_post_id_multiple'] as $count => $customPostMultiple) {
606 self::buildMultipleProfileBottom($form, $count, 'additional_', ts('Profile for Additional Participants'));
607 }
608 $form->assign('profilePostMultipleAdd', $values['additional_custom_post_id_multiple']);
609 }
610 }
611 }
612
613 if (!empty($errorMsg)) {
614 return $errorMsg;
615 }
616
617 return TRUE;
618 }
619
620 /**
621 * Collect all email fields for an array of profile ids.
622 *
623 * @param $profileIds
624 * @return bool
625 */
626 public static function getEmailFields($profileIds) {
627 $emailFields = [];
628 foreach ($profileIds as $profileId) {
629 if ($profileId && is_numeric($profileId)) {
630 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
631 foreach ($fields as $field) {
632 if (substr_count($field['name'], 'email')) {
633 $emailFields[] = $field;
634 }
635 }
636 }
637 }
638 return $emailFields;
639 }
640
641 /**
642 * Check if a profile contains required fields.
643 *
644 * @param $profileIds
645 * @return bool
646 */
647 public static function isProfileComplete($profileIds) {
648 $profileReqFields = [];
649 foreach ($profileIds as $profileId) {
650 if ($profileId && is_numeric($profileId)) {
651 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
652 foreach ($fields as $field) {
653 switch (TRUE) {
654 case substr_count($field['name'], 'email'):
655 $profileReqFields[] = 'email';
656 break;
657
658 case substr_count($field['name'], 'first_name'):
659 $profileReqFields[] = 'first_name';
660 break;
661
662 case substr_count($field['name'], 'last_name'):
663 $profileReqFields[] = 'last_name';
664 break;
665 }
666 }
667 }
668 }
669 $profileComplete = (in_array('email', $profileReqFields)
670 || (in_array('first_name', $profileReqFields) && in_array('last_name', $profileReqFields))
671 );
672 return $profileComplete;
673 }
674
675 /**
676 * Check if the profiles collect enough information to dedupe.
677 *
678 * @param $profileIds
679 * @param int $rgId
680 * @return bool
681 */
682 public static function canProfilesDedupe($profileIds, $rgId = 0) {
683 // find the unsupervised rule
684 $rgParams = [
685 'used' => 'Unsupervised',
686 'contact_type' => 'Individual',
687 ];
688 if ($rgId > 0) {
689 $rgParams['id'] = $rgId;
690 }
691 $activeRg = CRM_Dedupe_BAO_DedupeRuleGroup::dedupeRuleFieldsWeight($rgParams);
692
693 // get the combinations that could be a match for the rule
694 $okCombos = $combos = [];
695 CRM_Dedupe_BAO_DedupeRuleGroup::combos($activeRg[0], $activeRg[1], $combos);
696
697 // create an index of what combinations involve each field
698 $index = [];
699 foreach ($combos as $comboid => $combo) {
700 foreach ($combo as $cfield) {
701 $index[$cfield][$comboid] = TRUE;
702 }
703 $combos[$comboid] = array_fill_keys($combo, 0);
704 $okCombos[$comboid] = array_fill_keys($combo, 2);
705 }
706
707 // get profiles and see if they have the necessary combos
708 $profileReqFields = [];
709 foreach ($profileIds as $profileId) {
710 if ($profileId && is_numeric($profileId)) {
711 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
712
713 // walk through the fields in the profile
714 foreach ($fields as $field) {
715
716 // check each of the fields in the index against the profile field
717 foreach ($index as $ifield => $icombos) {
718 if (strpos($field['name'], $ifield) !== FALSE) {
719
720 // we found the field in the profile, now record it in the index
721 foreach ($icombos as $icombo => $dontcare) {
722 $combos[$icombo][$ifield] = ($combos[$icombo][$ifield] != 2 && !$field['is_required']) ? 1 : 2;
723
724 if ($combos[$icombo] == $okCombos[$icombo]) {
725 // if any combo is complete with 2s (all fields are present and required), we can go home
726 return 2;
727 }
728 }
729 }
730 }
731 }
732 }
733 }
734
735 // check the combos to see if everything is > 0
736 foreach ($combos as $comboid => $combo) {
737 $complete = FALSE;
738 foreach ($combo as $cfield) {
739 if ($cfield > 0) {
740 $complete = TRUE;
741 }
742 else {
743 // this combo isn't complete--skip to the next combo
744 continue 2;
745 }
746 }
747 if ($complete) {
748 return 1;
749 }
750 }
751
752 // no combo succeeded
753 return 0;
754 }
755
756 /**
757 * Add additional profiles from the form to an array of profile ids.
758 *
759 * @param array $profileIds
760 * @param array $values
761 * @param string $field
762 */
763 public static function addMultipleProfiles(&$profileIds, $values, $field) {
764 if ($multipleProfiles = CRM_Utils_Array::value($field, $values)) {
765 foreach ($multipleProfiles as $profileId) {
766 $profileIds[] = $profileId;
767 }
768 }
769 }
770
771 /**
772 * Process the form submission.
773 *
774 *
775 * @return void
776 */
777 public function postProcess() {
778 $params = $this->exportValues();
779
780 $params['id'] = $this->_id;
781
782 // format params
783 $params['is_online_registration'] = CRM_Utils_Array::value('is_online_registration', $params, FALSE);
784 // CRM-11182
785 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE);
786 $params['is_multiple_registrations'] = CRM_Utils_Array::value('is_multiple_registrations', $params, FALSE);
787 $params['allow_same_participant_emails'] = CRM_Utils_Array::value('allow_same_participant_emails', $params, FALSE);
788 $params['requires_approval'] = CRM_Utils_Array::value('requires_approval', $params, FALSE);
789
790 // reset is_email confirm if not online reg
791 if (!$params['is_online_registration']) {
792 $params['is_email_confirm'] = FALSE;
793 }
794 if (!empty($params['allow_selfcancelxfer'])) {
795 $params['selfcancelxfer_time'] = !empty($params['selfcancelxfer_time']) ? $params['selfcancelxfer_time'] : 0;
796 }
797
798 CRM_Event_BAO_Event::add($params);
799
800 // also update the ProfileModule tables
801 $ufJoinParams = [
802 'is_active' => 1,
803 'module' => 'CiviEvent',
804 'entity_table' => 'civicrm_event',
805 'entity_id' => $this->_id,
806 ];
807
808 // first delete all past entries
809 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
810
811 $uf = [];
812 $wt = 2;
813 if (!empty($params['custom_pre_id'])) {
814 $uf[1] = $params['custom_pre_id'];
815 $wt = 1;
816 }
817
818 if (!empty($params['custom_post_id'])) {
819 $uf[2] = $params['custom_post_id'];
820 }
821
822 if (!empty($params['custom_post_id_multiple'])) {
823 $uf = array_merge($uf, $params['custom_post_id_multiple']);
824 }
825 $uf = array_values($uf);
826 if (!empty($uf)) {
827 foreach ($uf as $weight => $ufGroupId) {
828 $ufJoinParams['weight'] = $weight + $wt;
829 $ufJoinParams['uf_group_id'] = $ufGroupId;
830 CRM_Core_BAO_UFJoin::create($ufJoinParams);
831 unset($ufJoinParams['id']);
832 }
833 }
834 // also update the ProfileModule tables
835 $ufJoinParamsAdd = [
836 'is_active' => 1,
837 'module' => 'CiviEvent_Additional',
838 'entity_table' => 'civicrm_event',
839 'entity_id' => $this->_id,
840 ];
841
842 // first delete all past entries
843 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParamsAdd);
844 if (!empty($params['is_multiple_registrations'])) {
845 $ufAdd = [];
846 $wtAdd = 2;
847
848 if (array_key_exists('additional_custom_pre_id', $params)) {
849 if (empty($params['additional_custom_pre_id'])) {
850 $ufAdd[1] = $params['custom_pre_id'];
851 $wtAdd = 1;
852 }
853 elseif (CRM_Utils_Array::value('additional_custom_pre_id', $params) == 'none') {
854 }
855 else {
856 $ufAdd[1] = $params['additional_custom_pre_id'];
857 $wtAdd = 1;
858 }
859 }
860
861 if (array_key_exists('additional_custom_post_id', $params)) {
862 if (empty($params['additional_custom_post_id'])) {
863 $ufAdd[2] = $params['custom_post_id'];
864 }
865 elseif (CRM_Utils_Array::value('additional_custom_post_id', $params) == 'none') {
866 }
867 else {
868 $ufAdd[2] = $params['additional_custom_post_id'];
869 }
870 }
871
872 if (!empty($params['additional_custom_post_id_multiple'])) {
873 $additionalPostMultiple = [];
874 foreach ($params['additional_custom_post_id_multiple'] as $key => $value) {
875 if (is_null($value) && !empty($params['custom_post_id'])) {
876 $additionalPostMultiple[$key] = $params['custom_post_id'];
877 }
878 elseif ($value == 'none') {
879 continue;
880 }
881 elseif ($value) {
882 $additionalPostMultiple[$key] = $value;
883 }
884 }
885 $ufAdd = array_merge($ufAdd, $additionalPostMultiple);
886 }
887
888 $ufAdd = array_values($ufAdd);
889 if (!empty($ufAdd)) {
890 foreach ($ufAdd as $weightAdd => $ufGroupIdAdd) {
891
892 $ufJoinParamsAdd['weight'] = $weightAdd + $wtAdd;
893 $ufJoinParamsAdd['uf_group_id'] = $ufGroupIdAdd;
894
895 CRM_Core_BAO_UFJoin::create($ufJoinParamsAdd);
896 unset($ufJoinParamsAdd['id']);
897 }
898 }
899 }
900
901 // get the profiles to evaluate what they collect
902 $profileIds = [
903 CRM_Utils_Array::value('custom_pre_id', $params),
904 CRM_Utils_Array::value('custom_post_id', $params),
905 ];
906 $additionalProfileIds = [
907 CRM_Utils_Array::value('additional_custom_pre_id', $params),
908 CRM_Utils_Array::value('additional_custom_post_id', $params),
909 ];
910 // additional profile fields default to main if not set
911 if (!is_numeric($additionalProfileIds[0])) {
912 $additionalProfileIds[0] = $profileIds[0];
913 }
914 if (!is_numeric($additionalProfileIds[1])) {
915 $additionalProfileIds[1] = $profileIds[1];
916 }
917 //add multiple profiles if set
918 self::addMultipleProfiles($profileIds, $params, 'custom_post_id_multiple');
919 self::addMultipleProfiles($additionalProfileIds, $params, 'additional_custom_post_id_multiple');
920
921 $cantDedupe = FALSE;
922 $rgId = CRM_Utils_Array::value('dedupe_rule_group_id', $params, 0);
923
924 switch (self::canProfilesDedupe($profileIds, $rgId)) {
925 case 0:
926 $dedupeTitle = 'Duplicate Matching Impossible';
927 $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.");
928 break;
929
930 case 1:
931 $dedupeTitle = 'Duplicate Contacts Possible';
932 $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.");
933 }
934 if (!empty($params['is_multiple_registrations'])) {
935 switch (self::canProfilesDedupe($additionalProfileIds, $rgId)) {
936 case 0:
937 $dedupeTitle = 'Duplicate Matching Impossible';
938 if ($cantDedupe) {
939 $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.");
940 }
941 else {
942 $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.");
943 }
944 break;
945
946 case 1:
947 if (!$cantDedupe) {
948 $dedupeTitle = 'Duplicate Contacts Possible';
949 $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.");
950 }
951 }
952 }
953 if ($cantDedupe) {
954 CRM_Core_Session::setStatus($cantDedupe, $dedupeTitle, 'alert dedupenotify', ['expires' => 0]);
955 }
956
957 // Update tab "disabled" css class
958 $this->ajaxResponse['tabValid'] = !empty($params['is_online_registration']);
959
960 parent::endPostProcess();
961 }
962
963 /**
964 * Return a descriptive name for the page, used in wizard header
965 *
966 * @return string
967 */
968 public function getTitle() {
969 return ts('Online Registration');
970 }
971
972 }