Following along with refining the "User System" for my first MODx site (a hand-coded site I’m converting)...
The "system" is successfully sending notification e-mail messages to an administrative account when a user registers and when a user updates his profile. Both e-mail messages contain the relevant information about the user (username, fullname, email, etc.). Now I’m attempting the final step, which is to send a notification e-mail message when the user activates his account (so that an admin can decide whether to add the user to a special user group).
Having RTFM (rtfm.modx.com
), the way to do this appeared straightforward:
- Write a plugin
- Have it listen for the "OnUserActivate" event
So, I did that (see code below). The notification e-mail message is sent, but the user information was not being displayed (the placeholder tags were unresolved).
I then added some debug code to display "stuff" at the end of the e-mail message (the first debugging technique I ever learnt!). It appeared my $formPlaceHolderPairs array only had the date entry I was adding as a time-stamp. So I then dumped the $scriptProperties array using the "foreach" technique shown in the code below. That confirmed, as per the documentation, that a "KEY: user" was being passed; but, of course, the value could not be converted to a string (it’s a handle) and nothing was displayed for the "VALUE:".
Then (back) to the current form of the plugin code (shown in full).
<?php
/* Plugin for Login:Activation. Sends an e-mail message regarding the activation to the membership committee.
Notes:
1. Refer to the documentation for the "login" extra for basic documentation.
2. Refer to the documentation for "plugins" (under Basic Development).
Notes:
1. This code is to be run when the system event, "OnUserActivate" is fired.
2. Its single "user" paramete contains the newly-activated modUser object.
*/
$actUser = $modx->getOption('user', $scriptProperties, '');
$formPlaceHolderPairs = $actUser->getSettings();
$formPlaceHolderPairs['date'] = date('Y-m-d H:i');
/* TESTING */
$output = 'formPlaceHolderPairs...<br />';
foreach($formPlaceHolderPairs as $PHKey => $PHValue) {
$output .= '(KEY: ' . $PHKey . ', VALUE: ' . $PHValue . ');<br />';
}
/* END TESTING */
$formTpl = $modx->getObject('modChunk', array( 'name' => 'wccrUserActiveNotifyTpl'));
$message = $formTpl->process($formPlaceHolderPairs);
/* TESTING */
$message .= '<p> <br />' . $output . '</p>';
/* END TESTING */
$emailSender = $modx->getOption('emailsender');
$wccrLoginNotify = $modx->getOption('wccr_login_notify');
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY, $message);
$modx->mail->set(modMail::MAIL_FROM, $emailSender);
$modx->mail->set(modMail::MAIL_FROM_NAME, 'WCCR Web Site User Admin');
$modx->mail->set(modMail::MAIL_SENDER, 'WCCR Web Site User Admin');
$modx->mail->set(modMail::MAIL_SUBJECT, 'WCCR: New User Activated');
$modx->mail->address('to', $wccrLoginNotify);
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$err);
}
$modx->mail->reset();
return true;
I wonder if someone can confirm my understanding of my critical lines (reproduced below) which are meant to implement what the documentation says: "ConfirmRegister also fires the OnUserActivate plugin event after the User is activated, and passes in a ’user’ parameter, which contains the newly-activated modUser object."
The first line gets the modUser object (i.e. a handle to the object).
The second line creates an associative array of attribute-name, attribute-value pairs (i.e. "username", "fullname", etc.).
Or do I just not understand at all what is going on?
$actUser = $modx->getOption('user', $scriptProperties, '');
$formPlaceHolderPairs = $actUser->getSettings();
Oh, it will (perhaps?) help to know that when the activation link is clicked and the activation page displayed (with the user logged in), the following two lines are added to the Error Log:
[2010-12-27 23:26:56] (ERROR @ /index.php) No foreign key definition for parentClass: modUser using relation alias: modUserSetting
[2010-12-27 23:26:56] (ERROR @ /index.php) [OnUserActivate]1