1 package SL::DB::Helper::Paginated;
 
   6 our @ISA    = qw(Exporter);
 
   7 our @EXPORT = qw(paginate disable_paginating);
 
   9 use List::MoreUtils qw(any);
 
  12   my ($self, %params) = @_;
 
  13   my $page = $params{page} || 1;
 
  14   my %args = %{ $params{args} || {} };
 
  18   $ret->{per_page} = per_page($self, %params);
 
  19   $ret->{max}    = ceil($self->get_all_count(%args), $ret->{per_page}) || 1;
 
  20   $ret->{cur}    = $page < 1 ? 1
 
  21                  : $page > $ret->{max} ? $ret->{max}
 
  23   $ret->{common} = make_common_pages($ret->{cur}, $ret->{max});
 
  25   $params{args}{page}     = $ret->{cur};
 
  26   $params{args}{per_page} = $ret->{per_page};
 
  27   delete $params{args}{limit};
 
  28   delete $params{args}{offset};
 
  34   my ($self, %params) = @_;
 
  36   return $params{per_page} if exists $params{per_page};
 
  37   return $self->default_objects_per_page;
 
  45   return $a / $b + ($a % $b ? 1 : 0);
 
  48 sub make_common_pages {
 
  54       visible => calc_visibility($cur, $max, $_),
 
  60   my ($cur, $max, $this) = @_;
 
  61   any { $_ } abs($cur - $this) < 5,
 
  64              any { abs ($cur - $this) == $_ } 10, 50, 100, 500, 1000, 5000;
 
  67 sub disable_paginating {
 
  68   my ($self, %params) = @_;
 
  70   delete $params{args}{page};
 
  71   delete $params{args}{per_page};
 
  82 SL::Helper::Paginated - Manager mixin for paginating results.
 
  88   use SL::Helper::Paginated;
 
  90   __PACKAGE__->default_objects_per_page(10); # optional, defaults to 20
 
  95     query => [ id         => $params{list_of_selected_ids},
 
  96                other_attr => $::form->{other_attr}, ],
 
  99   $self->{pages}   = SL::DB::Manager::MyObject->paginate(args => \%args, page => $::form->{page});
 
 100   $self->{objects} = SL::DB::Manager::MyObject->get_all(%args);
 
 104   [% PROCESS 'common/paginate.html'
 
 106     base_url=L.url_for(action='list', ...)
 
 113 =item C<paginate> args => HREF, page => $page, [ per_page => $per_page ]
 
 115 Paginate will prepare information to be used for paginating, change the given
 
 116 args to use them, and return a data structure containing information for later
 
 119 C<args> needs to contain a reference to a hash, which will be used as an
 
 120 argument for C<get_all>. After C<paginate> the keys C<page> and C<per_page>
 
 121 will be set. The keys C<limit> and C<offset> will be unset, should they exist,
 
 122 since they don't make sense with paginating.
 
 124 C<page> should contain a value between 1 and the maximum pages. Will be
 
 127 The parameter C<per_page> is optional. If not given the default value of the
 
 128 Manager will be used.
 
 132 =head1 TEMPLATE HELPERS
 
 136 =item C<common/paginate.html> pages=SELF.pages, base_url=URL
 
 138 The template will render a simple list of links to the
 
 139 various other pages. A C<base_url> must be given for the links to work.
 
 149 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>