213 lines
5.7 KiB
Perl
213 lines
5.7 KiB
Perl
package WebGUI::Asset::Wobject::NewsletterCollection;
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use Class::C3;
|
|
|
|
use base qw{
|
|
WebGUI::AssetAspect::Mailable
|
|
WebGUI::Asset::Wobject
|
|
};
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub canSubscribe {
|
|
my $self = shift;
|
|
|
|
return !$self->session->user->isVisitor && !$self->isSubscribed;
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub canUnsubscribe {
|
|
my $self = shift;
|
|
|
|
return !$self->session->user->isVisitor && $self->isSubscribed;
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub configureMailingForm {
|
|
my $self = shift;
|
|
my $f = $self->next::method;
|
|
|
|
tie my %issues, 'Tie::IxHash', (
|
|
'' => 'Choose issue...',
|
|
map { $_->getId => $_->getTitle }
|
|
@{ $self->getIssues }
|
|
);
|
|
|
|
$f->selectBox(
|
|
name => $issue,
|
|
label => 'Issue',
|
|
options => $self->getIssues
|
|
);
|
|
|
|
return $f;
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub definition {
|
|
my $class = shift;
|
|
my $session = shift;
|
|
my $definition = shift;
|
|
my $i18n = WebGUI::International->new( $session, 'Asset_NewsletterCollection' );
|
|
|
|
tie my %properties, 'Tie::IxHash', (
|
|
);
|
|
|
|
push @{ $definition }, {
|
|
assetName => $i18n->echo('newsletter'),
|
|
icon => 'newWobject.gif',
|
|
autoGenerateForms => 1,
|
|
tableName => 'NewsletterCollection',
|
|
className => 'WebGUI::Asset::Wobject::NewsletterCollection',
|
|
properties => \%properties
|
|
};
|
|
|
|
return $class->SUPER::definition( $session, $definition );
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub getIssues {
|
|
my $self = shift;
|
|
|
|
my $issues = $self->getLineage( [ 'children' ], {
|
|
returnObjects => 1,
|
|
orderByClause => 'revisionDate desc',
|
|
} );
|
|
|
|
return $issues;
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub getRecipients {
|
|
my $self = shift;
|
|
|
|
my @subscribers = $db->buildArray( 'select userId from NewsletterCollection_subscriptions where assetId=?', [
|
|
$self->getId,
|
|
] );
|
|
|
|
return \@subscribers;
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub generateEmailContent {
|
|
my $self = shift;
|
|
my $session = $self->session;
|
|
my $form = $session->form;
|
|
|
|
my $issueId = $form->get('issueId');
|
|
my $issue = WebGUI::Asset->newByDynamicClass( $session, $issueId );
|
|
|
|
return "Invalid issueId [$issueId]" unless $issue;
|
|
|
|
$issue->prepareView;
|
|
return $issue->view;
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub isSubscribed {
|
|
my $self = shift;
|
|
my $userId = shift || $self->session->user->getId;
|
|
my $db = $self->session->db;
|
|
|
|
return $db->quickScalar( 'select 1 from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
|
$self->getId,
|
|
$userId,
|
|
] );
|
|
}
|
|
|
|
##----------------------------------------------------------------------------
|
|
#sub prepareView {
|
|
# my $self = shift;
|
|
# $self->SUPER::prepareView();
|
|
# my $template = WebGUI::Asset::Template->new( $self->session, $self->get("templateIdView") );
|
|
# $template->prepare($self->getMetaDataAsTemplateVariables);
|
|
# $self->{_viewTemplate} = $template;
|
|
#}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub view {
|
|
my $self = shift;
|
|
my $session = $self->session;
|
|
|
|
my $output = $self->getToolbar if $session->var->isAdminOn;
|
|
|
|
my $issues = $self->getIssues;
|
|
return $output . "No issues yet" unless scalar @{ $issues };
|
|
|
|
$output .= $self->isSubscribed
|
|
? '<p><a href="' . $self->getUrl( 'func=unsubscribe' ) . '">Unsubscribe</a></p>'
|
|
: '<p><a href="' . $self->getUrl( 'func=subscribe' ) . '">Subscribe</a></p>'
|
|
;
|
|
|
|
$output .= '<p>Issues: <ul>';
|
|
$output .=
|
|
join '',
|
|
map {
|
|
'<li>'
|
|
.'<a href="' . $_->getUrl . '">' . $_->get('title') . '</a>'
|
|
.' :: '
|
|
.' <a href="' .$self->getUrl('func=previewEmail;issueId='.$_->getId.';userId=3').'">preview</a>'
|
|
.'</li>'
|
|
}
|
|
@{ $issues };
|
|
$output .= '</ul></p>';
|
|
|
|
$issues->[0]->prepareView;
|
|
$output .= $issues->[0]->view;
|
|
|
|
return $output;
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub subscribe {
|
|
my $self = shift;
|
|
my $userId = shift || $self->session->user->getId;
|
|
my $db = $self->session->db;
|
|
|
|
$db->write( 'replace into NewsletterCollection_subscriptions set assetId=?, userId=?', [
|
|
$self->getId,
|
|
$userId,
|
|
] );
|
|
|
|
return;
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub unsubscribe {
|
|
my $self = shift;
|
|
my $userId = shift || $self->session->user->getId;
|
|
my $db = $self->session->db;
|
|
|
|
$db->write( 'delete from NewsletterCollection_subscriptions where assetId=? and userId=?', [
|
|
$self->getId,
|
|
$userId,
|
|
] );
|
|
|
|
return;
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub www_subscribe {
|
|
my $self = shift;
|
|
|
|
if ( $self->canSubscribe ) {
|
|
$self->subscribe;
|
|
}
|
|
|
|
return $self->www_view;
|
|
}
|
|
|
|
#----------------------------------------------------------------------------
|
|
sub www_unsubscribe {
|
|
my $self = shift;
|
|
|
|
if ( $self->canUnsubscribe ) {
|
|
$self->unsubscribe;
|
|
}
|
|
|
|
return $self->www_view;
|
|
}
|
|
|
|
1;
|
|
|