sub Min { my $min=shift; $_<$min and $min=$_ for @_; $min } sub Max { my $max=shift; $_>$max and $max=$_ for @_; $max } sub CalcNavigation { my %P = ref($_[0]) eq "HASH" ? %{ $_[0] } : (); $P{DefaultCount} = 20 unless defined $P{DefaultCount}; $P{PrevPages} = 10 unless defined $P{PrevPages}; $P{NextPages} = 10 unless defined $P{NextPages}; $P{$_} = 0 for grep {! defined $P{$_}} qw(From Count Records); $P{$_} = int($P{$_}) for qw(DefaultCount PrevPages NextPages From Count Records); $P{DefaultCount} = Max($P{DefaultCount}, 1); $P{Records} = 0 unless $P{Records} > 0; $P{From} = Min(Max($P{From}, 1), Max($P{Records}, 1)); $P{Count}= Max($P{Count}, 0) || $P{DefaultCount}; $P{Page} = int(($P{From}-1)/$P{Count})+1; $P{From} = ($P{Page}-1)*$P{Count}+1; $P{To} = Min($P{From}+$P{Count}-1, $P{Records}); $P{Prev} = Max($P{From}-$P{Count}, 1); $P{Prev} = undef if $P{From} == 1; $P{Next} = $P{From}+$P{Count}; $P{Next} = undef if $P{Next} > $P{Records}; $P{Pages}= [ map {{ Page => $_, From => ($_-1)*$P{Count}+1, To => Min($_*$P{Count}, $P{Records}) }} Max(1, $P{Page}-$P{PrevPages}) .. Min($P{Page}+$P{NextPages}, ($P{Records}-1)/$P{Count}+1) ]; return \%P; }