Turn subscribe part of NewsletterCollection into an aspect.
This commit is contained in:
parent
1167a11ba7
commit
22c900047f
3 changed files with 268 additions and 225 deletions
|
|
@ -9,43 +9,10 @@ use WebGUI::User::SpecialState;
|
|||
|
||||
use base qw{
|
||||
WebGUI::AssetAspect::Mailable
|
||||
WebGUI::AssetAspect::Subscriber
|
||||
WebGUI::Asset::Wobject
|
||||
};
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub canSubscribe {
|
||||
my $self = shift;
|
||||
|
||||
return !$self->session->user->isVisitor && !$self->isSubscribed;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub canUnsubscribe {
|
||||
my $self = shift;
|
||||
|
||||
return !$self->session->user->isVisitor && $self->isSubscribed;
|
||||
}
|
||||
|
||||
##----------------------------------------------------------------------------
|
||||
#sub configureMailingForm {
|
||||
# my $self = shift;
|
||||
# my $f = $self->next::method;
|
||||
#
|
||||
# tie my %issues, 'Tie::IxHash', (
|
||||
# '' => 'Choose issue...',
|
||||
# map { $_->getId => $_->getTitle }
|
||||
# @{ $self->getIssues }
|
||||
# );
|
||||
#
|
||||
# $f->selectBox(
|
||||
# name => 'issue',
|
||||
# label => 'Issue',
|
||||
# options => $self->getIssues
|
||||
# );
|
||||
#
|
||||
# return $f;
|
||||
#}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub definition {
|
||||
my $class = shift;
|
||||
|
|
@ -54,12 +21,6 @@ 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 }, {
|
||||
|
|
@ -113,18 +74,6 @@ sub generateEmailContent {
|
|||
return $issue->view;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub isSubscribed {
|
||||
my $self = shift;
|
||||
my $userId = shift || $self->session->user->getId;
|
||||
my $db = $self->session->db;
|
||||
|
||||
return $db->quickScalar( 'select 1 from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
||||
$self->getId,
|
||||
$userId,
|
||||
] );
|
||||
}
|
||||
|
||||
##----------------------------------------------------------------------------
|
||||
#sub prepareView {
|
||||
# my $self = shift;
|
||||
|
|
@ -134,39 +83,6 @@ sub isSubscribed {
|
|||
# $self->{_viewTemplate} = $template;
|
||||
#}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub getSubscriptionForm {
|
||||
my $self = shift;
|
||||
my $session = $self->session;
|
||||
my $i18n = WebGUI::International->new( $session, 'Asset_NewsletterCollection' );
|
||||
|
||||
my $formHeader =
|
||||
WebGUI::Form::formHeader( $session, { action => $self->getUrl } )
|
||||
. WebGUI::Form::hidden( $session, { name => 'func', value => 'processSubscription' } )
|
||||
;
|
||||
my $subscribeButton =
|
||||
sprintf '<button type="submit" name="action" value="subscribe">%s</button>', $i18n->get('subscribe');
|
||||
my $unsubscribeButton =
|
||||
sprintf '<button type="submit" name="action" value="unsubscribe">%s</button>', $i18n->get('unsubscribe');
|
||||
my $emailBox = WebGUI::Form::email( $session, { name => 'email', value => '' } );
|
||||
my $formFooter = WebGUI::Form::formFooter( $session );
|
||||
|
||||
my $output = '';
|
||||
if ( $session->user->isRegistered ) {
|
||||
$output .= $subscribeButton if $self->canSubscribe;
|
||||
$output .= $unsubscribeButton if $self->canUnsubscribe;
|
||||
|
||||
return unless $output;
|
||||
return $formHeader . $output . $formFooter;
|
||||
}
|
||||
#### TODO: anonymousAllowed
|
||||
else {
|
||||
return $formHeader . $emailBox . $subscribeButton . $unsubscribeButton . $formFooter;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub view {
|
||||
my $self = shift;
|
||||
|
|
@ -202,144 +118,5 @@ sub view {
|
|||
return $output;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub sendSubscriptionConfirmation {
|
||||
my $self = shift;
|
||||
my $userId = shift;
|
||||
my $code = shift;
|
||||
my $session = $self->session;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub unsubscribe {
|
||||
my $self = shift;
|
||||
my $userId = shift || $self->session->user->getId;
|
||||
my $db = $self->session->db;
|
||||
|
||||
$db->write( 'delete from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
||||
$self->getId,
|
||||
$userId,
|
||||
] );
|
||||
|
||||
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;
|
||||
my $session = $self->session;
|
||||
my $form = $session->form;
|
||||
|
||||
my $action = $form->get( 'action' );
|
||||
return unless $action eq 'subscribe' || $action eq 'unsubscribe';
|
||||
|
||||
if ( $session->user->isRegistered ) {
|
||||
if ( $action eq 'subscribe' && $self->canSubscribe ) {
|
||||
$self->subscribe;
|
||||
}
|
||||
if ( $action eq 'unsubscribe' && $self->canUnsubscribe ) {
|
||||
$self->unsubscribe;
|
||||
}
|
||||
}
|
||||
# TODO: anonymousAllowed
|
||||
else {
|
||||
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' );
|
||||
$self->unsubscribe( $emailUser->getId );
|
||||
}
|
||||
if ( $action eq 'subscribe' ) {
|
||||
if ( !defined $emailUser ) {
|
||||
$emailUser = WebGUI::User::SpecialState->create( $session );
|
||||
$emailUser->disable;
|
||||
$emailUser->username( $emailUser->getId );
|
||||
$emailUser->update( { email => $email } );
|
||||
}
|
||||
$emailUser->addSpecialState( 'Newsletter', $self->getId );
|
||||
|
||||
$session->log->warn( 'sub anon ' . $emailUser->username);
|
||||
$self->subscribe( $emailUser->getId, 1 );
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub www_subscribe {
|
||||
my $self = shift;
|
||||
|
||||
if ( $self->canSubscribe ) {
|
||||
$self->subscribe;
|
||||
}
|
||||
|
||||
return $self->www_view;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub www_unsubscribe {
|
||||
my $self = shift;
|
||||
|
||||
if ( $self->canUnsubscribe ) {
|
||||
$self->unsubscribe;
|
||||
}
|
||||
|
||||
return $self->www_view;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
|
|
|
|||
248
lib/WebGUI/AssetAspect/Subscriber.pm
Normal file
248
lib/WebGUI/AssetAspect/Subscriber.pm
Normal file
|
|
@ -0,0 +1,248 @@
|
|||
package WebGUI::AssetAspect::Subscriber;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Class::C3;
|
||||
|
||||
use WebGUI::Macro;
|
||||
use Tie::IxHash;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub definition {
|
||||
my $class = shift;
|
||||
my $session = shift;
|
||||
my $definition = shift;
|
||||
# my $i18n = WebGUI::International->new( $session,'AssetAspect_Subscriber' );
|
||||
|
||||
tie my %properties, 'Tie::IxHash', (
|
||||
subscriptionEnabled => {
|
||||
fieldType => 'yesNo',
|
||||
defaultValue => 1,
|
||||
label => 'Enable subscription',
|
||||
tab => 'subscription',
|
||||
},
|
||||
alwaysConfirmSubscription => {
|
||||
fieldType => 'yesNo',
|
||||
defaultValue => 0,
|
||||
label => 'Always require subscription confirmation',
|
||||
tab => 'subscription',
|
||||
},
|
||||
);
|
||||
|
||||
push( @{ $definition }, {
|
||||
autoGenerateForms => 1,
|
||||
tableName => 'assetAspectSubscriber',
|
||||
className => 'WebGUI::AssetAspect::Subscriber',
|
||||
properties => \%properties
|
||||
} );
|
||||
|
||||
return $class->next::method( $session, $definition );
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub getEditTabs {
|
||||
my $self = shift;
|
||||
|
||||
return ( $self->next::method, [ 'subscription', 'Subscription', 9 ] );
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub canSubscribe {
|
||||
my $self = shift;
|
||||
|
||||
return !$self->session->user->isVisitor && !$self->isSubscribed;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub canUnsubscribe {
|
||||
my $self = shift;
|
||||
|
||||
return !$self->session->user->isVisitor && $self->isSubscribed;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub isSubscribed {
|
||||
my $self = shift;
|
||||
my $userId = shift || $self->session->user->getId;
|
||||
my $db = $self->session->db;
|
||||
|
||||
return $db->quickScalar( 'select 1 from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
||||
$self->getId,
|
||||
$userId,
|
||||
] );
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub getSubscriptionForm {
|
||||
my $self = shift;
|
||||
my $session = $self->session;
|
||||
my $i18n = WebGUI::International->new( $session, 'Asset_NewsletterCollection' );
|
||||
|
||||
my $formHeader =
|
||||
WebGUI::Form::formHeader( $session, { action => $self->getUrl } )
|
||||
. WebGUI::Form::hidden( $session, { name => 'func', value => 'processSubscription' } )
|
||||
;
|
||||
my $subscribeButton =
|
||||
sprintf '<button type="submit" name="action" value="subscribe">%s</button>', $i18n->get('subscribe');
|
||||
my $unsubscribeButton =
|
||||
sprintf '<button type="submit" name="action" value="unsubscribe">%s</button>', $i18n->get('unsubscribe');
|
||||
my $emailBox = WebGUI::Form::email( $session, { name => 'email', value => '' } );
|
||||
my $formFooter = WebGUI::Form::formFooter( $session );
|
||||
|
||||
my $output = '';
|
||||
if ( $session->user->isRegistered ) {
|
||||
$output .= $subscribeButton if $self->canSubscribe;
|
||||
$output .= $unsubscribeButton if $self->canUnsubscribe;
|
||||
|
||||
return unless $output;
|
||||
return $formHeader . $output . $formFooter;
|
||||
}
|
||||
#### TODO: anonymousAllowed
|
||||
else {
|
||||
return $formHeader . $emailBox . $subscribeButton . $unsubscribeButton . $formFooter;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub sendSubscriptionConfirmation {
|
||||
my $self = shift;
|
||||
my $userId = shift;
|
||||
my $code = shift;
|
||||
my $session = $self->session;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub unsubscribe {
|
||||
my $self = shift;
|
||||
my $userId = shift || $self->session->user->getId;
|
||||
my $db = $self->session->db;
|
||||
|
||||
$db->write( 'delete from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
||||
$self->getId,
|
||||
$userId,
|
||||
] );
|
||||
|
||||
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;
|
||||
my $session = $self->session;
|
||||
my $form = $session->form;
|
||||
|
||||
my $action = $form->get( 'action' );
|
||||
return unless $action eq 'subscribe' || $action eq 'unsubscribe';
|
||||
|
||||
if ( $session->user->isRegistered ) {
|
||||
if ( $action eq 'subscribe' && $self->canSubscribe ) {
|
||||
$self->subscribe;
|
||||
}
|
||||
if ( $action eq 'unsubscribe' && $self->canUnsubscribe ) {
|
||||
$self->unsubscribe;
|
||||
}
|
||||
}
|
||||
# TODO: anonymousAllowed
|
||||
else {
|
||||
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' );
|
||||
$self->unsubscribe( $emailUser->getId );
|
||||
}
|
||||
if ( $action eq 'subscribe' ) {
|
||||
if ( !defined $emailUser ) {
|
||||
$emailUser = WebGUI::User::SpecialState->create( $session );
|
||||
$emailUser->disable;
|
||||
$emailUser->username( $emailUser->getId );
|
||||
$emailUser->update( { email => $email } );
|
||||
}
|
||||
$emailUser->addSpecialState( 'Newsletter', $self->getId );
|
||||
|
||||
$session->log->warn( 'sub anon ' . $emailUser->username);
|
||||
$self->subscribe( $emailUser->getId, 1 );
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub www_subscribe {
|
||||
my $self = shift;
|
||||
|
||||
if ( $self->canSubscribe ) {
|
||||
$self->subscribe;
|
||||
}
|
||||
|
||||
return $self->www_view;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub www_unsubscribe {
|
||||
my $self = shift;
|
||||
|
||||
if ( $self->canUnsubscribe ) {
|
||||
$self->unsubscribe;
|
||||
}
|
||||
|
||||
return $self->www_view;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
|
|
@ -20,6 +20,7 @@ GetOptions(
|
|||
|
||||
my $session = start( $webguiRoot, $configFile );
|
||||
|
||||
installSubscriberAspectTable( $session );
|
||||
installMailableAspectTable( $session );
|
||||
installNewsletterCollection( $session );
|
||||
installMailingTables( $session );
|
||||
|
|
@ -46,6 +47,24 @@ EOSQL
|
|||
print "Done.\n";
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub installSubscriberAspectTable {
|
||||
my $session = shift;
|
||||
print "\tCreating Subscriber aspect table...";
|
||||
|
||||
$session->db->write(<<EOSQL);
|
||||
create table if not exists assetAspectSubscriber (
|
||||
assetId char(22) binary not null,
|
||||
revisionDate bigint(20) not null,
|
||||
subscriptionEnabled tinyint(1) not null default 0,
|
||||
alwaysConfirmSubscription tinyint(1) not null default 0,
|
||||
primary key( assetId, revisionDate )
|
||||
);
|
||||
EOSQL
|
||||
|
||||
print "Done.\n";
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub installNewsletterCollection {
|
||||
my $session = shift;
|
||||
|
|
@ -55,7 +74,6 @@ sub installNewsletterCollection {
|
|||
create table if not exists NewsletterCollection (
|
||||
assetId char(22) binary not null,
|
||||
revisionDate bigint(20) not null,
|
||||
alwaysConfirmSubscription tinyint(1) not null default 0,
|
||||
primary key( assetId, revisionDate )
|
||||
);
|
||||
EOSQL
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue