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