| 1 | package Template::Plugin::Siesta; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use base qw( Template::Plugin Class::Accessor::Fast ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | __PACKAGE__->mk_accessors(qw( errors context success cgi user )); | ||
| 5 | 1 | ▶ | use Siesta; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Siesta::Message; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Siesta::Deferred; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use CGI (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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 | my $referent = shift; | ||
| 34 | my $context = shift; | ||
| 35 | my %args = ref($_[0]) eq 'HASH' ? %{ $_[0] } : @ _; | ||
| 36 | |||
| 37 | my $class = ref $referent || $referent; | ||
| 38 | my $self = bless { %args, | ||
| 39 | errors => [], | ||
| 40 | context => $context, | ||
| 41 | cgi => CGI->new, | ||
| 42 | }, $class; | ||
| 43 | |||
| 44 | $self->_perform_action | ||
| 45 | if $self->action && $self->cgi->param('submit'); | ||
| 46 | |||
| 47 | 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 | my ($self, $action) = @_; | ||
| 60 | |||
| 61 | if ($action) { | ||
| 62 | if ($self->can("ACTION_$action") ) { | ||
| 63 | $self->{action} = $action; | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | $self->error("Template::Siesta::Plugin - Unknown action $action"); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | return $self->{action}; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _perform_action { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | my $action_method = "ACTION_" . $self->action; | ||
| 76 | $self->errors([]); # zero the errors from previous action. | ||
| 77 | $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 | my $self = shift; | ||
| 84 | |||
| 85 | my ($pass1) = $self->_getParam('pass1',"(\\w{$MIN_PASS,40})"); | ||
| 86 | my ($pass2) = $self->_getParam('pass2',"(\\w{$MIN_PASS,40})"); | ||
| 87 | my ($email) = $self->_getParam('email','(\S{6,40})' ); | ||
| 88 | |||
| 89 | unless (defined $pass1) { | ||
| 90 | $self->error("Passwords must be at least $MIN_PASS long"); | ||
| 91 | } | ||
| 92 | if ( defined($pass1) && defined($pass2) && $pass1 ne $pass2) { | ||
| 93 | $self->error("Password and confirmation must match"); | ||
| 94 | } | ||
| 95 | |||
| 96 | my $user = Siesta::Member->load($email); | ||
| 97 | if ($user) { | ||
| 98 | $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 | my @subscriptions = $self->cgi->param('subscribe'); | ||
| 105 | |||
| 106 | $user ||= Siesta::Member->create({ email => $email }); | ||
| 107 | $user->password($pass1); | ||
| 108 | $user->update; | ||
| 109 | |||
| 110 | foreach my $list_name (@subscriptions) { | ||
| 111 | #print "list name $list_name"; | ||
| 112 | my($list) = Siesta::List->load( $list_name ); | ||
| 113 | unless ($list) { | ||
| 114 | $self->error( "Failed to find a list called $list_name" ); | ||
| 115 | next; | ||
| 116 | } | ||
| 117 | $list->add_member($user); | ||
| 118 | |||
| 119 | my $mail = Siesta::Message->new(); | ||
| 120 | $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 | return 1; # success | ||
| 129 | } | ||
| 130 | |||
| 131 | sub ACTION_login { | ||
| 132 | my $self = shift; | ||
| 133 | my ($email) = $self->_getParam('email', '(\S+)' ); | ||
| 134 | my ($pass) = $self->_getParam('pass', '(\S+)' ); | ||
| 135 | |||
| 136 | my $user = Siesta::Member->load( $email ) or return; | ||
| 137 | |||
| 138 | # no null passwords | ||
| 139 | return unless $pass; | ||
| 140 | if ($pass eq $user->password) { | ||
| 141 | return $user; | ||
| 142 | } | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub ACTION_move_plugin { | ||
| 147 | my $self = shift; | ||
| 148 | |||
| 149 | my $plugin = Siesta::Plugin->retrieve( $self->_getParam('id', '(\d+)' )) | ||
| 150 | or return; | ||
| 151 | my $list = $plugin->list; | ||
| 152 | return unless $self->user == $list->owner; | ||
| 153 | |||
| 154 | my ($to) = $self->_getParam( 'to', '(\d+)' ); | ||
| 155 | # the rest of the queue | ||
| 156 | my @queue = grep { $_ != $plugin } $list->plugins( $plugin->queue ); | ||
| 157 | splice @queue, $to - 1, 0, $plugin; | ||
| 158 | $list->set_plugins( $plugin->queue => map { $_->name } @queue ); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub ACTION_add_plugin { | ||
| 162 | my $self = shift; | ||
| 163 | |||
| 164 | my $list = Siesta::List->load( $self->_getParam('list', '(\S+)') ) | ||
| 165 | or return; | ||
| 166 | return unless $self->user->id == $list->owner->id; | ||
| 167 | |||
| 168 | my ($queue) = $self->_getParam('queue', '(\S+)'); | ||
| 169 | my ($type) = $self->_getParam('type', '(\S+)'); | ||
| 170 | |||
| 171 | # mmm, evil tastes sooo good | ||
| 172 | eval { | ||
| 173 | $list->add_plugin( $queue, | ||
| 174 | ( $self->_getParam('personal', '(\S+)') ? '+' : '') . $type ); | ||
| 175 | } or do { $self->error( $@ ); return }; | ||
| 176 | return 1; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub ACTION_delete_plugin { | ||
| 180 | my $self = shift; | ||
| 181 | |||
| 182 | my ($id) = $self->_getParam( 'id', '(\d+)' ); | ||
| 183 | my $plugin = Siesta::Plugin->retrieve( $id ) or return; | ||
| 184 | return unless $plugin->list->owner->id == $self->user->id; | ||
| 185 | $plugin->delete; | ||
| 186 | return 1; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub ACTION_resume_message { | ||
| 190 | my $self = shift; | ||
| 191 | |||
| 192 | my $message = Siesta::Deferred->retrieve( | ||
| 193 | $self->_getParam( 'id', '(\d+)' ) | ||
| 194 | ) | ||
| 195 | or return; | ||
| 196 | return unless $self->user->id == $message->who; | ||
| 197 | |||
| 198 | Siesta::Message->resume( $message->id ); | ||
| 199 | return 1; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub ACTION_set_pref { | ||
| 203 | my $self = shift; | ||
| 204 | |||
| 205 | my $list = Siesta::List->retrieve( $self->_getParam( 'list', | ||
| 206 | qr/^(\d+)$/ ) ) | ||
| 207 | or return; | ||
| 208 | for my $plugin (map { $_->promote } Siesta::Plugin->search({ list => $list })) { | ||
| 209 | for my $pref (keys %{ $plugin->options }) { | ||
| 210 | my $val; | ||
| 211 | if ($plugin->personal && | ||
| 212 | ( ($val) = $self->_getParam( "personal_$pref", '(.*)' ) ) ) { | ||
| 213 | $plugin->member( $self->user ); | ||
| 214 | $plugin->pref( $pref, $val ); | ||
| 215 | } | ||
| 216 | if (( $plugin->list->owner == $self->user ) && | ||
| 217 | ( ($val) = $self->_getParam( "list_$pref", '(.*)' ) ) ) { | ||
| 218 | $plugin->member( undef ); | ||
| 219 | $plugin->pref( $pref, $val ); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | return 1; | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | sub _getParam { | ||
| 228 | my ($self,$param,$regex) = @_; | ||
| 229 | |||
| 230 | my $var = $self->cgi->param($param); | ||
| 231 | if (defined $var) { | ||
| 232 | return $var =~ /$regex/; | ||
| 233 | } | ||
| 234 | return; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub user { $_[0]->context->stash->get('session.user') } | ||
| 238 | |||
| 239 | sub available_plugins { | ||
| 240 | [ Siesta->available_plugins ]; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub lists { | ||
| 244 | [ Siesta::List->retrieve_all ]; | ||
| 245 | } | ||
| 246 | |||
| 247 | sub list { | ||
| 248 | my ($self, $list) = @_; | ||
| 249 | Siesta::List->load( $list ); | ||
| 250 | } | ||
| 251 | |||
| 252 | # messages deferred for the current user | ||
| 253 | sub deferred { | ||
| 254 | my $self = shift; | ||
| 255 | my $id = shift; | ||
| 256 | if ($id) { | ||
| 257 | return Siesta::Deferred->search( who => $self->user, id => $id); | ||
| 258 | } | ||
| 259 | [ 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 | my $self = shift; | ||
| 270 | push @{ $self->errors }, @_; | ||
| 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; | ||