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 warnings;
|
||||||
use Class::C3;
|
use Class::C3;
|
||||||
|
|
||||||
|
use WebGUI::Asset::Template;
|
||||||
use WebGUI::Macro;
|
use WebGUI::Macro;
|
||||||
use Tie::IxHash;
|
use Tie::IxHash;
|
||||||
|
|
||||||
|
|
@ -39,6 +40,32 @@ sub definition {
|
||||||
label => 'Allow anonymous subscription',
|
label => 'Allow anonymous subscription',
|
||||||
tab => '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 }, {
|
push( @{ $definition }, {
|
||||||
|
|
@ -113,11 +140,6 @@ sub isSubscribed {
|
||||||
my $db = $self->session->db;
|
my $db = $self->session->db;
|
||||||
|
|
||||||
return $user->isInGroup( $self->getSubscriptionGroup->getId );
|
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;
|
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 {
|
sub sendSubscriptionConfirmation {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $user = shift;
|
my $user = shift;
|
||||||
my $code = shift;
|
my $code = shift;
|
||||||
|
my $action = shift || 'subscribe';
|
||||||
my $session = $self->session;
|
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" );
|
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, {
|
my $mail = WebGUI::Mail::Send->create( $self->session, {
|
||||||
to => $user->get('email'),
|
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;
|
$mail->send;
|
||||||
|
|
||||||
return;
|
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 {
|
sub subscribe {
|
||||||
|
|
@ -195,16 +284,21 @@ sub subscribe {
|
||||||
my $db = $self->session->db;
|
my $db = $self->session->db;
|
||||||
|
|
||||||
if ( $requireConfirm ) {
|
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=?', [
|
$db->write( 'replace into NewsletterCollection_subscriptions set assetId=?, userId=?, code=?, confirmed=?', [
|
||||||
$self->getId,
|
$self->getId,
|
||||||
$user->getId,
|
$user->getId,
|
||||||
$code,
|
$code,
|
||||||
0,
|
0,
|
||||||
] );
|
] );
|
||||||
|
|
||||||
$self->sendSubscriptionConfirmation( $user, $code );
|
$self->sendSubscriptionConfirmation( $user, $code );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$self->getSubscriptionGroup->addUsers( [ $user->getId ] );
|
$self->getSubscriptionGroup->addUsers( [ $user->getId ] );
|
||||||
|
|
@ -217,15 +311,9 @@ sub subscribe {
|
||||||
sub unsubscribe {
|
sub unsubscribe {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $user = shift || $self->session->user;
|
my $user = shift || $self->session->user;
|
||||||
# my $db = $self->session->db;
|
|
||||||
|
|
||||||
$self->getSubscriptionGroup->deleteUsers( [ $user->getId ] );
|
$self->getSubscriptionGroup->deleteUsers( [ $user->getId ] );
|
||||||
|
|
||||||
# $db->write( 'delete from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
|
||||||
# $self->getId,
|
|
||||||
# $user->getId,
|
|
||||||
# ] );
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,16 @@ sub addSpecialState {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub isAdHocUser {
|
||||||
|
my $class = shift;
|
||||||
|
my $user = shift;
|
||||||
|
|
||||||
|
return
|
||||||
|
$user->username eq $user->getId
|
||||||
|
&& !$user->isEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
sub removeSpecialState {
|
sub removeSpecialState {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $state = shift || croak 'state is required';
|
my $state = shift || croak 'state is required';
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,10 @@ sub installSubscriberAspectTable {
|
||||||
subscriptionEnabled tinyint(1) not null default 0,
|
subscriptionEnabled tinyint(1) not null default 0,
|
||||||
alwaysConfirmSubscription tinyint(1) not null default 0,
|
alwaysConfirmSubscription tinyint(1) not null default 0,
|
||||||
allowAnonymousSubscription tinyint(1) not null default 0,
|
allowAnonymousSubscription tinyint(1) not null default 0,
|
||||||
|
confirmationEmailTemplateId char(22) binary,
|
||||||
|
confirmationEmailSubject varchar(255),
|
||||||
|
noMutationEmailTemplateId char(22) binary,
|
||||||
|
noMutationEmailSubject varchar(255),
|
||||||
primary key( assetId, revisionDate )
|
primary key( assetId, revisionDate )
|
||||||
);
|
);
|
||||||
EOSQL
|
EOSQL
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue