4c7e03ebb2182831335dad258a791b204b4d028d
[technostate.git] / cgi / persons.cgi
1 #!/usr/bin/perl -Tw
2 # $Id: persons.cgi,v 1.1 1999-01-27 09:55:05 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;
13 use CGI::Carp qw(fatalsToBrowser);
14 use DBI;
15
16 $data_source = "DBI:mysql:test";
17 $user = "ivan";
18 $password = "";
19
20 $table = "PERSONS";
21 @fields = qw( ID NAME EMAIL AFFILIATION );
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->do( "SELECT * FROM PERSONS" ) or die $dbh->errstr;
31
32   print $cgi->header,
33         $cgi->start_html('Person listing'),
34         $cgi->h1('Person listing'),
35   ;
36
37   unless ( $sth eq '0E0' ) {
38
39     print $cgi->start_table,
40           $cgi->tr(
41             map {
42               $cgi->th($_)
43             } @{$sth->{NAME}}
44           ),
45           map {
46             $cgi->tr(
47               map {
48                 $cgi->td( $_ )
49               } @{ $_ }
50             )
51           } @{ $sth->fetchall_arrayref },
52           $cgi->end_table,
53     ;
54   }
55
56   $cgi->param('magic', 'new_form');
57   print '<P><A HREF="', $cgi->self_url, '">Add new person</A>';
58   print $cgi->end_html;
59
60   exit;
61
62 } elsif ( $cgi->param('magic') eq 'new_form' ) {
63   $cgi->param('ID', 0);
64   $cgi->param('magic', 'process_form');
65   &print_form( $cgi, "Add person" );
66   exit;
67 } elsif ( $cgi->param('magic') eq 'process_form' ) {
68   print $cgi->header,
69         "process form stub"
70   ;
71   exit;
72 }
73
74 sub print_form {
75   my $cgi = shift;
76   my $action = shift;
77   print $cgi->header,
78         $cgi->start_html($action),
79         $cgi->h1($action),
80         $cgi->start_form,
81         $cgi->hidden( -name => 'ID' ),
82         "Name: ", $cgi->textfield( -name => 'NAME' ), "<BR>", 
83         "Email: ", $cgi->textfield( -name => 'EMAIL' ), "<BR>", 
84         "Affiliation: ", $cgi->textfield( -name => 'AFFILIATION' ), "<BR>",
85         $cgi->hidden( -name => 'magic'),
86         $cgi->submit('Submit'),
87         $cgi->end_form,
88         $cgi->end_html;
89   ;
90 }