test
[technostate.git] / cgi / studios.cgi
1 #!/usr/bin/perl -Tw
2 # $Id: studios.cgi,v 1.1 1999-04-23 06:34:07 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 = "STUDIOS";
21 @fields = qw( STUDIO_ID NAME ISLIVE );
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('Studio listing'),
37         $cgi->h1('Studio 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
62   $cgi->param('magic', 'new_form');
63   print '<P><A HREF="', $cgi->self_url, '">Add new studio</A>';
64   print $cgi->end_html;
65
66   exit;
67
68 } elsif ( $cgi->param('magic') eq 'new_form' ) {
69   $cgi->param('STUDIO_ID', 0);
70   $cgi->param('magic', 'process_form');
71   &print_form( $cgi, "Add studio" );
72   exit;
73 } elsif ( $cgi->param('magic') eq 'process_form' ) {
74
75   my $field;
76   foreach $field ( @fields ) {
77     if ( $cgi->param( $field ) ) {
78       $cgi->param( $field ) =~ /^(.*)$/;
79       my $param = $1 || 0;
80       if ( (DBI::looks_like_number($param))[0] ) {
81         $cgi->param( $field, $param );
82       } else {
83         $cgi->param( $field, $dbh->quote($param) );
84       }
85     }
86   }
87   my $statement = "INSERT INTO $table ( ".
88                   join(', ', @fields ).
89                   ' ) VALUES ( '.
90                   join( ', ', map { $cgi->param($_) } @fields ).
91                   ' )'
92   ;
93   my $sth = $dbh->prepare($statement)
94     or die $dbh->errstr;
95   my $rv = $sth->execute;
96   die $sth->errstr unless $rv;
97
98   my $url = $cgi->url;
99   $url =~ s/^\/[\/]+$//;
100   print $cgi->redirect($url);
101 }
102
103 sub print_form {
104   my $cgi = shift;
105   my $action = shift;
106   print $cgi->header,
107         $cgi->start_html($action),
108         $cgi->h1($action),
109         $cgi->start_form,
110         $cgi->hidden( -name => 'STUDIO_ID' ),
111         "NAME: ", $cgi->textfield( -name => 'NAME' ), "<BR>", 
112         "ISLIVE", $cgi->textfield( -name => 'ISLIVE' ), "<BR>", 
113         $cgi->hidden( -name => 'magic'),
114         $cgi->submit('Submit'),
115         $cgi->end_form,
116         $cgi->end_html;
117   ;
118 }