/usr/share/perl5/Data/Page/Pageset.pm is in libdata-page-pageset-perl 1.02-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | package Data::Page::Pageset;
use Carp;
use strict;
use Data::Page::Pageset::Chunk;
use POSIX;
use base 'Class::Accessor::Fast';
__PACKAGE__->mk_accessors( qw(
total_pages
first_pageset
last_pageset
current_pageset
previous_pageset
next_pageset
) );
# add this for make more version to be 1.02
our $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /: (\d+)\.(\d+)/;
sub new {
my $class = shift;
my $page = shift;
my $param = shift;
croak("The first is not a Data::Page object!")
unless $page->UNIVERSAL::isa('Data::Page');
my $self = bless {}, $class;
$self->{_page} = $page;
$self->{_total_pagesets} = [];
$self->{_MAX_PAGESETS} = 1;
$self->{_PAGE_PER_SET} = 2;
if ( not defined $param or ref $param eq '' ){
$self->pages_per_set( $param || 10 );
}elsif ( defined $param->{max_pagesets} ){
$self->max_pagesets( $param->{max_pagesets} );
}elsif ( defined $param->{pages_per_set} ){
$self->pages_per_set( $param->{pages_per_set} );
}
return $self;
}
sub total_pagesets {
my $self = shift;
if ( $_[0] ){
$self->{_total_pagesets} = $_[0];
}
return ( wantarray ) ? @{$self->{_total_pagesets}} : $self->{_total_pagesets};
}
sub max_pagesets {
my ( $self, $number ) = @_;
if ( defined $number ){
$number = int $number;
croak("Fewer than one pagesets!") if $number < 1;
return $self->{_MAX_PAGESETS} if $number == $self->{_MAX_PAGESETS};
$self->{_MAX_PAGESETS} = $number;
$self->pages_per_set( POSIX::ceil( $self->{_page}->last_page / $number ) );
}
return $self->{_MAX_PAGESETS};
}
sub pages_per_set {
my ( $self, $number ) = @_;
if ( defined $number ){
$number = int $number;
croak("Fewer than two pages per set!") if $number < 2;
return $self->{_PAGE_PER_SET} if $number == $self->{_PAGE_PER_SET};
$self->{_PAGE_PER_SET} = $number;
$self->_refresh;
}
return $self->{_PAGE_PER_SET};
}
sub _refresh {
my $self = shift;
my $number = $self->pages_per_set;
my $page = $self->{_page};
my $current_page = $page->current_page;
my @total_pages = ( $page->first_page .. $page->last_page );
$self->total_pages( scalar @total_pages );
my @pageset;
while ( @total_pages ){
my @array = splice( @total_pages, 0, $number );
my $chunk = Data::Page::Pageset::Chunk->new( @array );
push @pageset, $chunk;
if ( $current_page >= $chunk->first and $current_page <= $chunk->last ){
$chunk->is_current(1);
$self->current_pageset( $chunk );
$self->previous_pageset( $pageset[-2] ) if $#pageset;
}
$self->next_pageset( $chunk ) if ( $#pageset and $pageset[-2]->is_current );
}
$self->first_pageset( $pageset[0] );
$self->last_pageset( $pageset[-1] ) if $#pageset;
$self->total_pagesets( \@pageset );
}
1;
=head1 NAME
Data::Page::Pageset - change long page list to be shorter and well navigate
=head1 DESCRIPTION
Pages number can be very high, and it is not comfortable to show user from the first page to the last page list. Sometimes we need split the page list into some sets to shorten the page list, the form is like:
1-6 7-12 13 14 15 16 17 18 19-24 25-30 31-36 37-41
the first two part indicats the two pagesets, and in current pageset, we provide the normal page list from the first one to the last one, and provide the rest pagesets to indicate the pages scope.
In this module, you can specify the pages_per_set or max_pagesets for fine showing.
=head1 SYNOPSIS
use Data::Page::Pageset;
# we use Data::Page object, so do prepare
my $page = Data::Page->new($total_entries, $entries_per_page, $current_page);
# create the pageset object
my $pageset = Data::Page::Pageset->new( $page );
my $pageset = Data::Page::Pageset->new( $page, 12 );
my $pageset = Data::Page::Pageset->new( $page, { max_pagesets => $max_pagesets } );
my $pageset = Data::Page::Pageset->new( $page, { pages_per_set => $pages_per_set } );
# for using
foreach my $chunk ( $pageset->total_pagesets ){
if ( $chunk->is_current ){
map { print "$_ " } ( $chunk->first .. $chunk->last );
}else{
print "$chunk ";
}
}
=head1 METHODS
=over
=item new()
# default page_per_set is 10
my $pageset = Data::Page::Pageset->new( $page );
# set the page_per_set to be 12
my $pageset = Data::Page::Pageset->new( $page, 12 );
# another the same by passing hashref
my $pageset = Data::Page::Pageset->new( $page, { pages_per_set => $pages_per_set } );
# set the max_pagesets value, default is 1
my $pageset = Data::Page::Pageset->new( $page, { max_pagesets => $max_pagesets } );
# if max_pagesets supplies, the pages_per_set setting will be ignore
my $pageset = Data::Page::Pageset->new( $page,
{ max_pagesets => $max_pagesets, pages_per_set => $pages_per_set } );
We must need $page(isa Data::Page) object.
=item max_pagesets( $number )
# set the max_pagesets value, and the $pageset's info will changed immediately
$pageset->max_pagesets( $number );
=item pages_per_set( $number )
# set the pages_per_set value, and the $pageset's info will changed immediately
$pageset->pages_per_set( $number );
my $present_setting = $pageset->pages_per_set();
=item $pageset->total_pages
return total pages' number.
=item $pageset->total_pagesets
return the actual pagesets number.
=item $pageset->first_pageset
my $chunk = $pageset->first_pageset;
return the first pageset, it's Data::Page::Pageset::Chunk object
=item $pageset->last_pageset
my $chunk = $pageset->last_pageset;
return the last pageset, it's Data::Page::Pageset::Chunk object
=item $pageset->current_pageset
my $chunk = $pageset->current_pageset;
return the current pageset which current page is in this pageset, it's Data::Page::Pageset::Chunk object
=item $pageset->previous_pageset
my $chunk = $pageset->previous_pageset;
return the previous pageset, it's Data::Page::Pageset::Chunk object
=item $pageset->next_pageset
my $chunk = $pageset->next_pageset;
return the next pageset, it's Data::Page::Pageset::Chunk object
=back
=head1 Data::Page::Pageset::Chunk object
a $pageset gives you some $chunk to do more stuff as you see above. Here gives the $chunk methods
=over
=item first
# return the first page number in this chunk
$chunk->first;
=item last
# return the last page number in this chunk
$chunk->last;
=item middle
# return the middle page number in this chunk
$chunk->middle;
=item pages
# return the pages number in this chunk
$chunk->pages;
=item is_current
# return true if this $chunk contains the current_page
$chunk->is_current;
=item as_string
# if this chunk is from page 3 to 7, then print '3-7'
print $chunk;
print $chunk->as_string;
print $chunk->as_string('-');
# you can change default '-' as:
print $chunk->as_string('~');
if the $chunk only contains one page, it will only return the page number.
=back
=head1 SEE ALSO
L<Data::Page|Data::Page> is what we need, L<Data::Pageset|Data::Pageset> is the similar one, L<Template::Plugin::Pageset|Template::Plugin::Pageset> is the wrapper for this to using it more converiently in TT2 tempale.
=head1 BUGS
just mail me to <me@chunzi.org>
=head1 COPYRIGHT AND LICENSE
Copyright 2004 by Chun Sheng.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
Chun Sheng, <me@chunzi.org>
=cut
|