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