webgui_newsletter/lib/WebGUI/Mailing/Bounce.pm
2010-12-09 14:10:19 +01:00

162 lines
5.2 KiB
Perl

package WebGUI::Mailing::Bounce;
use strict;
use warnings;
use WebGUI::Mailing::Admin;
#----------------------------------------------------------------------------
sub bounceScoreOk {
my $self = shift;
my $address = shift;
my $setting = $self->session->setting;
my $threshold = $setting->get('newsletterBounceScoreThreshold');
return 1 unless $threshold;
return $self->getBounceScore( $address ) < $threshold;
}
#----------------------------------------------------------------------------
sub getBounceScore {
my $self = shift;
my $address = shift;
my $session = $self->session;
my $it = WebGUI::Mailing::Email->getAllIterator( $session, {
constraints => [
{ 'sentTo=?' => [ $address ] },
{ 'status<>?' => [ 'queued' ] },
],
orderBy => 'sendDate desc',
limit => 10,
} );
my $score = 0;
while ( my $email = $it->() ) {
$score += 1 if $email->get('status') eq 'bounced';
}
return $score;
}
#----------------------------------------------------------------------------
sub new {
my $class = shift;
my $session = shift;
bless { _session => $session }, $class;
}
#----------------------------------------------------------------------------
sub session {
return (shift)->{ _session };
}
#----------------------------------------------------------------------------
sub www_bounceReport {
my $self = shift;
my $session = $self->session;
my $db = $session->db;
my $i18n = WebGUI::International->new( $session, 'MailingManager' );
my $windowSize = 10;
my $sql = <<EOSQL;
select
sentTo,
count(status),
bounceReason,
errorMessage
from
Mailing_email as t1
where
status='bounced'
and
(
(select count(*) from Mailing_email as t2 where t1.sentTo=t2.sentTo) < ?
or
(select lastUpdated from Mailing_email as t2 where t1.sentTo=t2.sentTo order by lastUpdated desc limit ?,1 )
)
group by
sentTo
order by
sentTo, lastUpdated
EOSQL
my $sth = $db->read( $sql, [ $windowSize, $windowSize - 1 ] );
my $output = '<div class="yui-skin-sam"><div id="tableWrapper"><table id="bounceScoreTable"><thead><tr><th>'
. join( '</th><th>',
$i18n->get('email'),
$i18n->get('bounce score'),
$i18n->get('bounce reason'),
$i18n->get('bounce message')
)
. '</th></tr></thead><tbody>';
while ( my $values = $sth->arrayRef ) {
$output .= '<tr><td>'. join( '</td><td>', @$values ) . '</td></tr>';
}
$output .= '</tbody></table></div></div>';
$self->addBounceScoreTableJS;
return WebGUI::Mailing::Admin->new( $session )->getAdminConsole->render( $output, $i18n->get( 'bounce scores' ) );
}
sub addBounceScoreTableJS {
my $self = shift;
my ($style, $url) = $self->session->quick( qw{ style url } );
my $i18n = WebGUI::International->new( $self->session, 'MailingManager' );
my $emailLabel = $i18n->get('email');
my $scoreLabel = $i18n->get('bounce score');
my $reasonLabel = $i18n->get('bounce reason');
my $messageLabel= $i18n->get('bounce message');
my $js = <<EOJS;
<script type="text/javascript">
//<!--
YAHOO.util.Event.onDOMReady( function () {
var columnDefs = [
{ key : "email", label : "$emailLabel", sortable : true },
{ key : "bounceScore", label : "$scoreLabel", sortable : true },
{ key : "bounceReason", label : "$reasonLabel", sortable : true },
{ key : "bounceMessage", label : "$messageLabel", sortable : true }
];
var ds = new YAHOO.util.DataSource( YAHOO.util.Dom.get('bounceScoreTable') );
ds.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
ds.responseSchema = {
fields: [
{ key : 'email' },
{ key : 'bounceScore', parser : 'number' },
{ key : 'bounceReason' },
{ key : 'bounceMessage' }
]
};
var dt = new YAHOO.widget.DataTable( 'tableWrapper', columnDefs, ds, {
sortedBy : {
key : 'bounceScore',
dir : 'desc'
}
} );
} );
//-->
</script>
EOJS
$style->setLink( $url->extras('yui/build/datatable/assets/skins/sam/datatable.css'), { type => 'text/css', rel => 'stylesheet' } );
$style->setScript( $url->extras('yui/build/yahoo-dom-event/yahoo-dom-event.js'), { type => 'text/javascript' } );
$style->setScript( $url->extras('yui/build/element/element-min.js'), { type => 'text/javascript' } );
$style->setScript( $url->extras('yui/build/datasource/datasource-min.js'), { type => 'text/javascript' } );
$style->setScript( $url->extras('yui/build/datatable/datatable-min.js'), { type => 'text/javascript' } );
$style->setRawHeadTags( $js );
return;
}
1;