test
[technostate.git] / cgi / persons.cgi
index e062124..e4eb179 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -Tw
-# $Id: persons.cgi,v 1.3 1999-04-22 04:27:47 ivan Exp $
+# $Id: persons.cgi,v 1.9 1999-04-22 06:24:57 ivan Exp $
 # Copyright (c) 1999 Ivan Kohler.  All rights reserved.
 # This program is free software; you can redistribute it and/or modify it under
 # the same terms as perl itself
@@ -9,7 +9,7 @@ use vars qw ( $data_source $user $password $table @fields
               $cgi $dbh
             );
 use subs qw( print_form );
-use CGI;
+use CGI qw(tr th td);
 use CGI::Carp qw(fatalsToBrowser);
 use DBI;
 
@@ -18,7 +18,7 @@ $user = "agent";
 $password = "t3chno";
 
 $table = "PERSONS";
-@fields = qw( PERSON_ID NAME EMAIL AFFILIATION );
+@fields = qw( PERSON_ID NAME EMAIL AFFILIATION HOMEPAGE );
 
 $cgi = new CGI;
 
@@ -27,30 +27,42 @@ $dbh = DBI->connect( $data_source, $user, $password )
 
 unless ( $cgi->param('magic') ) { #first time through
 
-  my $sth = $dbh->do( "SELECT * FROM PERSONS" ) or die $dbh->errstr;
+  my $sth = $dbh->prepare( "SELECT * FROM $table" )
+    or die $dbh->errstr;
+  my $rv = $sth->execute;
+  die $sth->errstr unless $rv;
 
-  print $cgi->header,
+  print $cgi->header( '-expires' => 'now' ),
         $cgi->start_html('Person listing'),
         $cgi->h1('Person listing'),
   ;
 
   unless ( $sth eq '0E0' ) {
 
+    my @columns = @{ $sth->{'NAME'} };
+
     print $cgi->start_table,
           $cgi->tr(
             map {
               $cgi->th($_)
-            } @{$sth->{NAME}}
-          ),
-          map {
-            $cgi->tr(
-              map {
-                $cgi->td( $_ )
-              } @{ $_ }
-            )
-          } @{ $sth->fetchall_arrayref },
-          $cgi->end_table,
+            } @columns
+          )
     ;
+
+    my %hash = ();
+    my $hashref = undef;
+    while ( $hashref = $sth->fetchrow_hashref ) {
+      %hash = %{$hashref};
+      $hash{'EMAIL'} = '<A HREF="mailto:'. $hash{'EMAIL'}. '">'.
+                          $hash{'EMAIL'}. "</A>";
+      $hash{'HOMEPAGE'} = 'http://'. $hash{'HOMEPAGE'}
+        unless $hash{'HOMEPAGE'} =~ /^http\:\/\//;
+      $hash{'HOMEPAGE'} = '<A HREF="'. $hash{'HOMEPAGE'}. '">'.
+                          $hash{'HOMEPAGE'}. "</A>";
+      print $cgi->tr( map { $cgi->td( $hash{$_} ) } @columns );
+    }
+    print $cgi->end_table;
+
   }
 
   $cgi->param('magic', 'new_form');
@@ -65,13 +77,23 @@ unless ( $cgi->param('magic') ) { #first time through
   &print_form( $cgi, "Add person" );
   exit;
 } elsif ( $cgi->param('magic') eq 'process_form' ) {
-  foreach $field ( @fields ) {
-    if $cgi->param( $field ) 
 
-  my $statement = 'INSERT INTO PERSONS ( ', 
-                  join(', ', @fields ),
-                  ' ) VALUES ( ',
-                  join( ', ', map { $cgi->param($_) } @fields ), 
+  my $field;
+  foreach $field ( @fields ) {
+    if ( $cgi->param( $field ) ) {
+      $cgi->param( $field ) =~ /^(.*)$/;
+      my $param = $1 || 0;
+      if ( (DBI::looks_like_number($param))[0] ) {
+        $cgi->param( $field, $param );
+      } else {
+        $cgi->param( $field, $dbh->quote($param) );
+      }
+    }
+  }
+  my $statement = "INSERT INTO $table ( ".
+                  join(', ', @fields ).
+                  ' ) VALUES ( '.
+                  join( ', ', map { $cgi->param($_) } @fields ).
                   ' )'
   ;
   my $sth = $dbh->prepare($statement)
@@ -79,9 +101,9 @@ unless ( $cgi->param('magic') ) { #first time through
   my $rv = $sth->execute;
   die $sth->errstr unless $rv;
 
-  print $cgi->header, 
-        "person added?"
-  ;
+  my $url = $cgi->url;
+  $url =~ s/^\/[\/]+$//;
+  print $cgi->redirect($url);
 }
 
 sub print_form {
@@ -95,6 +117,7 @@ sub print_form {
         "Name: ", $cgi->textfield( -name => 'NAME' ), "<BR>", 
         "Email: ", $cgi->textfield( -name => 'EMAIL' ), "<BR>", 
         "Affiliation: ", $cgi->textfield( -name => 'AFFILIATION' ), "<BR>",
+        "Homepage: ", $cgi->textfield( -name => 'HOMEPAGE' ), "<BR>",
         $cgi->hidden( -name => 'magic'),
         $cgi->submit('Submit'),
         $cgi->end_form,