Convert subscriptions to use groups instead of homebrew table.
This commit is contained in:
parent
22c900047f
commit
c10bac9bfc
3 changed files with 117 additions and 37 deletions
|
|
@ -15,6 +15,12 @@ sub definition {
|
|||
# my $i18n = WebGUI::International->new( $session,'AssetAspect_Subscriber' );
|
||||
|
||||
tie my %properties, 'Tie::IxHash', (
|
||||
subscriptionGroupId => {
|
||||
fieldType => 'subscriptionGroup',
|
||||
label => 'Subscription group',
|
||||
tab => 'subscription',
|
||||
noFormPost => 1,
|
||||
},
|
||||
subscriptionEnabled => {
|
||||
fieldType => 'yesNo',
|
||||
defaultValue => 1,
|
||||
|
|
@ -27,6 +33,12 @@ sub definition {
|
|||
label => 'Always require subscription confirmation',
|
||||
tab => 'subscription',
|
||||
},
|
||||
allowAnonymousSubscription => {
|
||||
fieldType => 'yesNo',
|
||||
defaultValue => 1,
|
||||
label => 'Allow anonymous subscription',
|
||||
tab => 'subscription',
|
||||
},
|
||||
);
|
||||
|
||||
push( @{ $definition }, {
|
||||
|
|
@ -39,6 +51,18 @@ sub definition {
|
|||
return $class->next::method( $session, $definition );
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub duplicate {
|
||||
my $self = shift;
|
||||
my $properties = shift;
|
||||
my $newSelf = $self->next::method( $properties );
|
||||
|
||||
$newSelf->update({ subscriptionGroupId => '' });
|
||||
$newSelf->createSubscriptionGroup;
|
||||
|
||||
return $newSelf;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub getEditTabs {
|
||||
my $self = shift;
|
||||
|
|
@ -60,16 +84,40 @@ sub canUnsubscribe {
|
|||
return !$self->session->user->isVisitor && $self->isSubscribed;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub createSubscriptionGroup {
|
||||
my $self = shift;
|
||||
|
||||
if ( my $groupId = $self->get('subscriptionGroupId') ) {
|
||||
return WebGUI::Group->new( $self->session, $groupId );
|
||||
}
|
||||
else {
|
||||
my $group = WebGUI::Group->new($self->session, "new");
|
||||
$group->name( "Subscription " . $self->getTitle );
|
||||
$group->description( "Subscription Group for " . $self->getTitle . "(" . $self->getId . ")" );
|
||||
$group->isEditable( 0 );
|
||||
$group->showInForms( 0 );
|
||||
$group->deleteGroups( [ "3" ] ); # admins don't want to be auto subscribed to this thing
|
||||
$self->update({
|
||||
subscriptionGroupId => $group->getId
|
||||
});
|
||||
|
||||
return $group;
|
||||
}
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub isSubscribed {
|
||||
my $self = shift;
|
||||
my $userId = shift || $self->session->user->getId;
|
||||
my $user = shift || $self->session->user;
|
||||
my $db = $self->session->db;
|
||||
|
||||
return $db->quickScalar( 'select 1 from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
||||
$self->getId,
|
||||
$userId,
|
||||
] );
|
||||
return $user->isInGroup( $self->getSubscriptionGroup->getId );
|
||||
|
||||
# return $db->quickScalar( 'select 1 from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
||||
# $self->getId,
|
||||
# $userId,
|
||||
# ] );
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
@ -105,14 +153,26 @@ sub getSubscriptionForm {
|
|||
return;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub getSubscriptionGroup {
|
||||
my $self = shift;
|
||||
my $groupId = $self->get( "subscriptionGroupId" );
|
||||
my $group = $groupId
|
||||
? WebGUI::Group->new( $self->session, $groupId )
|
||||
: $self->createSubscriptionGroup
|
||||
;
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
sub sendSubscriptionConfirmation {
|
||||
my $self = shift;
|
||||
my $userId = shift;
|
||||
my $user = shift;
|
||||
my $code = shift;
|
||||
my $session = $self->session;
|
||||
|
||||
my $user = WebGUI::User->new( $session, $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, {
|
||||
|
|
@ -129,21 +189,26 @@ sub sendSubscriptionConfirmation {
|
|||
#----------------------------------------------------------------------------
|
||||
sub subscribe {
|
||||
my $self = shift;
|
||||
my $userId = shift || $self->session->user->getId;
|
||||
my $user = shift || $self->session->user;
|
||||
my $requireConfirm = shift // $self->get('alwaysConfirmSubscription');
|
||||
my $session = $self->session;
|
||||
my $db = $self->session->db;
|
||||
|
||||
my $code = $self->session->id->generate if $requireConfirm;
|
||||
if ( $requireConfirm ) {
|
||||
my $code = $self->session->id->generate;
|
||||
|
||||
$db->write( 'replace into NewsletterCollection_subscriptions set assetId=?, userId=?, code=?, confirmed=?', [
|
||||
$self->getId,
|
||||
$userId,
|
||||
$user->getId,
|
||||
$code,
|
||||
$requireConfirm ? 0 : 1,
|
||||
0,
|
||||
] );
|
||||
|
||||
$self->sendSubscriptionConfirmation( $userId, $code ) if $requireConfirm;
|
||||
$self->sendSubscriptionConfirmation( $user, $code );
|
||||
}
|
||||
else {
|
||||
$self->getSubscriptionGroup->addUsers( [ $user->getId ] );
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
@ -151,13 +216,15 @@ sub subscribe {
|
|||
#----------------------------------------------------------------------------
|
||||
sub unsubscribe {
|
||||
my $self = shift;
|
||||
my $userId = shift || $self->session->user->getId;
|
||||
my $db = $self->session->db;
|
||||
my $user = shift || $self->session->user;
|
||||
# my $db = $self->session->db;
|
||||
|
||||
$db->write( 'delete from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
||||
$self->getId,
|
||||
$userId,
|
||||
] );
|
||||
$self->getSubscriptionGroup->deleteUsers( [ $user->getId ] );
|
||||
|
||||
# $db->write( 'delete from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
||||
# $self->getId,
|
||||
# $user->getId,
|
||||
# ] );
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
@ -170,11 +237,22 @@ sub www_confirmSubscription {
|
|||
my $code = $form->get('code');
|
||||
return unless $code;
|
||||
|
||||
$db->write( 'update NewsletterCollection_subscriptions set confirmed=1, code=NULL where code=?', [
|
||||
$code
|
||||
my $userId = $db->quickScalar( 'select userId from NewsletterCollection_subscriptions where confirmed=? and code=?', [
|
||||
0,
|
||||
$code,
|
||||
] );
|
||||
# $db->write( 'update NewsletterCollection_subscriptions set confirmed=1, code=NULL where code=?', [
|
||||
# $code
|
||||
# ] );
|
||||
|
||||
return $self->processStyle( 'U bent aangemeld voor deze newsbrief' );
|
||||
if ( $userId ) {
|
||||
$self->getSubscriptionGroup->addUsers( [ $userId ] );
|
||||
}
|
||||
else {
|
||||
return $self->processStyle( 'Verkeerde code' );
|
||||
}
|
||||
|
||||
return $self->processStyle( 'U bent aangemeld voor deze nieuwsbrief' );
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
@ -194,30 +272,27 @@ sub www_processSubscription {
|
|||
$self->unsubscribe;
|
||||
}
|
||||
}
|
||||
# TODO: anonymousAllowed
|
||||
else {
|
||||
elsif ( $self->get('allowAnonymousSubscription') ) {
|
||||
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 );
|
||||
$self->unsubscribe( $emailUser );
|
||||
}
|
||||
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 );
|
||||
$emailUser->addSpecialState( 'Subscriber', $self->getId );
|
||||
|
||||
$session->log->warn( 'sub anon ' . $emailUser->username);
|
||||
$self->subscribe( $emailUser->getId, 1 );
|
||||
$self->subscribe( $emailUser, 1 );
|
||||
}
|
||||
}
|
||||
else {
|
||||
return 'anonymous subscription is not allowed.'
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,11 @@ sub create {
|
|||
my $session = shift;
|
||||
|
||||
my $self = $class->SUPER::create( $session, @_ );
|
||||
$self->disable;
|
||||
$self->username( $self->getId );
|
||||
|
||||
return $self->new( $session, $self->getId );
|
||||
# WebGUI::User->create always returns an object of class WebGUI::User, so we must instanciate again.
|
||||
return $class->new( $session, $self->getId );
|
||||
}
|
||||
|
||||
sub addSpecialState {
|
||||
|
|
|
|||
|
|
@ -56,8 +56,10 @@ sub installSubscriberAspectTable {
|
|||
create table if not exists assetAspectSubscriber (
|
||||
assetId char(22) binary not null,
|
||||
revisionDate bigint(20) not null,
|
||||
subscriptionGroupId char(22) binary,
|
||||
subscriptionEnabled tinyint(1) not null default 0,
|
||||
alwaysConfirmSubscription tinyint(1) not null default 0,
|
||||
allowAnonymousSubscription tinyint(1) not null default 0,
|
||||
primary key( assetId, revisionDate )
|
||||
);
|
||||
EOSQL
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue