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