Subversion Repositories CoffeeCatalog

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/perl

#if ($ENV{SHELL}) { die "This script shouldn't be executed from the command line!\n"; }

use CGI qw/param cookie header start_html url/;
use HTML::Tiny;
use scanFunctions;
our $h = HTML::Tiny->new( mode => 'html' );


my $pageTitle = "CC Roasters";
my $prefscookie = "CCRoastersPrefs";
our $DBTABLE = 'roasters';
my %COLUMNS = (
        roaster   =>  [qw(Roaster     5    text     static)],
        url       =>  [qw(URL        10    text     default)],
        location  =>  [qw(Location   15    text     default)],
        note      =>  [qw(Notes      20    text)]
);
my $stylesheet = "style.css";
my $homeURL = '/';

# If we need to modify line item values, create a subroutine named "modify_$columnname"
#    It will recieve a hashref to the object lineitem

sub modify_roaster {
  # Turn it into a link to the manage_roaster.pl page
  my $lineItem = shift;
  my $rster = $h->url_encode ($lineItem->{'roaster'});
  return $h->a ({ href=>"/manage_roaster.pl?roaster=$rster" }, $lineItem->{'roaster'});
}

sub modify_url {
  # Make the URL an actual link
  my $lineItem = shift; 
  return $h->a ({ href=>$lineItem->{'url'}, -target=>"_blank" }, $lineItem->{'url'});
}




# Ideally, nothing below this comment needs to change
#-------------------------------------------------------------------------------


our %NAME              = map  { $_ => $COLUMNS{$_}->[0] } keys %COLUMNS;
our %colOrderHash      = map  { $_ => $COLUMNS{$_}->[1] } keys %COLUMNS;
our %colFilterTypeHash = map  { $_ => $COLUMNS{$_}->[2] } keys %COLUMNS;
our @staticFields      = grep { $COLUMNS{$_}->[3] eq 'static' } keys %COLUMNS;
our @defaultFields     = grep { defined $COLUMNS{$_}->[3] } keys %COLUMNS;
#our @defaultFields     = grep { $COLUMNS{$_}->[3] eq 'default' or inArray ($_, \@staticFields) } keys %COLUMNS;

our @allFields = sort byfield keys %NAME;
our @displayFields = ();
our @hideFields = ();
my $QUERY_STRING;


our %FORM;
foreach (param()) {
        $FORM{$_} = param($_);                          # Retrieve all of the FORM data submitted
        
        if ((/^filter/) and ($FORM{$_} ne '')) {        # Build a set of filters to apply
                my ($filter,$field) = split /-/, $_;            
                $FILTER->{$field} = $FORM{$_};
        }       elsif ($FORM{$_} eq "true")                     # Compile list of fields to display
                { push @displayFields, $_; }
}


if (exists $FORM{autoload})     {                       # If the FORM was submitted (i.e. the page is being redisplayed),
                                                                            #   build the data for the cookie that remembers the page setup
        my $disFields = join ":", @displayFields;
        my $fils = join ":", map { "$_=$FILTER->{$_}" } keys %{$FILTER};
                
        $QUERY_STRING = $disFields.'&'.$fils.'&'.$FORM{autoload};
}


if (!(exists $FORM{autoload}))  {                       # No FORM was submitted...
        if (my $prefs = cookie($prefscookie))   { # Check for cookies from previous visits.
                my ($disF, $filts, $al) = split /&/,$prefs;
                @displayFields = split /:/,$disF;
                
                foreach $pair (split /:/, $filts)       {
                        my ($key, $value) = split /=/, $pair;
                        $FORM{"filter-$key"} = $value;
                        $FILTER->{$key} = $value;
                }
                
                $FORM{autoload} = $al;
                $QUERY_STRING = $prefs;
        }       else { @displayFields = @defaultFields; } # Otherwise suppply a default list of columns.
}

# let's just make sure the columns are in the right order (and there aren't any missing)
@displayFields = sort byfield uniq @displayFields, @staticFields;

# If the field isn't in the displayFields list, then add it to the hideFields list
@hideFields = grep { notInArray ($_, \@displayFields) } @allFields;

