with this script, it's easy to see which koha-DB fields are not mapped to MARC-DB...
[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 CGI;
24 use C4::Search;
25 use C4::Context;
26 use C4::Biblio;
27 use HTML::Template;
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 = gettemplate("parameters/koha2marclinks.tmpl",0);
37
38 if ($op) {
39 $template->param(script_name => $script_name,
40                                                 $op              => 1); # we show only the TMPL_VAR names $op
41 } else {
42 $template->param(script_name => $script_name,
43                                                 else              => 1); # we show only the TMPL_VAR names $op
44 }
45         
46 my $dbh = C4::Context->dbh;
47
48 ################## ADD_FORM ##################################
49 # called by default. Used to create form to add or  modify a record
50 if ($op eq 'add_form') {
51         my $data;
52         my $sth = $dbh->prepare("select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where kohafield=?");
53         $sth->execute($tablename.".".$kohafield);
54         my ($defaulttagfield, $defaulttagsubfield,$defaultliblibrarian) = $sth->fetchrow;
55
56         for (my $i=0;$i<=9;$i++) {
57                 my $sth2=$dbh->prepare("select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where tagfield like '$i%'");
58                 $sth2->execute;
59                 my @marcarray;
60                 push @marcarray," ";
61                 while (my ($field, $tagsubfield, $liblibrarian) = $sth2->fetchrow_array) {
62         #               warn "$field$tagsubfield - $liblibrarian";
63                         push @marcarray, "$field $tagsubfield - $liblibrarian";
64                 }
65                 my $marclist = CGI::scrolling_list(-name=>"marc",
66                                                 -values=> \@marcarray,
67                                                 -default=>"$defaulttagfield $defaulttagsubfield - $defaultliblibrarian",
68                                                 -size=>1,
69                                                 -multiple=>0,
70                                                 );
71                 $template->param("marclist$i" => $marclist);
72         }
73         $template->param(       tablename => $tablename,
74                                                         kohafield => $kohafield);
75
76                                                                                                         # END $OP eq ADD_FORM
77 ################## ADD_VALIDATE ##################################
78 # called by add_form, used to insert/modify data in DB
79 } elsif ($op eq 'add_validate') {
80         #----- empty koha field :
81         $dbh->do("update marc_subfield_structure set kohafield='' where kohafield='$tablename.$kohafield'");
82         #---- reload if not empty
83         my @temp = split / /,$input->param('marc');
84         $dbh->do("update marc_subfield_structure set kohafield='$tablename.$kohafield' where tagfield='$temp[0]' and tagsubfield='$temp[1]'");
85         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=koha2marclinks.pl?tablename=$tablename\"></html>";
86         exit;
87
88                                                                                                         # END $OP eq ADD_VALIDATE
89 ################## DEFAULT ##################################
90 } else { # DEFAULT
91         my $env;
92         my $sth=$dbh->prepare("Select tagfield,tagsubfield,liblibrarian,kohafield from marc_subfield_structure");
93         $sth->execute;
94         my %fields;
95         while ((my $tagfield,my $tagsubfield,my $liblibrarian,my $kohafield) = $sth->fetchrow) {
96                 $fields{$kohafield}->{tagfield} = $tagfield;
97                 $fields{$kohafield}->{tagsubfield} = $tagsubfield;
98                 $fields{$kohafield}->{liblibrarian} = $liblibrarian;
99         }
100         my $sth2=$dbh->prepare("SHOW COLUMNS from $tablename");
101         $sth2->execute;
102
103         my $toggle="white";
104         my @loop_data = ();
105         while ((my $field) = $sth2->fetchrow_array) {
106                 if ($toggle eq 'white'){
107                         $toggle="#ffffcc";
108                 } else {
109                         $toggle="white";
110                 }
111                 my %row_data;  # get a fresh hash for the row data
112                 $row_data{tagfield} = $fields{$tablename.".".$field}->{tagfield};
113                 $row_data{tagsubfield} = $fields{$tablename.".".$field}->{tagsubfield};
114                 $row_data{liblibrarian} = $fields{$tablename.".".$field}->{liblibrarian};
115                 $row_data{kohafield} = $field;
116                 $row_data{edit} = "$script_name?op=add_form&tablename=$tablename&kohafield=$field";
117                 $row_data{bgcolor} = $toggle;
118                 push(@loop_data,\%row_data);
119         }
120         $template->param(loop => \@loop_data,
121                                                         tablename => CGI::scrolling_list(-name=>'tablename',
122                                                                                                                                         -values=>['biblio','biblioitems','items'],
123                                                                                                                                         -default=>$tablename,
124                                                                                                                                         -size=>1,
125                                                                                                                                         -multiple=>0
126                                                                                                                                         )
127                                                         );
128 } #---- END $OP eq DEFAULT
129
130 print "Content-Type: text/html\n\n", $template->output;