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