'
)) . "\n";
}
sub print_group_item_totals {
my $self = shift;
my $field = shift;
my $field_value = shift;
my $totals = "";
foreach my $i(0 .. (($self->{colspan})- scalar(@{$self->{group_fields_array}}))) {
# if(!(defined $self->{group_totals}{$self->{columns}{names}[$i]})){
# $totals .= '
';
# next;
# }
#Operaciones
my $total = "";
if(defined $self->{group_totals}{$self->{columns}{names}[$i]}{operation}){
if($self->{group_totals}{$self->{columns}{names}[$i]}{operation} eq "SUM"){
$total = $self->group_SUM($self->{columns}{names}[$i],$field,$field_value);
}elsif($self->{group_totals}{$self->{columns}{names}[$i]}{operation} eq "AVG"){
$total = $self->group_AVG($self->{columns}{names}[$i],$field,$field_value);
}elsif($self->{group_totals}{$self->{columns}{names}[$i]}{operation} eq "COUNT"){
$total = $self->group_COUNT($self->{columns}{names}[$i],$field,$field_value);
}
}
#Formatos
if($self->{group_totals}{$self->{columns}{names}[$i]}{format} eq "price"){
use Number::Format;
my $NF = Number::Format->new(%{$self->{Number_Format}});
$total = $NF->format_price($total);
}
if($self->{group_totals}{$self->{columns}{names}[$i]}{label}){
my $total_label = $total;
$total = $self->{group_totals}{$self->{columns}{names}[$i]}{label};
$total =~ s/%%/$total_label/g;
}
if(defined $self->{columns_align}){
$self->{group_item_totals}{td}{params}{align} = $self->{columns_align}[$i];
$totals .= td($self->{group_item_totals}{td}{params},$total);
}else{
$totals .= td($self->{group_item_totals}{td}{params},$total);
}
}
return " " . Tr ($self->{group_item_totals}{Tr}{params},$totals) . "\n";
}
sub SUM {
my $self = shift;
my $field = shift || "";
return 0 if(!$field);
my $total = 0;
foreach my $rec(@{$self->{rs}}) {
$total += $rec->{$field};
}
return $total;
}
sub group_SUM {
my $self = shift;
my $field = shift || "";
my $filter = shift;
my $filter_value = shift;
return 0 if(!$field);
my $total = 0;
foreach my $rec(@{$self->{rs}}) {
next if($rec->{$filter} ne $filter_value);
$total += $rec->{$field};
}
return $total;
}
sub COUNT {
my $self = shift;
my $field = shift || "";
return 0 if(!$field);
return scalar( @{$self->{rs}});
}
sub group_COUNT {
my $self = shift;
my $field = shift || "";
my $filter = shift;
my $filter_value = shift;
return 0 if(!$field);
my $total = 0;
foreach my $rec(@{$self->{rs}}) {
next if($rec->{$filter} ne $filter_value);
$total += 1;
}
return $total;
}
sub AVG {
my $self = shift;
my $field = shift || "";
return 0 if(!$field);
my $avg = 0;
my $it = 0;
foreach my $rec(@{$self->{rs}}) {
$it++;
$avg += $rec->{$field};
}
eval {
$avg = $avg / $it;
};
if($@){
$avg = "";
}
$avg = neares(.01,$avg);
return $avg;
}
sub group_AVG {
my $self = shift;
my $field = shift || "";
my $filter = shift;
my $filter_value = shift;
return 0 if(!$field);
my $avg = 0;
my $it = 0;
foreach my $rec(@{$self->{rs}}) {
next if($rec->{$filter} ne $filter_value);
$it++;
$avg += $rec->{$field};
}
eval {
$avg = $avg / $it;
};
if($@){
$avg = "";
}
$avg = neares(.01,$avg);
return $avg;
}
sub headers_groups {
my $self = shift;
$self->{headers_groups} = shift;
}
sub orders {
my $self = shift;
$self->{orders} = shift;
if($self->{orders}->{param("cg_order")}){
$self->{sql}{order_by} = $self->{orders}->{param("cg_order")};
$self->{sql}{order_by} .= " DESC " if (param("cg_side"));
$self->{sql}{order_by} .= " ASC " if (!param("cg_side"));
}
}
=head1 SYNOPSIS
Easily create html lists whit auto order, auto pagination, grouping and conditional formats.
Perhaps a little code snippet.
use CGI::List;
#We need a DBH Handle
$dbh = DBI->connect(.....);
#Create List Object
$list = CGI::List->new(
dbh => $dbh,
sql => {
select => "foo, bar ",
from => "table1",
limit => "20",
where => "some_column1=? AND some_column2=?",
params=>["Value1","Value2"],
order_by => "foo DESC",
},
);
#Print
print $list->print();
=head1 FEATURES
* Auto Order
* Auto Pagination
* CSS based. Contact developer for CSS examples
* Column totals(Only SUM, COUNT and AVG are supported)
* Conditional formats for rows
* Conditional Formats for cells
* Auto detect column names
* 2 row formats for better visualization
* Row grouping
* Http Link and highlight on rows based in rows keys
* Opener action for pop up windows
* And more
=head1 METHODS
=head2 new()
This method creates a new $list object, which you then use to generate and process your list.
my $list = CGI::List->new();
The following is a description of each option, in alphabetical order:
name => 'list_name'
If you use a multi lists pages you need to specify a name for each list
on_errors => 'print',
If you have SQL errors you can print(default), warn or die
debug => 0 | 1, default 0
If is set to 1 this print the query executed on SQL errors
caption => 'list title'
This create a list title with the caption html tag
auto_order => 1 | 0, default 1
Enable, disable auto order mechanism on the list
pagination => 1 | 0, default 1
Enable or disable auto pagination on the list
nav_pages => $number, default 4
Number of pages you can see on pagination
Number_Format => {THOUSANDS_SEP=>",",DECIMAL_POINT=>".",MON_THOUSANDS_SEP=>",","MON_DECIMAL_POINT"=>".","INT_CURR_SYMBOL"=>'$'};
On SUM otions you can format the result to price ($1,234.00), whit this parameters THOUSANDS_SEP, DECIMAL_POINT, MON_THOUSANDS_SEP, MON_DECIMAL_POINT, INT_CURR_SYMBOL.
table => {}
Propiedades de la tabla, default {width => "100%",class => "cg_table",align => "center",cellpadding=>"0",cellspacing=>"0"}
labels => {
page_of => 'Page _PAGE_ of _OF_',
no_data => 'No records found',
link_up => '↑',
link_down => '↓',
next_page => '»',
previous_page => '«',
number_of_rows => "_NUMBER_ rows",
};
This are the text printed on the list, you can traslate to other language
=head2 print()
This function renders the list into HTML, and returns a string containing the list.
print $list->print;
=head2 group()
This method Create groups of data:
$list->group(key=>'key_field',fields=>[qw/key_field other_field other_field/]);
=head2 group_total()
This method calculate row totals on each group:
$list->group_total(key=>'key_field',type=>"MATH",operation=>'SUM',label=>"%% some text",format=>'price');
Operation support only SUM, AVG, and COUNT, the format parameter are optional
=head2 total()
This method calculate row totals:
$list->total(key=>'key_field',type=>"MATH",operation=>'SUM',label=>"%% some text",format=>'price');
Operation suport only SUM, AVG, and COUNT, the format parameter are optional
=head2 row_format()
This function specify a format of row depending on their value
$list->row_format(name=>"field_name",condition=>"'%%' eq 'urgent'",params=>{class=>"cg_row_urgent"});
%% is the cell value, on this example you need to create 2 css class cg_row_urgent and cg_row_urgent_hover
for the hover action
=head2 cell_format();
This function specify a format of cell depending on their value
$list->cell_format(name=>"field_name",condition=>"'%%' eq 'urgent'",params=>{class=>"cg_cell_urgent"});
%% is the cell value, on this example you need to create 2 css class cg_cell_urgent and cg_cell_urgent_hover
for the hover action
=head2 columns_width()
This function specify the width of each column
$list->columns_width(["100","200","300"]);
On this example you have a 3 columns query and 100, 200, 300 are the width of each column
=head2 columns_align()
This function specify the horizontal align of each column
$list->columns_align(["left","center","right"]);
On this example you have a 3 columns query and left, center, right are the alignment of each column data
=head2 columns_headers_align()
This function specify the horizontal align of each column header
$list->columns_headers_align(["left","center","right"]);
On this example you have a 3 columns query and left, center, right are the alignment of each column header data
=head1 Examples
This example provides an list of data with auto order, auto pagination and action on each row click
my $list = CGI::List->new(
dbh => $dbh,
name => "pays_list",
sql => {
select => "p.pay_id, p.date, pr.name, " .
"IF(p.is_cancel,'Cancel','Active') AS 'status'",
from => "pays p INNER JOIN partners pr ON p.pay_id=pr.pay_id ",
limit => "20",
where => "some_column=? AND some_column=?",
params=>["Value1","Value2"],
order_by => "p.date DESC",
},
link => {
key => "pay_id",
hidde_key_col => 1,
location => "pays.pl",
transit_params => {some_param_to_be_present_everywere=>"value"},
},
);
$list->print();
=head1 AUTHOR
David Romero Garcia, C<< >>
=head1 COLABORATORS
Juan C. Sanchez-DelBarrio
=head1 BUGS
Please report any bugs or feature requests to
C, or through the web interface at
L.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc CGI::List
You can also look for information at:
L.
L.
=over 4
=item * AnnoCPAN: Annotated CPAN documentation
L
=item * CPAN Ratings
L
=item * RT: CPAN's request tracker
L
=item * Search CPAN
L
=back
=head1 ACKNOWLEDGEMENTS
=head1 COPYRIGHT & LICENSE
Copyright 2007 David Romero GarcĂa, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1; # End of CGI::List