package WebGUI::Registration::Step::MailingSubscribe; use strict; use warnings; use Tie::IxHash; use List::MoreUtils qw{ uniq }; use WebGUI::Form::Checkbox; 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 $i18n = WebGUI::International->new( $session, 'RegistrationStep_MailingSubscribe' ); my @fields; # Fetch preferred homepage url my $mailings = $self->getConfigurationData->{ subscribeMailings }; my @assets = grep { defined $_ } map { WebGUI::Asset->newByDynamicClass( $session, $_ ) } @{ $mailings }; push @fields, { field_label => $i18n->get( 'Subscribe to mailings' ), field_value => join( ', ', map { $_->getTitle } @assets ), }; # 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 $form = $session->form; my $user = $self->registration->instance->user; my $i18n = WebGUI::International->new( $session, 'RegistrationStep_MailingSubscribe' ); my $availableMailings = $self->getAvailableMailings; # Figure out what mailings to check in the form my @subscribeMailings; if ( $form->checkList( 'subscribeMailings' ) ) { # The user just posted but made an error @subscribeMailings = $form->checkList( 'subscribeMailings' ); } elsif ( exists $self->getConfigurationData->{'subscribeMailings'} ) { # The step is being changed @subscribeMailings = @{ $self->getConfigurationData->{'subscribeMailings'} || [] }; } else { # The step hasn't been done yet. @subscribeMailings = grep { $_->isSubscribed( $user ) } @{ $availableMailings }; } # Create lookup table my %isSubscribed = map { $_ => 1 } @subscribeMailings; my $var = $self->SUPER::getViewVars; # Add form elems in alphabetic order. foreach my $mailing ( sort { $a->getTitle cmp $b->getTitle } @{ $availableMailings } ) { my $id = $mailing->getId; push @{ $var->{ field_loop } }, ( { field_label => $mailing->getTitle, field_formElement => WebGUI::Form::checkbox( $session, { name => 'subscribeMailings', value => $id, checked => $isSubscribed{ $id }, label => $i18n->get( 'Subscribe to this mailing' ), }), } ); } 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;