Page 1 of 1

Sending out "FEU Invites" for invitation only websites

Posted: Mon Mar 07, 2011 1:15 am
by Wishbone
A UDT that sends an email to newly registered FEU users asking them to activate their account for "Invitation Only" websites.

Please let me know if there is a module or a better method for this.. I've searched for hours, and asked a couple of CMSMS gurus, but hadn't found a solution.

I'm currently working on a website for my daughter's preschool as a straight trade for her education. This website has a 'Parents Only' section which has blog entries, a calendar, and photo galleries that the school doesn't want to be available to the public.

Naturally I turned to the FrontEndUsers module to handle the user login. This was my first time using FEU, and was a bit daunted by the huge control panel for it. Luckily configuring and using it was easy enough.

Obviously we don't want the users to register themselves, but the site owner didn't want to have to manually send out mails to parents each time she created an account. I decided to develop a way to send out 'invitations' to new users once their account was created asking them to click HERE to activate their account. Luckily FEU has an extensive API and I was able to create a UDT to handle this.

The UDT: FEUInvite

Code: Select all

global $gCms;


# Get default page ID.. This is where the 'reset password' will take place

$cntnt = $gCms->GetContentOperations();
$page_id = $cntnt->GetDefaultPageID();


# Tell FEU that this account needs their password reset

$FEU = $gCms->GetModuleInstance('FrontEndUsers');
$code = $FEU->GenerateRandomPrintableString();
$FEU->SetUserTempCode( $params['id'], $code );


# Create activation link

$parms = array( 'input_uid' => $params['id'], 'input_code' => $code );
$activate_link = $FEU->CreateLink('m1_','verifycode',$page_id,'',$parms, '', true, false, '', false);

# Loop through user properties and add them to $user_props

foreach ($FEU->GetUserProperties($params['id']) as $prop) {
  $user_props[$prop['title']] = $prop['data'];
}


# Create Smarty variables for user properties, the activation link and user name. These will be used in the invitation templates (GCBs);

$smarty->assign('user_props', $user_props);
$smarty->assign('activate_link', $activate_link);
$smarty->assign('user_name', $params['name']);


# Get email from FEU, subject and body of mail from GCBs

$email = $FEU->GetEmail($params['id']);
$subject = $smarty->fetch('globalcontent:FEUInviteSubject');
$body = $smarty->fetch('globalcontent:FEUInviteBody');


# Send the mail

$cmsmailer = $gCms->GetModuleInstance('CMSMailer');
$cmsmailer->AddAddress($email);
$cmsmailer->SetSubject($subject);
$cmsmailer->SetBody($body);
$cmsmailer->IsHTML(true);
$cmsmailer->Send();

Creating the Event Handler

Now we need to add our UDT as an event handler for when new accounts are created. Go to Extensions -> Event Manager. Locate 'Frontend User Management - OnCreateUser' and add your new UDT to the list.


Creating the Email Template

Now we need to create the templates for the email subject and body.

Email subject: Create a Global Content Block called 'FEUInviteSubject'. Make sure that WYSIWYG is off. Click off 'Use WYSIWYG' not 'Turn WYSIWYG on/off'. This option is only in CMSMS version 1.9+. There is a fine distinction between these two options. The 'Click off' option doesn't use Tiny at all. The 'Turn off' option just disables the WYSIWYG, but all the Tiny filtering rules apply. We don't want Tiny messing with the subject line at all. In the GCB, add the subject line you want. Something like 'You are invited to login to mysite.com'

Email body: Now for the body template. Create another GCB called 'FEUInviteBody'. This time 'Turn Off' the WYSIWYG and enter in something like:

Code: Select all

{* {$user_props.property} is available to access all of your FEU properties. Example: $user_prop.age *}
<p>{$user_name},</p>
<p>An account was created for you. You can use this account to access the VIP area of the website</p>
<p>Username: {$user_name}</p>
<p>Please click <a href="{$activate_link}">HERE</a> to activate your account.</p>
Then 'Turn on' WYSIWYG and format as desired. That should be it! When the admin creates the account, they can enter in any password, such as '123456', doesn't matter. When the account is created, the UDT tricks FEU into thinking that the user lost their password and has requested a password reset. The email body template that we created sends the user to the password reset page (home page, but with content replaced with reset stuff). The user enters their new password and their new account is activated!

Re: Sending out "FEU Invites" for invitation only websites

Posted: Mon Mar 07, 2011 6:58 pm
by Nullig
Excellent tip, Wishbone.

Nullig

Re: Sending out "FEU Invites" for invitation only websites

Posted: Mon Mar 07, 2011 7:21 pm
by Wishbone
I would love to make it an official plug-in but it seems that you can't use plug-ins as event handlers, though I might find a way to get around it. It also seems to be too much other things that need to be done to use it. GCBs, event handlers, etc.. It looks like it should be a proper module, but would need more options and different templates per group, being able to register a user without the admin needing to set a fake password, etc.

EDIT: On second thought, I can make a mode:

{FEUInvite mode=install}

which could set up the event handler and add the GCBs. :)

Re: Sending out "FEU Invites" for invitation only websites

Posted: Mon Mar 07, 2011 10:26 pm
by Nullig
I think you should develop a module out of this. That way, it can be customised for use in any number of situations.

Also, instead of creating the account in FEU, create it in FEUInvite and have it create the FEU account along with sending the email notification.

Nullig

Re: Sending out "FEU Invites" for invitation only websites

Posted: Mon Dec 19, 2011 7:23 pm
by ooopie
This is great, and exactly what I wanted to do...

...except, I am importing users from a .csv file

Is there a way to loop thru the users and run this UDT for each user already imported?

Re: Sending out "FEU Invites" for invitation only websites

Posted: Wed Dec 21, 2011 1:27 am
by Wishbone
If you import a list of users into FEU, does it trigger the OnCreateUser trigger? If so, this should work.

Re: Sending out "FEU Invites" for invitation only websites

Posted: Mon Feb 18, 2013 9:06 am
by zounars
Hello,

I'm trying to use this tip but I get the same error as mentionned by smanny

"Fatal error: Call to protected method FrontEndUsers::GenerateRandomPrintableString() from context '' in /home/.../lib/classes/class.usertagoperations.inc.php(243) : eval()'d code on line 13"

I noticed that the user is added to FEU the same.

Can someone please update this tip to work with cms 1.11.4.

Thank

Infos:
CMSMS 1.11.4
FEU 1.21.2
PHP 5.2.17
MySQL 5.5.23