# Process any filters provided in the form to pass to the database
my @whereClause = map { filter ($_, $FILTER->{$_}) } grep { defined $FILTER->{$_} } @displayFields;

                                                        #  Given the fields to display and the where conditions,
                                                        #         "getData" will return a reference to an array of
                                                        #         hash references of the results.
my @ProductList = @{ getData (\@displayFields, \@whereClause, $DBTABLE) };
my $x = scalar @ProductList; # How many results were returned?

# Set some cookie stuff...
my $path = `dirname $ENV{REQUEST_URI}`; chomp $path; $path .= '/' unless $path eq "/";
my $queryCookie = cookie(-NAME=>$prefscookie,
                        -VALUE=>"$QUERY_STRING",
                        -PATH=>"$path",
                        -EXPIRES=>'+365d');

print header (-cookie=> [ $queryCookie ] );

#       print "<!-- FORM \n\n";                         # Debug code to dump the FORM to a html comment
#       print "I'm catching updates!!!\n\n";
#       foreach $key (sort (keys %FORM))                #       Must be done after the header is written!
#               { print "\t$key:  $FORM{$key}\n"; }
#       print "--> \n\n";
#       
# 
#       print "<!-- ENV \n\n";                          # Debug code to dump the ENV to a html comment
#       foreach $key (sort (keys %ENV))                 #       Must be done after the header is written!
#               { print "\t$key:  $ENV{$key}\n"; }
#       print "--> \n\n";
# 
#       print "\n\n\n\n<!-- $QUERY_STRING --> \n\n\n\n";


#------------------

# Toggle the autoload fields within the table elements 
our ($onClick, $onChange);   # (also used in scanFunctions)
my ($radiobutton, $refreshbutton);
if ($FORM{autoload}) {                                    
        $onClick = "onClick='submit();'";
        $onChange = "onChange='submit();'";
  $radiobutton = $h->div ({ class=>'autoload' },
    ["Autoload Changes: ",
    $h->input ({ type=>radio, name=>'autoload', class=>'accent', value=>1, onClick=>'submit();', checked=>[] }), "On ", 
    $h->input ({ type=>radio, name=>'autoload', class=>'accent', value=>0, onClick=>'submit();' }), "Off ",
    ]);
} else {
  $onClick = "";
        $onChange = "";
  $radiobutton = $h->div ({ class=>'autoload' }, 
    ["Autoload Changes: ",
    $h->input ({ type=>radio, name=>'autoload', class=>'accent', value=>1, onClick=>'submit();' }), "On ", 
    $h->input ({ type=>radio, name=>'autoload', class=>'accent', value=>0, onClick=>'submit();', checked=>[] }), "Off ",
    ]);
  $refreshbutton = $h->input ({ type=>"button", value=>"Apply Changes", onClick=>"submit(); return false;" });
}

         
print start_html (-title => $pageTitle, -style => {'src' => $stylesheet} );

# Print the header

print $h->open ('form', { action=>url, method=>'POST', name=>'Req' });
print $h->div ({ class => "accent pageheader" }, [
  $h->h1 ($pageTitle),
  $h->div ({ class=>"sp0" }, [
    $h->div ({ class=>"spLeft" }, [
      $radiobutton
    ]),
    $h->div ({ class=>"spRight" }, [
      $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
      $refreshbutton
    ]),
  ]),
]);

# Print the Hidden fields' check boxes (if there are any)

my $c = 1;
my @hiddencheckboxes;
my @hiddenrows;
foreach $field (sort { $NAME{$a} cmp $NAME{$b}; } @hideFields) {
  if ($FORM{autoload}) {
    push @hiddencheckboxes, $h->div ({ class=>'rTableCell quarters', nowrap=>[], onClick=>"Req.$field.click();" }, [ $h->input ({ type=>'checkbox', class=>'accent', name=>$field, value=>'true', onClick=>"event.stopPropagation(); submit();" }), $NAME{$field} ]);
  } else {
    push @hiddencheckboxes, $h->div ({ class=>'rTableCell quarters', nowrap=>[], onClick=>"Req.$field.checked=!Req.$field.checked;" }, [ $h->input ({ type=>'checkbox', class=>'accent', name=>$field, value=>'true', onClick=>"event.stopPropagation();" }), $NAME{$field} ]);
  }
  if (! $c % 4) {
    push @hiddenrows, $h->div ({ class=>'rTableRow' }, [ @hiddencheckboxes ]);
    @hiddencheckboxes = [];
    $c++;
  }
}
push @hiddenrows, $h->div ({ class=>'rTableRow' }, [ @hiddencheckboxes ]) unless ! $c % 4;


