update sets
[technostate.git] / cgi / sets.cgi
1 #!/usr/bin/perl -Tw
2 # $Id: sets.cgi,v 1.2 1999-04-22 05:54:53 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 = "SETS";
21 @fields = qw( SET_ID SETSTART DURATION FILENAME FILESIZE DESCRIPTION INFO KEYWORDS DOWNLOADS SHOW_ID );
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       print $cgi->tr( map { $cgi->td( $hash{$_} ) } @columns );
57     }
58     print $cgi->end_table;
59   }
60
61   $cgi->param('magic', 'new_form');
62   print '<P><A HREF="', $cgi->self_url, '">Add new person</A>';
63   print $cgi->end_html;
64
65   exit;
66
67 } elsif ( $cgi->param('magic') eq 'new_form' ) {
68   $cgi->param('PERSON_ID', 0);
69   $cgi->param('magic', 'process_form');
70   &print_form( $cgi, "Add person" );
71   exit;
72 } elsif ( $cgi->param('magic') eq 'process_form' ) {
73
74   my $field;
75   foreach $field ( @fields ) {
76     if ( $cgi->param( $field ) ) {
77       $cgi->param( $field ) =~ /^(.*)$/;
78       $cgi->param( $field, $1);
79     }
80   }
81   my $statement = 'INSERT INTO PERSONS ( '.
82                   join(', ', @fields ).
83                   ' ) VALUES ( '.
84                   join( ', ', map { $dbh->quote($cgi->param($_)) } @fields ).
85                   ' )'
86   ;
87   my $sth = $dbh->prepare($statement)
88     or die $dbh->errstr;
89   my $rv = $sth->execute;
90   die $sth->errstr unless $rv;
91
92   my $url = $cgi->url;
93   $url =~ s/^\/[\/]+$//;
94   print $cgi->redirect($url);
95
96 }
97
98 sub print_form {
99   my $cgi = shift;
100   my $action = shift;
101   print $cgi->header( '-expires' => 'now' ),
102         $cgi->start_html($action),
103         $cgi->h1($action),
104         $cgi->start_form,
105         $cgi->hidden( -name => 'ID' ),
106         "Start Time: ", $cgi->textfield( -name => 'SETSTART' ), "<BR>", 
107         "Length: ", $cgi->textfield( -name => 'DURATION' ), "<BR>",
108         "Filename: ", $cgi->textfield( -name => 'FILENAME' ), "<BR>",
109         "Short Description: ", $cgi->textarea( -name => 'DESCRIPTION', -cols => '45', -rows => '2' ), "<BR>",
110         "Long Description: ", $cgi->textarea( -name => 'INFO', -cols => '45', -rows => '5' ), "<BR>",
111         "Keywords: ", $cgi->textfield( -name => 'KEYWORDS' ), "<BR>",
112         "Show?: ", $cgi->textfield( -name => 'SHOW_ID' ), "<BR>",
113         $cgi->hidden( -name => 'magic'),
114         $cgi->submit('Submit'),
115         $cgi->end_form,
116         $cgi->end_html;
117   ;
118 }