d01191329d7ff4313abeb218ad9a44c04c046f08
[technostate.git] / cgi / persons.cgi
1 #!/usr/bin/perl -Tw
2 # $Id: persons.cgi,v 1.7 1999-04-22 05:58:06 ivan Exp $
3 # Copyright (c) 1999 Ivan Kohler.  All rights reserved.
4 # This program is free software; you can redistribute it and/or modify it under
5 # the same terms as perl itself
6
7 use strict;
8 use vars qw ( $data_source $user $password $table @fields
9               $cgi $dbh
10             );
11 use subs qw( print_form );
12 use CGI qw(tr th td);
13 use CGI::Carp qw(fatalsToBrowser);
14 use DBI;
15
16 $data_source = "DBI:mysql:technostate";
17 $user = "agent";
18 $password = "t3chno";
19
20 $table = "PERSONS";
21 @fields = qw( PERSON_ID NAME EMAIL AFFILIATION HOMEPAGE );
22
23 $cgi = new CGI;
24
25 $dbh = DBI->connect( $data_source, $user, $password )
26   or die "Cannot connect: ". $DBI::errstr;
27
28 unless ( $cgi->param('magic') ) { #first time through
29
30   my $sth = $dbh->prepare( "SELECT * FROM $table" )
31     or die $dbh->errstr;
32   my $rv = $sth->execute;
33   die $sth->errstr unless $rv;
34
35   print $cgi->header( '-expires' => 'now' ),
36         $cgi->start_html('Person listing'),
37         $cgi->h1('Person listing'),
38   ;
39
40   unless ( $sth eq '0E0' ) {
41
42     my @columns = @{ $sth->{'NAME'} };
43
44     print $cgi->start_table,
45           $cgi->tr(
46             map {
47               $cgi->th($_)
48             } @columns
49           )
50     ;
51
52     my %hash = ();
53     my $hashref = undef;
54     while ( $hashref = $sth->fetchrow_hashref ) {
55       %hash = %{$hashref};
56       $hash{'EMAIL'} = '<A HREF="mailto:'. $hash{'EMAIL'}. '">'.
57                           $hash{'EMAIL'}. "</A>";
58       $hash{'HOMEPAGE'} = 'http://'. $hash{'HOMEPAGE'}
59         unless $hash{'HOMEPAGE'} =~ /^http\:\/\//;
60       $hash{'HOMEPAGE'} = '<A HREF="'. $hash{'HOMEPAGE'}. '">'.
61                           $hash{'HOMEPAGE'}. "</A>";
62       print $cgi->tr( map { $cgi->td( $hash{$_} ) } @columns );
63     }
64     print $cgi->end_table;
65
66   }
67
68   $cgi->param('magic', 'new_form');
69   print '<P><A HREF="', $cgi->self_url, '">Add new person</A>';
70   print $cgi->end_html;
71
72   exit;
73
74 } elsif ( $cgi->param('magic') eq 'new_form' ) {
75   $cgi->param('PERSON_ID', 0);
76   $cgi->param('magic', 'process_form');
77   &print_form( $cgi, "Add person" );
78   exit;
79 } elsif ( $cgi->param('magic') eq 'process_form' ) {
80
81   my $field;
82   foreach $field ( @fields ) {
83     if ( $cgi->param( $field ) ) {
84       $cgi->param( $field ) =~ /^(.*)$/;
85       $cgi->param( $field, $1);
86     }
87   }
88   my $statement = "INSERT INTO $table ( ".
89                   join(', ', @fields ).
90                   ' ) VALUES ( '.
91                   join( ', ', map { $dbh->quote($cgi->param($_)) } @fields ).
92                   ' )'
93   ;
94   my $sth = $dbh->prepare($statement)
95     or die $dbh->errstr;
96   my $rv = $sth->execute;
97   die $sth->errstr unless $rv;
98
99   my $url = $cgi->url;
100   $url =~ s/^\/[\/]+$//;
101   print $cgi->redirect($url);
102 }
103
104 sub print_form {
105   my $cgi = shift;
106   my $action = shift;
107   print $cgi->header( '-expires' => 'now' ),
108         $cgi->start_html($action),
109         $cgi->h1($action),
110         $cgi->start_form,
111         $cgi->hidden( -name => 'PERSON_ID' ),
112         "Name: ", $cgi->textfield( -name => 'NAME' ), "<BR>", 
113         "Email: ", $cgi->textfield( -name => 'EMAIL' ), "<BR>", 
114         "Affiliation: ", $cgi->textfield( -name => 'AFFILIATION' ), "<BR>",
115         "Homepage: ", $cgi->textfield( -name => 'HOMEPAGE' ), "<BR>",
116         $cgi->hidden( -name => 'magic'),
117         $cgi->submit('Submit'),
118         $cgi->end_form,
119         $cgi->end_html;
120   ;
121 }