News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Birthday on registration for SMF v2.1

Started by Rumpa, November 06, 2024, 07:52:42 AM

Previous topic - Next topic

Rumpa

Hi want to make a mod name birthday on registration for SMF v2.1.4

I attached some file need help regarding hook

with my presentation does not effect at all.

Diego Andrés

Why are you adding a new column? There's already a birthday column.
https://github.com/SimpleMachines/SMF/blob/1468660f32128b76955e9dd58d9860a0434d61ce/other/install_2-1_mysql.sql#L712

That package-info.xml doesn't do much at all, it's not unexpected that it doesn't work.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Rumpa

Thanks for reply @Diego Andrés

 ok my bad for adding that column
can you explain which integration hook i used in package-info

// Hook into the registration process
add_integration_function('register', 'displayBirthdayInput', 'BirthdayHook');

Rumpa

Here is the code
<?php

// Ensure this file is being called from SMF
if (!defined('SMF')) {
   die(
'Hacking attempt...');
}

/**
* Function to display the birthday input fields during registration.
*/
function displayBirthdayInput()
{
   global
$txt;

   
// HTML for the birthday input fields
   
echo '
   <dl class="register_form">
       <dt>
           <strong>'
. $txt['dob'] . '</strong><br />
           <span class="smalltext">'
. $txt['dob_info'] . '</span>
       </dt>
       <dd>
           <input type="text" name="bday_year" size="4" maxlength="4" placeholder="'
. $txt['dob_year'] . '" /> -
           <input type="text" name="bday_month" size="2" maxlength="2" placeholder="'
. $txt['dob_month'] . '" /> -
           <input type="text" name="bday_day" size="2" maxlength="2" placeholder="'
. $txt['dob_day'] . '" />
       </dd>
   </dl>'
;
}

/**
* Function to validate and save the birthday input.
*
* @param array $birthdayData An array containing the birthday data.
* @return string A message indicating the result of the operation.
*/
function saveBirthdayInput($birthdayData)
{
   global
$smcFunc, $txt;

   
// Validate the input
   
if (empty($birthdayData['bday_year']) || empty($birthdayData['bday_month']) || empty($birthdayData['bday_day'])) {
       return
$txt['dob_required'];
   }

   
// Validate the date format
   
$year = (int)$birthdayData['bday_year'];
   
$month = (int)$birthdayData['bday_month'];
   
$day = (int)$birthdayData['bday_day'];

   if (!
checkdate($month, $day, $year)) {
       return
$txt['invalid_dob'];
   }

   
// Save the date of birth to the database (example)
   
$birthDate = sprintf('%04d-%02d-%02d', $year, $month, $day);
   
   
// Assuming you have a member ID to associate with the date of birth
   
$memberId = $_SESSION['member_id']; // Replace with actual member ID retrieval
   
$smcFunc['db_query']('', "
       UPDATE {db_prefix}members
       SET birth_date = {string:birth_date}
       WHERE id_member = {int:member_id}"
,
       array(
           
'birth_date' => $birthDate,
           
'member_id' => $memberId,
       )
   );

   return
$txt['dob_success'];
}

// Hook into the registration process
add_integration_function('register', 'displayBirthdayInput', 'BirthdayHook');

?>


Diego Andrés


SMF Tricks - Free & Premium Responsive Themes for SMF.

Rumpa



Rumpa

#7
ok now i tried with bellow code
<?php
if (!defined('SMF'))
   die(
'Hacking attempt...');

function
modify_registration_fields($regOptions)
{
   
// Add a birthday field to the registration form
   
$regOptions['fields']['birthday'] = array(
       
'type' => 'text',
       
'label' => 'Birthday (YYYY-MM-DD)',
       
'required' => false,
       
'validation' => 'validate_birthday',
   );
}

function
validate_birthday($birthday)
{
   
// Validate the birthday format
   
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthday)) {
       return
'Invalid birthday format. Please use YYYY-MM-DD.';
   }
   return
true;
}

function
save_birthday($member_id, $birthday)
{
   
// Save the birthday to the database
   
$birthday = htmlspecialchars($birthday, ENT_QUOTES);
   
$db = database();
   
$db->query('', '
       UPDATE {db_prefix}members
       SET birthday = {string:birthday}
       WHERE id_member = {int:member_id}'
,
       array(
           
'birthday' => $birthday,
           
'member_id' => $member_id,
       )
   );
}

function
register_hooks()
{
   
// Hook into the registration process
   
add_hook('register', 'modify_registration_fields');
   
add_hook('register_save', 'save_birthday');
}

register_hooks();

?>

and with bellow hooks into the package-info file but nothing will happens can someone guide me what i missing or doing wrong

<require-file name="BirthdayOnRegistration.php" destination="$sourcedir" />
        <hook hook="integrate_register" file="BirthdayOnRegistration.php" function="modify_registration_fields" />
        <hook hook="integrate_register_save" file="BirthdayOnRegistration.php" function="save_birthday" />



If someone contribute to this project then go to this github link https://github.com/Rumpa20/Birthday-On-Registration

Bugo

function register_hooks()
{
    // Hook into the registration process
    add_hook('register', 'modify_registration_fields');
    add_hook('register_save', 'save_birthday');
}

register_hooks();

You don't need this code because the hooks are already defined in package-info.xml.

Rumpa

Thanks for reply @Bugo

Should i add any custom_field in code

Bugo

I made my own version that does not require localization and does not add new columns to the members table.

Rumpa

Quote from: Bugo on November 17, 2024, 10:51:08 AMI made my own version that does not require localization and does not add new columns to the members table.
Wow great work.

Advertisement: