File: PageIndex.pm

package info (click to toggle)
sporum 1.8b1-3.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,108 kB
  • ctags: 676
  • sloc: perl: 14,300; makefile: 52
file content (93 lines) | stat: -rw-r--r-- 2,305 bytes parent folder | download
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
package Templates::Admin::PageIndex;

use SmallPigVars qw($config);
use SmallPig;

use strict;
use vars qw(@ISA);

@ISA = qw(Templates::Admin::OptBlock);

my %CACHE;

sub new{
  my ($class, $totalnums, $postsper, $page, $mode, $theme) = @_; 

  my $classvars = {('bdrcolor' => $theme->{'inbgcolor'},
		    'bgcolor' => $theme->{'inbgcolor'})};

  my $self = Templates::Admin::OptBlock->new($classvars, $theme);
  $self->{'totalnums'} = $totalnums;
  $self->{'postsper'} = $postsper;
  $self->{'page'} = $page;
  $self->{'mode'} = $mode;
  
  my $id = join '', sort values %$self, "pageindex";
  
  return $CACHE{$id} if $CACHE{$id};
  return $CACHE{$id} = bless $self, $class;
}

sub get{
  my $self = shift;
  $self->{'content'} = $self->_get_index(@_);
  $self->SUPER::get();
}

sub _get_index{
  my ($self, $op, $url) = @_;
  my ($totalnums, $postsper, $page, $mode) =   
    map{ $self->{$_} } 
  qw(totalnums postsper page mode);

  return if $totalnums==0;

  $page ||= 1;
  
  my $ret = "";
  $ret .= "<TR><TD ALIGN=center valign=middle CLASS=header3><B>Pages: ";
  if($postsper <= 0){
    return " ";
  }
  my $totalpages = int $totalnums/$postsper;
  my $remain = $totalnums % $postsper;
  $totalpages++ if $remain > 0;
  my $pagelimit = 20;

  my ($start, $end, $larrow, $rarrow) = 
      (1, ($pagelimit < $totalpages)?$pagelimit:$totalpages, 0, 0);
  if($totalpages > $pagelimit && $page + 1< $totalpages){
      $rarrow = $pagelimit + 1; 
  }
  if($page >= $pagelimit){
      $start = ($page >= $totalpages)?
	  $page - $pagelimit + 1:$page - $pagelimit + 2;
      $end = ($start + $pagelimit> $totalpages)?
	  $totalpages:$start + $pagelimit - 1;
      $larrow = $page - 1;
      $rarrow = $page + 1 if $page + 1< $totalpages;
  }
  
  my $cgi = $ENV{SCRIPT_NAME};

  $ret .= qq!<A HREF=$config->{'basewwwdir'}$cgi?op=$op$url&page=$larrow&mode=$mode>&lt;&lt;</A>! 
      if $larrow;
  for(my $i=$start; $i<=$end; $i++){
      if($i == $page){
	  $ret .= "<B> $i </B>";
      }
      else{
	  my $opts = "$url&page=$i&mode=$mode";
	  $ret .= qq!
<A HREF=$config->{'basewwwdir'}$cgi?op=$op$opts>$i</A>
    !;
      }
  }
  $ret .= qq!<A HREF=$config->{'basewwwdir'}$cgi?op=$op$url&page=$rarrow&mode=$mode>&gt;&gt;</A>! 
      if $rarrow;
  $ret .= "<\TR><\TD>";
  
  return $ret;
}

return 1;