Many thanks for your ideas ! I've used them for my own customisations.
I need to allow the user to subscribe or unsubscribe to a particular group. This group allows me to send specific emails to people who want to get that type of email. The new user registration page needs to include a field to opt in or out. The user edit page ("edit account" or "edit profile") must allow the user to change this selection. When the user selects "yes", the code needs to add this user to the group; if the user selects no, the code needs to remove the user from the group.
Here's what I did :
1. include/registerform.php, around line 82:
Quote:
...
$reg_form->addElement(new XoopsFormPassword(_US_PASSWORD, "pass", 10, 32), true);
$reg_form->addElement(new XoopsFormPassword(_US_VERIFYPASS, "vpass", 10, 32), true);
// 17 Aug 2004 - Charles Thompson
// Added "Junior Club Weekly Draw"
$jr_draw_email = isset($HTTP_POST_VARS['jr_draw_email']) ? $HTTP_POST_VARS['jr_draw_email'] : "1";
$reg_form->addElement(new XoopsFormRadioYN("Do you wish to receive the weekly Junior Club draw by email ?",
"jr_draw_email", $jr_draw_email));
// end change - 17 Aug 2004 - Charles Thompson
$reg_form->addElement(new XoopsFormRadioYN(_US_MAILOK, 'user_mailok', 1));
if ($xoopsConfigUser['reg_dispdsclmr'] != 0 && $xoopsConfigUser['reg_disclaimer'] != '') {
...
This code sets up the new user registration form. Here, I add a new form row after the password row, before the "okay to send mail" row. The new row includes a prompt and a yes/no radio group. I hard-coded the text in English; I should have added this to language/english/user.php and referenced it here, but I couln't be bothered. The field name, jr_draw_email will contain a 1 for yes or a 0 for no, and is referenced in the next bit of code ...
2. register.php, around line 153 :
Quote:
This is the block that sets up the form post variables, which will be posted back to this program when the user submits the form. All I have done here is add a hidden field for the yes/no value selected by the user -- $jr_draw_email.
3. register.php, around line 220 :
Quote:
...
echo _US_REGISTERNG;
include 'footer.php';
exit();
}
// 17 Aug 2004 - Charles Thompson
// If user wants to belong, add user to group "Junior Weekly Draw"
if ($jr_draw_email == 1) {
$gid = 5; // change this some time so that it calls member->getGroup to find the correct ID
$member_handler->addUserToGroup($gid, $newid);
}
// end change - 17 Aug 2004 - Charles Thompson
if ($xoopsConfigUser['activation_type'] == 1) {
redirect_header('index.php', 4, _US_ACTLOGIN);
...
This is in the block that processes the submitted form fields. $jr_draw_email is populated automatically from the form POST values, from the hidden field added in (2) above. addUserToGroup is defined in kernel/member.php. I have hard-coded the group ID because I can't find an easy way to get a group ID from a text string (the group name). Remember, a value of 1 means the user wants to join the group.
4. edituser.php, around line 117 :
Quote:
...
} else {
setcookie($xoopsConfig['usercookie']);
}
// 17 Aug 2004 - Charles Thompson
// If user wants to belong, add user to group "Junior Weekly Draw"
$gid = 5; // change this some time so that it calls member->getGroup to find the correct ID
$groups =& $xoopsUser->getGroups();
if ($jr_draw_email == 1) {
if (!in_array($gid, $groups)) {
$member_handler->addUserToGroup($gid, $uid);
}
} else {
if (in_array($gid, $groups)) {
$uid_arr = array($uid);
$member_handler->removeUsersFromGroup($gid, $uid_arr);
}
}
// end change - 17 Aug 2004 - Charles Thompson
if (!$member_handler->insertUser($edituser)) {
include XOOPS_ROOT_PATH.'/header.php';
...
Unlike the registration form, the edit profile page is processed with just one php file - edituser.php. This section of code is executed when the user saves the updated details. Again, I hard-coded the group ID, and again, I use getGroups and addUserToGroup, as described above. removeUsersFromGroup is found in kernel/member.php. It requires an array of users.
5. edituser.php, around line 225 :
Quote:
...
$form->addElement($pwd_tray);
$form->addElement($cookie_radio);
// 17 Aug 2004 - Charles Thompson
// Add "Jr Club Weekly Draw" option
$groups =& $xoopsUser->getGroups();
// Group 5 is "Junior Club Weekly Draw"
// ths will need to be fixed some time in the future
$jr_draw_email_val = in_array('5', $groups) ? '1' : '0';
$jr_draw_email = new XoopsFormRadioYN(
"Do you wish to receive the weekly Junior Club draw by email ?",
"jr_draw_email", $jr_draw_email_val);
$form->addElement($jr_draw_email);
// end change - 17 Aug 2004 - Charles Thompson
$form->addElement($mailok_radio);
$form->addElement($uid_hidden);
...
Later in edituser.php, we find the code that displays the form for the user to edit. Again, I hard-coded the group ID and prompt text (see above).
Please note that the group membership changes don't seem to appear until the user logs out and back in again. I can't work out why that is ! After changing the group membership this way, the admin can see the user really is attached to the group (using the admin/groups page).
Can anyone help with my two problems :
1. How to get the group ID from the group name (without going straight to the database) ?
2. How to avoid logging out and in again in order for the user to see this change in group membership ?