removing all useless %env / $env
[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::Interface::CGI::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 }
59
60 my $dbh = C4::Context->dbh;
61
62 ################## ADD_FORM ##################################
63 # called by default. Used to create form to add or  modify a record
64 if ( $op eq 'add_form' ) {
65     my $data;
66     my $sth =
67       $dbh->prepare(
68 "select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where kohafield=?"
69       );
70     $sth->execute( $tablename . "." . $kohafield );
71     my ( $defaulttagfield, $defaulttagsubfield, $defaultliblibrarian ) =
72       $sth->fetchrow;
73
74     for ( my $i = 0 ; $i <= 9 ; $i++ ) {
75         my $sth2 =
76           $dbh->prepare(
77 "select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where tagfield like ?"
78           );
79         $sth2->execute("$i%");
80         my @marcarray;
81         push @marcarray, " ";
82         while ( my ( $field, $tagsubfield, $liblibrarian ) =
83             $sth2->fetchrow_array )
84         {
85             push @marcarray, "$field $tagsubfield - $liblibrarian";
86         }
87         my $marclist = CGI::scrolling_list(
88             -name    => "marc",
89             -values  => \@marcarray,
90             -default =>
91               "$defaulttagfield $defaulttagsubfield - $defaultliblibrarian",
92             -size     => 1,
93             -tabindex => '',
94             -multiple => 0,
95         );
96         $template->param( "marclist$i" => $marclist );
97     }
98     $template->param(
99         tablename => $tablename,
100         kohafield => $kohafield
101     );
102
103     # END $OP eq ADD_FORM
104 ################## ADD_VALIDATE ##################################
105     # called by add_form, used to insert/modify data in DB
106 }
107 elsif ( $op eq 'add_validate' ) {
108
109     #----- empty koha field :
110     $dbh->do(
111 "update marc_subfield_structure set kohafield='' where kohafield='$tablename.$kohafield'"
112     );
113
114     #---- reload if not empty
115     my @temp = split / /, $input->param('marc');
116     $dbh->do(
117 "update marc_subfield_structure set kohafield='$tablename.$kohafield' where tagfield='$temp[0]' and tagsubfield='$temp[1]'"
118     );
119     print
120 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=koha2marclinks.pl?tablename=$tablename\"></html>";
121     exit;
122
123     # END $OP eq ADD_VALIDATE
124 ################## DEFAULT ##################################
125 }
126 else {    # DEFAULT
127     my $sth =
128       $dbh->prepare(
129 "Select tagfield,tagsubfield,liblibrarian,kohafield from marc_subfield_structure"
130       );
131     $sth->execute;
132     my %fields;
133     while ( ( my $tagfield, my $tagsubfield, my $liblibrarian, my $kohafield ) =
134         $sth->fetchrow )
135     {
136         $fields{$kohafield}->{tagfield}     = $tagfield;
137         $fields{$kohafield}->{tagsubfield}  = $tagsubfield;
138         $fields{$kohafield}->{liblibrarian} = $liblibrarian;
139     }
140
141   #XXX: This might not work. Maybe should use a DBI call instead of SHOW COLUMNS
142     my $sth2 = $dbh->prepare("SHOW COLUMNS from $tablename");
143     $sth2->execute;
144
145     my $toggle    = "white";
146     my @loop_data = ();
147     while ( ( my $field ) = $sth2->fetchrow_array ) {
148         if ( $toggle eq 'white' ) {
149             $toggle = "#ffffcc";
150         }
151         else {
152             $toggle = "white";
153         }
154         my %row_data;    # get a fresh hash for the row data
155         $row_data{tagfield} = $fields{ $tablename . "." . $field }->{tagfield};
156         $row_data{tagsubfield} =
157           $fields{ $tablename . "." . $field }->{tagsubfield};
158         $row_data{liblibrarian} =
159           $fields{ $tablename . "." . $field }->{liblibrarian};
160         $row_data{kohafield} = $field;
161         $row_data{edit}      =
162 "$script_name?op=add_form&amp;tablename=$tablename&amp;kohafield=$field";
163         $row_data{bgcolor} = $toggle;
164         push( @loop_data, \%row_data );
165     }
166     $template->param(
167         loop      => \@loop_data,
168         tablename => CGI::scrolling_list(
169             -name   => 'tablename',
170             -values => [
171                 'biblio',
172                 'biblioitems',
173                 'items',
174             ],
175             -default  => $tablename,
176             -size     => 1,
177             -tabindex => '',
178             -multiple => 0
179         )
180     );
181 }    #---- END $OP eq DEFAULT
182 $template->param(
183     intranetcolorstylesheet =>
184       C4::Context->preference("intranetcolorstylesheet"),
185     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
186     IntranetNav        => C4::Context->preference("IntranetNav"),
187 );
188 output_html_with_http_headers $input, $cookie, $template->output;