Skip to content

Commit

Permalink
Various enhancements for custom paging subroutine.
Browse files Browse the repository at this point in the history
Add support for [more], [matches], [match-count] and any [-anchor] tag inside [more-list].
Store current position into paging_first_match scratch variable.
Fix calculation of page count.
  • Loading branch information
racke committed Mar 20, 2012
1 parent 7262d93 commit 61c8c4e
Showing 1 changed file with 102 additions and 9 deletions.
111 changes: 102 additions & 9 deletions code/paging.sub
@@ -1,15 +1,16 @@
Sub paging <<EOS
sub {
my ($next, $prev, $page, $border, $border_selected, $opt, $r) = @_;
my ($q, $pages, $first, $curpage, $url, @links, @labels, @more, $base_url,
$prefix, $suffix, $session, $form_arg, $nav, $ml, $matches, $replace,
$out, $link_prefix, $link_suffix);

my $q;

$q = $opt->{object} || $::Instance->{SearchObject}{$opt->{label}};
return '' unless $q->{matches} > $q->{mv_matchlimit}
and $q->{mv_matchlimit} > 0;

my ($pages, $first, $curpage, $url, @links, @more, $base_url, $prefix, $suffix, $session, $form_arg, $nav, $ml, $matches);

# turn form parameter into a hash
my %form;

Expand All @@ -26,17 +27,66 @@ sub {

$ml = $q->{mv_matchlimit};
$matches = $q->{matches};
$pages = $matches / $ml;
$pages = int($matches / $ml);
if ($matches % $ml) {
$pages++;
}

$curpage = $opt->{paging_page} || $CGI->{category_page};

if ($curpage) {
$first = ($curpage - 1) * $ml;
}
else {
$first = $q->{mv_first_match} || 1;
}

if ($r =~ /\[more\]/) {
# check for [more] replacement
$replace = 1;
}

if ($r =~ /\[match/) {
my ($range_start, $range_end);

$range_start = $first;
$range_end = $first + $ml - 1;

if ($range_end > $matches) {
$range_end = $matches;
}

$r =~ s/\[matches]/$range_start-$range_end/;
$r =~ s/\[match-count]/$matches/;

return $r unless $replace;
}

# extract anchor labels
my %anchor_labels;

for my $anchor qw(first prev next last) {
if ($r =~ s:\[$anchor[-_]anchor\](.*?)\[/$anchor[-_]anchor\]::i) {
$anchor_labels{$anchor} = $1;
}
}

# link prefix / link suffix
$link_prefix = (exists $opt->{link_prefix}) ? $opt->{link_prefix} : '<li>';
$link_suffix = (exists $opt->{link_suffix}) ? $opt->{link_suffix} : '</li>';

$first = $q->{mv_first_match} || 0;
$Scratch->{paging_first_match} = $first;

$session = $q->{mv_cache_key};

$prefix = q{<div id="pagenumbers"><ul>};
$suffix = q{</ul></div>};
if ($replace) {
$prefix = $suffix = '';
}
else {
$prefix = q{<div id="pagenumbers"><ul>};
$suffix = q{</ul></div>};
}

$search_page = $q->{mv_search_page} || $Tag->var('MV_PAGE',1);

Expand Down Expand Up @@ -68,22 +118,65 @@ sub {
$url = $Tag->area({href => "scan/MM=$nav", form => $form_arg});
}
$links[$i] = $url;
$labels[$i] = $i;
}

# current page
$links[$curpage] = '';
$labels[$curpage] = $curpage;

for (my $i = 1; $i <= $pages; $i++) {
if ($links[$i]) {
push(@more, qq{<li><a href="$links[$i]">$i</a></li>});
push(@more, qq{$link_prefix<a href="$links[$i]">$labels[$i]</a>$link_suffix});
}
else {
push(@more, qq{<li class="down">$i</li>});
push(@more, qq{$link_prefix$labels[$i]$link_suffix});
}
}

unless ($curpage == 1) {
$url = $links[$curpage-1];

if ($anchor_labels{first}) {
unshift(@more, qq{$link_prefix<a href="$url">$anchor_labels{first}</a>$link_suffix});
}
else {
unshift(@more, qq{$link_prefix<a href="$url">first</a>$link_suffix});
}

if ($anchor_labels{prev}) {
unshift(@more, qq{$link_prefix<a href="$url">$anchor_labels{prev}</a>$link_suffix});
}
else {
unshift(@more, qq{$link_prefix<a href="$url">previous &lt;</a>$link_suffix});
}
}

unless ($curpage == int($pages)) {
$url = $links[$curpage+1];
push(@more, qq{<li><a href="$url">next &gt;</a></li>});

if ($anchor_labels{next}) {
push(@more, qq{$link_prefix<a href="$url">$anchor_labels{next}</a>$link_suffix});
}
else {
push(@more, qq{$link_prefix<a href="$url">next &gt;</a>$link_suffix});
}

if ($anchor_labels{last}) {
push(@more, qq{$link_prefix<a href="$url">$anchor_labels{last}</a>$link_suffix});
}
else {
push(@more, qq{$link_prefix<a href="$url">$more</a>$link_suffix});
}
}

$out = $prefix . join(' ', @more) . $suffix;

if ($replace) {
$r =~ s/\[more\]/$out/g;
return $r;
}

return $prefix . join(' ', @more) . $suffix;
return $out;
}
EOS

0 comments on commit 61c8c4e

Please sign in to comment.