| 1 | #!/usr/bin/perl | ||||||||||
| 2 | |||||||||||
| 3 | package DBI::ResultPager; | ||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use CGI qw/:standard/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Exporter (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | our ($VERSION, @ISA); | |||||||||
| 13 | 1 | $VERSION = '0.9.2'; | |||||||||
| 14 | 1 | @ISA = qw(Exporter); | |||||||||
| 15 | } | ||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 19 | 0 | my $self = {}; | |||||||||
| 20 | 0 | $self->{DBH} = ''; | |||||||||
| 21 | 0 | $self->{QUERY} = ''; | |||||||||
| 22 | 0 | $self->{PERPAGE} = 30; | |||||||||
| 23 | 0 | $self->{DEFAULTORDER} = ''; | |||||||||
| 24 | 0 | $self->{ALIGN} = ''; | |||||||||
| 25 | |||||||||||
| 26 | 0 | my %fmt = (); | |||||||||
| 27 | 0 | my %hidden = (); | |||||||||
| 28 | 0 | my @cc; | |||||||||
| 29 | |||||||||||
| 30 | 0 | $self->{FORMATTERS} = \%fmt; | |||||||||
| 31 | 0 | $self->{CUSTOMCOLUMNS} = \@cc; | |||||||||
| 32 | 0 | $self->{HIDDENCOLUMNS} = \%hidden; | |||||||||
| 33 | |||||||||||
| 34 | 0 | bless($self); | |||||||||
| 35 | 0 | return $self; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | sub display { | ||||||||
|
Subroutine
display: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 39 | 0 | my ($self, @params) = (@_); | |||||||||
| 40 | 0 | my $rv = getOutput($self, @params); | |||||||||
| 41 | 0 | print $rv; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | sub getOutput { | ||||||||
|
Subroutine
getOutput: not called CC=19 SCAR=59.4
Pod
undocumented
| |||||||||||
| 45 | 0 | my ($self, @params) = (@_); | |||||||||
| 46 | |||||||||||
| 47 | 0 | my $output = ''; | |||||||||
| 48 | |||||||||||
| 49 | 0 | my @columns; | |||||||||
| 50 | 0 | my $page = 1; | |||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | if(defined(url_param('page'))) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | $page = url_param('page'); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | my $hidden = $self->{HIDDENCOLUMNS}; | |||||||||
| 57 | 0 | my $query = $self->{QUERY}; | |||||||||
| 58 | 0 | my $perPage = $self->{PERPAGE}; | |||||||||
| 59 | 0 | my $offset = (($page - 1) * $perPage); | |||||||||
| 60 | 0 | my $dbh = $self->{DBH}; | |||||||||
| 61 | 0 | my $order = $self->{DEFAULTORDER}; | |||||||||
| 62 | |||||||||||
| 63 | 0 | my $ccref = $self->{CUSTOMCOLUMNS}; | |||||||||
| 64 | 0 | my @customColumns = @$ccref; | |||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | if($order ne '') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | $query = $query . ' order by ' . $order; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 0 | $query = $query . ' limit ' . ($perPage + 1); | |||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | if($page ne 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | $query = $query . " offset $offset"; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | my $sth = $dbh->prepare($query); | |||||||||
| 77 | 0 | ▶ | $sth->execute(@params) or die "Error in $query: $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | 0 | $output .= '<center><table width="90%" border="0">'; | |||||||||
| 80 | |||||||||||
| 81 | 0 | my $nref = $sth->{NAME}; | |||||||||
| 82 | 0 | my @names = @$nref; | |||||||||
| 83 | |||||||||||
| 84 | # Print the header. | ||||||||||
| 85 | 0 | $output .= '<tr>'; | |||||||||
| 86 | 0 | foreach(@names) { | |||||||||
| 87 | 0 | ▶ | if(!defined($hidden->{$_})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | $output .= '<th>' . $_ . '</th>'; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 0 | push(@columns, $_); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 0 | foreach(@customColumns) { | |||||||||
| 95 | 0 | $output .= '<th>' . $_->{'columnName'} . '</th>'; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | # Add headers for any custom columns | ||||||||||
| 99 | |||||||||||
| 100 | 0 | $output .= "</tr>\n"; | |||||||||
| 101 | |||||||||||
| 102 | 0 | my $formatters = $self->{FORMATTERS}; | |||||||||
| 103 | 0 | my $count = 0; | |||||||||
| 104 | 0 | ▶ | while(my @row = $sth->fetchrow_array()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 0 | $count++; | |||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | if($count > $perPage) { next; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | |||||||||||
| 109 | 0 | my $color = ""; | |||||||||
| 110 | 0 | ▶ | if( ($count %2) eq 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | $color = ' bgcolor="#EEEEEE"'; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | $output .= "<tr$color>"; | |||||||||
| 115 | 0 | my $colcount = 0; | |||||||||
| 116 | 0 | my $al = ''; | |||||||||
| 117 | 0 | my $align = $self->{ALIGN}; | |||||||||
| 118 | 0 | ▶ | if($align ne '') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | $al = ' valign="' . $align . '"'; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 0 | foreach(@row) { | |||||||||
| 123 | 0 | $colcount++; | |||||||||
| 124 | 0 | my $colname = $columns[$colcount - 1]; | |||||||||
| 125 | |||||||||||
| 126 | 0 | ▶ | if(defined($hidden->{$colname})) { next; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | |||||||||||
| 128 | 0 | $output .= '<td' . $al . '>'; | |||||||||
| 129 | |||||||||||
| 130 | # Check if this column has a custom formatter | ||||||||||
| 131 | 0 | ▶ | if(defined($formatters->{$colname})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | my $subref = $formatters->{$colname}; | |||||||||
| 133 | 0 | $output .= &$subref($_, \@row); | |||||||||
| 134 | } else { | ||||||||||
| 135 | 0 | $output .= $_; | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 0 | $output .= '</td>'; | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 0 | foreach(@customColumns) { | |||||||||
| 142 | 0 | $output .= '<td' . $al . '>'; | |||||||||
| 143 | 0 | my $cref = $_->{'codeRef'}; | |||||||||
| 144 | 0 | $output .= &$cref(@row) . '</td>'; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 0 | $output .= "</tr>\n"; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 0 | $output .= '</table>'; | |||||||||
| 151 | |||||||||||
| 152 | 0 | my $prev = 0; | |||||||||
| 153 | 0 | ▶ | if($offset ne 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | 0 | my $u = manglePage($page - 1); | |||||||||
| 155 | 0 | $output .= '<a href="' . $u . '">Previous Page</a>'; | |||||||||
| 156 | 0 | $prev = 1; | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | 0 | ▶ | if($count > $perPage) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | |||||||||||
| 161 | 0 | ▶ | if($prev ne 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | 0 | $output .= ' | '; | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | 0 | my $u = manglePage($page + 1); | |||||||||
| 166 | 0 | $output .= '<a href="'. $u . '">Next Page</a>'; | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | 0 | $output .= '</center>'; | |||||||||
| 170 | } | ||||||||||
| 171 | |||||||||||
| 172 | 0 | ▶ | sub alignRows { | ||||||||
|
Subroutine
alignRows: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 173 | 0 | my ($self, $align) = (@_); | |||||||||
| 174 | 0 | $self->{ALIGN} = $align; | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | # Returns the current URL with the given page number. | ||||||||||
| 178 | # FIXME: URL params only | ||||||||||
| 179 | 0 | ▶ | sub manglePage { | ||||||||
|
Subroutine
manglePage: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 180 | 0 | my ($page) = (@_); | |||||||||
| 181 | |||||||||||
| 182 | 0 | my $u = url(-relative=>1, -query=>1); | |||||||||
| 183 | 0 | ▶ | if($u =~ /[\?&;]page=/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 184 | 0 | $u =~ s/[\?&;]page=[0-9]*//; | |||||||||
| 185 | } | ||||||||||
| 186 | |||||||||||
| 187 | 0 | ▶ | if($u =~ /\?/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | 0 | $u = $u . '&page=' . $page; | |||||||||
| 189 | } else { | ||||||||||
| 190 | 0 | $u = $u . '?page=' . $page; | |||||||||
| 191 | } | ||||||||||
| 192 | |||||||||||
| 193 | 0 | return $u; | |||||||||
| 194 | } | ||||||||||
| 195 | |||||||||||
| 196 | 0 | ▶ | sub hideColumn { | ||||||||
|
Subroutine
hideColumn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 197 | 0 | my ($self, $columnName) = (@_); | |||||||||
| 198 | 0 | my $ar = $self->{HIDDENCOLUMNS}; | |||||||||
| 199 | 0 | $ar->{$columnName} = 1; | |||||||||
| 200 | } | ||||||||||
| 201 | |||||||||||
| 202 | 0 | ▶ | sub addCustomColumn { | ||||||||
|
Subroutine
addCustomColumn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 203 | 0 | my ($self, $columnName, $codeRef, $identityColumn) = (@_); | |||||||||
| 204 | |||||||||||
| 205 | 0 | my %cc = (); | |||||||||
| 206 | |||||||||||
| 207 | 0 | $cc{'columnName'} = $columnName; | |||||||||
| 208 | 0 | $cc{'codeRef'} = $codeRef; | |||||||||
| 209 | |||||||||||
| 210 | 0 | my $ar = $self->{CUSTOMCOLUMNS}; | |||||||||
| 211 | 0 | push(@$ar, \%cc); # Push the hashref onto the arrayref. | |||||||||
| 212 | } | ||||||||||
| 213 | |||||||||||
| 214 | 0 | ▶ | sub addColumnFormatter { | ||||||||
|
Subroutine
addColumnFormatter: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 215 | 0 | my ($self, $column, $formatref) = (@_); | |||||||||
| 216 | |||||||||||
| 217 | 0 | my $formatters = $self->{FORMATTERS}; | |||||||||
| 218 | 0 | $formatters->{$column} = $formatref; | |||||||||
| 219 | } | ||||||||||
| 220 | |||||||||||
| 221 | 0 | ▶ | sub defaultOrder { | ||||||||
|
Subroutine
defaultOrder: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 222 | 0 | my $self = shift; | |||||||||
| 223 | 0 | ▶ | if(@_) { $self->{DEFAULTORDER} = shift; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 224 | 0 | return $self->{DEFAULTORDER}; | |||||||||
| 225 | } | ||||||||||
| 226 | |||||||||||
| 227 | 0 | ▶ | sub dbh { | ||||||||
|
Subroutine
dbh: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 228 | 0 | my $self = shift; | |||||||||
| 229 | 0 | ▶ | if(@_) { $self->{DBH} = shift; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 230 | 0 | return $self->{DBH}; | |||||||||
| 231 | } | ||||||||||
| 232 | |||||||||||
| 233 | 0 | ▶ | sub perPage { | ||||||||
|
Subroutine
perPage: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 234 | 0 | my $self = shift; | |||||||||
| 235 | 0 | ▶ | if(@_) { $self->{PERPAGE} = shift; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 236 | 0 | return $self->{PERPAGE}; | |||||||||
| 237 | } | ||||||||||
| 238 | |||||||||||
| 239 | 0 | ▶ | sub query { | ||||||||
|
Subroutine
query: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 240 | 0 | my $self = shift; | |||||||||
| 241 | 0 | ▶ | if(@_) { $self->{QUERY} = shift; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 242 | 0 | return $self->{QUERY}; | |||||||||
| 243 | } | ||||||||||
| 244 | |||||||||||
| 245 | 1 | 1; | |||||||||
| 246 | __END__ | ||||||||||