line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Pagination;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
13726
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
942
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.44';
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# {{{ new()
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# Constructor.
|
10
|
|
|
|
|
|
|
# Making all calculations and storing results at class properties.
|
11
|
|
|
|
|
|
|
# Param int $total_entries Total number of entries (>= 1)
|
12
|
|
|
|
|
|
|
# Param int $entries_per_page Number of entries per page (>= 1)
|
13
|
|
|
|
|
|
|
# Param int $pages_per_set Number of pages per set (>= 1)
|
14
|
|
|
|
|
|
|
# Param int $current_page Current number of page
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new {
|
17
|
0
|
|
|
0
|
1
|
|
my ($class, $total_entries, $entries_per_page, $pages_per_set, $current_page) = @_;
|
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
|
my $self = bless {} => $class;
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# total number of entries (>= 1) (copied from arguments)
|
22
|
0
|
|
|
|
|
|
$self->{total_entries} = $total_entries;
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# number of entries per page (>= 1) (copied from arguments)
|
25
|
0
|
|
|
|
|
|
$self->{entries_per_page} = $entries_per_page;
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# number of pages per set (>= 1) (copied from arguments)
|
28
|
0
|
|
|
|
|
|
$self->{pages_per_set} = $pages_per_set;
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# total number of pages (>= 1)
|
31
|
0
|
|
|
|
|
|
$self->{total_pages} = int(($self->{total_entries} - 1) / $self->{entries_per_page}) + 1;
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# current number of page (>= 1) (corrected)
|
34
|
0
|
|
|
|
|
|
$self->{current_page} = $current_page;
|
35
|
0
|
0
|
|
|
|
|
if ($self->{current_page} < 1) {
|
|
|
0
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$self->{current_page} = 1;
|
37
|
|
|
|
|
|
|
} elsif ($self->{current_page} > $self->{total_pages}) {
|
38
|
0
|
|
|
|
|
|
$self->{current_page} = $self->{total_pages};
|
39
|
|
|
|
|
|
|
}
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# previous number of page (>= 1 or undef)
|
42
|
0
|
|
|
|
|
|
$self->{prev_page} = $self->{current_page} - 1;
|
43
|
0
|
0
|
|
|
|
|
if ($self->{prev_page} < 1) {
|
44
|
0
|
|
|
|
|
|
$self->{prev_page} = undef;
|
45
|
|
|
|
|
|
|
}
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# next number of page (>= 1 or undef)
|
48
|
0
|
|
|
|
|
|
$self->{next_page} = $self->{current_page} + 1;
|
49
|
0
|
0
|
|
|
|
|
if ($self->{next_page} > $self->{total_pages}) {
|
50
|
0
|
|
|
|
|
|
$self->{next_page} = undef;
|
51
|
|
|
|
|
|
|
}
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# start position of current set (>= 1)
|
54
|
0
|
|
|
|
|
|
$self->{start_of_set} = int(($self->{current_page} - 1) / $self->{pages_per_set}) * $self->{pages_per_set} + 1;
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# end position of current set (>= 1)
|
57
|
0
|
|
|
|
|
|
$self->{end_of_set} = $self->{start_of_set} + $self->{pages_per_set} - 1;
|
58
|
0
|
0
|
|
|
|
|
if ($self->{end_of_set} > $self->{total_pages}) {
|
59
|
0
|
|
|
|
|
|
$self->{end_of_set} = $self->{total_pages};
|
60
|
|
|
|
|
|
|
}
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# numbers of set (one or more numbers in array)
|
63
|
0
|
|
|
|
|
|
$self->{numbers_of_set} = [];
|
64
|
0
|
|
|
|
|
|
for (my $i = $self->{start_of_set}; $i <= $self->{end_of_set}; ++$i) {
|
65
|
0
|
|
|
|
|
|
push(@{$self->{numbers_of_set}}, $i);
|
|
0
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
}
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# nearest page number of the previous set (>= 1 or undef)
|
69
|
0
|
|
|
|
|
|
$self->{page_of_prev_set} = $self->{start_of_set} - 1;
|
70
|
0
|
0
|
|
|
|
|
if ($self->{page_of_prev_set} < 1) {
|
71
|
0
|
|
|
|
|
|
$self->{page_of_prev_set} = undef;
|
72
|
|
|
|
|
|
|
}
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# nearest page number of the next set (>= 1 or undef)
|
75
|
0
|
|
|
|
|
|
$self->{page_of_next_set} = $self->{end_of_set} + 1;
|
76
|
0
|
0
|
|
|
|
|
if ($self->{page_of_next_set} > $self->{total_pages}) {
|
77
|
0
|
|
|
|
|
|
$self->{page_of_next_set} = undef;
|
78
|
|
|
|
|
|
|
}
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# starting position of the slice (>= 0)
|
81
|
0
|
|
|
|
|
|
$self->{start_of_slice} = ($self->{current_page} - 1) * $self->{entries_per_page};
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# ending position of the slice (>= 0)
|
84
|
0
|
|
|
|
|
|
$self->{end_of_slice} = $self->{start_of_slice} + $self->{entries_per_page} - 1;
|
85
|
0
|
0
|
|
|
|
|
if ($self->{end_of_slice} > $self->{total_entries} - 1) {
|
86
|
0
|
|
|
|
|
|
$self->{end_of_slice} = $self->{total_entries} - 1;
|
87
|
|
|
|
|
|
|
}
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# length of the slice (>= 1)
|
90
|
0
|
|
|
|
|
|
$self->{length_of_slice} = $self->{end_of_slice} - $self->{start_of_slice} + 1;
|
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
return $self;
|
93
|
|
|
|
|
|
|
}
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# }}}
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1;
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__END__
|