Merge remote branch 'kc/new/bug_4885' into kcmaster
[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;
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     $dbh->disconnect;
50     return ( $cnt, \@results );
51 }
52
53 sub marc_subfield_structure_exists {
54     my ($tagfield, $tagsubfield, $frameworkcode) = @_;
55     my $dbh  = C4::Context->dbh;
56     my $sql  = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
57     my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
58     return @$rows > 0;
59 }
60
61 my $input         = new CGI;
62 my $tagfield      = $input->param('tagfield');
63 my $tagsubfield   = $input->param('tagsubfield');
64 my $frameworkcode = $input->param('frameworkcode');
65 my $pkfield       = "tagfield";
66 my $offset        = $input->param('offset');
67 my $script_name   = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
68
69 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
70     {
71         template_name   => "admin/marc_subfields_structure.tmpl",
72         query           => $input,
73         type            => "intranet",
74         authnotrequired => 0,
75         flagsrequired   => { parameters => 1 },
76         debug           => 1,
77     }
78 );
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{tab} = CGI::scrolling_list(
183             -name   => 'tab',
184             -id     => "tab$i",
185             -values =>
186               [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
187             -labels => {
188                 '-1' => 'ignore',
189                 '0'  => '0',
190                 '1'  => '1',
191                 '2'  => '2',
192                 '3'  => '3',
193                 '4'  => '4',
194                 '5'  => '5',
195                 '6'  => '6',
196                 '7'  => '7',
197                 '8'  => '8',
198                 '9'  => '9',
199                 '10' => 'items (10)',
200             },
201             -default  => $data->{'tab'},
202             -size     => 1,
203             -multiple => 0,
204         );
205         $row_data{tagsubfield} =
206             $data->{'tagsubfield'}
207           . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
208           . $data->{'tagsubfield'}
209           . "\" id=\"tagsubfield\" />";
210         $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
211         $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
212         $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
213         $row_data{libopac}      = CGI::escapeHTML( $data->{'libopac'} );
214         $row_data{seealso}      = CGI::escapeHTML( $data->{'seealso'} );
215         $row_data{kohafield}    = CGI::scrolling_list(
216             -name     => "kohafield",
217             -id       => "kohafield$i",
218             -values   => \@kohafields,
219             -default  => "$data->{'kohafield'}",
220             -size     => 1,
221             -multiple => 0,
222         );
223         $row_data{authorised_value} = CGI::scrolling_list(
224             -name     => "authorised_value",
225             -id       => "authorised_value$i",
226             -values   => \@authorised_values,
227             -default  => $data->{'authorised_value'},
228             -size     => 1,
229             -multiple => 0,
230         );
231         $row_data{value_builder} = CGI::scrolling_list(
232             -name     => "value_builder",
233             -id       => "value_builder$i",
234             -values   => \@value_builder,
235             -default  => $data->{'value_builder'},
236             -size     => 1,
237             -multiple => 0,
238         );
239         $row_data{authtypes} = CGI::scrolling_list(
240             -name     => "authtypecode",
241             -id       => "authtypecode$i",
242             -values   => \@authtypes,
243             -default  => $data->{'authtypecode'},
244             -size     => 1,
245             -multiple => 0,
246         );
247         $row_data{repeatable} = CGI::checkbox(
248             -name     => "repeatable$i",
249             -checked  => $data->{'repeatable'} ? 'checked' : '',
250             -value    => 1,
251             -label    => '',
252             -id       => "repeatable$i"
253         );
254         $row_data{mandatory} = CGI::checkbox(
255             -name     => "mandatory$i",
256             -checked  => $data->{'mandatory'} ? 'checked' : '',
257             -value    => 1,
258             -label    => '',
259             -id       => "mandatory$i"
260         );
261         $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
262         $row_data{isurl}  = CGI::checkbox(
263             -name     => "isurl$i",
264             -id       => "isurl$i",
265             -checked  => $data->{'isurl'} ? 'checked' : '',
266             -value    => 1,
267             -label    => ''
268         );
269         $row_data{row}    = $i;
270         $row_data{link}   = CGI::escapeHTML( $data->{'link'} ); 
271         push( @loop_data, \%row_data );
272         $i++;
273     }
274
275     # add more_subfields empty lines for add if needed
276     for ( my $j = 1 ; $j <= 1 ; $j++ ) {
277         my %row_data;    # get a fresh hash for the row data
278         $row_data{'new_subfield'} = 1;
279         $row_data{'subfieldcode'} = '';
280
281         $row_data{tab} = CGI::scrolling_list(
282             -name   => 'tab',
283             -id     => "tab$j",
284             -values =>
285               [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
286             -labels => {
287                 '-1' => 'ignore',
288                 '0'  => '0',
289                 '1'  => '1',
290                 '2'  => '2',
291                 '3'  => '3',
292                 '4'  => '4',
293                 '5'  => '5',
294                 '6'  => '6',
295                 '7'  => '7',
296                 '8'  => '8',
297                 '9'  => '9',
298                 '10' => 'items (10)',
299             },
300             -default  => "",
301             -size     => 1,
302             -multiple => 0,
303         );
304         $row_data{tagsubfield} =
305             "<input type=\"text\" name=\"tagsubfield\" value=\""
306           . $data->{'tagsubfield'}
307           . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
308         $row_data{liblibrarian} = "";
309         $row_data{libopac}      = "";
310         $row_data{seealso}      = "";
311         $row_data{kohafield}    = CGI::scrolling_list(
312             -name     => 'kohafield',
313             -id       => "kohafield$j",
314             -values   => \@kohafields,
315             -default  => "",
316             -size     => 1,
317             -multiple => 0,
318         );
319         $row_data{hidden}     = "";
320         $row_data{repeatable} = CGI::checkbox(
321             -name     => "repeatable$j",
322             -id       => "repeatable$j",
323             -checked  => '',
324             -value    => 1,
325             -label    => ''
326         );
327         $row_data{mandatory} = CGI::checkbox(
328             -name     => "mandatory$j",
329             -id       => "mandatory$j",
330             -checked  => '',
331             -value    => 1,
332             -label    => ''
333         );
334         $row_data{isurl} = CGI::checkbox(
335             -name     => "isurl$j",
336             -id       => "isurl$j",
337             -checked  => '',
338             -value    => 1,
339             -label    => ''
340         );
341         $row_data{value_builder} = CGI::scrolling_list(
342             -name     => "value_builder",
343             -id       => "value_builder$j",
344             -values   => \@value_builder,
345             -default  => $data->{'value_builder'},
346             -size     => 1,
347             -multiple => 0,
348         );
349         $row_data{authorised_value} = CGI::scrolling_list(
350             -name     => "authorised_value",
351             -id       => "authorised_value$j",
352             -values   => \@authorised_values,
353             -size     => 1,
354             -multiple => 0,
355         );
356         $row_data{authtypes} = CGI::scrolling_list(
357             -name     => "authtypecode",
358             -id       => "authtypecode$j",
359             -values   => \@authtypes,
360             -size     => 1,
361             -multiple => 0,
362         );
363         $row_data{link}   = CGI::escapeHTML( $data->{'link'} );
364         $row_data{row}    = $j;
365         push( @loop_data, \%row_data );
366     }
367     $template->param( 'use_heading_flags_p'      => 1 );
368     $template->param( 'heading_edit_subfields_p' => 1 );
369     $template->param(
370         action   => "Edit subfields",
371         tagfield => $tagfield,
372         loop           => \@loop_data,
373         more_subfields => $more_subfields,
374         more_tag       => $tagfield
375     );
376
377     # END $OP eq ADD_FORM
378 ################## ADD_VALIDATE ##################################
379     # called by add_form, used to insert/modify data in DB
380 }
381 elsif ( $op eq 'add_validate' ) {
382     my $dbh = C4::Context->dbh;
383     $template->param( tagfield => "$input->param('tagfield')" );
384 #     my $sth = $dbh->prepare(
385 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
386 #                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
387 #     );
388     my $sth_insert = $dbh->prepare(qq{
389         insert into marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
390         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
391     });
392     my $sth_update = $dbh->prepare(qq{
393         update marc_subfield_structure set tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, authtypecode=?, value_builder=?, hidden=?, isurl=?, frameworkcode=?,  link=?, defaultvalue=?
394         where tagfield=? and tagsubfield=? and frameworkcode=?
395     });
396     my @tagsubfield       = $input->param('tagsubfield');
397     my @liblibrarian      = $input->param('liblibrarian');
398     my @libopac           = $input->param('libopac');
399     my @kohafield         = $input->param('kohafield');
400     my @tab               = $input->param('tab');
401     my @seealso           = $input->param('seealso');
402     my @hidden            = $input->param('hidden');
403     my @authorised_values = $input->param('authorised_value');
404     my @authtypecodes     = $input->param('authtypecode');
405     my @value_builder     = $input->param('value_builder');
406     my @link              = $input->param('link');
407     my @defaultvalue      = $input->param('defaultvalue');
408     
409     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
410         my $tagfield    = $input->param('tagfield');
411         my $tagsubfield = $tagsubfield[$i];
412         $tagsubfield = "@" unless $tagsubfield ne '';
413         $tagsubfield = "@" if $tagsubfield eq '_';
414         my $liblibrarian     = $liblibrarian[$i];
415         my $libopac          = $libopac[$i];
416         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
417         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
418         my $kohafield        = $kohafield[$i];
419         my $tab              = $tab[$i];
420         my $seealso          = $seealso[$i];
421         my $authorised_value = $authorised_values[$i];
422         my $authtypecode     = $authtypecodes[$i];
423         my $value_builder    = $value_builder[$i];
424         my $hidden = $hidden[$i];                     #input->param("hidden$i");
425         my $isurl  = $input->param("isurl$i") ? 1 : 0;
426         my $link   = $link[$i];
427         my $defaultvalue = $defaultvalue[$i];
428         
429         if ($liblibrarian) {
430             unless ( C4::Context->config('demo') eq 1 ) {
431                 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
432                     $sth_update->execute(
433                         $tagfield,
434                         $tagsubfield,
435                         $liblibrarian,
436                         $libopac,
437                         $repeatable,
438                         $mandatory,
439                         $kohafield,
440                         $tab,
441                         $seealso,
442                         $authorised_value,
443                         $authtypecode,
444                         $value_builder,
445                         $hidden,
446                         $isurl,
447                         $frameworkcode,
448                         $link,
449                         $defaultvalue,
450                         (
451                             $tagfield,
452                             $tagsubfield,
453                             $frameworkcode,
454                         ),
455                     );
456                 } else {
457                     $sth_insert->execute(
458                         $tagfield,
459                         $tagsubfield,
460                         $liblibrarian,
461                         $libopac,
462                         $repeatable,
463                         $mandatory,
464                         $kohafield,
465                         $tab,
466                         $seealso,
467                         $authorised_value,
468                         $authtypecode,
469                         $value_builder,
470                         $hidden,
471                         $isurl,
472                         $frameworkcode,
473                         $link,
474                         $defaultvalue,
475                     );
476                 }
477             }
478         }
479     }
480     $sth_insert->finish;
481     $sth_update->finish;
482     print
483 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
484     exit;
485
486     # END $OP eq ADD_VALIDATE
487 ################## DELETE_CONFIRM ##################################
488     # called by default form, used to confirm deletion of data in DB
489 }
490 elsif ( $op eq 'delete_confirm' ) {
491     my $dbh = C4::Context->dbh;
492     my $sth =
493       $dbh->prepare(
494 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
495       );
496
497     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
498     my $data = $sth->fetchrow_hashref;
499     $sth->finish;
500     $template->param(
501         liblibrarian  => $data->{'liblibrarian'},
502         tagsubfield   => $data->{'tagsubfield'},
503         delete_link   => $script_name,
504         tagfield      => $tagfield,
505         tagsubfield   => $tagsubfield,
506         frameworkcode => $frameworkcode,
507     );
508
509     # END $OP eq DELETE_CONFIRM
510 ################## DELETE_CONFIRMED ##################################
511   # called by delete_confirm, used to effectively confirm deletion of data in DB
512 }
513 elsif ( $op eq 'delete_confirmed' ) {
514     my $dbh = C4::Context->dbh;
515     unless ( C4::Context->config('demo') eq 1 ) {
516         my $sth =
517           $dbh->prepare(
518 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
519           );
520         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
521         $sth->finish;
522     }
523     print
524 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
525     exit;
526     $template->param( tagfield => $tagfield );
527
528     # END $OP eq DELETE_CONFIRMED
529 ################## DEFAULT ##################################
530 }
531 else {    # DEFAULT
532     my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
533     my @loop_data = ();
534     for ( my $i = 0; $i < $count; $i++ ) {
535         my %row_data;    # get a fresh hash for the row data
536         $row_data{tagfield}         = $results->[$i]{'tagfield'};
537         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
538         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
539         $row_data{kohafield}        = $results->[$i]{'kohafield'};
540         $row_data{repeatable}       = $results->[$i]{'repeatable'};
541         $row_data{mandatory}        = $results->[$i]{'mandatory'};
542         $row_data{tab}              = $results->[$i]{'tab'};
543         $row_data{seealso}          = $results->[$i]{'seealso'};
544         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
545         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
546         $row_data{value_builder}    = $results->[$i]{'value_builder'};
547         $row_data{hidden}           = $results->[$i]{'hidden'};
548         $row_data{isurl}            = $results->[$i]{'isurl'};
549         $row_data{link}             = $results->[$i]{'link'};
550
551         if ( $row_data{tab} eq -1 ) {
552             $row_data{subfield_ignored} = 1;
553         }
554
555         push( @loop_data, \%row_data );
556     }
557     $template->param( loop => \@loop_data );
558     $template->param(
559         edit_tagfield      => $tagfield,
560         edit_frameworkcode => $frameworkcode
561     );
562
563 }    #---- END $OP eq DEFAULT
564
565 output_html_with_http_headers $input, $cookie, $template->output;