bah
[technostate.git] / cgi / sets.cgi
1 #!/usr/bin/perl -Tw
2 # $Id: sets.cgi,v 1.4 1999-04-22 06:06:16 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('Set listing'),
37         $cgi->h1('Set 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 set</A>';
63   print $cgi->end_html;
64
65   exit;
66
67 } elsif ( $cgi->param('magic') eq 'new_form' ) {
68   $cgi->param('SET_ID', 0);
69   $cgi->param('magic', 'process_form');
70   &print_form( $cgi, "Add set" );
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   $cgi->param('FILESIZE', 0);
82   $cgi->param('DOWNLOADS', 0);
83   
84   my $statement = "INSERT INTO $table ( ".
85                   join(', ', @fields ).
86                   ' ) VALUES ( '.
87                   join( ', ', map { $dbh->quote($cgi->param($_)) } @fields ).
88                   ' )'
89   ;
90   my $sth = $dbh->prepare($statement)
91     or die $dbh->errstr;
92   my $rv = $sth->execute;
93   die $sth->errstr unless $rv;
94
95   my $url = $cgi->url;
96   $url =~ s/^\/[\/]+$//;
97   print $cgi->redirect($url);
98
99 }
100
101 sub print_form {
102   my $cgi = shift;
103   my $action = shift;
104   print $cgi->header,
105         $cgi->start_html($action),
106         $cgi->h1($action),
107         $cgi->start_form,
108         $cgi->hidden( -name => 'ID' ),
109         "Start Time: ", $cgi->textfield( -name => 'SETSTART' ), "<BR>", 
110         "Length: ", $cgi->textfield( -name => 'DURATION' ), "<BR>",
111         "Filename: ", $cgi->textfield( -name => 'FILENAME' ), "<BR>",
112         "Short Description: ", $cgi->textarea( -name => 'DESCRIPTION', -cols => '45', -rows => '2' ), "<BR>",
113         "Long Description: ", $cgi->textarea( -name => 'INFO', -cols => '45', -rows => '5' ), "<BR>",
114         "Keywords: ", $cgi->textfield( -name => 'KEYWORDS' ), "<BR>",
115         "Show?: ", $cgi->textfield( -name => 'SHOW_ID' ), "<BR>",
116         $cgi->hidden( -name => 'magic'),
117         $cgi->submit('Submit'),
118         $cgi->end_form,
119         $cgi->end_html;
120   ;
121 }