(bug #2996) fix and improve 100 and 105 unimarc plugins
[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             -multiple => 0,
202         );
203         $row_data{tagsubfield} =
204             $data->{'tagsubfield'}
205           . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
206           . $data->{'tagsubfield'}
207           . "\" id=\"tagsubfield\" />";
208         $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
209         $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
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             -multiple => 0,
220         );
221         $row_data{authorised_value} = CGI::scrolling_list(
222             -name     => "authorised_value",
223             -id       => "authorised_value$i",
224             -values   => \@authorised_values,
225             -default  => $data->{'authorised_value'},
226             -size     => 1,
227             -multiple => 0,
228         );
229         $row_data{value_builder} = CGI::scrolling_list(
230             -name     => "value_builder",
231             -id       => "value_builder$i",
232             -values   => \@value_builder,
233             -default  => $data->{'value_builder'},
234             -size     => 1,
235             -multiple => 0,
236         );
237         $row_data{authtypes} = CGI::scrolling_list(
238             -name     => "authtypecode",
239             -id       => "authtypecode$i",
240             -values   => \@authtypes,
241             -default  => $data->{'authtypecode'},
242             -size     => 1,
243             -multiple => 0,
244         );
245         $row_data{repeatable} = CGI::checkbox(
246             -name     => "repeatable$i",
247             -checked  => $data->{'repeatable'} ? 'checked' : '',
248             -value    => 1,
249             -label    => '',
250             -id       => "repeatable$i"
251         );
252         $row_data{mandatory} = CGI::checkbox(
253             -name     => "mandatory",
254             -checked  => $data->{'mandatory'} ? 'checked' : '',
255             -value    => 1,
256             -label    => '',
257             -id       => "mandatory$i"
258         );
259         $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
260         $row_data{isurl}  = CGI::checkbox(
261             -name     => "isurl$i",
262             -id       => "isurl$i",
263             -checked  => $data->{'isurl'} ? 'checked' : '',
264             -value    => 1,
265             -label    => ''
266         );
267         $row_data{row}    = $i;
268         $row_data{toggle} = $toggle;
269         $row_data{link}   = CGI::escapeHTML( $data->{'link'} ); 
270         push( @loop_data, \%row_data );
271         $i++;
272     }
273
274     # add more_subfields empty lines for add if needed
275     for ( my $j = 1 ; $j <= 1 ; $j++ ) {
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$j",
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$j",
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$j",
321             -id       => "repeatable$j",
322             -checked  => '',
323             -value    => 1,
324             -label    => ''
325         );
326         $row_data{mandatory} = CGI::checkbox(
327             -name     => "mandatory$j",
328             -id       => "mandatory$j",
329             -checked  => '',
330             -value    => 1,
331             -label    => ''
332         );
333         $row_data{isurl} = CGI::checkbox(
334             -name     => "isurl$j",
335             -id       => "isurl$j",
336             -checked  => '',
337             -value    => 1,
338             -label    => ''
339         );
340         $row_data{value_builder} = CGI::scrolling_list(
341             -name     => "value_builder",
342             -id       => "value_builder$j",
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$j",
351             -values   => \@authorised_values,
352             -size     => 1,
353             -multiple => 0,
354         );
355         $row_data{authtypes} = CGI::scrolling_list(
356             -name     => "authtypecode",
357             -id       => "authtypecode$j",
358             -values   => \@authtypes,
359             -size     => 1,
360             -multiple => 0,
361         );
362         $row_data{link}   = CGI::escapeHTML( $data->{'link'} );
363         $row_data{toggle} = $toggle;
364         $row_data{row}    = $j;
365         push( @loop_data, \%row_data );
366         use Data::Dumper;
367     }
368     $template->param( 'use-heading-flags-p'      => 1 );
369     $template->param( 'heading-edit-subfields-p' => 1 );
370     $template->param(
371         action   => "Edit subfields",
372         tagfield => $tagfield,
373         loop           => \@loop_data,
374         more_subfields => $more_subfields,
375         more_tag       => $tagfield
376     );
377
378     # END $OP eq ADD_FORM
379 ################## ADD_VALIDATE ##################################
380     # called by add_form, used to insert/modify data in DB
381 }
382 elsif ( $op eq 'add_validate' ) {
383     my $dbh = C4::Context->dbh;
384     $template->param( tagfield => "$input->param('tagfield')" );
385     my $sth = $dbh->prepare(
386 "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
387                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
388     );
389     my @tagsubfield       = $input->param('tagsubfield');
390     my @liblibrarian      = $input->param('liblibrarian');
391     my @libopac           = $input->param('libopac');
392     my @kohafield         = $input->param('kohafield');
393     my @tab               = $input->param('tab');
394     my @seealso           = $input->param('seealso');
395     my @hidden            = $input->param('hidden');
396     my @authorised_values = $input->param('authorised_value');
397     my @authtypecodes     = $input->param('authtypecode');
398     my @value_builder     = $input->param('value_builder');
399     my @link              = $input->param('link');
400     my @defaultvalue      = $input->param('defaultvalue');
401     
402     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
403         my $tagfield    = $input->param('tagfield');
404         my $tagsubfield = $tagsubfield[$i];
405         $tagsubfield = "@" unless $tagsubfield ne '';
406         $tagsubfield = "@" if $tagsubfield eq '_';
407         my $liblibrarian     = $liblibrarian[$i];
408         my $libopac          = $libopac[$i];
409         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
410         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
411         my $kohafield        = $kohafield[$i];
412         my $tab              = $tab[$i];
413         my $seealso          = $seealso[$i];
414         my $authorised_value = $authorised_values[$i];
415         my $authtypecode     = $authtypecodes[$i];
416         my $value_builder    = $value_builder[$i];
417         my $hidden = $hidden[$i];                     #input->param("hidden$i");
418         my $isurl  = $input->param("isurl$i") ? 1 : 0;
419         my $link   = $link[$i];
420         my $defaultvalue = $defaultvalue[$i];
421         
422         if ($liblibrarian) {
423             unless ( C4::Context->config('demo') eq 1 ) {
424                 $sth->execute(
425                     $tagfield,
426                     $tagsubfield,
427                     $liblibrarian,
428                     $libopac,
429                     $repeatable,
430                     $mandatory,
431                     $kohafield,
432                     $tab,
433                     $seealso,
434                     $authorised_value,
435                     $authtypecode,
436                     $value_builder,
437                     $hidden,
438                     $isurl,
439                     $frameworkcode,
440                     $link,
441                     $defaultvalue,
442                 );
443             }
444         }
445     }
446     $sth->finish;
447     print
448 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
449     exit;
450
451     # END $OP eq ADD_VALIDATE
452 ################## DELETE_CONFIRM ##################################
453     # called by default form, used to confirm deletion of data in DB
454 }
455 elsif ( $op eq 'delete_confirm' ) {
456     my $dbh = C4::Context->dbh;
457     my $sth =
458       $dbh->prepare(
459 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
460       );
461
462     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
463     my $data = $sth->fetchrow_hashref;
464     $sth->finish;
465     $template->param(
466         liblibrarian  => $data->{'liblibrarian'},
467         tagsubfield   => $data->{'tagsubfield'},
468         delete_link   => $script_name,
469         tagfield      => $tagfield,
470         tagsubfield   => $tagsubfield,
471         frameworkcode => $frameworkcode,
472     );
473
474     # END $OP eq DELETE_CONFIRM
475 ################## DELETE_CONFIRMED ##################################
476   # called by delete_confirm, used to effectively confirm deletion of data in DB
477 }
478 elsif ( $op eq 'delete_confirmed' ) {
479     my $dbh = C4::Context->dbh;
480     unless ( C4::Context->config('demo') eq 1 ) {
481         my $sth =
482           $dbh->prepare(
483 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
484           );
485         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
486         $sth->finish;
487     }
488     print
489 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
490     exit;
491     $template->param( tagfield => $tagfield );
492
493     # END $OP eq DELETE_CONFIRMED
494 ################## DEFAULT ##################################
495 }
496 else {    # DEFAULT
497     my ( $count, $results ) = StringSearch( $tagfield, $frameworkcode );
498     my $toggle    = 1;
499     my @loop_data = ();
500     for (
501         my $i = $offset ;
502         $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
503         $i++
504       )
505     {
506         if ( $toggle eq 1 ) {
507             $toggle = 0;
508         }
509         else {
510             $toggle = 1;
511         }
512         my %row_data;    # get a fresh hash for the row data
513         $row_data{tagfield}         = $results->[$i]{'tagfield'};
514         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
515         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
516         $row_data{kohafield}        = $results->[$i]{'kohafield'};
517         $row_data{repeatable}       = $results->[$i]{'repeatable'};
518         $row_data{mandatory}        = $results->[$i]{'mandatory'};
519         $row_data{tab}              = $results->[$i]{'tab'};
520         $row_data{seealso}          = $results->[$i]{'seealso'};
521         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
522         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
523         $row_data{value_builder}    = $results->[$i]{'value_builder'};
524         $row_data{hidden}           = $results->[$i]{'hidden'};
525         $row_data{isurl}            = $results->[$i]{'isurl'};
526         $row_data{link}             = $results->[$i]{'link'};
527         $row_data{delete}           =
528 "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield="
529           . $results->[$i]{'tagsubfield'}
530           . "&amp;frameworkcode=$frameworkcode";
531         $row_data{toggle} = $toggle;
532
533         if ( $row_data{tab} eq -1 ) {
534             $row_data{subfield_ignored} = 1;
535         }
536
537         push( @loop_data, \%row_data );
538     }
539     $template->param( loop => \@loop_data );
540     $template->param(
541         edit_tagfield      => $tagfield,
542         edit_frameworkcode => $frameworkcode
543     );
544
545     if ( $offset > 0 ) {
546         my $prevpage = $offset - $pagesize;
547         $template->param(
548             prev => "<a href=\"$script_name?offset=$prevpage\&tagfield=$tagfield\&frameworkcode=$frameworkcode \">" );
549     }
550     if ( $offset + $pagesize < $count ) {
551         my $nextpage = $offset + $pagesize;
552         $template->param(
553             next => "<a href=\"$script_name?offset=$nextpage\&tagfield=$tagfield\&frameworkcode=$frameworkcode \">" );
554     }
555 }    #---- END $OP eq DEFAULT
556
557 output_html_with_http_headers $input, $cookie, $template->output;