Bug 11944: use CGI( -utf8 ) everywhere
[koha.git] / admin / marc_subfields_structure.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; FIXME - Bug 2505
22 use C4::Output;
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26
27
28 sub string_search {
29     my ( $searchstring, $frameworkcode ) = @_;
30     my $dbh = C4::Context->dbh;
31     $searchstring =~ s/\'/\\\'/g;
32     my @data  = split( ' ', $searchstring );
33     my $count = @data;
34     my $sth   =
35       $dbh->prepare(
36 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
37       );
38     $sth->execute( "$searchstring%", $frameworkcode );
39     my @results;
40     my $cnt = 0;
41     my $u   = 1;
42
43     while ( my $data = $sth->fetchrow_hashref ) {
44         push( @results, $data );
45         $cnt++;
46         $u++;
47     }
48     $sth->finish;
49     return ( $cnt, \@results );
50 }
51
52 sub marc_subfield_structure_exists {
53     my ($tagfield, $tagsubfield, $frameworkcode) = @_;
54     my $dbh  = C4::Context->dbh;
55     my $sql  = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
56     my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
57     return @$rows > 0;
58 }
59
60 my $input         = new CGI;
61 my $tagfield      = $input->param('tagfield');
62 my $tagsubfield   = $input->param('tagsubfield');
63 my $frameworkcode = $input->param('frameworkcode');
64 my $pkfield       = "tagfield";
65 my $offset        = $input->param('offset');
66 my $script_name   = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
67
68 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
69     {
70         template_name   => "admin/marc_subfields_structure.tt",
71         query           => $input,
72         type            => "intranet",
73         authnotrequired => 0,
74         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
75         debug           => 1,
76     }
77 );
78 my $cache = Koha::Cache->get_instance();
79
80 my $op       = $input->param('op');
81 $tagfield =~ s/\,//g;
82
83 if ($op) {
84     $template->param(
85         script_name   => $script_name,
86         tagfield      => $tagfield,
87         frameworkcode => $frameworkcode,
88         $op           => 1
89     );    # we show only the TMPL_VAR names $op
90 }
91 else {
92     $template->param(
93         script_name   => $script_name,
94         tagfield      => $tagfield,
95         frameworkcode => $frameworkcode,
96         else          => 1
97     );    # we show only the TMPL_VAR names $op
98 }
99
100 ################## ADD_FORM ##################################
101 # called by default. Used to create form to add or  modify a record
102 if ( $op eq 'add_form' ) {
103     my $data;
104     my $dbh            = C4::Context->dbh;
105     my $more_subfields = $input->param("more_subfields") + 1;
106
107     # builds kohafield tables
108     my @kohafields;
109     push @kohafields, "";
110     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
111     $sth2->execute;
112     while ( ( my $field ) = $sth2->fetchrow_array ) {
113         push @kohafields, "biblio." . $field;
114     }
115     $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
116     $sth2->execute;
117     while ( ( my $field ) = $sth2->fetchrow_array ) {
118         if ( $field eq 'notes' ) { $field = 'bnotes'; }
119         push @kohafields, "biblioitems." . $field;
120     }
121     $sth2 = $dbh->prepare("SHOW COLUMNS from items");
122     $sth2->execute;
123     while ( ( my $field ) = $sth2->fetchrow_array ) {
124         push @kohafields, "items." . $field;
125     }
126
127     # build authorised value list
128     $sth2->finish;
129     $sth2 = $dbh->prepare("select distinct category from authorised_values");
130     $sth2->execute;
131     my @authorised_values;
132     push @authorised_values, "";
133     while ( ( my $category ) = $sth2->fetchrow_array ) {
134         push @authorised_values, $category;
135     }
136     push( @authorised_values, "branches" );
137     push( @authorised_values, "itemtypes" );
138     push( @authorised_values, "cn_source" );
139
140     # build thesaurus categories list
141     $sth2->finish;
142     $sth2 = $dbh->prepare("select authtypecode from auth_types");
143     $sth2->execute;
144     my @authtypes;
145     push @authtypes, "";
146     while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
147         push @authtypes, $authtypecode;
148     }
149
150     # build value_builder list
151     my @value_builder = ('');
152
153     # read value_builder directory.
154     # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
155     # on a standard install, /cgi-bin need to be added.
156     # test one, then the other
157     my $cgidir = C4::Context->intranetdir . "/cgi-bin";
158     unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
159         $cgidir = C4::Context->intranetdir;
160         opendir( DIR, "$cgidir/cataloguing/value_builder" )
161           || die "can't opendir $cgidir/value_builder: $!";
162     }
163     while ( my $line = readdir(DIR) ) {
164         if ( $line =~ /\.pl$/ ) {
165             push( @value_builder, $line );
166         }
167     }
168     @value_builder= sort {$a cmp $b} @value_builder;
169     closedir DIR;
170
171     # build values list
172     my $sth =
173       $dbh->prepare(
174 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
175       );    # and tagsubfield='$tagsubfield'");
176     $sth->execute( $tagfield, $frameworkcode );
177     my @loop_data = ();
178     my $i         = 0;
179     while ( $data = $sth->fetchrow_hashref ) {
180         my %row_data;    # get a fresh hash for the row data
181         $row_data{defaultvalue} = $data->{defaultvalue};
182         $row_data{maxlength} = $data->{maxlength};
183         $row_data{tab} = {
184                     id      => "tab$i",
185                     default => $data->{'tab'},
186                     };
187
188         $row_data{tagsubfield} =
189             $data->{'tagsubfield'}
190           . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
191           . $data->{'tagsubfield'}
192           . "\" id=\"tagsubfield\" />";
193         $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
194         $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
195         $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
196         $row_data{libopac}      = CGI::escapeHTML( $data->{'libopac'} );
197         $row_data{seealso}      = CGI::escapeHTML( $data->{'seealso'} );
198         $row_data{kohafield} = {
199                     id      => "kohafield$i",
200                     values  => \@kohafields,
201                     default => "$data->{'kohafield'}",
202                     };
203         $row_data{authorised_value} = {
204                     id      => "authorised_value$i",
205                     values  => \@authorised_values,
206                     default => $data->{'authorised_value'},
207         };
208         $row_data{value_builder} = {
209                     id      => "value_builder$i",
210                     values  => \@value_builder,
211                     default => $data->{'value_builder'},
212         };
213         $row_data{authtypes} = {
214                     id      => "authtypecode$i",
215                     values  => \@authtypes,
216                     default => $data->{'authtypecode'},
217         };
218         $row_data{repeatable} = CGI::checkbox(
219             -name     => "repeatable$i",
220             -checked  => $data->{'repeatable'} ? 'checked' : '',
221             -value    => 1,
222             -label    => '',
223             -id       => "repeatable$i"
224         );
225         $row_data{mandatory} = CGI::checkbox(
226             -name     => "mandatory$i",
227             -checked  => $data->{'mandatory'} ? 'checked' : '',
228             -value    => 1,
229             -label    => '',
230             -id       => "mandatory$i"
231         );
232         $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
233         $row_data{isurl}  = CGI::checkbox(
234             -name     => "isurl$i",
235             -id       => "isurl$i",
236             -checked  => $data->{'isurl'} ? 'checked' : '',
237             -value    => 1,
238             -label    => ''
239         );
240         $row_data{row}    = $i;
241         $row_data{link}   = CGI::escapeHTML( $data->{'link'} ); 
242         push( @loop_data, \%row_data );
243         $i++;
244     }
245
246     # add more_subfields empty lines for add if needed
247         my %row_data;    # get a fresh hash for the row data
248         $row_data{'new_subfield'} = 1;
249         $row_data{'subfieldcode'} = '';
250         $row_data{'maxlength'} = 9999;
251
252         $row_data{tab} = {
253                     id      => "tab$i",
254                     default => $data->{'tab'},
255                     };
256         $row_data{tagsubfield} =
257             "<input type=\"text\" name=\"tagsubfield\" value=\""
258           . $data->{'tagsubfield'}
259           . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
260         $row_data{liblibrarian} = "";
261         $row_data{libopac}      = "";
262         $row_data{seealso}      = "";
263         $row_data{kohafield} = {
264                     id      => "kohafield$i",
265                     values  => \@kohafields,
266                     default => "$data->{'kohafield'}",
267                     };
268         $row_data{hidden}     = "";
269         $row_data{repeatable} = CGI::checkbox(
270             -name     => "repeatable$i",
271             -id       => "repeatable$i",
272             -checked  => '',
273             -value    => 1,
274             -label    => ''
275         );
276         $row_data{mandatory} = CGI::checkbox(
277             -name     => "mandatory$i",
278             -id       => "mandatory$i",
279             -checked  => '',
280             -value    => 1,
281             -label    => ''
282         );
283         $row_data{isurl} = CGI::checkbox(
284             -name     => "isurl$i",
285             -id       => "isurl$i",
286             -checked  => '',
287             -value    => 1,
288             -label    => ''
289         );
290         $row_data{value_builder} = {
291                     id      => "value_builder$i",
292                     values  => \@value_builder,
293                     default => $data->{'value_builder'},
294         };
295         $row_data{authorised_value} = {
296                     id      => "authorised_value$i",
297                     values  => \@authorised_values,
298                     default => $data->{'authorised_value'},
299         };
300         $row_data{authtypes} = {
301                     id      => "authtypecode$i",
302                     values  => \@authtypes,
303                     default => $data->{'authtypecode'},
304         };
305         $row_data{link}   = CGI::escapeHTML( $data->{'link'} );
306         $row_data{row}    = $i;
307         push( @loop_data, \%row_data );
308
309     $template->param( 'use_heading_flags_p'      => 1 );
310     $template->param( 'heading_edit_subfields_p' => 1 );
311     $template->param(
312         action   => "Edit subfields",
313         tagfield => $tagfield,
314         loop           => \@loop_data,
315         more_subfields => $more_subfields,
316         more_tag       => $tagfield
317     );
318
319     # END $OP eq ADD_FORM
320 ################## ADD_VALIDATE ##################################
321     # called by add_form, used to insert/modify data in DB
322 }
323 elsif ( $op eq 'add_validate' ) {
324     my $dbh = C4::Context->dbh;
325     $template->param( tagfield => "$input->param('tagfield')" );
326 #     my $sth = $dbh->prepare(
327 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
328 #                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
329 #     );
330     my $sth_insert = $dbh->prepare(qq{
331         insert into marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue,maxlength)
332         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
333     });
334     my $sth_update = $dbh->prepare(qq{
335         update marc_subfield_structure set tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, authtypecode=?, value_builder=?, hidden=?, isurl=?, frameworkcode=?,  link=?, defaultvalue=?, maxlength=?
336         where tagfield=? and tagsubfield=? and frameworkcode=?
337     });
338     my @tagsubfield       = $input->param('tagsubfield');
339     my @liblibrarian      = $input->param('liblibrarian');
340     my @libopac           = $input->param('libopac');
341     my @kohafield         = $input->param('kohafield');
342     my @tab               = $input->param('tab');
343     my @seealso           = $input->param('seealso');
344     my @hidden            = $input->param('hidden');
345     my @authorised_values = $input->param('authorised_value');
346     my @authtypecodes     = $input->param('authtypecode');
347     my @value_builder     = $input->param('value_builder');
348     my @link              = $input->param('link');
349     my @defaultvalue      = $input->param('defaultvalue');
350     my @maxlength         = $input->param('maxlength');
351     
352     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
353         my $tagfield    = $input->param('tagfield');
354         my $tagsubfield = $tagsubfield[$i];
355         $tagsubfield = "@" unless $tagsubfield ne '';
356         $tagsubfield = "@" if $tagsubfield eq '_';
357         my $liblibrarian     = $liblibrarian[$i];
358         my $libopac          = $libopac[$i];
359         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
360         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
361         my $kohafield        = $kohafield[$i];
362         my $tab              = $tab[$i];
363         my $seealso          = $seealso[$i];
364         my $authorised_value = $authorised_values[$i];
365         my $authtypecode     = $authtypecodes[$i];
366         my $value_builder    = $value_builder[$i];
367         my $hidden = $hidden[$i];                     #input->param("hidden$i");
368         my $isurl  = $input->param("isurl$i") ? 1 : 0;
369         my $link   = $link[$i];
370         my $defaultvalue = $defaultvalue[$i];
371         my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
372         
373         if (defined($liblibrarian) && $liblibrarian ne "") {
374             unless ( C4::Context->config('demo') eq 1 ) {
375                 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
376                     $sth_update->execute(
377                         $tagfield,
378                         $tagsubfield,
379                         $liblibrarian,
380                         $libopac,
381                         $repeatable,
382                         $mandatory,
383                         $kohafield,
384                         $tab,
385                         $seealso,
386                         $authorised_value,
387                         $authtypecode,
388                         $value_builder,
389                         $hidden,
390                         $isurl,
391                         $frameworkcode,
392                         $link,
393                         $defaultvalue,
394                         $maxlength,
395                         (
396                             $tagfield,
397                             $tagsubfield,
398                             $frameworkcode,
399                         ),
400                     );
401                 } else {
402                     $sth_insert->execute(
403                         $tagfield,
404                         $tagsubfield,
405                         $liblibrarian,
406                         $libopac,
407                         $repeatable,
408                         $mandatory,
409                         $kohafield,
410                         $tab,
411                         $seealso,
412                         $authorised_value,
413                         $authtypecode,
414                         $value_builder,
415                         $hidden,
416                         $isurl,
417                         $frameworkcode,
418                         $link,
419                         $defaultvalue,
420                         $maxlength,
421                     );
422                 }
423             }
424         }
425     }
426     $sth_insert->finish;
427     $sth_update->finish;
428     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
429     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
430
431     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
432     exit;
433
434     # END $OP eq ADD_VALIDATE
435 ################## DELETE_CONFIRM ##################################
436     # called by default form, used to confirm deletion of data in DB
437 }
438 elsif ( $op eq 'delete_confirm' ) {
439     my $dbh = C4::Context->dbh;
440     my $sth =
441       $dbh->prepare(
442 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
443       );
444
445     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
446     my $data = $sth->fetchrow_hashref;
447     $sth->finish;
448     $template->param(
449         liblibrarian  => $data->{'liblibrarian'},
450         tagsubfield   => $data->{'tagsubfield'},
451         delete_link   => $script_name,
452         tagfield      => $tagfield,
453         tagsubfield   => $tagsubfield,
454         frameworkcode => $frameworkcode,
455     );
456
457     # END $OP eq DELETE_CONFIRM
458 ################## DELETE_CONFIRMED ##################################
459   # called by delete_confirm, used to effectively confirm deletion of data in DB
460 }
461 elsif ( $op eq 'delete_confirmed' ) {
462     my $dbh = C4::Context->dbh;
463     unless ( C4::Context->config('demo') eq 1 ) {
464         my $sth =
465           $dbh->prepare(
466 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
467           );
468         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
469         $sth->finish;
470     }
471     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
472     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
473     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
474     exit;
475
476     # END $OP eq DELETE_CONFIRMED
477 ################## DEFAULT ##################################
478 }
479 else {    # DEFAULT
480     my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
481     my @loop_data = ();
482     for ( my $i = 0; $i < $count; $i++ ) {
483         my %row_data;    # get a fresh hash for the row data
484         $row_data{tagfield}         = $results->[$i]{'tagfield'};
485         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
486         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
487         $row_data{kohafield}        = $results->[$i]{'kohafield'};
488         $row_data{repeatable}       = $results->[$i]{'repeatable'};
489         $row_data{mandatory}        = $results->[$i]{'mandatory'};
490         $row_data{tab}              = $results->[$i]{'tab'};
491         $row_data{seealso}          = $results->[$i]{'seealso'};
492         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
493         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
494         $row_data{value_builder}    = $results->[$i]{'value_builder'};
495         $row_data{hidden}           = $results->[$i]{'hidden'};
496         $row_data{isurl}            = $results->[$i]{'isurl'};
497         $row_data{link}             = $results->[$i]{'link'};
498
499         if ( $row_data{tab} eq -1 ) {
500             $row_data{subfield_ignored} = 1;
501         }
502
503         push( @loop_data, \%row_data );
504     }
505     $template->param( loop => \@loop_data );
506     $template->param(
507         edit_tagfield      => $tagfield,
508         edit_frameworkcode => $frameworkcode
509     );
510
511 }    #---- END $OP eq DEFAULT
512
513 output_html_with_http_headers $input, $cookie, $template->output;