diff --git a/lib/WebGUI/Registration/Step/MailingSubscribe.pm b/lib/WebGUI/Registration/Step/MailingSubscribe.pm new file mode 100644 index 0000000..a9205cc --- /dev/null +++ b/lib/WebGUI/Registration/Step/MailingSubscribe.pm @@ -0,0 +1,182 @@ +package WebGUI::Registration::Step::MailingSubscribe; + +use strict; + +use Tie::IxHash; +use List::MoreUtils qw{ uniq }; + + +use base qw{ WebGUI::Registration::Step }; + +#------------------------------------------------------------------- +sub getAvailableMailings { + my $self = shift; + my $session = $self->session; + + my $availableMailings = WebGUI::Asset->getRoot( $session )->getLineage( ['descendants'], { + returnObjects => 1, + isa => 'WebGUI::Asset::Wobject::NewsletterCollection', + } ); + + return $availableMailings; +} + +#------------------------------------------------------------------- +sub apply { + my $self = shift; + my $user = $self->registration->instance->user; + + my $subscribeTo = { + map { $_ => 1 } @{ $self->getConfigurationData->{ subscribeMailings } || [] } + }; + + my $availableMailings = $self->getAvailableMailings; + + my $sendNotification = 0; + foreach my $mailing ( @{ $availableMailings } ) { + next unless $mailing->isa( 'WebGUI::AssetAspect::Subscriber' ); + + if ( $subscribeTo->{ $mailing->getId } ) { + $mailing->subscribe( $user, $sendNotification ) unless $mailing->isSubscribed( $user ); + } + else { + $mailing->unsubscribe( $user, $sendNotification ) if $mailing->isSubscribed( $user ); + } + } + + return; +} + +##------------------------------------------------------------------- +#sub crud_definition { +# my $class = shift; +# my $session = shift; +# my $definition = $class->SUPER::crud_definition( $session ); +# my $i18n = WebGUI::International->new( $session, 'Registration_Step_Homepage' ); +# +# +# $definition->{ dynamic }->{ urlStorageField } = { +# fieldType => 'selectBox', +# label => 'Store homepage url in field', +# options => \%profileFields, +# }; +# +# return $definition; +#} + +#------------------------------------------------------------------- +sub getSummaryTemplateVars { + my $self = shift; + my $includeAdminControls = shift; + my $session = $self->session; + my @fields; + + # Fetch preferred homepage url + my $mailings = $self->getConfigurationData->{ subscribeMailings }; + + my @assets = + grep { defined $_ } + map { WebGUI::Asset->newByDynamicClass( $session, $_ ) } + @{ $mailings }; + + # Preferred homepage url + push @fields, { + field_label => 'Subscribe to mailings', + field_value => join( ', ', map { $_->getTitle } @assets ), +# field_formElement => WebGUI::Form::text( $session, { +# name => 'preferredHomepageUrl', +# value => $preferredHomepageUrl, +# }), + }; + + # Setup tmpl_var + my $var = { + field_loop => \@fields, + category_label => $self->get('title'), + category_edit_url => $self->changeStepDataUrl, + }; + + return ( $var ); +} + +#------------------------------------------------------------------- +sub getViewVars { + my $self = shift; + my $session = $self->session; + my $user = $self->registration->instance->user; + + my $var = $self->SUPER::getViewVars; + + my @subscribed; + tie my %options, 'Tie::IxHash'; + my $availableMailings = $self->getAvailableMailings; + foreach my $mailing ( sort { $a->getTitle cmp $b->getTitle } @{ $availableMailings } ) { + push @subscribed, $mailing->getId if $mailing->isSubscribed( $user ); + + $options{ $mailing->getId } = $mailing->getTitle; + }; + + my @subscribeMailings = ( + $self->session->form->checkList( 'subscribeMailings' ) + || @{ $self->getConfigurationData->{'subscribeMailings'} || [] } + || @subscribed + ); + + push @{ $var->{ field_loop } }, ( + { + field_label => 'Subscribe to mailings', + field_formElement => + WebGUI::Form::checkList($self->session, { + name => 'subscribeMailings', + options => \%options, + value => [ uniq @subscribeMailings ], + vertical=> 1, + }), +# field_subtext => 'Hier komt de subtext voor dit veld' + } + ); + + return $var; +} + +#------------------------------------------------------------------- +sub isComplete { + my $self = shift; + + return defined $self->getConfigurationData->{subscribeMailings}; +} + +#------------------------------------------------------------------- +sub onDeleteAccount { + my $self = shift; + my $doit = shift; + my $session = $self->session; + + $self->SUPER::onDeleteAccount( $doit ); + + return; +} + +#------------------------------------------------------------------- +sub updateFromFormPost { + my $self = shift; + my $session = $self->session; + + $self->SUPER::updateFromFormPost; + +# $self->update( { +# } ); +} + +#------------------------------------------------------------------- +sub processStepFormData { + my $self = shift; + my $session = $self->session; + + my @subscribeMailings = $session->form->checkList( 'subscribeMailings' ); + + $self->setConfigurationData( subscribeMailings => \@subscribeMailings ); + return []; +} + +1;