X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FPaginated.pm;h=02e772814039283c71ba54003253d3eb7051ae38;hb=fb03d191f53516cbf1022e755665556e7f1acb82;hp=a5a07d0679a1d5cff2fc07f0fd187265305c484d;hpb=1a254be1009a000fbe872878088da9c8b69208f8;p=kivitendo-erp.git diff --git a/SL/DB/Helper/Paginated.pm b/SL/DB/Helper/Paginated.pm index a5a07d067..02e772814 100644 --- a/SL/DB/Helper/Paginated.pm +++ b/SL/DB/Helper/Paginated.pm @@ -4,23 +4,25 @@ use strict; require Exporter; our @ISA = qw(Exporter); -our @EXPORT = qw(paginate); +our @EXPORT = qw(paginate disable_paginating); + +use List::MoreUtils qw(any); sub paginate { - my ($self, %params) = @_; - my $page = $params{page} || 1; - my %args = %{ $params{args} || {} }; + my ($self, %params) = @_; + my $page = $params{page} || 1; + my %args = %{ $params{args} || {} }; - my $ret = { }; + my $ret = { }; - $ret->{per_page} = per_page($self, %params); - $ret->{max} = ceil($self->get_all_count(%args), $ret->{per_page}) || 1; - $ret->{cur} = $page < 1 ? 1 - : $page > $ret->{max} ? $ret->{max} - : $page; - $ret->{common} = make_common_pages($ret->{cur}, $ret->{max}); + $ret->{per_page} = per_page($self, %params); + $ret->{max} = ceil($self->get_all_count(%args), $ret->{per_page}) || 1; + $ret->{page} = $page < 1 ? 1 + : $page > $ret->{max} ? $ret->{max} + : $page; + $ret->{common} = make_common_pages($ret->{page}, $ret->{max}); - $params{args}{page} = $ret->{cur}; + $params{args}{page} = $ret->{page}; $params{args}{per_page} = $ret->{per_page}; delete $params{args}{limit}; delete $params{args}{offset}; @@ -47,12 +49,28 @@ sub make_common_pages { my ($cur, $max) = @_; return [ map { - active => $_ != $cur, - page => $_, + active => $_ != $cur, + page => $_, + visible => calc_visibility($cur, $max, $_), }, 1 .. $max ]; } +sub calc_visibility { + my ($cur, $max, $this) = @_; + any { $_ } abs($cur - $this) < 5, + $this <= 3, + $this == $max, + any { abs ($cur - $this) == $_ } 10, 50, 100, 500, 1000, 5000; +} + +sub disable_paginating { + my ($self, %params) = @_; + + delete $params{args}{page}; + delete $params{args}{per_page}; +} + 1; __END__ @@ -96,7 +114,7 @@ In the template: Paginate will prepare information to be used for paginating, change the given args to use them, and return a data structure containing information for later -display. +display. See L for information how the return is formatted. C needs to contain a reference to a hash, which will be used as an argument for C. After C the keys C and C @@ -111,6 +129,52 @@ Manager will be used. =back +=head1 STRUCTURE OF PAGES + +The returned hashref will have the following structure: + + { per_page => 20, # how many entries per page + max => 5, # number of the last page + page => 2, # number of the current page + common => [ # an array of hashes for each page + ..., + { active => 1, # set if this is the active page + page => 2, # the string to display for this page + visible => 1, # should this be displayed in the paginating controls + }, + ... + ] + } + +You may assume that C is sanitized to be within 1..C. + +The common list is kept arbitrary by design, so that the algorithm to display +the paginating controls can be changed by solely changing the +C algorithm. If you need different glyphs for the pages or +different boundaries, translate the C entry for the page. + +The initial algorithm will show the following pages: + +=over 4 + +=item * + +1, 2, 3 + +=item * + +Last page + +=item * + +Current page +/- 5 pages + +=item * + +Current page +/- 10, 50, 100, 500, 1000, 5000 + +=back + =head1 TEMPLATE HELPERS =over 4