File Coverage

blib/lib/Template/Plugin/Siesta.pm
Criterion Covered Total %
statement 18 131 13.7
branch 0 52 0.0
condition 0 21 0.0
subroutine 6 23 26.0
pod 3 15 20.0
total 27 242 11.1


line stmt bran cond sub pod time code
1             package Template::Plugin::Siesta;
2 1     1   28467 use strict;
  1         3  
  1         47  
3 1     1   6 use base qw( Template::Plugin Class::Accessor::Fast );
  1         2  
  1         1011  
4             __PACKAGE__->mk_accessors(qw( errors context success cgi user ));
5 1     1   1023 use Siesta;
  1         2  
  1         11  
6 1     1   23 use Siesta::Message;
  1         2  
  1         16  
7 1     1   27 use Siesta::Deferred;
  1         2  
  1         18  
8 1     1   5092 use CGI ();
  1         16965  
  1         1766  
9              
10             =head1 NAME
11              
12             Template::Plugin::Siesta - convenience class for Siesta template pages
13              
14             =head1 METHODS
15              
16             =item ->new( {foo => 'bar'} )
17              
18             creates a new Template::Siesta::Plugin from, using a hashref to
19             provide arguments,
20              
21             =item ->new( foo => 'bar' )
22              
23             creates a new Template::Siesta::Plugin from, using an array of name
24             value pairs to provide arguments,
25              
26             if the arguments contain an action request then ( see ->action() )
27             then the requested action will be performed before returning the new
28             object;
29              
30             =cut
31              
32             sub new {
33 0     0 1   my $referent = shift;
34 0           my $context = shift;
35 0 0         my %args = ref($_[0]) eq 'HASH' ? %{ $_[0] } : @ _;
  0            
36              
37 0   0       my $class = ref $referent || $referent;
38 0           my $self = bless { %args,
39             errors => [],
40             context => $context,
41             cgi => CGI->new,
42             }, $class;
43              
44 0 0 0       $self->_perform_action
45             if $self->action && $self->cgi->param('submit');
46              
47 0           return $self;
48             }
49              
50             =item ->action
51              
52             if called with no aruments, returns the currently defined action. if
53             called with a string value, sets the action or warns of an error if
54             the class cannot ->ACTION_$action
55              
56             =cut
57              
58             sub action {
59 0     0 1   my ($self, $action) = @_;
60              
61 0 0         if ($action) {
62 0 0         if ($self->can("ACTION_$action") ) {
63 0           $self->{action} = $action;
64             }
65             else {
66 0           $self->error("Template::Siesta::Plugin - Unknown action $action");
67             }
68             }
69 0           return $self->{action};
70             }
71              
72             sub _perform_action {
73 0     0     my $self = shift;
74              
75 0           my $action_method = "ACTION_" . $self->action;
76 0           $self->errors([]); # zero the errors from previous action.
77 0           $self->success( $self->$action_method() );
78             }
79              
80              
81             my $MIN_PASS = 6; # should come out of a config I guess ...
82             sub ACTION_register {
83 0     0 0   my $self = shift;
84              
85 0           my ($pass1) = $self->_getParam('pass1',"(\\w{$MIN_PASS,40})");
86 0           my ($pass2) = $self->_getParam('pass2',"(\\w{$MIN_PASS,40})");
87 0           my ($email) = $self->_getParam('email','(\S{6,40})' );
88              
89 0 0         unless (defined $pass1) {
90 0           $self->error("Passwords must be at least $MIN_PASS long");
91             }
92 0 0 0       if ( defined($pass1) && defined($pass2) && $pass1 ne $pass2) {
      0        
93 0           $self->error("Password and confirmation must match");
94             }
95              
96 0           my $user = Siesta::Member->load($email);
97 0 0         if ($user) {
98 0           $self->error("This email address is already subscribed");
99             }
100              
101             # should return a list of the ticked checkboxes need to confirm
102             # they are public lists, as you shouldnt be able to sub to private
103             # lists before you are subscribed I guess.
104 0           my @subscriptions = $self->cgi->param('subscribe');
105              
106 0   0       $user ||= Siesta::Member->create({ email => $email });
107 0           $user->password($pass1);
108 0           $user->update;
109              
110 0           foreach my $list_name (@subscriptions) {
111             #print "list name $list_name";
112 0           my($list) = Siesta::List->load( $list_name );
113 0 0         unless ($list) {
114 0           $self->error( "Failed to find a list called $list_name" );
115 0           next;
116             }
117 0           $list->add_member($user);
118              
119 0           my $mail = Siesta::Message->new();
120 0           $mail->reply( to => $list->owner->email,
121             subject => 'web subscription',
122             body => Siesta->bake('subscribe_notify',
123             list => $list,
124             user => $user,
125             message => $mail ),
126             );
127             }
128 0           return 1; # success
129             }
130              
131             sub ACTION_login {
132 0     0 0   my $self = shift;
133 0           my ($email) = $self->_getParam('email', '(\S+)' );
134 0           my ($pass) = $self->_getParam('pass', '(\S+)' );
135              
136 0 0         my $user = Siesta::Member->load( $email ) or return;
137              
138             # no null passwords
139 0 0         return unless $pass;
140 0 0         if ($pass eq $user->password) {
141 0           return $user;
142             }
143 0           return;
144             }
145              
146             sub ACTION_move_plugin {
147 0     0 0   my $self = shift;
148              
149 0 0         my $plugin = Siesta::Plugin->retrieve( $self->_getParam('id', '(\d+)' ))
150             or return;
151 0           my $list = $plugin->list;
152 0 0         return unless $self->user == $list->owner;
153              
154 0           my ($to) = $self->_getParam( 'to', '(\d+)' );
155             # the rest of the queue
156 0           my @queue = grep { $_ != $plugin } $list->plugins( $plugin->queue );
  0            
157 0           splice @queue, $to - 1, 0, $plugin;
158 0           $list->set_plugins( $plugin->queue => map { $_->name } @queue );
  0            
159             }
160              
161             sub ACTION_add_plugin {
162 0     0 0   my $self = shift;
163              
164 0 0         my $list = Siesta::List->load( $self->_getParam('list', '(\S+)') )
165             or return;
166 0 0         return unless $self->user->id == $list->owner->id;
167              
168 0           my ($queue) = $self->_getParam('queue', '(\S+)');
169 0           my ($type) = $self->_getParam('type', '(\S+)');
170              
171             # mmm, evil tastes sooo good
172             eval {
173 0 0         $list->add_plugin( $queue,
174             ( $self->_getParam('personal', '(\S+)') ? '+' : '') . $type );
175 0 0         } or do { $self->error( $@ ); return };
  0            
  0            
176 0           return 1;
177             }
178              
179             sub ACTION_delete_plugin {
180 0     0 0   my $self = shift;
181              
182 0           my ($id) = $self->_getParam( 'id', '(\d+)' );
183 0 0         my $plugin = Siesta::Plugin->retrieve( $id ) or return;
184 0 0         return unless $plugin->list->owner->id == $self->user->id;
185 0           $plugin->delete;
186 0           return 1;
187             }
188              
189             sub ACTION_resume_message {
190 0     0 0   my $self = shift;
191              
192 0 0         my $message = Siesta::Deferred->retrieve(
193             $self->_getParam( 'id', '(\d+)' )
194             )
195             or return;
196 0 0         return unless $self->user->id == $message->who;
197              
198 0           Siesta::Message->resume( $message->id );
199 0           return 1;
200             }
201              
202             sub ACTION_set_pref {
203 0     0 0   my $self = shift;
204              
205 0 0         my $list = Siesta::List->retrieve( $self->_getParam( 'list',
206             qr/^(\d+)$/ ) )
207             or return;
208 0           for my $plugin (map { $_->promote } Siesta::Plugin->search({ list => $list })) {
  0            
209 0           for my $pref (keys %{ $plugin->options }) {
  0            
210 0           my $val;
211 0 0 0       if ($plugin->personal &&
212             ( ($val) = $self->_getParam( "personal_$pref", '(.*)' ) ) ) {
213 0           $plugin->member( $self->user );
214 0           $plugin->pref( $pref, $val );
215             }
216 0 0 0       if (( $plugin->list->owner == $self->user ) &&
217             ( ($val) = $self->_getParam( "list_$pref", '(.*)' ) ) ) {
218 0           $plugin->member( undef );
219 0           $plugin->pref( $pref, $val );
220             }
221             }
222             }
223 0           return 1;
224             }
225              
226              
227             sub _getParam {
228 0     0     my ($self,$param,$regex) = @_;
229              
230 0           my $var = $self->cgi->param($param);
231 0 0         if (defined $var) {
232 0           return $var =~ /$regex/;
233             }
234 0           return;
235             }
236              
237 0     0 0   sub user { $_[0]->context->stash->get('session.user') }
238              
239             sub available_plugins {
240 0     0 0   [ Siesta->available_plugins ];
241             }
242              
243             sub lists {
244 0     0 0   [ Siesta::List->retrieve_all ];
245             }
246              
247             sub list {
248 0     0 0   my ($self, $list) = @_;
249 0           Siesta::List->load( $list );
250             }
251              
252             # messages deferred for the current user
253             sub deferred {
254 0     0 0   my $self = shift;
255 0           my $id = shift;
256 0 0         if ($id) {
257 0           return Siesta::Deferred->search( who => $self->user, id => $id);
258             }
259 0           [ Siesta::Deferred->search( who => $self->user ) ];
260             }
261              
262             =item ->error( $what )
263              
264             blow an error
265              
266             =cut
267              
268             sub error {
269 0     0 1   my $self = shift;
270 0           push @{ $self->errors }, @_;
  0            
271             }
272              
273             =item ->errors
274              
275             returns a list of errors that ocurred during an action request.
276              
277             =item ->success
278              
279             Return value of the action
280              
281             =cut
282              
283             1;