test
[technostate.git] / cgi / sets.cgi
1 #!/usr/bin/perl -Tw
2 # $Id: sets.cgi,v 1.7 1999-07-18 05:08:29 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 { $cgi->th($_) } @columns ),
47             $cgi->th('People')
48           )
49     ;
50
51     my %hash = ();
52     my $hashref = undef;
53     while ( $hashref = $sth->fetchrow_hashref ) {
54       %hash = %{$hashref};
55       print $cgi->tr(
56         ( map { $cgi->td( $hash{$_} ) } @columns ) ),
57         ;
58     }
59     print $cgi->end_table;
60   }
61
62   $cgi->param('magic', 'new_form');
63   print '<P><A HREF="', $cgi->self_url, '">Add new set</A>';
64   print $cgi->end_html;
65
66   exit;
67
68 } elsif ( $cgi->param('magic') eq 'new_form' ) {
69   $cgi->param('SET_ID', 0);
70   $cgi->param('magic', 'process_form');
71   &print_form( $cgi, "Add set" );
72   exit;
73 } elsif ( $cgi->param('magic') eq 'process_form' ) {
74
75   $cgi->param('FILESIZE', 0);
76   $cgi->param('DOWNLOADS', 0);
77
78   my $field;
79   foreach $field ( @fields ) {
80     if ( $cgi->param( $field ) ) {
81       $cgi->param( $field ) =~ /^(.*)$/;
82       my $param = $1 || 0;
83       if ( (DBI::looks_like_number($param))[0] ) {
84         $cgi->param( $field, $param );
85       } else {
86         $cgi->param( $field, $dbh->quote($param) );
87       }
88     }
89   }
90   
91   my $statement = "INSERT INTO $table ( ".
92                   join(', ', @fields ).
93                   ' ) VALUES ( '.
94                   join( ', ', map { $cgi->param($_) } @fields ).
95                   ' )'
96   ;
97   warn $statement;
98   my $sth = $dbh->prepare($statement)
99     or die $dbh->errstr;
100   my $rv = $sth->execute;
101   die $sth->errstr unless $rv;
102
103   my $set_id = $sth->{'insertid'};
104   warn $set_id;
105
106   $dbh->do( "DELETE FROM PERSONS_SETS WHERE ( SET_ID = $set_id )" )
107     or die $dbh->errstr;
108
109   my $person_id;
110   foreach $person_id ( $cgi->param('PERSON_ID') ) {
111     $dbh->do(
112       "INSERT INTO PERSONS_SETS ( PERSON_ID, SET_ID ) ".
113       "VALUES ( $person_id, $set_id )"
114     ) or die $dbh->errstr;
115   }
116   
117   #my $rv = $sth->execute;
118   #die $sth->errstr unless $rv;
119
120   my $url = $cgi->url;
121   $url =~ s/^\/[\/]+$//;
122   print $cgi->redirect($url);
123
124 }
125
126 sub print_form {
127   my $cgi = shift;
128   my $action = shift;
129
130   my %persons = map { @{$_}; }
131          @{$dbh->selectall_arrayref( "SELECT PERSON_ID, NAME FROM PERSONS" )};
132   print $cgi->header,
133         $cgi->start_html($action),
134         $cgi->h1($action),
135         $cgi->start_form,
136         $cgi->hidden( -name => 'SET_ID' ),
137         "Start Time: ", $cgi->textfield( -name => 'SETSTART' ), "<BR>", 
138         "Length: ", $cgi->textfield( -name => 'DURATION' ), "<BR>",
139         "Filename: ", $cgi->textfield( -name => 'FILENAME' ), "<BR>",
140         "Short Description: ", $cgi->textarea( -name => 'DESCRIPTION', -cols => '45', -rows => '2' ), "<BR>",
141         "Long Description: ", $cgi->textarea( -name => 'INFO', -cols => '45', -rows => '5' ), "<BR>",
142         "Keywords: ", $cgi->textfield( -name => 'KEYWORDS' ), "<BR>",
143         "People: ", $cgi->scrolling_list(
144           -name => "PERSON_ID",
145           '-values' => [ sort { $persons{$a} cmp $persons{$b} } keys(%persons) ],
146           '-labels' => \%persons,
147           '-multiple' => 'true',
148         ),
149         "Show?: ", $cgi->textfield( -name => 'SHOW_ID' ), "<BR>",
150         $cgi->hidden( -name => 'magic'),
151         $cgi->submit('Submit'),
152         $cgi->end_form,
153         $cgi->end_html;
154   ;
155 }