some (minor, functionnaly speaking) bugfixes
[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     my $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     my $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
130     # build thesaurus categories list
131     $sth2->finish;
132     $sth2 = $dbh->prepare("select authtypecode from auth_types");
133     $sth2->execute;
134     my @authtypes;
135     push @authtypes, "";
136     while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
137         push @authtypes, $authtypecode;
138     }
139
140     # build value_builder list
141     my @value_builder = ('');
142
143     # read value_builder directory.
144     # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
145     # on a standard install, /cgi-bin need to be added.
146     # test one, then the other
147     my $cgidir = C4::Context->intranetdir . "/cgi-bin";
148     unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
149         $cgidir = C4::Context->intranetdir;
150         opendir( DIR, "$cgidir/cataloguing/value_builder" )
151           || die "can't opendir $cgidir/value_builder: $!";
152     }
153     while ( my $line = readdir(DIR) ) {
154         if ( $line =~ /\.pl$/ ) {
155             push( @value_builder, $line );
156         }
157     }
158     @value_builder= sort {$a cmp $b} @value_builder;
159     closedir DIR;
160
161     # build values list
162     my $sth =
163       $dbh->prepare(
164 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
165       );    # and tagsubfield='$tagsubfield'");
166     $sth->execute( $tagfield, $frameworkcode );
167     my @loop_data = ();
168     my $toggle    = 1;
169     my $i         = 0;
170     while ( $data = $sth->fetchrow_hashref ) {
171         my %row_data;    # get a fresh hash for the row data
172         if ( $toggle eq 1 ) {
173             $toggle = 0;
174         }
175         else {
176             $toggle = 1;
177         }
178         $row_data{defaultvalue} = $data->{defaultvalue};
179         $row_data{tab} = CGI::scrolling_list(
180             -name   => 'tab',
181             -id     => "tab$i",
182             -values =>
183               [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
184             -labels => {
185                 '-1' => 'ignore',
186                 '0'  => '0',
187                 '1'  => '1',
188                 '2'  => '2',
189                 '3'  => '3',
190                 '4'  => '4',
191                 '5'  => '5',
192                 '6'  => '6',
193                 '7'  => '7',
194                 '8'  => '8',
195                 '9'  => '9',
196                 '10' => 'items (10)',
197             },
198             -default  => $data->{'tab'},
199             -size     => 1,
200             -tabindex => '',
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'};
209         $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
210         $row_data{libopac}      = CGI::escapeHTML( $data->{'libopac'} );
211         $row_data{seealso}      = CGI::escapeHTML( $data->{'seealso'} );
212         $row_data{kohafield}    = CGI::scrolling_list(
213             -name     => "kohafield",
214             -id       => "kohafield$i",
215             -values   => \@kohafields,
216             -default  => "$data->{'kohafield'}",
217             -size     => 1,
218             -tabindex => '',
219             -multiple => 0,
220         );
221         $row_data{authorised_value} = CGI::scrolling_list(
222             -name     => 'authorised_value',
223             -id       => 'authorised_value',
224             -values   => \@authorised_values,
225             -default  => $data->{'authorised_value'},
226             -size     => 1,
227             -tabindex => '',
228             -multiple => 0,
229         );
230         $row_data{value_builder} = CGI::scrolling_list(
231             -name     => 'value_builder',
232             -id       => 'value_builder',
233             -values   => \@value_builder,
234             -default  => $data->{'value_builder'},
235             -size     => 1,
236             -tabindex => '',
237             -multiple => 0,
238         );
239         $row_data{authtypes} = CGI::scrolling_list(
240             -name     => 'authtypecode',
241             -id       => 'authtypecode',
242             -values   => \@authtypes,
243             -default  => $data->{'authtypecode'},
244             -size     => 1,
245             -tabindex => '',
246             -multiple => 0,
247         );
248         $row_data{repeatable} = CGI::checkbox(
249             -name     => "repeatable$i",
250             -checked  => $data->{'repeatable'} ? 'checked' : '',
251             -value    => 1,
252             -tabindex => '',
253             -label    => '',
254             -id       => "repeatable$i"
255         );
256         $row_data{mandatory} = CGI::checkbox(
257             -name     => "mandatory$i",
258             -checked  => $data->{'mandatory'} ? 'checked' : '',
259             -value    => 1,
260             -tabindex => '',
261             -label    => '',
262             -id       => "mandatory$i"
263         );
264         $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
265         $row_data{isurl}  = CGI::checkbox(
266             -name     => "isurl$i",
267             -id       => "isurl$i",
268             -checked  => $data->{'isurl'} ? 'checked' : '',
269             -value    => 1,
270             -tabindex => '',
271             -label    => ''
272         );
273         $row_data{row}    = $i;
274         $row_data{toggle} = $toggle;
275         $row_data{link}   = CGI::escapeHTML( $data->{'link'} ); 
276         push( @loop_data, \%row_data );
277         $i++;
278     }
279
280     # add more_subfields empty lines for add if needed
281     for ( my $j = 1 ; $j <= 1 ; $j++ ) {
282         my %row_data;    # get a fresh hash for the row data
283         $row_data{'subfieldcode'} = '';
284
285         $row_data{tab} = CGI::scrolling_list(
286             -name   => 'tab',
287             -id     => "tab$j",
288             -values =>
289               [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
290             -labels => {
291                 '-1' => 'ignore',
292                 '0'  => '0',
293                 '1'  => '1',
294                 '2'  => '2',
295                 '3'  => '3',
296                 '4'  => '4',
297                 '5'  => '5',
298                 '6'  => '6',
299                 '7'  => '7',
300                 '8'  => '8',
301                 '9'  => '9',
302                 '10' => 'items (10)',
303             },
304             -default  => "",
305             -size     => 1,
306             -tabindex => '',
307             -multiple => 0,
308         );
309         $row_data{tagsubfield} =
310             "<input type=\"text\" name=\"tagsubfield\" value=\""
311           . $data->{'tagsubfield'}
312           . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
313         $row_data{liblibrarian} = "";
314         $row_data{libopac}      = "";
315         $row_data{seealso}      = "";
316         $row_data{kohafield}    = CGI::scrolling_list(
317             -name     => 'kohafield',
318             -id       => "kohafield$j",
319             -values   => \@kohafields,
320             -default  => "",
321             -size     => 1,
322             -tabindex => '',
323             -multiple => 0,
324         );
325         $row_data{hidden}     = "";
326         $row_data{repeatable} = CGI::checkbox(
327             -name     => "repeatable$j",
328             -id       => "repeatable$j",
329             -checked  => '',
330             -value    => 1,
331             -tabindex => '',
332             -label    => ''
333         );
334         $row_data{mandatory} = CGI::checkbox(
335             -name     => "mandatory$j",
336             -id       => "mandatory$j",
337             -checked  => '',
338             -value    => 1,
339             -tabindex => '',
340             -label    => ''
341         );
342         $row_data{isurl} = CGI::checkbox(
343             -name     => "isurl$j",
344             -id       => "isurl$j",
345             -checked  => '',
346             -value    => 1,
347             -tabindex => '',
348             -label    => ''
349         );
350         $row_data{value_builder} = CGI::scrolling_list(
351             -name     => 'value_builder',
352             -id       => 'value_builder',
353             -values   => \@value_builder,
354             -default  => $data->{'value_builder'},
355             -size     => 1,
356             -tabindex => '',
357             -multiple => 0,
358         );
359         $row_data{authorised_value} = CGI::scrolling_list(
360             -name     => 'authorised_value',
361             -id       => 'authorised_value',
362             -values   => \@authorised_values,
363             -size     => 1,
364             -tabindex => '',
365             -multiple => 0,
366         );
367         $row_data{authtypes} = CGI::scrolling_list(
368             -name     => 'authtypecode',
369             -id       => 'authtypecode',
370             -values   => \@authtypes,
371             -size     => 1,
372             -tabindex => '',
373             -multiple => 0,
374         );
375         $row_data{link}   = CGI::escapeHTML( $data->{'link'} );
376         $row_data{toggle} = $toggle;
377         $row_data{row}    = $j;
378         push( @loop_data, \%row_data );
379         use Data::Dumper;
380         warn "push : ".Dumper(%row_data);
381     }
382     $template->param( 'use-heading-flags-p'      => 1 );
383     $template->param( 'heading-edit-subfields-p' => 1 );
384     $template->param(
385         action   => "Edit subfields",
386         tagfield =>
387 "<input type=\"hidden\" name=\"tagfield\" value=\"$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         my $liblibrarian     = $liblibrarian[$i];
422         my $libopac          = $libopac[$i];
423         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
424         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
425         my $kohafield        = $kohafield[$i];
426         my $tab              = $tab[$i];
427         my $seealso          = $seealso[$i];
428         my $authorised_value = $authorised_values[$i];
429         my $authtypecode     = $authtypecodes[$i];
430         my $value_builder    = $value_builder[$i];
431         my $hidden = $hidden[$i];                     #input->param("hidden$i");
432         my $isurl  = $input->param("isurl$i") ? 1 : 0;
433         my $link   = $link[$i];
434         my $defaultvalue = $defaultvalue[$i];
435         
436         if ($liblibrarian) {
437             unless ( C4::Context->config('demo') eq 1 ) {
438                 $sth->execute(
439                     $tagfield,
440                     $tagsubfield,
441                     $liblibrarian,
442                     $libopac,
443                     $repeatable,
444                     $mandatory,
445                     $kohafield,
446                     $tab,
447                     $seealso,
448                     $authorised_value,
449                     $authtypecode,
450                     $value_builder,
451                     $hidden,
452                     $isurl,
453                     $frameworkcode,
454                     $link,
455                     $defaultvalue,
456                 );
457             }
458         }
459     }
460     $sth->finish;
461     print
462 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
463     exit;
464
465     # END $OP eq ADD_VALIDATE
466 ################## DELETE_CONFIRM ##################################
467     # called by default form, used to confirm deletion of data in DB
468 }
469 elsif ( $op eq 'delete_confirm' ) {
470     my $dbh = C4::Context->dbh;
471     my $sth =
472       $dbh->prepare(
473 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
474       );
475
476     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
477     my $data = $sth->fetchrow_hashref;
478     $sth->finish;
479     $template->param(
480         liblibrarian  => $data->{'liblibrarian'},
481         tagsubfield   => $data->{'tagsubfield'},
482         delete_link   => $script_name,
483         tagfield      => $tagfield,
484         tagsubfield   => $tagsubfield,
485         frameworkcode => $frameworkcode,
486     );
487
488     # END $OP eq DELETE_CONFIRM
489 ################## DELETE_CONFIRMED ##################################
490   # called by delete_confirm, used to effectively confirm deletion of data in DB
491 }
492 elsif ( $op eq 'delete_confirmed' ) {
493     my $dbh = C4::Context->dbh;
494     unless ( C4::Context->config('demo') eq 1 ) {
495         my $sth =
496           $dbh->prepare(
497 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
498           );
499         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
500         $sth->finish;
501     }
502     print
503 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
504     exit;
505     $template->param( tagfield => $tagfield );
506
507     # END $OP eq DELETE_CONFIRMED
508 ################## DEFAULT ##################################
509 }
510 else {    # DEFAULT
511     my ( $count, $results ) = StringSearch( $tagfield, $frameworkcode );
512     my $toggle    = 1;
513     my @loop_data = ();
514     for (
515         my $i = $offset ;
516         $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
517         $i++
518       )
519     {
520         if ( $toggle eq 1 ) {
521             $toggle = 0;
522         }
523         else {
524             $toggle = 1;
525         }
526         my %row_data;    # get a fresh hash for the row data
527         $row_data{tagfield}         = $results->[$i]{'tagfield'};
528         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
529         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
530         $row_data{kohafield}        = $results->[$i]{'kohafield'};
531         $row_data{repeatable}       = $results->[$i]{'repeatable'};
532         $row_data{mandatory}        = $results->[$i]{'mandatory'};
533         $row_data{tab}              = $results->[$i]{'tab'};
534         $row_data{seealso}          = $results->[$i]{'seealso'};
535         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
536         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
537         $row_data{value_builder}    = $results->[$i]{'value_builder'};
538         $row_data{hidden}           = $results->[$i]{'hidden'};
539         $row_data{isurl}            = $results->[$i]{'isurl'};
540         $row_data{link}             = $results->[$i]{'link'};
541         $row_data{delete}           =
542 "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield="
543           . $results->[$i]{'tagsubfield'}
544           . "&frameworkcode=$frameworkcode";
545         $row_data{toggle} = $toggle;
546
547         if ( $row_data{tab} eq -1 ) {
548             $row_data{subfield_ignored} = 1;
549         }
550
551         push( @loop_data, \%row_data );
552     }
553     $template->param( loop => \@loop_data );
554     $template->param(
555         edit_tagfield      => $tagfield,
556         edit_frameworkcode => $frameworkcode
557     );
558
559     if ( $offset > 0 ) {
560         my $prevpage = $offset - $pagesize;
561         $template->param(
562             prev => "<a href=\"$script_name?offset=$prevpage\">" );
563     }
564     if ( $offset + $pagesize < $count ) {
565         my $nextpage = $offset + $pagesize;
566         $template->param(
567             next => "<a href=\"$script_name?offset=$nextpage\">" );
568     }
569 }    #---- END $OP eq DEFAULT
570
571 output_html_with_http_headers $input, $cookie, $template->output;