| 1 | package Miril::CGI::Application; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use autodie; | ||
| 6 | |||
| 7 | use Try::Tiny; | ||
| 8 | use Exception::Class; | ||
| 9 | |||
| 10 | use base 'CGI::Application'; | ||
| 11 | |||
| 12 | use CGI::Application::Plugin::Authentication; | ||
| 13 | use CGI::Application::Plugin::Redirect; | ||
| 14 | use CGI::Application::Plugin::Forward; | ||
| 15 | use Module::Load; | ||
| 16 | use File::Spec::Functions qw(catfile); | ||
| 17 | use Data::AsObject qw(dao); | ||
| 18 | use Data::Page; | ||
| 19 | use Ref::List qw(list); | ||
| 20 | use Miril; | ||
| 21 | use Miril::Exception; | ||
| 22 | use Miril::Theme::Flashyweb; | ||
| 23 | use Miril::View; | ||
| 24 | use Miril::InputValidator; | ||
| 25 | use Miril::Publisher; | ||
| 26 | use File::Copy qw(copy); | ||
| 27 | use Number::Format qw(format_bytes); | ||
| 28 | use POSIX qw(strftime); | ||
| 29 | use Syntax::Keyword::Gather qw(gather take); | ||
| 30 | |||
| 31 | ### ACCESSORS ### | ||
| 32 | |||
| 33 | use Object::Tiny qw( | ||
| 34 | view | ||
| 35 | user_manager | ||
| 36 | miril | ||
| 37 | validator | ||
| 38 | ); | ||
| 39 | |||
| 40 | ### SETUP ### | ||
| 41 | |||
| 42 | sub setup { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | # setup runmodes | ||
| 46 | |||
| 47 | $self->mode_param('action'); | ||
| 48 | $self->run_modes( | ||
| 49 | 'list' => 'posts_list', | ||
| 50 | 'edit' => 'posts_edit', | ||
| 51 | 'create' => 'posts_create', | ||
| 52 | 'delete' => 'posts_delete', | ||
| 53 | 'view' => 'posts_view', | ||
| 54 | 'update' => 'posts_update', | ||
| 55 | 'publish' => 'posts_publish', | ||
| 56 | 'files' => 'files_list', | ||
| 57 | 'upload' => 'files_upload', | ||
| 58 | 'unlink' => 'files_delete', | ||
| 59 | 'search' => 'search', | ||
| 60 | 'login' => 'login', | ||
| 61 | 'logout' => 'logout', | ||
| 62 | 'account' => 'account', | ||
| 63 | ); | ||
| 64 | |||
| 65 | $self->start_mode('list'); | ||
| 66 | $self->error_mode('error'); | ||
| 67 | |||
| 68 | # setup miril | ||
| 69 | my $miril_dir = $self->param('miril_dir'); | ||
| 70 | my $site = $self->param('site'); | ||
| 71 | $self->{miril}= Miril->new($miril_dir, $site); | ||
| 72 | |||
| 73 | # configure authentication | ||
| 74 | try { | ||
| 75 | my $user_manager_name = "Miril::UserManager::" . $self->miril->cfg->user_manager; | ||
| 76 | load $user_manager_name; | ||
| 77 | $self->{user_manager} = $user_manager_name->new($self->miril); | ||
| 78 | } catch { | ||
| 79 | Miril::Exception->throw( | ||
| 80 | errorvar => $_, | ||
| 81 | message => 'Could not load user manager', | ||
| 82 | ); | ||
| 83 | }; | ||
| 84 | |||
| 85 | $self->authen->config( | ||
| 86 | DRIVER => [ 'Generic', $self->user_manager->verification_callback ], | ||
| 87 | LOGIN_RUNMODE => 'login', | ||
| 88 | LOGOUT_RUNMODE => 'logout', | ||
| 89 | CREDENTIALS => [ 'authen_username', 'authen_password' ], | ||
| 90 | STORE => [ 'Cookie', SECRET => $self->miril->cfg->secret, EXPIRY => '+30d', NAME => 'miril_authen' ], | ||
| 91 | ); | ||
| 92 | |||
| 93 | $self->authen->protected_runmodes(':all'); | ||
| 94 | |||
| 95 | |||
| 96 | # load view | ||
| 97 | $self->{view} = Miril::View->new( | ||
| 98 | theme => Miril::Theme::Flashyweb->new, | ||
| 99 | is_authenticated => $self->authen->is_authenticated, | ||
| 100 | latest => $self->miril->store->get_latest, | ||
| 101 | miril => $self->miril, | ||
| 102 | |||
| 103 | ); | ||
| 104 | |||
| 105 | $self->{validator} = Miril::InputValidator->new; | ||
| 106 | $self->header_add( -type => 'text/html; charset=utf-8'); | ||
| 107 | |||
| 108 | } | ||
| 109 | |||
| 110 | ### RUN MODES ### | ||
| 111 | |||
| 112 | sub error { | ||
| 113 | my ($self, $e) = @_; | ||
| 114 | |||
| 115 | my @error_stack; | ||
| 116 | |||
| 117 | if ($e->isa('Miril::Exception')) { | ||
| 118 | warn $e->errorvar; | ||
| 119 | } elsif ($e->isa('autodie::exception')) { | ||
| 120 | warn $e; | ||
| 121 | $e = Miril::Exception->new( | ||
| 122 | message => "Unspecified error", | ||
| 123 | errorvar => $e->stringify, | ||
| 124 | ); | ||
| 125 | } else { | ||
| 126 | warn $e; | ||
| 127 | $e = Miril::Exception->new( | ||
| 128 | message => "Unspecified error", | ||
| 129 | errorvar => $e, | ||
| 130 | ); | ||
| 131 | } | ||
| 132 | $self->view->{fatal} = $e; | ||
| 133 | my $tmpl = $self->view->load('error'); | ||
| 134 | return $tmpl->output; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub posts_list { | ||
| 138 | my $self = shift; | ||
| 139 | my $q = $self->query; | ||
| 140 | |||
| 141 | my @posts = $self->miril->store->get_posts( | ||
| 142 | author => ( $q->param('author') or undef ), | ||
| 143 | title => ( $q->param('title' ) or undef ), | ||
| 144 | type => ( $q->param('type' ) or undef ), | ||
| 145 | status => ( $q->param('status') or undef ), | ||
| 146 | topic => ( $q->param('topic' ) or undef ), | ||
| 147 | ); | ||
| 148 | |||
| 149 | my @current_posts = $self->_paginate(@posts); | ||
| 150 | |||
| 151 | my $tmpl = $self->view->load('list'); | ||
| 152 | $tmpl->param('posts', {list => \@current_posts}); | ||
| 153 | return $tmpl->output; | ||
| 154 | |||
| 155 | } | ||
| 156 | |||
| 157 | sub search { | ||
| 158 | my $self = shift; | ||
| 159 | |||
| 160 | my $cfg = $self->miril->cfg; | ||
| 161 | |||
| 162 | my $tmpl = $self->view->load('search'); | ||
| 163 | |||
| 164 | $tmpl->param('statuses', $self->_prepare_statuses ); | ||
| 165 | $tmpl->param('types', $self->_prepare_types ); | ||
| 166 | $tmpl->param('topics', $self->_prepare_topics ) if $cfg->topics->list; | ||
| 167 | $tmpl->param('authors', $self->_prepare_authors ) if $cfg->authors->list; | ||
| 168 | |||
| 169 | return $tmpl->output; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub posts_create { | ||
| 173 | my $self = shift; | ||
| 174 | |||
| 175 | my $cfg = $self->miril->cfg; | ||
| 176 | |||
| 177 | my $empty_post; | ||
| 178 | |||
| 179 | $empty_post->{statuses} = $self->_prepare_statuses; | ||
| 180 | $empty_post->{types} = $self->_prepare_types; | ||
| 181 | $empty_post->{authors} = $self->_prepare_authors if $cfg->authors->list; | ||
| 182 | $empty_post->{topics} = $self->_prepare_topics if $cfg->topics->list; | ||
| 183 | |||
| 184 | my $tmpl = $self->view->load('edit'); | ||
| 185 | $tmpl->param('post', $empty_post); | ||
| 186 | |||
| 187 | return $tmpl->output; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub posts_edit { | ||
| 191 | my ($self, $invalid_id) = @_; | ||
| 192 | |||
| 193 | my $cfg = $self->miril->cfg; | ||
| 194 | my $q = $self->query; | ||
| 195 | my $post; | ||
| 196 | |||
| 197 | if ($invalid_id) { | ||
| 198 | my %cur_topics; | ||
| 199 | if ($q->param('topic')) { | ||
| 200 | %cur_topics = map {$_ => 1} $q->param('topic'); | ||
| 201 | } | ||
| 202 | $post = dao { | ||
| 203 | id => $q->param('id'), | ||
| 204 | old_id => $q->param('old_id'), | ||
| 205 | source => $q->param('source'), | ||
| 206 | title => $q->param('title'), | ||
| 207 | authors => $self->_prepare_authors($q->param('author')), | ||
| 208 | topics => $self->_prepare_topics(%cur_topics), | ||
| 209 | statuses => $self->_prepare_statuses($q->param('status')), | ||
| 210 | types => $self->_prepare_types($q->param('type')), | ||
| 211 | }; | ||
| 212 | use Data::Dumper; | ||
| 213 | warn Dumper $post; | ||
| 214 | } else { | ||
| 215 | #TODO check if $post is defined | ||
| 216 | $post = $self->miril->store->get_post($q->param('id')); | ||
| 217 | |||
| 218 | my %cur_topics; | ||
| 219 | |||
| 220 | #FIXME | ||
| 221 | if ( list $post->topics ) | ||
| 222 | { | ||
| 223 | %cur_topics = map {$_->id => 1} list $post->topics; | ||
| 224 | } | ||
| 225 | |||
| 226 | $post->{authors} = $self->_prepare_authors($post->author) if $cfg->authors->list; | ||
| 227 | $post->{topics} = $self->_prepare_topics(%cur_topics) if $cfg->topics->list; | ||
| 228 | $post->{statuses} = $self->_prepare_statuses($post->status); | ||
| 229 | $post->{types} = $self->_prepare_types($post->type->id); | ||
| 230 | } | ||
| 231 | |||
| 232 | my $tmpl = $self->view->load('edit'); | ||
| 233 | $tmpl->param('post', $post); | ||
| 234 | $tmpl->param('invalid', $self->param('invalid')); | ||
| 235 | |||
| 236 | $self->miril->store->add_to_latest($post->id, $post->title); | ||
| 237 | |||
| 238 | return $tmpl->output; | ||
| 239 | } | ||
| 240 | |||
| 241 | sub posts_update { | ||
| 242 | my $self = shift; | ||
| 243 | my $q = $self->query; | ||
| 244 | |||
| 245 | my $invalid = $self->validator->validate({ | ||
| 246 | id => 'text_id required', | ||
| 247 | author => 'line_text', | ||
| 248 | status => 'text_id', | ||
| 249 | source => 'paragraph_text', | ||
| 250 | title => 'line_text required', | ||
| 251 | type => 'text_id required', | ||
| 252 | old_id => 'text_id', | ||
| 253 | }, $q->Vars); | ||
| 254 | |||
| 255 | if ($invalid) { | ||
| 256 | $self->param('invalid', $invalid); | ||
| 257 | return $self->forward('edit', $q->param('old_id')); | ||
| 258 | } | ||
| 259 | |||
| 260 | my %post = ( | ||
| 261 | 'id' => $q->param('id'), | ||
| 262 | 'author' => ( $q->param('author') or undef ), | ||
| 263 | 'status' => ( $q->param('status') or undef ), | ||
| 264 | 'source' => ( $q->param('source') or undef ), | ||
| 265 | 'title' => ( $q->param('title') or undef ), | ||
| 266 | 'type' => ( $q->param('type') or undef ), | ||
| 267 | 'old_id' => ( $q->param('old_id') or undef ), | ||
| 268 | ); | ||
| 269 | |||
| 270 | # SHOULD NOT BE HERE | ||
| 271 | $post{topics} = [$q->param('topic')] if $q->param('topic'); | ||
| 272 | |||
| 273 | $self->miril->store->save(%post); | ||
| 274 | |||
| 275 | return $self->redirect("?action=view&id=" . $post{id}); | ||
| 276 | } | ||
| 277 | |||
| 278 | sub posts_delete { | ||
| 279 | my $self = shift; | ||
| 280 | |||
| 281 | my $id = $self->query->param('old_id'); | ||
| 282 | $self->miril->store->delete($id); | ||
| 283 | |||
| 284 | return $self->redirect("?action=list"); | ||
| 285 | } | ||
| 286 | |||
| 287 | sub posts_view { | ||
| 288 | my $self = shift; | ||
| 289 | |||
| 290 | my $q = $self->query; | ||
| 291 | my $id = $q->param('old_id') ? $q->param('old_id') : $q->param('id'); | ||
| 292 | |||
| 293 | my $post = $self->miril->store->get_post($id); | ||
| 294 | if ($post) { | ||
| 295 | $post->{body} = $post->body; | ||
| 296 | |||
| 297 | my $tmpl = $self->view->load('view'); | ||
| 298 | $tmpl->param('post', $post); | ||
| 299 | return $tmpl->output; | ||
| 300 | } else { | ||
| 301 | return $self->redirect("?action=list"); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | sub login { | ||
| 306 | my $self = shift; | ||
| 307 | |||
| 308 | my $tmpl = $self->view->load('login'); | ||
| 309 | return $tmpl->output; | ||
| 310 | } | ||
| 311 | |||
| 312 | sub logout { | ||
| 313 | my $self = shift; | ||
| 314 | |||
| 315 | $self->authen->logout(); | ||
| 316 | |||
| 317 | return $self->redirect("?action=login"); | ||
| 318 | } | ||
| 319 | |||
| 320 | sub account | ||
| 321 | { | ||
| 322 | my $self = shift; | ||
| 323 | my $q = $self->query; | ||
| 324 | |||
| 325 | if ( $q->param('name') or $q->param('new_password') ) | ||
| 326 | { | ||
| 327 | |||
| 328 | my $username = $q->param('username'); | ||
| 329 | my $name = $q->param('name'); | ||
| 330 | my $new_password = $q->param('new_password'); | ||
| 331 | my $retype_password = $q->param('retype_password'); | ||
| 332 | my $password = $q->param('password'); | ||
| 333 | |||
| 334 | my $user = $self->user_manager->get_user($username); | ||
| 335 | my $encrypted = $self->user_manager->encrypt($password); | ||
| 336 | |||
| 337 | unless ( ($user->{password} eq $password) or ($user->{password} eq $encrypted) ) | ||
| 338 | { | ||
| 339 | $self->miril->push_warning( | ||
| 340 | message => 'Wrong existing password!', | ||
| 341 | errorvar => '', | ||
| 342 | ); | ||
| 343 | return $self->redirect("?action=account") | ||
| 344 | } | ||
| 345 | |||
| 346 | $user->{name} = $name; | ||
| 347 | if ( $new_password and ($new_password eq $retype_password) ) { | ||
| 348 | $user->{password} = $self->user_manager->encrypt($new_password); | ||
| 349 | } | ||
| 350 | $self->user_manager->set_user($user); | ||
| 351 | return $self->redirect("?"); | ||
| 352 | |||
| 353 | |||
| 354 | } else { | ||
| 355 | |||
| 356 | my $username = $self->authen->username; | ||
| 357 | my $user = $self->user_manager->get_user($username); | ||
| 358 | |||
| 359 | my $tmpl = $self->view->load('account'); | ||
| 360 | $tmpl->param('user', $user); | ||
| 361 | return $tmpl->output; | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | sub files_list { | ||
| 366 | my $self = shift; | ||
| 367 | |||
| 368 | my $cfg = $self->miril->cfg; | ||
| 369 | |||
| 370 | my $files_path = $cfg->files_path; | ||
| 371 | my $files_http_dir = $cfg->files_http_dir; | ||
| 372 | my @files; | ||
| 373 | |||
| 374 | try { | ||
| 375 | opendir(my $dir, $files_path); | ||
| 376 | @files = grep { -f catfile($files_path, $_) } readdir($dir); | ||
| 377 | closedir $dir; | ||
| 378 | } catch { | ||
| 379 | Miril::Exception->throw( | ||
| 380 | errorvar => $_, | ||
| 381 | message => 'Could not read files directory', | ||
| 382 | ); | ||
| 383 | }; | ||
| 384 | |||
| 385 | my @current_files = $self->_paginate(@files); | ||
| 386 | |||
| 387 | my @files_with_data = gather | ||
| 388 | { | ||
| 389 | for my $file (@current_files) | ||
| 390 | { | ||
| 391 | my $filepath = catfile($files_path, $file); | ||
| 392 | my @modified = localtime( time() - ( (-M $filepath) * 60 * 60 * 24 ) ); | ||
| 393 | |||
| 394 | take { | ||
| 395 | name => $file, | ||
| 396 | href => $files_http_dir . $file, | ||
| 397 | size => format_bytes( -s $filepath ), | ||
| 398 | modified => strftime( "%d/%m/%Y %H:%M", @modified), | ||
| 399 | }; | ||
| 400 | } | ||
| 401 | }; | ||
| 402 | |||
| 403 | my $tmpl = $self->view->load('files'); | ||
| 404 | $tmpl->param('files', \@files_with_data); | ||
| 405 | return $tmpl->output; | ||
| 406 | } | ||
| 407 | |||
| 408 | sub files_upload { | ||
| 409 | my $self = shift; | ||
| 410 | my $q = $self->query; | ||
| 411 | my $cfg = $self->miril->cfg; | ||
| 412 | |||
| 413 | if ( $q->param('file') or $q->upload('file') ) { | ||
| 414 | |||
| 415 | my @filenames = $q->param('file'); | ||
| 416 | my @fhs = $q->upload('file'); | ||
| 417 | |||
| 418 | for ( my $i = 0; $i < @fhs; $i++) { | ||
| 419 | |||
| 420 | my $filename = $filenames[$i]; | ||
| 421 | my $fh = $fhs[$i]; | ||
| 422 | |||
| 423 | if ($filename and $fh) { | ||
| 424 | my $new_filename = catfile($cfg->files_path, $filename); | ||
| 425 | try { | ||
| 426 | my $new_fh = IO::File->new($new_filename, "w"); | ||
| 427 | copy($fh, $new_fh); | ||
| 428 | $new_fh->close; | ||
| 429 | } catch { | ||
| 430 | Miril::Exception->throw( | ||
| 431 | errorvar => $_, | ||
| 432 | message => 'Could not upload file', | ||
| 433 | ); | ||
| 434 | } | ||
| 435 | } | ||
| 436 | } | ||
| 437 | |||
| 438 | return $self->redirect("?action=files"); | ||
| 439 | |||
| 440 | } else { | ||
| 441 | my $tmpl = $self->view->load('upload'); | ||
| 442 | return $tmpl->output; | ||
| 443 | } | ||
| 444 | } | ||
| 445 | |||
| 446 | sub files_delete { | ||
| 447 | my $self = shift; | ||
| 448 | my $cfg = $self->miril->cfg; | ||
| 449 | my $q = $self->query; | ||
| 450 | |||
| 451 | my @filenames = $q->param('file'); | ||
| 452 | |||
| 453 | try { | ||
| 454 | for (@filenames) { | ||
| 455 | try { | ||
| 456 | unlink( catfile($cfg->files_path, $_) ) | ||
| 457 | } catch { | ||
| 458 | Miril::Exception->throw( | ||
| 459 | errorvar => $_, | ||
| 460 | message => 'Could not delete file', | ||
| 461 | ); | ||
| 462 | }; | ||
| 463 | } | ||
| 464 | }; | ||
| 465 | |||
| 466 | return $self->redirect("?action=files"); | ||
| 467 | } | ||
| 468 | |||
| 469 | sub posts_publish { | ||
| 470 | my $self = shift; | ||
| 471 | |||
| 472 | my $cfg = $self->miril->cfg; | ||
| 473 | |||
| 474 | my $do = $self->query->param("do"); | ||
| 475 | my $rebuild = $self->query->param("rebuild"); | ||
| 476 | |||
| 477 | if ($do) { | ||
| 478 | Miril::Publisher->publish($self->miril, $rebuild); | ||
| 479 | return $self->redirect("?action=list"); | ||
| 480 | } else { | ||
| 481 | my $tmpl = $self->view->load('publish'); | ||
| 482 | return $tmpl->output; | ||
| 483 | } | ||
| 484 | } | ||
| 485 | |||
| 486 | ### PRIVATE METHODS ### | ||
| 487 | |||
| 488 | # form generation utilities: this stuff is ugly, should be replaced with HTML::FillInForm::Lite | ||
| 489 | |||
| 490 | sub _prepare_authors { | ||
| 491 | my ($self, $selected) = @_; | ||
| 492 | my $cfg = $self->miril->cfg; | ||
| 493 | my @authors; | ||
| 494 | if ($selected) { | ||
| 495 | @authors = map {{ name => $_, id => $_ , selected => $_ eq $selected }} $cfg->authors->list; | ||
| 496 | } else { | ||
| 497 | @authors = map {{ name => $_, id => $_ }} $cfg->authors->list; | ||
| 498 | } | ||
| 499 | return \@authors; | ||
| 500 | } | ||
| 501 | |||
| 502 | sub _prepare_statuses { | ||
| 503 | my ($self, $selected) = @_; | ||
| 504 | my $cfg = $self->miril->cfg; | ||
| 505 | my @statuses; | ||
| 506 | if ($selected) | ||
| 507 | { | ||
| 508 | @statuses = map {{ name => $_, id => $_, selected => $_ eq $selected }} $cfg->statuses->list; | ||
| 509 | } | ||
| 510 | else | ||
| 511 | { | ||
| 512 | @statuses = map {{ name => $_, id => $_ }} $cfg->statuses->list; | ||
| 513 | } | ||
| 514 | return \@statuses; | ||
| 515 | } | ||
| 516 | |||
| 517 | sub _prepare_topics { | ||
| 518 | my ($self, %selected) = @_; | ||
| 519 | my $cfg = $self->miril->cfg; | ||
| 520 | if (%selected) | ||
| 521 | { | ||
| 522 | my @topics = map {{ name => $_->name, id => $_->id, selected => $selected{$_->id} }} $cfg->topics->list; | ||
| 523 | return \@topics; | ||
| 524 | } | ||
| 525 | else | ||
| 526 | { | ||
| 527 | my @topics = map {{ name => $_->name, id => $_->id, }} $cfg->topics->list; | ||
| 528 | return \@topics; | ||
| 529 | } | ||
| 530 | } | ||
| 531 | |||
| 532 | sub _prepare_types { | ||
| 533 | my ($self, $selected) = @_; | ||
| 534 | my $cfg = $self->miril->cfg; | ||
| 535 | my @types; | ||
| 536 | if ($selected) | ||
| 537 | { | ||
| 538 | @types = map {{ name => $_->name, id => $_->id, selected => $_->id eq $selected }} $cfg->types->list; | ||
| 539 | } | ||
| 540 | else | ||
| 541 | { | ||
| 542 | @types = map {{ name => $_->name, id => $_->id }} $cfg->types->list; | ||
| 543 | } | ||
| 544 | return \@types; | ||
| 545 | } | ||
| 546 | |||
| 547 | # pagination: needs to be abstracted so that different UI's (e.g. Mojo) could use it | ||
| 548 | |||
| 549 | sub _paginate { | ||
| 550 | my $self = shift; | ||
| 551 | my @posts = @_; | ||
| 552 | |||
| 553 | my $cfg = $self->miril->cfg; | ||
| 554 | |||
| 555 | return unless @posts; | ||
| 556 | |||
| 557 | if (@posts > $cfg->posts_per_page) { | ||
| 558 | |||
| 559 | my $page = Data::Page->new; | ||
| 560 | $page->total_entries(scalar @posts); | ||
| 561 | $page->entries_per_page($cfg->posts_per_page); | ||
| 562 | $page->current_page($self->query->param('page_no') ? $self->query->param('page_no') : 1); | ||
| 563 | |||
| 564 | my $pager; | ||
| 565 | |||
| 566 | if ($page->current_page > 1) { | ||
| 567 | $pager->{first} = $self->_generate_paged_url($page->first_page); | ||
| 568 | $pager->{previous} = $self->_generate_paged_url($page->previous_page); | ||
| 569 | } | ||
| 570 | |||
| 571 | if ($page->current_page < $page->last_page) { | ||
| 572 | $pager->{'last'} = $self->_generate_paged_url($page->last_page); | ||
| 573 | $pager->{'next'} = $self->_generate_paged_url($page->next_page); | ||
| 574 | } | ||
| 575 | |||
| 576 | $self->view->{pager} = $pager; | ||
| 577 | return $page->splice(\@posts); | ||
| 578 | |||
| 579 | } else { | ||
| 580 | return @posts; | ||
| 581 | } | ||
| 582 | } | ||
| 583 | |||
| 584 | sub _generate_paged_url { | ||
| 585 | my $self = shift; | ||
| 586 | my $page_no = shift; | ||
| 587 | |||
| 588 | my $q = $self->query; | ||
| 589 | |||
| 590 | my $paged_url = '?action=' . $q->param('action'); | ||
| 591 | |||
| 592 | if ( | ||
| 593 | $q->param('title') or | ||
| 594 | $q->param('author') or | ||
| 595 | $q->param('type') or | ||
| 596 | $q->param('status') or | ||
| 597 | $q->param('topic') | ||
| 598 | ) { | ||
| 599 | $paged_url .= '&title=' . $q->param('title') | ||
| 600 | . '&author=' . $q->param('author') | ||
| 601 | . '&type=' . $q->param('type') | ||
| 602 | . '&status=' . $q->param('status') | ||
| 603 | . '&topic=' . $q->param('topic'); | ||
| 604 | } | ||
| 605 | |||
| 606 | $paged_url .= "&page_no=$page_no"; | ||
| 607 | |||
| 608 | return $paged_url; | ||
| 609 | } | ||
| 610 | |||
| 611 | 1; |