Adding subscription confirmation.
This commit is contained in:
parent
8abcb9a10e
commit
12e6ba8735
2 changed files with 63 additions and 7 deletions
|
|
@ -2,6 +2,7 @@ package WebGUI::Asset::Wobject::NewsletterCollection;
|
|||
|
||||
use strict;
|
||||
use warnings;
|
||||
use 5.010;
|
||||
|
||||
use Class::C3;
|
||||
use WebGUI::User::SpecialState;
|
||||
|
|
@ -53,6 +54,12 @@ sub definition {
|
|||
my $i18n = WebGUI::International->new( $session, 'Asset_NewsletterCollection' );
|
||||
|
||||
tie my %properties, 'Tie::IxHash', (
|
||||
alwaysConfirmSubscription => {
|
||||
fieldType => 'yesNo',
|
||||
defaultValue => 0,
|
||||
label => 'Always require subscription confirmation',
|
||||
tab => 'mailable',
|
||||
},
|
||||
);
|
||||
|
||||
push @{ $definition }, {
|
||||
|
|
@ -196,16 +203,45 @@ sub view {
|
|||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub subscribe {
|
||||
sub sendSubscriptionConfirmation {
|
||||
my $self = shift;
|
||||
my $userId = shift || $self->session->user->getId;
|
||||
my $db = $self->session->db;
|
||||
my $userId = shift;
|
||||
my $code = shift;
|
||||
my $session = $self->session;
|
||||
|
||||
$db->write( 'replace into NewsletterCollection_subscriptions set assetId=?, userId=?', [
|
||||
my $user = WebGUI::User->new( $session, $userId );
|
||||
my $url = $session->url->getSiteURL . $self->getUrl( "func=confirmSubscription;code=$code" );
|
||||
|
||||
my $mail = WebGUI::Mail::Send->create( $self->session, {
|
||||
to => $user->get('email'),
|
||||
subject => 'Confirm your subscription',
|
||||
} );
|
||||
$mail->addText( "Click here: $url" );
|
||||
$mail->send;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub subscribe {
|
||||
my $self = shift;
|
||||
my $userId = shift || $self->session->user->getId;
|
||||
my $requireConfirm = shift // $self->get('alwaysConfirmSubscription');
|
||||
my $session = $self->session;
|
||||
my $db = $self->session->db;
|
||||
|
||||
my $code = $self->session->id->generate if $requireConfirm;
|
||||
|
||||
$db->write( 'replace into NewsletterCollection_subscriptions set assetId=?, userId=?, code=?, confirmed=?', [
|
||||
$self->getId,
|
||||
$userId,
|
||||
$code,
|
||||
$requireConfirm ? 0 : 1,
|
||||
] );
|
||||
|
||||
$self->sendSubscriptionConfirmation( $userId, $code ) if $requireConfirm;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -223,6 +259,21 @@ sub unsubscribe {
|
|||
return;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub www_confirmSubscription {
|
||||
my $self = shift;
|
||||
my ($form, $db) = $self->session->quick( 'form', 'db' );
|
||||
|
||||
my $code = $form->get('code');
|
||||
return unless $code;
|
||||
|
||||
$db->write( 'update NewsletterCollection_subscriptions set confirmed=1, code=NULL where code=?', [
|
||||
$code
|
||||
] );
|
||||
|
||||
return $self->processStyle( 'U bent aangemeld voor deze newsbrief' );
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub www_processSubscription {
|
||||
my $self = shift;
|
||||
|
|
@ -245,6 +296,7 @@ sub www_processSubscription {
|
|||
my $email = $form->email( 'email' );
|
||||
return 'no email' unless $email;
|
||||
|
||||
#### TODO: Break this out in seperate sub.
|
||||
my $emailUser = WebGUI::User::SpecialState->newByEmail( $session, $email );
|
||||
if ( $action eq 'unsubscribe' && defined $emailUser ) {
|
||||
$session->log->warn( 'unsub anon' );
|
||||
|
|
@ -260,7 +312,7 @@ sub www_processSubscription {
|
|||
$emailUser->addSpecialState( 'Newsletter', $self->getId );
|
||||
|
||||
$session->log->warn( 'sub anon ' . $emailUser->username);
|
||||
$self->subscribe( $emailUser->getId );
|
||||
$self->subscribe( $emailUser->getId, 1 );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,8 +50,9 @@ sub installNewsletterCollection {
|
|||
|
||||
$session->db->write(<<EOSQL);
|
||||
create table if not exists NewsletterCollection (
|
||||
assetId char(22) binary not null,
|
||||
revisionDate bigint(20) not null,
|
||||
assetId char(22) binary not null,
|
||||
revisionDate bigint(20) not null,
|
||||
alwaysConfirmSubscription tinyint(1) not null default 0,
|
||||
primary key( assetId, revisionDate )
|
||||
);
|
||||
EOSQL
|
||||
|
|
@ -60,6 +61,9 @@ EOSQL
|
|||
create table if not exists NewsletterCollection_subscriptions (
|
||||
assetId char(22) binary not null,
|
||||
userId char(22) binary not null,
|
||||
confirmed tinyint(1) default 0,
|
||||
code char(22) default null,
|
||||
creationDate timestamp default CURRENT_TIMESTAMP,
|
||||
primary key( assetId, userId )
|
||||
);
|
||||
EOSQL2
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue