Merge branch 'bug_8942' into 3.12-master
[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     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.tmpl",
71         query           => $input,
72         type            => "intranet",
73         authnotrequired => 0,
74         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
75         debug           => 1,
76     }
77 );
78
79 my $op       = $input->param('op');
80 $tagfield =~ s/\,//g;
81
82 if ($op) {
83     $template->param(
84         script_name   => $script_name,
85         tagfield      => $tagfield,
86         frameworkcode => $frameworkcode,
87         $op           => 1
88     );    # we show only the TMPL_VAR names $op
89 }
90 else {
91     $template->param(
92         script_name   => $script_name,
93         tagfield      => $tagfield,
94         frameworkcode => $frameworkcode,
95         else          => 1
96     );    # we show only the TMPL_VAR names $op
97 }
98
99 ################## ADD_FORM ##################################
100 # called by default. Used to create form to add or  modify a record
101 if ( $op eq 'add_form' ) {
102     my $data;
103     my $dbh            = C4::Context->dbh;
104     my $more_subfields = $input->param("more_subfields") + 1;
105
106     # builds kohafield tables
107     my @kohafields;
108     push @kohafields, "";
109     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
110     $sth2->execute;
111     while ( ( my $field ) = $sth2->fetchrow_array ) {
112         push @kohafields, "biblio." . $field;
113     }
114     $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
115     $sth2->execute;
116     while ( ( my $field ) = $sth2->fetchrow_array ) {
117         if ( $field eq 'notes' ) { $field = 'bnotes'; }
118         push @kohafields, "biblioitems." . $field;
119     }
120     $sth2 = $dbh->prepare("SHOW COLUMNS from items");
121     $sth2->execute;
122     while ( ( my $field ) = $sth2->fetchrow_array ) {
123         push @kohafields, "items." . $field;
124     }
125
126     # build authorised value list
127     $sth2->finish;
128     $sth2 = $dbh->prepare("select distinct category from authorised_values");
129     $sth2->execute;
130     my @authorised_values;
131     push @authorised_values, "";
132     while ( ( my $category ) = $sth2->fetchrow_array ) {
133         push @authorised_values, $category;
134     }
135     push( @authorised_values, "branches" );
136     push( @authorised_values, "itemtypes" );
137     push( @authorised_values, "cn_source" );
138
139     # build thesaurus categories list
140     $sth2->finish;
141     $sth2 = $dbh->prepare("select authtypecode from auth_types");
142     $sth2->execute;
143     my @authtypes;
144     push @authtypes, "";
145     while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
146         push @authtypes, $authtypecode;
147     }
148
149     # build value_builder list
150     my @value_builder = ('');
151
152     # read value_builder directory.
153     # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
154     # on a standard install, /cgi-bin need to be added.
155     # test one, then the other
156     my $cgidir = C4::Context->intranetdir . "/cgi-bin";
157     unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
158         $cgidir = C4::Context->intranetdir;
159         opendir( DIR, "$cgidir/cataloguing/value_builder" )
160           || die "can't opendir $cgidir/value_builder: $!";
161     }
162     while ( my $line = readdir(DIR) ) {
163         if ( $line =~ /\.pl$/ ) {
164             push( @value_builder, $line );
165         }
166     }
167     @value_builder= sort {$a cmp $b} @value_builder;
168     closedir DIR;
169
170     # build values list
171     my $sth =
172       $dbh->prepare(
173 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
174       );    # and tagsubfield='$tagsubfield'");
175     $sth->execute( $tagfield, $frameworkcode );
176     my @loop_data = ();
177     my $i         = 0;
178     while ( $data = $sth->fetchrow_hashref ) {
179         my %row_data;    # get a fresh hash for the row data
180         $row_data{defaultvalue} = $data->{defaultvalue};
181         $row_data{maxlength} = $data->{maxlength};
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         my %row_data;    # get a fresh hash for the row data
277         $row_data{'new_subfield'} = 1;
278         $row_data{'subfieldcode'} = '';
279
280         $row_data{tab} = CGI::scrolling_list(
281             -name   => 'tab',
282             -id     => "tab$i",
283             -values =>
284               [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
285             -labels => {
286                 '-1' => 'ignore',
287                 '0'  => '0',
288                 '1'  => '1',
289                 '2'  => '2',
290                 '3'  => '3',
291                 '4'  => '4',
292                 '5'  => '5',
293                 '6'  => '6',
294                 '7'  => '7',
295                 '8'  => '8',
296                 '9'  => '9',
297                 '10' => 'items (10)',
298             },
299             -default  => "",
300             -size     => 1,
301             -multiple => 0,
302         );
303         $row_data{tagsubfield} =
304             "<input type=\"text\" name=\"tagsubfield\" value=\""
305           . $data->{'tagsubfield'}
306           . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
307         $row_data{liblibrarian} = "";
308         $row_data{libopac}      = "";
309         $row_data{seealso}      = "";
310         $row_data{kohafield}    = CGI::scrolling_list(
311             -name     => 'kohafield',
312             -id       => "kohafield$i",
313             -values   => \@kohafields,
314             -default  => "",
315             -size     => 1,
316             -multiple => 0,
317         );
318         $row_data{hidden}     = "";
319         $row_data{repeatable} = CGI::checkbox(
320             -name     => "repeatable$i",
321             -id       => "repeatable$i",
322             -checked  => '',
323             -value    => 1,
324             -label    => ''
325         );
326         $row_data{mandatory} = CGI::checkbox(
327             -name     => "mandatory$i",
328             -id       => "mandatory$i",
329             -checked  => '',
330             -value    => 1,
331             -label    => ''
332         );
333         $row_data{isurl} = CGI::checkbox(
334             -name     => "isurl$i",
335             -id       => "isurl$i",
336             -checked  => '',
337             -value    => 1,
338             -label    => ''
339         );
340         $row_data{value_builder} = CGI::scrolling_list(
341             -name     => "value_builder",
342             -id       => "value_builder$i",
343             -values   => \@value_builder,
344             -default  => $data->{'value_builder'},
345             -size     => 1,
346             -multiple => 0,
347         );
348         $row_data{authorised_value} = CGI::scrolling_list(
349             -name     => "authorised_value",
350             -id       => "authorised_value$i",
351             -values   => \@authorised_values,
352             -size     => 1,
353             -multiple => 0,
354         );
355         $row_data{authtypes} = CGI::scrolling_list(
356             -name     => "authtypecode",
357             -id       => "authtypecode$i",
358             -values   => \@authtypes,
359             -size     => 1,
360             -multiple => 0,
361         );
362         $row_data{link}   = CGI::escapeHTML( $data->{'link'} );
363         $row_data{row}    = $i;
364         push( @loop_data, \%row_data );
365
366     $template->param( 'use_heading_flags_p'      => 1 );
367     $template->param( 'heading_edit_subfields_p' => 1 );
368     $template->param(
369         action   => "Edit subfields",
370         tagfield => $tagfield,
371         loop           => \@loop_data,
372         more_subfields => $more_subfields,
373         more_tag       => $tagfield
374     );
375
376     # END $OP eq ADD_FORM
377 ################## ADD_VALIDATE ##################################
378     # called by add_form, used to insert/modify data in DB
379 }
380 elsif ( $op eq 'add_validate' ) {
381     my $dbh = C4::Context->dbh;
382     $template->param( tagfield => "$input->param('tagfield')" );
383 #     my $sth = $dbh->prepare(
384 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
385 #                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
386 #     );
387     my $sth_insert = $dbh->prepare(qq{
388         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)
389         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
390     });
391     my $sth_update = $dbh->prepare(qq{
392         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=?
393         where tagfield=? and tagsubfield=? and frameworkcode=?
394     });
395     my @tagsubfield       = $input->param('tagsubfield');
396     my @liblibrarian      = $input->param('liblibrarian');
397     my @libopac           = $input->param('libopac');
398     my @kohafield         = $input->param('kohafield');
399     my @tab               = $input->param('tab');
400     my @seealso           = $input->param('seealso');
401     my @hidden            = $input->param('hidden');
402     my @authorised_values = $input->param('authorised_value');
403     my @authtypecodes     = $input->param('authtypecode');
404     my @value_builder     = $input->param('value_builder');
405     my @link              = $input->param('link');
406     my @defaultvalue      = $input->param('defaultvalue');
407     my @maxlength         = $input->param('maxlength');
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         my $maxlength = $maxlength[$i];
429         
430         if (defined($liblibrarian) && $liblibrarian ne "") {
431             unless ( C4::Context->config('demo') eq 1 ) {
432                 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
433                     $sth_update->execute(
434                         $tagfield,
435                         $tagsubfield,
436                         $liblibrarian,
437                         $libopac,
438                         $repeatable,
439                         $mandatory,
440                         $kohafield,
441                         $tab,
442                         $seealso,
443                         $authorised_value,
444                         $authtypecode,
445                         $value_builder,
446                         $hidden,
447                         $isurl,
448                         $frameworkcode,
449                         $link,
450                         $defaultvalue,
451                         $maxlength,
452                         (
453                             $tagfield,
454                             $tagsubfield,
455                             $frameworkcode,
456                         ),
457                     );
458                 } else {
459                     $sth_insert->execute(
460                         $tagfield,
461                         $tagsubfield,
462                         $liblibrarian,
463                         $libopac,
464                         $repeatable,
465                         $mandatory,
466                         $kohafield,
467                         $tab,
468                         $seealso,
469                         $authorised_value,
470                         $authtypecode,
471                         $value_builder,
472                         $hidden,
473                         $isurl,
474                         $frameworkcode,
475                         $link,
476                         $defaultvalue,
477                         $maxlength,
478                     );
479                 }
480             }
481         }
482     }
483     $sth_insert->finish;
484     $sth_update->finish;
485     print
486 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
487     exit;
488
489     # END $OP eq ADD_VALIDATE
490 ################## DELETE_CONFIRM ##################################
491     # called by default form, used to confirm deletion of data in DB
492 }
493 elsif ( $op eq 'delete_confirm' ) {
494     my $dbh = C4::Context->dbh;
495     my $sth =
496       $dbh->prepare(
497 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
498       );
499
500     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
501     my $data = $sth->fetchrow_hashref;
502     $sth->finish;
503     $template->param(
504         liblibrarian  => $data->{'liblibrarian'},
505         tagsubfield   => $data->{'tagsubfield'},
506         delete_link   => $script_name,
507         tagfield      => $tagfield,
508         tagsubfield   => $tagsubfield,
509         frameworkcode => $frameworkcode,
510     );
511
512     # END $OP eq DELETE_CONFIRM
513 ################## DELETE_CONFIRMED ##################################
514   # called by delete_confirm, used to effectively confirm deletion of data in DB
515 }
516 elsif ( $op eq 'delete_confirmed' ) {
517     my $dbh = C4::Context->dbh;
518     unless ( C4::Context->config('demo') eq 1 ) {
519         my $sth =
520           $dbh->prepare(
521 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
522           );
523         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
524         $sth->finish;
525     }
526     print
527 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
528     exit;
529     $template->param( tagfield => $tagfield );
530
531     # END $OP eq DELETE_CONFIRMED
532 ################## DEFAULT ##################################
533 }
534 else {    # DEFAULT
535     my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
536     my @loop_data = ();
537     for ( my $i = 0; $i < $count; $i++ ) {
538         my %row_data;    # get a fresh hash for the row data
539         $row_data{tagfield}         = $results->[$i]{'tagfield'};
540         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
541         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
542         $row_data{kohafield}        = $results->[$i]{'kohafield'};
543         $row_data{repeatable}       = $results->[$i]{'repeatable'};
544         $row_data{mandatory}        = $results->[$i]{'mandatory'};
545         $row_data{tab}              = $results->[$i]{'tab'};
546         $row_data{seealso}          = $results->[$i]{'seealso'};
547         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
548         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
549         $row_data{value_builder}    = $results->[$i]{'value_builder'};
550         $row_data{hidden}           = $results->[$i]{'hidden'};
551         $row_data{isurl}            = $results->[$i]{'isurl'};
552         $row_data{link}             = $results->[$i]{'link'};
553
554         if ( $row_data{tab} eq -1 ) {
555             $row_data{subfield_ignored} = 1;
556         }
557
558         push( @loop_data, \%row_data );
559     }
560     $template->param( loop => \@loop_data );
561     $template->param(
562         edit_tagfield      => $tagfield,
563         edit_frameworkcode => $frameworkcode
564     );
565
566 }    #---- END $OP eq DEFAULT
567
568 output_html_with_http_headers $input, $cookie, $template->output;