| 1 | package Template::Flute::Paginator; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use Sub::Quote; | ||
| 8 | use Template::Flute::Iterator; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Template::Flute::Paginator - Generic paginator class for Template::Flute | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | $paginator = Template::Flute::Paginator->new; | ||
| 17 | |||
| 18 | # set page size | ||
| 19 | $paginator->page_size(10); | ||
| 20 | |||
| 21 | # retrieve number of pages | ||
| 22 | $paginator->pages; | ||
| 23 | |||
| 24 | # retrieve current page (starting with 1) | ||
| 25 | $paginator->current_page; | ||
| 26 | |||
| 27 | # retrieve global position numbers for current page | ||
| 28 | $paginator->position_first; | ||
| 29 | $paginator->position_last; | ||
| 30 | |||
| 31 | # select a page (starting with 1) | ||
| 32 | $paginator->select_page; | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | has iterator => ( | ||
| 37 | is => 'rw', | ||
| 38 | lazy => 1, | ||
| 39 | default => quote_sub q{return Template::Flute::Iterator->new;}, | ||
| 40 | ); | ||
| 41 | |||
| 42 | has page_size => ( | ||
| 43 | is => 'rw', | ||
| 44 | lazy => 1, | ||
| 45 | default => quote_sub q{return 0;}, | ||
| 46 | ); | ||
| 47 | |||
| 48 | has page_position => ( | ||
| 49 | is => 'ro', | ||
| 50 | lazy => 1, | ||
| 51 | default => quote_sub q{return 0;}, | ||
| 52 | ); | ||
| 53 | |||
| 54 | =head1 METHODS | ||
| 55 | |||
| 56 | =head2 pages | ||
| 57 | |||
| 58 | Returns number of pages. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub pages { | ||
| 63 | my $self = shift; | ||
| 64 | my ($count, $pages); | ||
| 65 | |||
| 66 | $count = $self->iterator->count; | ||
| 67 | |||
| 68 | if ($self->page_size > 0) { | ||
| 69 | $pages = int($count / $self->page_size); | ||
| 70 | if ($count % $self->page_size) { | ||
| 71 | $pages++; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | elsif ($count > 0) { | ||
| 75 | $pages = 1; | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | $pages = 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | return $pages; | ||
| 82 | } | ||
| 83 | |||
| 84 | =head2 current_page | ||
| 85 | |||
| 86 | Returns current page, starting from 1. | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub current_page { | ||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | unless (exists $self->{current_page}) { | ||
| 94 | $self->{current_page} = 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | return $self->{current_page}; | ||
| 98 | } | ||
| 99 | |||
| 100 | =head2 select_page { | ||
| 101 | |||
| 102 | Select page, starting from 1. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub select_page { | ||
| 107 | my ($self, $page) = @_; | ||
| 108 | my ($new_position, $distance); | ||
| 109 | |||
| 110 | # calculate number of entries | ||
| 111 | $new_position = ($page - 1) * $self->page_size; | ||
| 112 | |||
| 113 | $distance = $new_position - $self->page_position + $page - 2; | ||
| 114 | |||
| 115 | if ($distance > 1) { | ||
| 116 | for (0 .. $distance) { | ||
| 117 | $self->next; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 position_first | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub position_first { | ||
| 127 | my $self = shift; | ||
| 128 | |||
| 129 | return ($self->current_page - 1) * $self->page_size + 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | =head2 position_last | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub position_last { | ||
| 137 | my $self = shift; | ||
| 138 | my $position; | ||
| 139 | |||
| 140 | $position = $self->current_page * $self->page_size; | ||
| 141 | |||
| 142 | if ($position > $self->count) { | ||
| 143 | $position = $self->count; | ||
| 144 | } | ||
| 145 | |||
| 146 | return $position; | ||
| 147 | } | ||
| 148 | |||
| 149 | =head2 next | ||
| 150 | |||
| 151 | Returns next record or undef. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub next { | ||
| 156 | my $self = shift; | ||
| 157 | |||
| 158 | if ($self->page_size > 0) { | ||
| 159 | if ($self->page_position < $self->page_size) { | ||
| 160 | $self->{page_position}++; | ||
| 161 | return $self->iterator->next; | ||
| 162 | } | ||
| 163 | else { | ||
| 164 | # advance current page | ||
| 165 | $self->{current_page}++; | ||
| 166 | $self->{page_position} = 0; | ||
| 167 | return; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | return $self->iterator->next; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | =head2 count | ||
| 176 | |||
| 177 | Returns number of records. | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub count { | ||
| 182 | my $self = shift; | ||
| 183 | |||
| 184 | $self->iterator->count; | ||
| 185 | } | ||
| 186 | |||
| 187 | =head2 seed | ||
| 188 | |||
| 189 | Seeds the iterator. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub seed { | ||
| 194 | my ($self, $data) = @_; | ||
| 195 | |||
| 196 | $self->iterator->seed($data); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub BUILDARGS { | ||
| 200 | my ($class, @args) = @_; | ||
| 201 | my ($iter, $data); | ||
| 202 | |||
| 203 | if (ref($args[0]) eq 'ARRAY') { | ||
| 204 | # create iterator | ||
| 205 | $data = shift @args; | ||
| 206 | $iter = Template::Flute::Iterator->new($data); | ||
| 207 | unshift @args, iterator => $iter; | ||
| 208 | } | ||
| 209 | |||
| 210 | return {@args}; | ||
| 211 | } | ||
| 212 | |||
| 213 | =head1 AUTHOR | ||
| 214 | |||
| 215 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 216 | |||
| 217 | =head1 LICENSE AND COPYRIGHT | ||
| 218 | |||
| 219 | Copyright 2010-2019 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 220 | |||
| 221 | This program is free software; you can redistribute it and/or modify it | ||
| 222 | under the terms of either: the GNU General Public License as published | ||
| 223 | by the Free Software Foundation; or the Artistic License. | ||
| 224 | |||
| 225 | See http://dev.perl.org/licenses/ for more information. | ||
| 226 | |||
| 227 | =head1 SEE ALSO | ||
| 228 | |||
| 229 | L<Template::Flute::Iterator> | ||
| 230 | |||
| 231 | =cut | ||
| 232 | |||
| 233 | 1; |