adding authentification with Auth.pm
[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::Auth;
24 use CGI;
25 use C4::Search;
26 use C4::Context;
27 use C4::Biblio;
28 use HTML::Template;
29
30 my $input = new CGI;
31 my $tablename=$input->param('tablename');
32 $tablename="biblio" unless ($tablename);
33 my $kohafield = $input->param('kohafield');
34 my $op=$input->param('op');
35 my $script_name = 'koha2marclinks.pl';
36
37 my ($template, $borrowernumber, $cookie)
38     = get_template_and_user({template_name => "parameters/koha2marclinks.tmpl",
39                              query => $input,
40                              type => "intranet",
41                              authnotrequired => 0,
42                              flagsrequired => {parameters => 1},
43                              debug => 1,
44                              });
45
46 if ($op) {
47 $template->param(script_name => $script_name,
48                                                 $op              => 1); # we show only the TMPL_VAR names $op
49 } else {
50 $template->param(script_name => $script_name,
51                                                 else              => 1); # we show only the TMPL_VAR names $op
52 }
53
54 my $dbh = C4::Context->dbh;
55
56 ################## ADD_FORM ##################################
57 # called by default. Used to create form to add or  modify a record
58 if ($op eq 'add_form') {
59         my $data;
60         my $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where kohafield=?");
61         $sth->execute($tablename.".".$kohafield);
62         my ($defaulttagfield, $defaulttagsubfield,$defaultliblibrarian) = $sth->fetchrow;
63
64         for (my $i=0;$i<=9;$i++) {
65                 my $sth2=$dbh->prepare("select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where tagfield like '$i%'");
66                 $sth2->execute;
67                 my @marcarray;
68                 push @marcarray," ";
69                 while (my ($field, $tagsubfield, $liblibrarian) = $sth2->fetchrow_array) {
70         #               warn "$field$tagsubfield - $liblibrarian";
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         my $sth2=$dbh->prepare("SHOW COLUMNS from $tablename");
109         $sth2->execute;
110
111         my $toggle="white";
112         my @loop_data = ();
113         while ((my $field) = $sth2->fetchrow_array) {
114                 if ($toggle eq 'white'){
115                         $toggle="#ffffcc";
116                 } else {
117                         $toggle="white";
118                 }
119                 my %row_data;  # get a fresh hash for the row data
120                 $row_data{tagfield} = $fields{$tablename.".".$field}->{tagfield};
121                 $row_data{tagsubfield} = $fields{$tablename.".".$field}->{tagsubfield};
122                 $row_data{liblibrarian} = $fields{$tablename.".".$field}->{liblibrarian};
123                 $row_data{kohafield} = $field;
124                 $row_data{edit} = "$script_name?op=add_form&tablename=$tablename&kohafield=$field";
125                 $row_data{bgcolor} = $toggle;
126                 push(@loop_data,\%row_data);
127         }
128         $template->param(loop => \@loop_data,
129                                                         tablename => CGI::scrolling_list(-name=>'tablename',
130                                                                                                                                         -values=>['biblio','biblioitems','items'],
131                                                                                                                                         -default=>$tablename,
132                                                                                                                                         -size=>1,
133                                                                                                                                         -multiple=>0
134                                                                                                                                         )
135                                                         );
136 } #---- END $OP eq DEFAULT
137
138 print $input->header(-cookie => $cookie), $template->output;