if (scalar @hideFields) {
  my @topleft;
  push @topleft, $h->div ({ class=>"nowrap" }, "Hidden Columns:");
  push @topleft, $h->div ({ class=>'rTable' }, [ @hiddenrows ]);
  
  print $h->div ({ class=>"sp0" }, [
    $h->div ({ class=>"spLeft"  }, [ @topleft ]),
    $h->div ({ class=>"spRight" }, [
    ])
  ]);
}

# Print the main table...............................................

print $h->open ('div', { class=>'rTable' });

my @tmptitlerow;
foreach $f (@displayFields)     {  # Print the Column headings
  if (inArray ($f, \@staticFields)) {
    push @tmptitlerow, $h->div ({ class=>'rTableHead' }, [ $h->input ({ type=>"hidden", name=>$f, value=>"true" }), $NAME{$f} ]);
  } else {
    if ($FORM{autoload}) {
      push @tmptitlerow, $h->div ({ class=>'rTableHead', onClick=>"Req.$f.click();" }, [ $h->input ({ type=>"checkbox", class=>"accent", name=>$f, value=>"true", checked=>[], onClick=>'event.stopPropagation(); submit();' }), $NAME{$f} ]);
    } else {
      push @tmptitlerow, $h->div ({ class=>'rTableHead', onClick=>"Req.$f.checked=!Req.$f.checked;" }, [ $h->input ({ type=>"checkbox", class=>"accent", name=>$f, value=>"true", checked=>[], onClick=>"event.stopPropagation();" }), $NAME{$f} ]);      
    }
  }
}

my @tmpfilters;
foreach $f (@displayFields) {  # and now the filter boxes
  push @tmpfilters, $h->div ({ class=>'rTableCell filters' }, filter ($f) )
}

print $h->div ({ class=>'rTableHeading' }, [ @tmptitlerow ], [ @tmpfilters ], $h->div ({ class=>"rTableCell" }));

foreach $t (@ProductList)       {               # Unt now we print the things!
  my @tmprow;
        foreach $f (@displayFields)
                {
                  # Look to see if there's a function named modify_<fieldname>() defined for this field
                  if (exists &{"modify_".$f}) {
                    $t->{$f} = &{"modify_".$f} ($t);
                  } 
                  
                  push @tmprow, $h->div ({ class=>'rTableCell' }, $t->{$f});
                }
  print $h->div ({ class=>'rTableRow shaded' }, [ @tmprow ]);
}

print $h->close ('div');

# close things out................................................

print $h->br; # print $h->br;
print $h->div ({ class=>"sp0" }, [
    $h->div ({ class=>"spLeft" }, [
      $h->div ({ class=>"footer" }, [
        "To bookmark, save, or send this exact view, use the ",
        $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
        $h->br,
        "This page was displayed on $now",
        $h->br,
        "Please direct questions, problems, and concerns to noone\@gmail.com"
      ])
    ]),
    $h->div ({ class=>"spRight" }, [
      $h->h5 ("$x Record". ($x == 1 ? "" : "s") ." Displayed")
    ]),
]);

#print $h->br; # print $h->br;
#print $h->h5 ("$x Record(s) Displayed");
#print $h->div ({ class=>"footer" }, [
#  "To bookmark, save, or send this exact view, use the ",
#  $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
#  $h->br,
#  "This page was displayed on $now",
#  $h->br,
#  "Please direct questions, problems, and concerns to noone\@gmail.com"
#]);


print $h->close('form');
print $h->close('html');