|
1 |
package SL::DB::Helper::Paginated;
|
|
2 |
|
|
3 |
use strict;
|
|
4 |
|
|
5 |
require Exporter;
|
|
6 |
our @ISA = qw(Exporter);
|
|
7 |
our @EXPORT = qw(paginate);
|
|
8 |
|
|
9 |
sub paginate {
|
|
10 |
my ($self, %params) = @_;
|
|
11 |
my $page = $params{page} || 1;
|
|
12 |
my %args = %{ $params{args} || {} };
|
|
13 |
|
|
14 |
my $ret = { };
|
|
15 |
|
|
16 |
$ret->{per_page} = per_page($self, %params);
|
|
17 |
$ret->{max} = ceil($self->get_all_count(%args), $ret->{per_page}) || 1;
|
|
18 |
$ret->{cur} = $page < 1 ? 1
|
|
19 |
: $page > $ret->{max} ? $ret->{max}
|
|
20 |
: $page;
|
|
21 |
$ret->{common} = make_common_pages($ret->{cur}, $ret->{max});
|
|
22 |
|
|
23 |
$params{args}{page} = $ret->{cur};
|
|
24 |
$params{args}{per_page} = $ret->{per_page};
|
|
25 |
delete $params{args}{limit};
|
|
26 |
delete $params{args}{offset};
|
|
27 |
|
|
28 |
return $ret;
|
|
29 |
}
|
|
30 |
|
|
31 |
sub per_page {
|
|
32 |
my ($self, %params) = @_;
|
|
33 |
|
|
34 |
return $params{per_page} if exists $params{per_page};
|
|
35 |
return $self->default_objects_per_page;
|
|
36 |
}
|
|
37 |
|
|
38 |
sub ceil {
|
|
39 |
my ($a, $b) = @_;
|
|
40 |
use integer;
|
|
41 |
|
|
42 |
return 1 unless $b;
|
|
43 |
return $a / $b + ($a % $b ? 1 : 0);
|
|
44 |
}
|
|
45 |
|
|
46 |
sub make_common_pages {
|
|
47 |
my ($cur, $max) = @_;
|
|
48 |
return [
|
|
49 |
map {
|
|
50 |
active => $_ != $cur,
|
|
51 |
page => $_,
|
|
52 |
}, 1 .. $max
|
|
53 |
];
|
|
54 |
}
|
|
55 |
|
|
56 |
1;
|
|
57 |
|
|
58 |
__END__
|
|
59 |
|
|
60 |
=encoding utf-8
|
|
61 |
|
|
62 |
=head1 NAME
|
|
63 |
|
|
64 |
SL::Helper::Paginated - Manager mixin for paginating results.
|
|
65 |
|
|
66 |
=head1 SYNOPSIS
|
|
67 |
|
|
68 |
In the manager:
|
|
69 |
|
|
70 |
use SL::Helper::Paginated;
|
|
71 |
|
|
72 |
__PACKAGE__->default_objects_per_page(10); # optional, defaults to 20
|
|
73 |
|
|
74 |
In the controller:
|
|
75 |
|
|
76 |
my %args = (
|
|
77 |
query => [ id => $params{list_of_selected_ids},
|
|
78 |
other_attr => $::form->{other_attr}, ],
|
|
79 |
);
|
|
80 |
|
|
81 |
$self->{pages} = SL::DB::Manager::MyObject->paginate(args => \%args, page => $::form->{page});
|
|
82 |
$self->{objects} = SL::DB::Manager::MyObject->get_all(%args);
|
|
83 |
|
|
84 |
In the template:
|
|
85 |
|
|
86 |
[% PROCESS 'common/paginate.html'
|
|
87 |
pages=SELF.pages
|
|
88 |
base_url=L.url_for(action='list', ...)
|
|
89 |
%]
|
|
90 |
|
|
91 |
=head1 FUNCTIONS
|
|
92 |
|
|
93 |
=over 4
|
|
94 |
|
|
95 |
=item C<paginate> args => HREF, page => $page, [ per_page => $per_page ]
|
|
96 |
|
|
97 |
Paginate will prepare information to be used for paginating, change the given
|
|
98 |
args to use them, and return a data structure containing information for later
|
|
99 |
display.
|
|
100 |
|
|
101 |
C<args> needs to contain a reference to a hash, which will be used as an
|
|
102 |
argument for C<get_all>. After C<paginate> the keys C<page> and C<per_page>
|
|
103 |
will be set. The keys C<limit> and C<offset> will be unset, should they exist,
|
|
104 |
since they don't make sense with paginating.
|
|
105 |
|
|
106 |
C<page> should contain a value between 1 and the maximum pages. Will be
|
|
107 |
sanitized.
|
|
108 |
|
|
109 |
The parameter C<per_page> is optional. If not given the default value of the
|
|
110 |
Manager will be used.
|
|
111 |
|
|
112 |
=back
|
|
113 |
|
|
114 |
=head1 TEMPLATE HELPERS
|
|
115 |
|
|
116 |
=over 4
|
|
117 |
|
|
118 |
=item C<common/paginate.html> pages=SELF.pages, base_url=URL
|
|
119 |
|
|
120 |
The template will render a simple list of links to the
|
|
121 |
various other pages. A C<base_url> must be given for the links to work.
|
|
122 |
|
|
123 |
=back
|
|
124 |
|
|
125 |
=head1 BUGS
|
|
126 |
|
|
127 |
None yet.
|
|
128 |
|
|
129 |
=head1 AUTHOR
|
|
130 |
|
|
131 |
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
|
|
132 |
|
|
133 |
=cut
|
Simples Pagination System als Manager Mixin.