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