Fix FSF address in directory admin/
[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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use C4::Output;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use C4::Biblio;
27
28
29 my $input       = new CGI;
30 my $tablename   = $input->param('tablename');
31 $tablename      = "biblio" unless ($tablename);
32 my $kohafield   = $input->param('kohafield');
33 my $op          = $input->param('op');
34 my $script_name = 'koha2marclinks.pl';
35
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
37     {
38         template_name   => "admin/koha2marclinks.tmpl",
39         query           => $input,
40         type            => "intranet",
41         authnotrequired => 0,
42         flagsrequired   => { parameters => 1 },
43         debug           => 1,
44     }
45 );
46
47 if ($op) {
48     $template->param(
49         script_name => $script_name,
50         $op         => 1
51     );    # we show only the TMPL_VAR names $op
52 }
53 else {
54     $template->param(
55         script_name => $script_name,
56         else        => 1
57     );    # we show only the TMPL_VAR names $op
58     $op = q{};
59 }
60
61 my $dbh = C4::Context->dbh;
62
63 ################## ADD_FORM ##################################
64 # called by default. Used to create form to add or  modify a record
65 if ( $op eq 'add_form' ) {
66     my $sth = $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 = $dbh->prepare(
75 "select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where tagfield like ?"
76         );
77         $sth2->execute("$i%");
78         my @marcarray;
79         push @marcarray, " ";
80         while ( my ( $field, $tagsubfield, $liblibrarian ) =
81             $sth2->fetchrow_array )
82         {
83             push @marcarray, "$field $tagsubfield - $liblibrarian";
84         }
85         my $marclist = CGI::scrolling_list(
86             -name   => 'marc',
87             -values => \@marcarray,
88             -default =>
89               "$defaulttagfield $defaulttagsubfield - $defaultliblibrarian",
90             -size     => 1,
91             -multiple => 0,
92         );
93         $template->param( "marclist$i" => $marclist );
94     }
95     $template->param(
96         tablename => $tablename,
97         kohafield => $kohafield
98     );
99
100     # END $OP eq ADD_FORM
101 ################## ADD_VALIDATE ##################################
102     # called by add_form, used to insert/modify data in DB
103 }
104 elsif ( $op eq 'add_validate' ) {
105
106     #----- empty koha field :
107     $dbh->do(
108 "update marc_subfield_structure set kohafield='' where kohafield='$tablename.$kohafield'"
109     );
110
111     #---- reload if not empty
112     my @temp = split / /, $input->param('marc');
113     $dbh->do(
114 "update marc_subfield_structure set kohafield='$tablename.$kohafield' where tagfield='$temp[0]' and tagsubfield='$temp[1]'"
115     );
116     print
117 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=koha2marclinks.pl?tablename=$tablename\"></html>";
118     exit;
119
120     # END $OP eq ADD_VALIDATE
121 ################## DEFAULT ##################################
122 }
123 else {    # DEFAULT
124     my $sth =
125       $dbh->prepare(
126 q|select tagfield,tagsubfield,liblibrarian,kohafield from marc_subfield_structure where kohafield is not NULL and kohafield != ''|
127       );
128     $sth->execute;
129     my %fields;
130     while ( ( my $tagfield, my $tagsubfield, my $liblibrarian, my $kohafield ) =
131         $sth->fetchrow ) {
132         $fields{$kohafield}->{tagfield}     = $tagfield;
133         $fields{$kohafield}->{tagsubfield}  = $tagsubfield;
134         $fields{$kohafield}->{liblibrarian} = $liblibrarian;
135     }
136
137   #XXX: This might not work. Maybe should use a DBI call instead of SHOW COLUMNS
138     my $sth2 = $dbh->prepare("SHOW COLUMNS from $tablename");
139     $sth2->execute;
140
141     my @loop_data = ();
142     while ( ( my $field ) = $sth2->fetchrow_array ) {
143         my %row_data;    # get a fresh hash for the row data
144         $row_data{tagfield} = $fields{ $tablename . "." . $field }->{tagfield};
145         $row_data{tagsubfield} =
146           $fields{ $tablename . "." . $field }->{tagsubfield};
147         $row_data{liblibrarian} =
148           $fields{ $tablename . "." . $field }->{liblibrarian};
149         $row_data{kohafield} = $field;
150         $row_data{edit} =
151 "$script_name?op=add_form&amp;tablename=$tablename&amp;kohafield=$field";
152         push( @loop_data, \%row_data );
153     }
154     $template->param(
155         loop      => \@loop_data,
156         tablename => CGI::scrolling_list(
157             -name   => 'tablename',
158             -values => [
159                 'biblio',
160                 'biblioitems',
161                 'items',
162             ],
163             -default  => $tablename,
164             -size     => 1,
165             -multiple => 0
166         )
167     );
168 }    #---- END $OP eq DEFAULT
169 output_html_with_http_headers $input, $cookie, $template->output;