diff --git a/lib/WebGUI/Mailing.pm b/lib/WebGUI/Mailing.pm index cb03089..baed9fa 100644 --- a/lib/WebGUI/Mailing.pm +++ b/lib/WebGUI/Mailing.pm @@ -2,6 +2,9 @@ package WebGUI::Mailing; use strict; use warnings; +use Carp; + +use WebGUI::Newsletter::Admin; use base 'WebGUI::Crud'; @@ -43,5 +46,115 @@ sub crud_definition { return $definition; } +sub getStatusLine { + my $self = shift; + my $db = $self->session->db; + + my $sth = $db->read( + 'select status, isTest, count( status ) as cnt from WGMailing_queue where mailingId=? group by status,isTest', + [ + $self->getId, + ], + ); + + my $status = {}; + while ( my $row = $sth->hashRef ) { + $status->{ $row->{status} }->{ $row->{ isTest } ? 'test' : 'regular' } = $row->{ cnt }; + }; + + my $output; + foreach ( qw{ queued sent error } ) { + $output .= sprintf '%s ( %i + %i (test) ) | ', + $_, + $status->{$_}->{regular}, + $status->{$_}->{test}, + ; + } + + return $output; +} + +sub queueTestEmails { + my $self = shift; + my $to = shift; + my $userIds = shift; + my $session = $self->session; + + croak "No or invalid to address: [$to]" unless ( $to && !ref $to ); + croak "User ids must be an array ref" unless ( ref $userIds eq 'ARRAY' ); + + foreach my $userId ( @{ $userIds } ) { + WebGUI::Mailing::Email->create( $session, { + mailingId => $self->getId, + userId => $userId, + recipientEmail => $to, + isTest => 1, + } ); + } + + return; +} + +sub www_sendTestEmails { + my $self = shift; + my $session = $self->session; + + my $asset = WebGUI::Asset->newByDynamicClass( $session, $self->get('assetId') ); + croak "Cannot instaciate asset " . $self->get('assetId') unless $asset; + + my $userIds = $asset->getRecipients; + + tie my %options, 'Tie::IxHash', ( + map { $_->getId => $_->username . "(" . $_->get('email') . ")" } + sort { $a->username <=> $b->username } + grep { defined $_ } + map { WebGUI::User->new( $session, $_ ) } + @{ $userIds } + ); + + my $f = WebGUI::HTMLForm->new( $session ); + $f->hidden( + name => 'newsletter', + value => 'mailing', + ); + $f->hidden( + name => 'func', + value => 'sendTestEmailsConfirm', + ); + $f->hidden( + name => 'id', + value => $self->getId, + ); + $f->email( + name => 'to', + label => 'Send emails to test address', + ); + $f->selectList( + name => 'userIds', + label => 'Generate test emails for user(s)', + size => 10, + multiple=> 1, + options => \%options, + ); + $f->submit( 'Send' ); + + return $f->print; +} + +sub www_sendTestEmailsConfirm { + my $self = shift; + my $session = $self->session; + my $form = $session->form; + + my $to = $form->email( 'to' ); + my @userIds = $form->selectList( 'userIds' ); + + $self->queueTestEmails( $to, \@userIds ); + + return WebGUI::Newsletter::Admin->new( $session )->www_view; +} + + + 1; diff --git a/lib/WebGUI/Newsletter/Admin.pm b/lib/WebGUI/Newsletter/Admin.pm index 7e98a36..aacef0f 100644 --- a/lib/WebGUI/Newsletter/Admin.pm +++ b/lib/WebGUI/Newsletter/Admin.pm @@ -62,7 +62,11 @@ sub www_view { my @mailings; my $it = WebGUI::Mailing->getAllIterator( $session, { sequenceKeyValue => $issueId } ); while ( my $mailing = $it->() ) { - push @mailings, $mailing->get; + push @mailings, { + %{ $mailing->get }, + status => $mailing->getStatusLine, + sendTestUrl => $url->page('newsletter=mailing;func=sendTestEmails;id='.$mailing->getId), + }; }; push @issues, {