Bug 12613: Remove CGI::scrolling_list from koha2marclinks.pl
[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.tt",
39         query           => $input,
40         type            => "intranet",
41         authnotrequired => 0,
42         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
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 $data;
67     my $sth =
68       $dbh->prepare(
69 "select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where kohafield=? AND frameworkcode=''"
70       );
71     $sth->execute( $tablename . "." . $kohafield );
72     my ( $defaulttagfield, $defaulttagsubfield, $defaultliblibrarian ) =
73       $sth->fetchrow;
74
75     for ( my $i = 0 ; $i <= 9 ; $i++ ) {
76         my $sth2 =
77           $dbh->prepare(
78 "select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where tagfield like ? AND frameworkcode=''"
79           );
80         $sth2->execute("$i%");
81         my @marcarray;
82         push @marcarray, " ";
83         while ( my ( $field, $tagsubfield, $liblibrarian ) =
84             $sth2->fetchrow_array )
85         {
86             push @marcarray, "$field $tagsubfield - $liblibrarian";
87         }
88         my $marclist = {
89             values  => \@marcarray,
90             default => "$defaulttagfield $defaulttagsubfield - $defaultliblibrarian",
91         };
92         $template->param( "marclist$i" => $marclist );
93     }
94     $template->param(
95         tablename => $tablename,
96         kohafield => $kohafield
97     );
98
99     # END $OP eq ADD_FORM
100 ################## ADD_VALIDATE ##################################
101     # called by add_form, used to insert/modify data in DB
102 }
103 elsif ( $op eq 'add_validate' ) {
104
105     #----- empty koha field :
106     $dbh->do(
107 "update marc_subfield_structure set kohafield='' where kohafield='$tablename.$kohafield'"
108     );
109
110     #---- reload if not empty
111     my @temp = split / /, $input->param('marc');
112     $dbh->do(
113 "update marc_subfield_structure set kohafield='$tablename.$kohafield' where tagfield='$temp[0]' and tagsubfield='$temp[1]'"
114     );
115     print
116 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=koha2marclinks.pl?tablename=$tablename\"></html>";
117     exit;
118
119     # END $OP eq ADD_VALIDATE
120 ################## DEFAULT ##################################
121 }
122 else {    # DEFAULT
123     my $sth =
124       $dbh->prepare(
125 q|select tagfield,tagsubfield,liblibrarian,kohafield from marc_subfield_structure where kohafield is not NULL and kohafield != ''|
126       );
127     $sth->execute;
128     my %fields;
129     while ( ( my $tagfield, my $tagsubfield, my $liblibrarian, my $kohafield ) =
130         $sth->fetchrow ) {
131         $fields{$kohafield}->{tagfield}     = $tagfield;
132         $fields{$kohafield}->{tagsubfield}  = $tagsubfield;
133         $fields{$kohafield}->{liblibrarian} = $liblibrarian;
134     }
135
136   #XXX: This might not work. Maybe should use a DBI call instead of SHOW COLUMNS
137     my $sth2 = $dbh->prepare("SHOW COLUMNS from $tablename");
138     $sth2->execute;
139
140     my @loop_data = ();
141     while ( ( my $field ) = $sth2->fetchrow_array ) {
142         my %row_data;    # get a fresh hash for the row data
143         $row_data{tagfield} = $fields{ $tablename . "." . $field }->{tagfield};
144         $row_data{tagsubfield} =
145           $fields{ $tablename . "." . $field }->{tagsubfield};
146         $row_data{liblibrarian} =
147           $fields{ $tablename . "." . $field }->{liblibrarian};
148         $row_data{kohafield} = $field;
149         $row_data{edit} =
150 "$script_name?op=add_form&amp;tablename=$tablename&amp;kohafield=$field";
151         push( @loop_data, \%row_data );
152     }
153     my $tablenames = {
154         values  => [ 'biblio', 'biblioitems', 'items' ],
155         default => $tablename,
156     };
157     $template->param(
158         loop      => \@loop_data,
159         tablename => $tablenames,
160     );
161 }    #---- END $OP eq DEFAULT
162 output_html_with_http_headers $input, $cookie, $template->output;