Subversion Repositories CoffeeCatalog

Rev

Blame | 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/:standard escape/;
use scanFunctions;


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

# 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 = escape ($lineItem->{'roaster'});
  return a ({-href=>"/manage_roaster.pl?roaster=$rster"}, $lineItem->{'roaster'});
}

sub modify_url {
  # Make the URL an actual link
  my $lineItem = shift; 
  return 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 @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                                  # Otherwise suppply a default list of columns.
                { @displayFields = @defaultFields; }
}

# let's just make sure the columns are in the right order
@displayFields = sort byfield @displayFields;

# 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";


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

our ($onClick, $onChange);
if ($FORM{autoload})                                      # Toggle the autoload fields within the table elements
        { $onClick = "onClick='submit();'";
         $onChange = "onChange='submit();'"; }
else
        { $onClick = "";
         $onChange = ""; }
         
print start_html (-title => $pageTitle, -style => {'src' => $stylesheet} );
print start_form (-action => url (), -method => "POST", -name => "Req");
$pageTitle = h1 ($pageTitle);

print<<header;
                        <div class="accent pageheader">$pageTitle</div>
                        <br>

<div class=sp0>
  <div class=spLeft>
                <div class=rTable>
header

# Print the Hidden fields' check boxes

my $tc = 1;
foreach $field (sort { $NAME{$a} cmp $NAME{$b}; } @hideFields) {
  if ($tc == 1) { print "<div class=rTableRow>\n"; }
        print "<div class=rTableCell nowrap><b><INPUT type=checkbox name=$field value=true $onClick class=accent>$NAME{$field}</div>\n";
  if ($tc == 4) { print "\t\t\t\t\t\t</div>\n"; $tc = 1; }
  $tc++;
}
print "</div>\n" unless $tc == 1;

if ($FORM{autoload}) {
  $trueChecked = "checked";
  $falseChecked = "";
} else {
  $trueChecked = "";
  $falseChecked = "checked";
}

print<<header3;
    </div>
    <div align=center>
          <b>Autoload: <INPUT type=radio name=autoload value=1 onClick='Req.submit();' $trueChecked class=accent>On  <INPUT type=radio name=autoload value=0 onClick='Req.submit();' $falseChecked class=accent>Off
      <br><br>
    </div>
  </div>

  <div class=spRight>
    <B><input type=button value="Refresh Display" onClick="window.document.Req.submit(); return false;"><br><br>
    Display <A HREF='' onClick="window.document.Req.method = 'GET'; window.document.Req.submit(); return false;">Full URL</a> : <a href=/>[Go HOME]</a>
    <br><br>
  </div>
</div>

<div class=rTable width=100\%>
  <div class=rTableRow>
header3

# Print the Column headings
foreach $f (@displayFields)     {
  if (inArray ($f, \@staticFields)) {
    print "<div class=rTableHead><INPUT type=hidden name=$f value=true>$NAME{$f}</div>\n";    
  } else {
    print "<div class=rTableHead><INPUT type=checkbox name=$f value=true checked $onClick class=accent>$NAME{$f}</div>\n";
  }
}

print "</div><div class=rTableRow>\n";

# and now the filter boxes                                                      
foreach $f (@displayFields) {
  print "\t\t\t\t\t<div class='rTableCell filters'>";
  print filter ($f);
  print "</div>\n";
}

print "</div>\n";
print end_form (), "\n";

my $cw = scalar @displayFields;

print<<header2;
  <div class=rTableRow>
                        <div class=rTableCell></div>
        </div class=rTableRow>
header2


foreach $t (@ProductList)                       # Unt now we print the things!
{
        print "\t\t\t\t<div class='rTableRow shaded'>\n";
        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);
                  } 
                  
                  print "<div class=rTableCell valign=top>".$t->{$f}."&nbsp;</div>\n";
                }
        print "</div>\n";       
}

print<<tail;
                        </div>



        <br><br>
        <FONT size=2><B>$x Record(s) Displayed</font>
        <BR><BR>
        <FONT size=1><B>This page was displayed on $now<BR>
        Please direct questions, problems, and concerns to noone\@gmail.com

        <SCRIPT language="JavaScript">
        <!--

        var ticket_window, severity_window, user_window;
        
        function NewWindow () {
                if ((ticket_window == null) || (ticket_window.closed == true))
                        ticket_window = open(\"\",\"Details\",\"width=650,height=650,menubar,scrollbars,resizable\");
                ticket_window.focus();
                return true;
                }

        function SevWindow () {
                if ((severity_window == null) || (severity_window.closed == true))
                        severity_window = open(\"\",\"SevDetails\",\"width=500,height=300,menubar\");
                severity_window.focus();
                return true;
                }
        
        function UserWindow () {
                if ((user_window == null) || (user_window.closed == true))
                        user_window = open(\"\",\"UserDetails\",\"width=310,height=115,menubar\");
                user_window.focus();
                return true;
                }

        //-->
        </SCRIPT>

tail

print end_html;