line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Magic::Pager ; |
2
|
|
|
|
|
|
|
$VERSION = 1.15 ; |
3
|
2
|
|
|
2
|
|
126407
|
use 5.006_001 ; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
109
|
|
4
|
2
|
|
|
2
|
|
10
|
use strict ; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
70
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# This file uses the "Perlish" coding style |
7
|
|
|
|
|
|
|
# please read http://perl.4pro.net/perlish_coding_style.html |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
; use Carp |
10
|
2
|
|
|
2
|
|
12
|
; $Carp::Internal{+__PACKAGE__}++ |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
3124
|
|
11
|
|
|
|
|
|
|
; our $no_template_magic_zone = 1 # prevents passing the zone object to properties |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
; sub new |
14
|
9
|
|
|
9
|
1
|
524321
|
{ my $c = shift |
15
|
9
|
50
|
|
|
|
41
|
; ref($c) && croak qq(Can't call method "new" on a reference) |
16
|
9
|
50
|
|
|
|
40
|
; (@_ % 2) && croak qq(Odd number of arguments for $c->new) |
17
|
9
|
|
|
|
|
36
|
; my $s = bless {}, $c |
18
|
9
|
|
|
|
|
54
|
; while ( my ($p, $v) = splice @_, 0, 2 ) |
19
|
27
|
50
|
|
|
|
313
|
{ if ($s->can($p)) # if method |
20
|
27
|
|
|
|
|
99
|
{ $s->$p( $v ) |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
else |
23
|
0
|
|
|
|
|
0
|
{ croak qq(No such property "$p") |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
9
|
100
|
|
|
|
498
|
; if ( ref $$s{total_results} eq 'ARRAY' ) |
27
|
7
|
|
|
|
|
17
|
{ my $r = $$s{total_results} |
28
|
7
|
|
|
|
|
16
|
; $$s{total_results} = @$r |
29
|
7
|
|
|
|
|
26
|
; $s->page_rows( [ @$r[ $s->_start_offset .. $s->_end_offset ] ] ) |
30
|
|
|
|
|
|
|
} |
31
|
9
|
100
|
|
|
|
348
|
; $$s{total_results} ? $s : undef |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
; use Object::props |
35
|
|
|
|
|
|
|
( { name => 'total_results' |
36
|
|
|
|
|
|
|
, validation => sub |
37
|
9
|
100
|
|
|
|
524
|
{ /^[\d]+$/ |
38
|
|
|
|
|
|
|
|| ref eq 'ARRAY' |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
, { name => 'page_rows' |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
, { name => 'page_number' |
44
|
|
|
|
|
|
|
, default => 1 |
45
|
|
|
|
|
|
|
, validation => sub |
46
|
9
|
50
|
|
|
|
341
|
{ /^[\d]+$/ |
47
|
|
|
|
|
|
|
&& $_ > 0 |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
, { name => [ qw | rows_per_page |
51
|
|
|
|
|
|
|
pages_per_index |
52
|
|
|
|
|
|
|
| |
53
|
|
|
|
|
|
|
] |
54
|
|
|
|
|
|
|
, default => 10 |
55
|
14
|
|
|
|
|
1329
|
, validation => sub{ /^[\d]+$/ } |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
) |
58
|
|
|
|
|
|
|
|
59
|
2
|
|
|
2
|
|
2067
|
; sub total_pages |
|
2
|
|
|
|
|
6046
|
|
|
2
|
|
|
|
|
50
|
|
60
|
28
|
|
|
28
|
1
|
13538
|
{ my $s = shift |
61
|
28
|
100
|
|
|
|
127
|
; int ($s->total_results / $s->rows_per_page) |
62
|
|
|
|
|
|
|
+ ($s->total_results % $s->rows_per_page ? 1 : 0) |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
; sub next_page |
66
|
10
|
|
|
10
|
1
|
5262
|
{ my $s = shift |
67
|
10
|
100
|
|
|
|
285
|
; $s->page_number + 1 if $s->page_number < $s->total_pages |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
; sub next |
71
|
5
|
100
|
|
5
|
1
|
123895
|
{ $_[0]->next_page && {} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
; sub previous_page |
75
|
10
|
|
|
10
|
1
|
4834
|
{ my $s = shift |
76
|
10
|
50
|
|
|
|
31
|
; $s->page_number - 1 if $s->page_number > 1 |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
; sub previous |
80
|
5
|
50
|
|
5
|
1
|
6209
|
{ $_[0]->previous_page && {} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
; sub _start_offset |
84
|
268
|
|
|
268
|
|
995
|
{ my ($s, $page_number) = @_ |
85
|
268
|
|
66
|
|
|
563
|
; $page_number ||= $s->page_number |
86
|
268
|
|
|
|
|
2359
|
; $s->rows_per_page * ($page_number - 1) |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
; sub _end_offset |
90
|
134
|
|
|
134
|
|
2547
|
{ my ($s, $page_number) = @_ |
91
|
134
|
|
|
|
|
239
|
; my $end = $s->_start_offset($page_number) + $s->rows_per_page - 1 |
92
|
134
|
100
|
|
|
|
5592
|
; $end > ($s->total_results - 1) |
93
|
|
|
|
|
|
|
? $s->total_results - 1 |
94
|
|
|
|
|
|
|
: $end |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
; sub start_result |
98
|
124
|
|
|
124
|
1
|
48276
|
{ my ($s, $page_number) = @_ |
99
|
124
|
|
|
|
|
222
|
; $s->_start_offset($page_number) + 1 |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
; sub end_result |
103
|
124
|
|
|
124
|
1
|
12492
|
{ my ($s, $page_number) = @_ |
104
|
124
|
|
|
|
|
220
|
; $s->_end_offset($page_number) + 1 |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
; sub index |
108
|
13
|
|
|
13
|
1
|
16302
|
{ my $s = shift |
109
|
13
|
|
|
|
|
19
|
; my ( $half, $start, $end ) |
110
|
13
|
|
|
|
|
39
|
; $half = int ($s->pages_per_index / 2) |
111
|
13
|
|
|
|
|
293
|
; my $page_number = $s->page_number |
112
|
13
|
|
|
|
|
438
|
; my $total_pages = $s->total_pages |
113
|
13
|
100
|
|
|
|
1142
|
; if ( $total_pages / 2 > $page_number) # if first half |
114
|
9
|
|
|
|
|
14
|
{ $start = $page_number - $half |
115
|
9
|
100
|
|
|
|
27
|
; $start = 1 if $start < 1 |
116
|
9
|
|
|
|
|
23
|
; $end = $start + $s->pages_per_index - 1 |
117
|
9
|
50
|
|
|
|
203
|
; $end = $total_pages if $end > $total_pages |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
else |
120
|
4
|
|
|
|
|
6
|
{ $end = $page_number + $half |
121
|
4
|
50
|
|
|
|
13
|
; $end = $total_pages if $end > $total_pages |
122
|
4
|
|
|
|
|
10
|
; $start = $end - $s->pages_per_index + 1 |
123
|
4
|
50
|
|
|
|
86
|
; $start = 1 if $start < 1 |
124
|
|
|
|
|
|
|
} |
125
|
130
|
100
|
|
|
|
2815
|
; my @i = map |
126
|
13
|
|
|
|
|
37
|
{ $_ != $page_number |
127
|
|
|
|
|
|
|
? { linked_page => {} |
128
|
|
|
|
|
|
|
, page_number => $_ |
129
|
|
|
|
|
|
|
, start_result => $s->start_result($_) |
130
|
|
|
|
|
|
|
, end_result => $s->end_result($_) |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
: { current_page => {} |
133
|
|
|
|
|
|
|
, page_number => $_ |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} $start .. $end |
136
|
|
|
|
|
|
|
; \@i |
137
|
13
|
|
|
|
|
401
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
; 1 |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
__END__ |