why the hell do we need to explicitly utf8 decode this string ? I really don't know...
[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::Interface::CGI::Output;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26
27
28 sub StringSearch {
29     my ( $env, $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 my $input         = new CGI;
54 my $tagfield      = $input->param('tagfield');
55 my $tagsubfield   = $input->param('tagsubfield');
56 my $frameworkcode = $input->param('frameworkcode');
57 my $pkfield       = "tagfield";
58 my $offset        = $input->param('offset');
59 my $script_name   = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
60
61 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
62     {
63         template_name   => "admin/marc_subfields_structure.tmpl",
64         query           => $input,
65         type            => "intranet",
66         authnotrequired => 0,
67         flagsrequired   => { parameters => 1 },
68         debug           => 1,
69     }
70 );
71 my $pagesize = 30;
72 my $op       = $input->param('op');
73 $tagfield =~ s/\,//g;
74
75 if ($op) {
76     $template->param(
77         script_name   => $script_name,
78         tagfield      => $tagfield,
79         frameworkcode => $frameworkcode,
80         $op           => 1
81     );    # we show only the TMPL_VAR names $op
82 }
83 else {
84     $template->param(
85         script_name   => $script_name,
86         tagfield      => $tagfield,
87         frameworkcode => $frameworkcode,
88         else          => 1
89     );    # we show only the TMPL_VAR names $op
90 }
91
92 ################## ADD_FORM ##################################
93 # called by default. Used to create form to add or  modify a record
94 if ( $op eq 'add_form' ) {
95     my $data;
96     my $dbh            = C4::Context->dbh;
97     my $more_subfields = $input->param("more_subfields") + 1;
98
99     # builds kohafield tables
100     my @kohafields;
101     push @kohafields, "";
102     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
103     $sth2->execute;
104     while ( ( my $field ) = $sth2->fetchrow_array ) {
105         push @kohafields, "biblio." . $field;
106     }
107     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
108     $sth2->execute;
109     while ( ( my $field ) = $sth2->fetchrow_array ) {
110         if ( $field eq 'notes' ) { $field = 'bnotes'; }
111         push @kohafields, "biblioitems." . $field;
112     }
113     my $sth2 = $dbh->prepare("SHOW COLUMNS from items");
114     $sth2->execute;
115     while ( ( my $field ) = $sth2->fetchrow_array ) {
116         push @kohafields, "items." . $field;
117     }
118
119     # build authorised value list
120     $sth2->finish;
121     $sth2 = $dbh->prepare("select distinct category from authorised_values");
122     $sth2->execute;
123     my @authorised_values;
124     push @authorised_values, "";
125     while ( ( my $category ) = $sth2->fetchrow_array ) {
126         push @authorised_values, $category;
127     }
128     push( @authorised_values, "branches" );
129     push( @authorised_values, "itemtypes" );
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     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 = $i ; $j <= $more_subfields + $i ; $j++ ) {
282         my %row_data;    # get a fresh hash for the row data
283         $row_data{tab} = CGI::scrolling_list(
284             -name   => 'tab',
285             -id     => "tab$j",
286             -values =>
287               [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
288             -labels => {
289                 '-1' => 'ignore',
290                 '0'  => '0',
291                 '1'  => '1',
292                 '2'  => '2',
293                 '3'  => '3',
294                 '4'  => '4',
295                 '5'  => '5',
296                 '6'  => '6',
297                 '7'  => '7',
298                 '8'  => '8',
299                 '9'  => '9',
300                 '10' => 'items (10)',
301             },
302             -default  => "",
303             -size     => 1,
304             -tabindex => '',
305             -multiple => 0,
306         );
307         $row_data{tagsubfield} =
308             "<input type=\"text\" name=\"tagsubfield\" value=\""
309           . $data->{'tagsubfield'}
310           . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
311         $row_data{liblibrarian} = "";
312         $row_data{libopac}      = "";
313         $row_data{seealso}      = "";
314         $row_data{kohafield}    = CGI::scrolling_list(
315             -name     => 'kohafield',
316             -id       => "kohafield$j",
317             -values   => \@kohafields,
318             -default  => "",
319             -size     => 1,
320             -tabindex => '',
321             -multiple => 0,
322         );
323         $row_data{hidden}     = "";
324         $row_data{repeatable} = CGI::checkbox(
325             -name     => "repeatable$j",
326             -id       => "repeatable$j",
327             -checked  => '',
328             -value    => 1,
329             -tabindex => '',
330             -label    => ''
331         );
332         $row_data{mandatory} = CGI::checkbox(
333             -name     => "mandatory$j",
334             -id       => "mandatory$j",
335             -checked  => '',
336             -value    => 1,
337             -tabindex => '',
338             -label    => ''
339         );
340         $row_data{isurl} = CGI::checkbox(
341             -name     => "isurl$j",
342             -id       => "isurl$j",
343             -checked  => '',
344             -value    => 1,
345             -tabindex => '',
346             -label    => ''
347         );
348         $row_data{value_builder} = CGI::scrolling_list(
349             -name     => 'value_builder',
350             -id       => 'value_builder',
351             -values   => \@value_builder,
352             -default  => $data->{'value_builder'},
353             -size     => 1,
354             -tabindex => '',
355             -multiple => 0,
356         );
357         $row_data{authorised_value} = CGI::scrolling_list(
358             -name     => 'authorised_value',
359             -id       => 'authorised_value',
360             -values   => \@authorised_values,
361             -size     => 1,
362             -tabindex => '',
363             -multiple => 0,
364         );
365         $row_data{authtypes} = CGI::scrolling_list(
366             -name     => 'authtypecode',
367             -id       => 'authtypecode',
368             -values   => \@authtypes,
369             -size     => 1,
370             -tabindex => '',
371             -multiple => 0,
372         );
373         $row_data{link}   = CGI::escapeHTML( $data->{'link'} );
374         $row_data{toggle} = $toggle;
375         $row_data{row}    = $j;
376         push( @loop_data, \%row_data );
377     }
378     $template->param( 'use-heading-flags-p'      => 1 );
379     $template->param( 'heading-edit-subfields-p' => 1 );
380     $template->param(
381         action   => "Edit subfields",
382         tagfield =>
383 "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
384         loop           => \@loop_data,
385         more_subfields => $more_subfields,
386         more_tag       => $tagfield
387     );
388
389     # END $OP eq ADD_FORM
390 ################## ADD_VALIDATE ##################################
391     # called by add_form, used to insert/modify data in DB
392 }
393 elsif ( $op eq 'add_validate' ) {
394     my $dbh = C4::Context->dbh;
395     $template->param( tagfield => "$input->param('tagfield')" );
396     my $sth = $dbh->prepare(
397 "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
398                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
399     );
400     my @tagsubfield       = $input->param('tagsubfield');
401     my @liblibrarian      = $input->param('liblibrarian');
402     my @libopac           = $input->param('libopac');
403     my @kohafield         = $input->param('kohafield');
404     my @tab               = $input->param('tab');
405     my @seealso           = $input->param('seealso');
406     my @hidden            = $input->param('hidden');
407     my @authorised_values = $input->param('authorised_value');
408     my @authtypecodes     = $input->param('authtypecode');
409     my @value_builder     = $input->param('value_builder');
410     my @link              = $input->param('link');
411     my @defaultvalue      = $input->param('defaultvalue');
412     
413     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
414         my $tagfield    = $input->param('tagfield');
415         my $tagsubfield = $tagsubfield[$i];
416         $tagsubfield = "@" unless $tagsubfield ne '';
417         my $liblibrarian     = $liblibrarian[$i];
418         my $libopac          = $libopac[$i];
419         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
420         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
421         my $kohafield        = $kohafield[$i];
422         my $tab              = $tab[$i];
423         my $seealso          = $seealso[$i];
424         my $authorised_value = $authorised_values[$i];
425         my $authtypecode     = $authtypecodes[$i];
426         my $value_builder    = $value_builder[$i];
427         my $hidden = $hidden[$i];                     #input->param("hidden$i");
428         my $isurl  = $input->param("isurl$i") ? 1 : 0;
429         my $link   = $link[$i];
430         my $defaultvalue = $defaultvalue[$i];
431         
432         if ($liblibrarian) {
433             unless ( C4::Context->config('demo') eq 1 ) {
434                 $sth->execute(
435                     $tagfield,
436                     $tagsubfield,
437                     $liblibrarian,
438                     $libopac,
439                     $repeatable,
440                     $mandatory,
441                     $kohafield,
442                     $tab,
443                     $seealso,
444                     $authorised_value,
445                     $authtypecode,
446                     $value_builder,
447                     $hidden,
448                     $isurl,
449                     $frameworkcode,
450                     $link,
451                     $defaultvalue,
452                 );
453             }
454         }
455     }
456     $sth->finish;
457     print
458 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
459     exit;
460
461     # END $OP eq ADD_VALIDATE
462 ################## DELETE_CONFIRM ##################################
463     # called by default form, used to confirm deletion of data in DB
464 }
465 elsif ( $op eq 'delete_confirm' ) {
466     my $dbh = C4::Context->dbh;
467     my $sth =
468       $dbh->prepare(
469 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
470       );
471
472     #FIXME : called with 2 bind variables when 3 are needed
473     $sth->execute( $tagfield, $tagsubfield );
474     my $data = $sth->fetchrow_hashref;
475     $sth->finish;
476     $template->param(
477         liblibrarian  => $data->{'liblibrarian'},
478         tagsubfield   => $data->{'tagsubfield'},
479         delete_link   => $script_name,
480         tagfield      => $tagfield,
481         tagsubfield   => $tagsubfield,
482         frameworkcode => $frameworkcode,
483     );
484
485     # END $OP eq DELETE_CONFIRM
486 ################## DELETE_CONFIRMED ##################################
487   # called by delete_confirm, used to effectively confirm deletion of data in DB
488 }
489 elsif ( $op eq 'delete_confirmed' ) {
490     my $dbh = C4::Context->dbh;
491     unless ( C4::Context->config('demo') eq 1 ) {
492         my $sth =
493           $dbh->prepare(
494 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
495           );
496         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
497         $sth->finish;
498     }
499     print
500 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
501     exit;
502     $template->param( tagfield => $tagfield );
503
504     # END $OP eq DELETE_CONFIRMED
505 ################## DEFAULT ##################################
506 }
507 else {    # DEFAULT
508     my $env;
509     my ( $count, $results ) = StringSearch( $env, $tagfield, $frameworkcode );
510     my $toggle    = 1;
511     my @loop_data = ();
512     for (
513         my $i = $offset ;
514         $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
515         $i++
516       )
517     {
518         if ( $toggle eq 1 ) {
519             $toggle = 0;
520         }
521         else {
522             $toggle = 1;
523         }
524         my %row_data;    # get a fresh hash for the row data
525         $row_data{tagfield}         = $results->[$i]{'tagfield'};
526         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
527         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
528         $row_data{kohafield}        = $results->[$i]{'kohafield'};
529         $row_data{repeatable}       = $results->[$i]{'repeatable'};
530         $row_data{mandatory}        = $results->[$i]{'mandatory'};
531         $row_data{tab}              = $results->[$i]{'tab'};
532         $row_data{seealso}          = $results->[$i]{'seealso'};
533         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
534         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
535         $row_data{value_builder}    = $results->[$i]{'value_builder'};
536         $row_data{hidden}           = $results->[$i]{'hidden'};
537         $row_data{isurl}            = $results->[$i]{'isurl'};
538         $row_data{link}             = $results->[$i]{'link'};
539         $row_data{delete}           =
540 "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield="
541           . $results->[$i]{'tagsubfield'}
542           . "&frameworkcode=$frameworkcode";
543         $row_data{toggle} = $toggle;
544
545         if ( $row_data{tab} eq -1 ) {
546             $row_data{subfield_ignored} = 1;
547         }
548
549         push( @loop_data, \%row_data );
550     }
551     $template->param( loop => \@loop_data );
552     $template->param(
553         edit_tagfield      => $tagfield,
554         edit_frameworkcode => $frameworkcode
555     );
556
557     if ( $offset > 0 ) {
558         my $prevpage = $offset - $pagesize;
559         $template->param(
560             prev => "<a href=\"$script_name?offset=$prevpage\">" );
561     }
562     if ( $offset + $pagesize < $count ) {
563         my $nextpage = $offset + $pagesize;
564         $template->param(
565             next => "<a href=\"$script_name?offset=$nextpage\">" );
566     }
567 }    #---- END $OP eq DEFAULT
568
569 output_html_with_http_headers $input, $cookie, $template->output;