script to edit the media type table.
[koha.git] / admin / koha2marclinks.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use C4::Output;
23 use C4::Interface::CGI::Output;
24 use C4::Auth;
25 use CGI;
26 use C4::Search;
27 use C4::Context;
28 use C4::Biblio;
29 use HTML::Template;
30
31 my $input = new CGI;
32 my $tablename=$input->param('tablename');
33 $tablename="biblio" unless ($tablename);
34 my $kohafield = $input->param('kohafield');
35 my $op=$input->param('op');
36 my $script_name = 'koha2marclinks.pl';
37
38 my ($template, $borrowernumber, $cookie)
39     = get_template_and_user({template_name => "parameters/koha2marclinks.tmpl",
40                              query => $input,
41                              type => "intranet",
42                              authnotrequired => 0,
43                              flagsrequired => {parameters => 1},
44                              debug => 1,
45                              });
46
47 if ($op) {
48 $template->param(script_name => $script_name,
49                                                 $op              => 1); # we show only the TMPL_VAR names $op
50 } else {
51 $template->param(script_name => $script_name,
52                                                 else              => 1); # we show only the TMPL_VAR names $op
53 }
54
55 my $dbh = C4::Context->dbh;
56
57 ################## ADD_FORM ##################################
58 # called by default. Used to create form to add or  modify a record
59 if ($op eq 'add_form') {
60         my $data;
61         my $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where kohafield=?");
62         $sth->execute($tablename.".".$kohafield);
63         my ($defaulttagfield, $defaulttagsubfield,$defaultliblibrarian) = $sth->fetchrow;
64
65         for (my $i=0;$i<=9;$i++) {
66                 my $sth2=$dbh->prepare("select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where tagfield like ?");
67                 $sth2->execute("$i%");
68                 my @marcarray;
69                 push @marcarray," ";
70                 while (my ($field, $tagsubfield, $liblibrarian) = $sth2->fetchrow_array) {
71                         push @marcarray, "$field $tagsubfield - $liblibrarian";
72                 }
73                 my $marclist = CGI::scrolling_list(-name=>"marc",
74                                                 -values=> \@marcarray,
75                                                 -default=>"$defaulttagfield $defaulttagsubfield - $defaultliblibrarian",
76                                                 -size=>1,
77                                                 -multiple=>0,
78                                                 );
79                 $template->param("marclist$i" => $marclist);
80         }
81         $template->param(       tablename => $tablename,
82                                                         kohafield => $kohafield);
83
84                                                                                                         # END $OP eq ADD_FORM
85 ################## ADD_VALIDATE ##################################
86 # called by add_form, used to insert/modify data in DB
87 } elsif ($op eq 'add_validate') {
88         #----- empty koha field :
89         $dbh->do("update marc_subfield_structure set kohafield='' where kohafield='$tablename.$kohafield'");
90         #---- reload if not empty
91         my @temp = split / /,$input->param('marc');
92         $dbh->do("update marc_subfield_structure set kohafield='$tablename.$kohafield' where tagfield='$temp[0]' and tagsubfield='$temp[1]'");
93         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=koha2marclinks.pl?tablename=$tablename\"></html>";
94         exit;
95
96                                                                                                         # END $OP eq ADD_VALIDATE
97 ################## DEFAULT ##################################
98 } else { # DEFAULT
99         my $env;
100         my $sth=$dbh->prepare("Select tagfield,tagsubfield,liblibrarian,kohafield from marc_subfield_structure");
101         $sth->execute;
102         my %fields;
103         while ((my $tagfield,my $tagsubfield,my $liblibrarian,my $kohafield) = $sth->fetchrow) {
104                 $fields{$kohafield}->{tagfield} = $tagfield;
105                 $fields{$kohafield}->{tagsubfield} = $tagsubfield;
106                 $fields{$kohafield}->{liblibrarian} = $liblibrarian;
107         }
108         #XXX: This might not work. Maybe should use a DBI call instead of SHOW COLUMNS
109         my $sth2=$dbh->prepare("SHOW COLUMNS from $tablename");
110         $sth2->execute;
111
112         my $toggle="white";
113         my @loop_data = ();
114         while ((my $field) = $sth2->fetchrow_array) {
115                 if ($toggle eq 'white'){
116                         $toggle="#ffffcc";
117                 } else {
118                         $toggle="white";
119                 }
120                 my %row_data;  # get a fresh hash for the row data
121                 $row_data{tagfield} = $fields{$tablename.".".$field}->{tagfield};
122                 $row_data{tagsubfield} = $fields{$tablename.".".$field}->{tagsubfield};
123                 $row_data{liblibrarian} = $fields{$tablename.".".$field}->{liblibrarian};
124                 $row_data{kohafield} = $field;
125                 $row_data{edit} = "$script_name?op=add_form&amp;tablename=$tablename&amp;kohafield=$field";
126                 $row_data{bgcolor} = $toggle;
127                 push(@loop_data,\%row_data);
128         }
129         $template->param(loop => \@loop_data,
130                                                         tablename => CGI::scrolling_list(-name=>'tablename',
131                                                                                                                                         -values=>['biblio','biblioitems','items','bibliosubject','bibliosubtitle','additionalauthors'],
132                                                                                                                                         -default=>$tablename,
133                                                                                                                                         -size=>1,
134                                                                                                                                         -multiple=>0
135                                                                                                                                         )
136                                                         );
137 } #---- END $OP eq DEFAULT
138
139 output_html_with_http_headers $input, $cookie, $template->output;