blib/lib/Data/SpreadPagination.pm

filter: Statementstmt 100% 85 / 85 Branchbran 100% 56 / 56 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 100% 183 / 183 CC 35 SCAR 21.2
CC
35
Cov
100%
pages_in_spread
12.0
_do_pagination
11.0
new
9.0
CRAP 8.3
↑ Summary
1 package Data::SpreadPagination;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Data::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Math::Round qw(round);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 @ISA = qw(Data::Page);
12 1 $VERSION = '0.1.2';
13  
14 =head1 NAME
15  
16 Data::SpreadPagination - Page numbering and spread pagination
17  
18 =head1 SYNOPSIS
19  
20 use Data::SpreadPagination;
21 my $pageInfo = Data::SpreadPagination->new({
22 totalEntries => $totalEntries,
23 entriesPerPage => $entriesPerPage,
24 # Optional, will use defaults otherwise.
25 # only 1 of currentPage / startEntry can be provided.
26 currentPage => $currentPage,
27 startEntry => $startEntry,
28 maxPages => $maxPages,
29 });
30  
31 # General page information
32 print " First page: ", $pageInfo->first_page, "\n";
33 print " Last page: ", $pageInfo->last_page, "\n";
34 print " Next page: ", $pageInfo->next_page, "\n";
35 print " Previous page: ", $pageInfo->previous_page, "\n";
36  
37 # Results on current page
38 print "First entry on page: ", $pageInfo->first, "\n";
39 print " Last entry on page: ", $pageInfo->last, "\n";
40  
41 # Page range information
42 my $pageRanges = $pageInfo->page_ranges;
43  
44 # Print out the page spread
45 foreach my $page ($pageInfo->pages_in_spread()) {
46 if (!defined $page) {
47 print "... ";
48 } elsif ($page == $pageInfo->current_page) {
49 print "<b>$page</b> ";
50 } else {
51 print "$page ";
52 }
53 }
54  
55 =head1 DESCRIPTION
56  
57 The object produced by Data::SpreadPagination can be used to create
58 a spread pagination navigator. It inherits from Data::Page, and has
59 access to all of the methods from this object.
60  
61 In addition, it also provides methods for creating a pagination spread,
62 to allow for keeping the number of pagenumbers displayed within a sensible
63 limit, but at the same time allowing easy navigation.
64  
65 The object can easily be passed to a templating system
66 such as Template Toolkit or be used within a script.
67  
68 =head1 METHODS
69  
70 =head2 new()
71  
72 my $pageInfo = Data::SpreadPagination->new({
73 totalEntries => $totalEntries,
74 entriesPerPage => $entriesPerPage,
75 # Optional, will use defaults otherwise.
76 # only 1 of currentPage / startEntry can be provided.
77 currentPage => $currentPage,
78 startEntry => $startEntry,
79 maxPages => $maxPages,
80 });
81  
82 This is the constructor of the object. It requires an anonymous
83 hash containing the 'totalEntries', how many data units you have,
84 and the number of 'entriesPerPage' to display. Optionally the
85 'currentPage' / 'startEntry' (defaults to page/entry 1) and
86 'maxPages' (how many pages to display in addition to the current
87 page) can be added.
88  
89 =cut
90  
91 59 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
92 59 my $class = shift;
93 59 my %params = %{shift()};
94  
95 croak "totalEntries and entriesPerPage must be supplied"
96 59 ▶ unless defined $params{totalEntries} and defined $params{entriesPerPage};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"totalEntries"} and defined $params{"entriesPerPage"} 3 56 59
Condition100%3/3
defined $params{"totalEntries"} and defined $params{"entriesPerPage"}
!ll&&!rl&&r
2156
MC/DC100%2/2
defined $params{"totalEntries"} and defined $params{"entriesPerPage"}
defined $params{"totalEntries"} defined $params{"entriesPerPage"}
Truth table100%3/3
A and B
A= defined $params{"totalEntries"}B= defined $params{"entriesPerPage"}
ABresult
0X0
100
111
97  
98 croak "currentPage and startEntry can not both be supplied"
99 56 ▶ if defined $params{currentPage} and defined $params{startEntry};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"currentPage"} and defined $params{"startEntry"} 1 55 56
Condition100%3/3
defined $params{"currentPage"} and defined $params{"startEntry"}
!ll&&!rl&&r
5501
MC/DC100%2/2
defined $params{"currentPage"} and defined $params{"startEntry"}
defined $params{"currentPage"} defined $params{"startEntry"}
Truth table100%3/3
A and B
A= defined $params{"currentPage"}B= defined $params{"startEntry"}
ABresult
0X0
100
111
100  
101 $params{currentPage} = 1
102 55 ▶ unless defined $params{currentPage} or defined $params{startEntry};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"currentPage"} or defined $params{"startEntry"} 1 54 55
Condition100%3/3
defined $params{"currentPage"} or defined $params{"startEntry"}
l!l&&r!l&&!r
5041
MC/DC100%2/2
defined $params{"currentPage"} or defined $params{"startEntry"}
defined $params{"currentPage"} defined $params{"startEntry"}
Truth table100%3/3
A or B
A= defined $params{"currentPage"}B= defined $params{"startEntry"}
ABresult
1X1
011
000
103  
104 $params{currentPage} = int( ($params{startEntry} - 1) / $params{entriesPerPage} ) + 1
105 55 ▶ if defined $params{startEntry};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"startEntry"} 4 51 55
106  
107 55 my $self = $class->SUPER::new($params{totalEntries}, $params{entriesPerPage}, $params{currentPage});
108
109 $params{maxPages} = ceil( $params{totalEntries} / $params{entriesPerPage} ) - 1
110 55 ▶ unless defined $params{maxPages};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"maxPages"} 5 50 55
111  
112 55 $self->{MAX_PAGES} = $params{maxPages};
113 55 $self->_do_pagination( @params{qw(totalEntries entriesPerPage currentPage maxPages)} );
114  
115 55 return $self;
116 }
117  
118 =head2 max_pages()
119  
120 print "Maximum additional pages to display is ", $pageInfo->max_pages(), "\n";
121  
122 This method returns the maximum number of pages that are included in the
123 spread pagination in addition to the current page.
124  
125 =cut
126  
127 56 ▶sub max_pages {
Subroutine max_pages: called CC=1 SCAR=0.0
Pod documented
128 56 my $self = shift;
129  
130 56 return $self->{MAX_PAGES};
131 }
132  
133 =head2 page_ranges()
134  
135 $ranges = $pageInfo->page_ranges();
136 for my $qtr (1..4) {
137 my $range = $ranges->[$qtr-1];
138 if (defined $range) {
139 print "Qtr $qtr: no pages\n";
140 } else {
141 print "Qtr $qtr: pages " . $range->[0] . " to " . $range->[1] . "\n";
142 }
143 }
144  
145 This method returns either an array or an arrayref (based upon context)
146 of the page ranges for each of the four quarters in the spread. Each range
147 is either undef for an empty quarter, or an array of the lower and upper
148 pages in the range.
149  
150 =cut
151  
152 100 ▶sub page_ranges {
Subroutine page_ranges: called CC=2 SCAR=6.9
Pod documented
153 100 my $self = shift;
154  
155 100 ▶ return wantarray ? @{ $self->{PAGE_RANGES} } : $self->{PAGE_RANGES};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 50 50 100
156 }
157  
158 =head2 pages_in_spread_raw()
159  
160 # Print out the page spread
161 foreach my $page ($pageInfo->pages_in_spread_raw()) {
162 if ($page == $pageInfo->current_page) {
163 print "<b>$page</b> ";
164 } else {
165 print "$page ";
166 }
167 }
168  
169 This method returns either an array or an arrayref (based upon context)
170 of purely the page numbers within the spread.
171  
172 =cut
173  
174 100 ▶sub pages_in_spread_raw {
Subroutine pages_in_spread_raw: called CC=5 SCAR=16.1
Pod documented
175 100 my $self = shift;
176 100 my $pages = [];
177  
178 100 for (0..3) {
179 push @$pages, $self->{PAGE_RANGES}[$_][0] .. $self->{PAGE_RANGES}[$_][1]
180 400 ▶ if defined $self->{PAGE_RANGES}[$_];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"PAGE_RANGES"}[$_] 216 184 400
181  
182 400 ▶ push @$pages, $self->current_page()
Branch: 1 branch
BranchTrueFalseTotal
if $_ == 1 100 300 400
183 if $_ == 1;
184 }
185
186 100 ▶ return wantarray ? @{ $pages } : $pages;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 50 50 100
187 }
188  
189 =head2 pages_in_spread()
190  
191 # Print out the page spread
192 foreach my $page ($pageInfo->pages_in_spread()) {
193 if (!defined $page) {
194 print "... ";
195 } elsif ($page == $pageInfo->current_page) {
196 print "<b>$page</b> ";
197 } else {
198 print "$page ";
199 }
200 }
201  
202 This method returns either an array or an arrayref (based upon context)
203 of the page numbers within the spread. Breaks in the sequence are
204 indicated with undef's.
205  
206 =cut
207  
208 100 ▶sub pages_in_spread {
Subroutine pages_in_spread: called CC=12 SCAR=24.8
Pod documented
209 100 my $self = shift;
210 100 my $ranges = $self->{PAGE_RANGES};
211 100 my $pages = [];
212  
213 100 ▶ if (!defined $ranges->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ranges->[0]) { } 44 56 100
214 44 ▶ push @$pages, undef if $self->current_page > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->current_page > 1 36 8 44
215 } else {
216 56 push @$pages, $ranges->[0][0] .. $ranges->[0][1];
217 56 ▶ push @$pages, undef if defined $ranges->[1] and ($ranges->[1][0] - $ranges->[0][1]) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ranges->[1] and $ranges->[1][0] - $ranges->[0][1] > 1 28 28 56
Condition100%3/3
defined $ranges->[1] and $ranges->[1][0] - $ranges->[0][1] > 1
!ll&&!rl&&r
22628
MC/DC100%2/2
defined $ranges->[1] and $ranges->[1][0] - $ranges->[0][1] > 1
defined $ranges->[1] $ranges->[1][0] - $ranges->[0][1] > 1
Truth table100%3/3
A and B
A= defined $ranges->[1]B= $ranges->[1][0] - $ranges->[0][1] > 1
ABresult
0X0
100
111
218 }
219  
220 100 ▶ push @$pages, $ranges->[1][0] .. $ranges->[1][1] if defined $ranges->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $ranges->[1] 52 48 100
221 100 push @$pages, $self->current_page;
222 100 ▶ push @$pages, $ranges->[2][0] .. $ranges->[2][1] if defined $ranges->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $ranges->[2] 52 48 100
223  
224 100 ▶ if (!defined $ranges->[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ranges->[3]) { } 44 56 100
225 44 ▶ push @$pages, undef if $self->current_page < $self->last_page;
Branch: 1 branch
BranchTrueFalseTotal
if $self->current_page < $self->last_page 36 8 44
226 } else {
227 56 ▶ push @$pages, undef if defined $ranges->[2] and ($ranges->[3][0] - $ranges->[2][1]) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ranges->[2] and $ranges->[3][0] - $ranges->[2][1] > 1 28 28 56
Condition100%3/3
defined $ranges->[2] and $ranges->[3][0] - $ranges->[2][1] > 1
!ll&&!rl&&r
22628
MC/DC100%2/2
defined $ranges->[2] and $ranges->[3][0] - $ranges->[2][1] > 1
defined $ranges->[2] $ranges->[3][0] - $ranges->[2][1] > 1
Truth table100%3/3
A and B
A= defined $ranges->[2]B= $ranges->[3][0] - $ranges->[2][1] > 1
ABresult
0X0
100
111
228 56 push @$pages, $ranges->[3][0] .. $ranges->[3][1];
229 }
230  
231 100 ▶ return wantarray ? @{ $pages } : $pages;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 50 50 100
232 }
233  
234 # Carry out the pagination calculations
235 # Algorithm description reverse-engineered from Squirrelmail
236 # Reimplemented from description only by Alex Gough
237 55 ▶sub _do_pagination {
Subroutine _do_pagination: called CC=11 SCAR=24.0
238 55 my $self= shift;
239 55 my $total_entries = $self->total_entries;
240 55 my $entries_per_page = $self->entries_per_page;
241 55 my $current_page = $self->current_page;
242 55 my $max_pages = $self->max_pages;
243  
244 # qNsizes
245 55 my @q_size = ();
246 55 my ($add_pages, $adj);
247  
248 # step 2
249 55 my $total_pages = ceil($total_entries / $entries_per_page);
250 55 ▶ my $visible_pages = $max_pages < ($total_pages-1)
Branch: 1 branch
BranchTrueFalseTotal
$max_pages < $total_pages - 1 ? : 40 15 55
251 ? $max_pages
252 : $total_pages - 1;
253 55 ▶ if ($total_pages - 1 <= $max_pages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_pages - 1 <= $max_pages) { } 15 40 55
254 15 @q_size = ($current_page - 1, 0, 0, $total_pages - $current_page);
255 }
256 else {
257 40 @q_size = (floor($visible_pages / 4),
258 round($visible_pages / 4),
259 ceil($visible_pages / 4),
260 round( ($visible_pages - round($visible_pages/4) )/3) );
261 40 ▶ if ($current_page - $q_size[0] < 1) {
Branch: 4 branches
BranchTrueFalseTotal
if ($current_page - $q_size[0] < 1) { } 2 38 40
elsif ($current_page - $q_size[1] - ceil($q_size[1] / 3) <= $q_size[0]) { } 6 32 38
elsif ($current_page + $q_size[3] >= $total_pages) { } 5 27 32
elsif ($current_page + $q_size[2] >= $total_pages - $q_size[3]) { } 4 23 27
262 2 $add_pages = $q_size[0] + $q_size[1] - $current_page +1;
263 2 @q_size = ($current_page -1, 0, $q_size[2] + ceil($add_pages/2),
264 $q_size[3] + floor($add_pages /2));
265 }
266 elsif ($current_page - $q_size[1] - ceil($q_size[1] / 3)<=$q_size[0]) {
267 6 $adj = ceil((3*($current_page - $q_size[0] - 1))/4);
268 6 $add_pages = $q_size[1] - $adj;
269 6 @q_size = ($q_size[0], $adj, $q_size[2] + ceil($add_pages/2),
270 $q_size[3] + floor($add_pages/2));
271 }
272 elsif ($current_page + $q_size[3] >= $total_pages) {
273 5 $add_pages = $q_size[2] + $q_size[3] - $total_pages +$current_page;
274 5 @q_size = ($q_size[0] + floor($add_pages / 2),
275 $q_size[1] + ceil($add_pages / 2), 0,
276 $total_pages - $current_page);
277 }
278 elsif ($current_page + $q_size[2] >= $total_pages - $q_size[3]) {
279 4 $adj = ceil((3*($total_pages - $current_page - $q_size[3]))/4);
280 4 $add_pages = $q_size[2] - $adj;
281 4 @q_size = ($q_size[0] + floor($add_pages/2),
282 $q_size[1] + ceil($add_pages/2), $adj, $q_size[3]);
283 }
284 }
285 # step 3 (PROFIT)
286 55 ▶ $self->{PAGE_RANGES} = [ $q_size[0] == 0 ? undef
Branch: 4 branches
BranchTrueFalseTotal
$q_size[0] == 0 ? : 25 30 55
$q_size[1] == 0 ? : 29 26 55
$q_size[2] == 0 ? : 29 26 55
$q_size[3] == 0 ? : 22 33 55
287 : [1,$q_size[0]],
288 $q_size[1] == 0 ? undef
289 : [$current_page - $q_size[1], $current_page-1],
290 $q_size[2] == 0 ? undef
291 : [$current_page+1, $current_page+$q_size[2]],
292 $q_size[3] == 0 ? undef
293 : [$total_pages - $q_size[3] + 1, $total_pages],
294 ];
295  
296 }
297  
298 =head1 BUGS
299  
300 Hopefully there aren't any nasty bugs lurking in here anywhere.
301 However, if you do find one, please report it via RT.
302  
303 =head1 ALGORITHM
304  
305 The algorithm used to create the pagination spread was reverse-engineered
306 out of Squirrelmail by myself, and then reimplemented from description
307 only by Alex Gough.
308  
309 =head1 THANKS, MANY
310  
311 Alex Gough for implementing the central algorithm from my description.
312  
313 =head1 AUTHOR
314  
315 Jody Belka C<knew@cpan.org>
316  
317 =head1 SEE ALSO
318  
319 L<Data::Page>.
320  
321 =head1 COPYRIGHT AND LICENSE
322  
323 Copyright 2004 by Jody Belka
324  
325 This library is free software; you can redistribute it and/or modify
326 it under the same terms as Perl itself.
327  
328 =cut
329  
330 1 <<QUOTE;
331 Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about
332 being on some really good drugs -- you know, there is no spoon. - flyingmoose
333 QUOTE
↑ Summary