Adding templatable email confirmation and status messages.
This commit is contained in:
parent
c10bac9bfc
commit
176938baf5
4 changed files with 124 additions and 22 deletions
|
|
@ -4,6 +4,7 @@ use strict;
|
|||
use warnings;
|
||||
use Class::C3;
|
||||
|
||||
use WebGUI::Asset::Template;
|
||||
use WebGUI::Macro;
|
||||
use Tie::IxHash;
|
||||
|
||||
|
|
@ -39,6 +40,32 @@ sub definition {
|
|||
label => 'Allow anonymous subscription',
|
||||
tab => 'subscription',
|
||||
},
|
||||
confirmationEmailSubject => {
|
||||
fieldType => 'text',
|
||||
defaultVale => 'Confirm your subscription mutation',
|
||||
label => 'Confirmation email subject',
|
||||
tab => 'subscription',
|
||||
},
|
||||
confirmationEmailTemplateId => {
|
||||
fieldType => 'template',
|
||||
defaultValue => '4aLemgMrTFhG3eqg57jCpQ',
|
||||
label => 'Confirmation email template',
|
||||
namespace => 'Subscriber/ConfirmationEmail',
|
||||
tab => 'subscription',
|
||||
},
|
||||
noMutationEmailSubject => {
|
||||
fieldType => 'text',
|
||||
defaultVale => '(Un)subscribe result',
|
||||
label => 'No mutation email subject',
|
||||
tab => 'subscription',
|
||||
},
|
||||
noMutationEmailTemplateId => {
|
||||
fieldType => 'template',
|
||||
defaultValue => '',
|
||||
label => 'No mutation email template',
|
||||
namespace => 'Subscriber/NoMutationEmail',
|
||||
tab => 'subscription',
|
||||
},
|
||||
);
|
||||
|
||||
push( @{ $definition }, {
|
||||
|
|
@ -113,11 +140,6 @@ sub isSubscribed {
|
|||
my $db = $self->session->db;
|
||||
|
||||
return $user->isInGroup( $self->getSubscriptionGroup->getId );
|
||||
|
||||
# return $db->quickScalar( 'select 1 from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
||||
# $self->getId,
|
||||
# $userId,
|
||||
# ] );
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
@ -165,26 +187,93 @@ sub getSubscriptionGroup {
|
|||
return $group;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub getEmailVars {
|
||||
my $self = shift;
|
||||
my $user = shift;
|
||||
|
||||
my $var = {};
|
||||
|
||||
my $userProperties = $user->get;
|
||||
while ( my ($key, $value) = each %{ $userProperties } ) {
|
||||
$var->{ "user_$key" } = $value;
|
||||
}
|
||||
$var->{ user_isRegular } = !WebGUI::User::SpecialState->isAdHocUser( $user );
|
||||
|
||||
my $assetProperties = $self->get;
|
||||
while ( my ($key, $value) = each %{ $assetProperties } ) {
|
||||
$var->{ "asset_$key" } = $value;
|
||||
}
|
||||
|
||||
return $var;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub sendSubscriptionConfirmation {
|
||||
my $self = shift;
|
||||
my $user = shift;
|
||||
my $code = shift;
|
||||
my $action = shift || 'subscribe';
|
||||
my $session = $self->session;
|
||||
|
||||
# my $user = WebGUI::User->new( $session, $userId );
|
||||
my $var = $self->getEmailVars( $user );
|
||||
my $url = $session->url->getSiteURL . $self->getUrl( "func=confirmSubscription;code=$code" );
|
||||
|
||||
$var->{ confirmUrl } = $url;
|
||||
$var->{ code } = $code;
|
||||
$var->{ actionIsSubscribe } = $action eq 'subscribe';
|
||||
|
||||
my $mail = WebGUI::Mail::Send->create( $self->session, {
|
||||
to => $user->get('email'),
|
||||
subject => 'Confirm your subscription',
|
||||
subject => $self->get('confirmationEmailSubject'),
|
||||
} );
|
||||
$mail->addText( "Click here: $url" );
|
||||
|
||||
my $templateId = $self->get('confirmationEmailTemplateId');
|
||||
my $template = WebGUI::Asset::Template->new( $session, $templateId );
|
||||
if ( $template ) {
|
||||
$mail->addHtml( $template->process( $var ) );
|
||||
}
|
||||
else {
|
||||
$session->log->error( "Cannot instanciate confirmation email template with id [$templateId]" );
|
||||
$mail->addText( "Click here to confirm your (un)subscription: $url" );
|
||||
}
|
||||
|
||||
$mail->send;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub sendNoMutationEmail {
|
||||
my $self = shift;
|
||||
my $user = shift;
|
||||
my $action = shift || 'subscribe';
|
||||
my $session = $self->session;
|
||||
|
||||
my $var = $self->getEmailVars( $user );
|
||||
$var->{ actionIsSubscribe } = $action eq 'subscribe';
|
||||
|
||||
my $mail = WebGUI::Mail::Send->create( $self->session, {
|
||||
to => $user->get('email'),
|
||||
subject => $self->get('noMutationEmailSubject'),
|
||||
} );
|
||||
|
||||
my $templateId = $self->get('noMutationEmailTemplateId');
|
||||
my $template = WebGUI::Asset::Template->new( $session, $templateId );
|
||||
if ( $template ) {
|
||||
$mail->addHtml( $template->process( $var ) );
|
||||
}
|
||||
else {
|
||||
$session->log->error( "Cannot instanciate no mutation email template with id [$templateId]" );
|
||||
if ( $action eq 'subscribe' ) {
|
||||
$mail->addText( "This email address is already subscribed to " . $self->get('title') );
|
||||
}
|
||||
}
|
||||
|
||||
$mail->send;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub subscribe {
|
||||
|
|
@ -195,16 +284,21 @@ sub subscribe {
|
|||
my $db = $self->session->db;
|
||||
|
||||
if ( $requireConfirm ) {
|
||||
my $code = $self->session->id->generate;
|
||||
if ( $user->isInGroup( $self->getSubscriptionGroup->getId ) ) {
|
||||
$self->sendNoMutationEmail( $user, 'subscribe' );
|
||||
}
|
||||
else {
|
||||
my $code = $self->session->id->generate;
|
||||
|
||||
$db->write( 'replace into NewsletterCollection_subscriptions set assetId=?, userId=?, code=?, confirmed=?', [
|
||||
$self->getId,
|
||||
$user->getId,
|
||||
$code,
|
||||
0,
|
||||
] );
|
||||
$db->write( 'replace into NewsletterCollection_subscriptions set assetId=?, userId=?, code=?, confirmed=?', [
|
||||
$self->getId,
|
||||
$user->getId,
|
||||
$code,
|
||||
0,
|
||||
] );
|
||||
|
||||
$self->sendSubscriptionConfirmation( $user, $code );
|
||||
$self->sendSubscriptionConfirmation( $user, $code );
|
||||
}
|
||||
}
|
||||
else {
|
||||
$self->getSubscriptionGroup->addUsers( [ $user->getId ] );
|
||||
|
|
@ -217,15 +311,9 @@ sub subscribe {
|
|||
sub unsubscribe {
|
||||
my $self = shift;
|
||||
my $user = shift || $self->session->user;
|
||||
# my $db = $self->session->db;
|
||||
|
||||
$self->getSubscriptionGroup->deleteUsers( [ $user->getId ] );
|
||||
|
||||
# $db->write( 'delete from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
||||
# $self->getId,
|
||||
# $user->getId,
|
||||
# ] );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue