bugfix for ???
[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 string_search {
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 sub marc_subfield_structure_exists {
53     my ($tagfield, $tagsubfield, $frameworkcode) = @_;
54     my $dbh  = C4::Context->dbh;
55     my $sql  = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
56     my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
57     return @$rows > 0;
58 }
59
60 my $input         = new CGI;
61 my $tagfield      = $input->param('tagfield');
62 my $tagsubfield   = $input->param('tagsubfield');
63 my $frameworkcode = $input->param('frameworkcode');
64 my $pkfield       = "tagfield";
65 my $offset        = $input->param('offset');
66 my $script_name   = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
67
68 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
69     {
70         template_name   => "admin/marc_subfields_structure.tmpl",
71         query           => $input,
72         type            => "intranet",
73         authnotrequired => 0,
74         flagsrequired   => { parameters => 1 },
75         debug           => 1,
76     }
77 );
78 my $pagesize = 30;
79 my $op       = $input->param('op');
80 $tagfield =~ s/\,//g;
81
82 if ($op) {
83     $template->param(
84         script_name   => $script_name,
85         tagfield      => $tagfield,
86         frameworkcode => $frameworkcode,
87         $op           => 1
88     );    # we show only the TMPL_VAR names $op
89 }
90 else {
91     $template->param(
92         script_name   => $script_name,
93         tagfield      => $tagfield,
94         frameworkcode => $frameworkcode,
95         else          => 1
96     );    # we show only the TMPL_VAR names $op
97 }
98
99 ################## ADD_FORM ##################################
100 # called by default. Used to create form to add or  modify a record
101 if ( $op eq 'add_form' ) {
102     my $data;
103     my $dbh            = C4::Context->dbh;
104     my $more_subfields = $input->param("more_subfields") + 1;
105
106     # builds kohafield tables
107     my @kohafields;
108     push @kohafields, "";
109     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
110     $sth2->execute;
111     while ( ( my $field ) = $sth2->fetchrow_array ) {
112         push @kohafields, "biblio." . $field;
113     }
114     $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
115     $sth2->execute;
116     while ( ( my $field ) = $sth2->fetchrow_array ) {
117         if ( $field eq 'notes' ) { $field = 'bnotes'; }
118         push @kohafields, "biblioitems." . $field;
119     }
120     $sth2 = $dbh->prepare("SHOW COLUMNS from items");
121     $sth2->execute;
122     while ( ( my $field ) = $sth2->fetchrow_array ) {
123         push @kohafields, "items." . $field;
124     }
125
126     # build authorised value list
127     $sth2->finish;
128     $sth2 = $dbh->prepare("select distinct category from authorised_values");
129     $sth2->execute;
130     my @authorised_values;
131     push @authorised_values, "";
132     while ( ( my $category ) = $sth2->fetchrow_array ) {
133         push @authorised_values, $category;
134     }
135     push( @authorised_values, "branches" );
136     push( @authorised_values, "itemtypes" );
137     push( @authorised_values, "cn_source" );
138
139     # build thesaurus categories list
140     $sth2->finish;
141     $sth2 = $dbh->prepare("select authtypecode from auth_types");
142     $sth2->execute;
143     my @authtypes;
144     push @authtypes, "";
145     while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
146         push @authtypes, $authtypecode;
147     }
148
149     # build value_builder list
150     my @value_builder = ('');
151
152     # read value_builder directory.
153     # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
154     # on a standard install, /cgi-bin need to be added.
155     # test one, then the other
156     my $cgidir = C4::Context->intranetdir . "/cgi-bin";
157     unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
158         $cgidir = C4::Context->intranetdir;
159         opendir( DIR, "$cgidir/cataloguing/value_builder" )
160           || die "can't opendir $cgidir/value_builder: $!";
161     }
162     while ( my $line = readdir(DIR) ) {
163         if ( $line =~ /\.pl$/ ) {
164             push( @value_builder, $line );
165         }
166     }
167     @value_builder= sort {$a cmp $b} @value_builder;
168     closedir DIR;
169
170     # build values list
171     my $sth =
172       $dbh->prepare(
173 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
174       );    # and tagsubfield='$tagsubfield'");
175     $sth->execute( $tagfield, $frameworkcode );
176     my @loop_data = ();
177     my $i         = 0;
178     while ( $data = $sth->fetchrow_hashref ) {
179         my %row_data;    # get a fresh hash for the row data
180         $row_data{defaultvalue} = $data->{defaultvalue};
181         $row_data{tab} = CGI::scrolling_list(
182             -name   => 'tab',
183             -id     => "tab$i",
184             -values =>
185               [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
186             -labels => {
187                 '-1' => 'ignore',
188                 '0'  => '0',
189                 '1'  => '1',
190                 '2'  => '2',
191                 '3'  => '3',
192                 '4'  => '4',
193                 '5'  => '5',
194                 '6'  => '6',
195                 '7'  => '7',
196                 '8'  => '8',
197                 '9'  => '9',
198                 '10' => 'items (10)',
199             },
200             -default  => $data->{'tab'},
201             -size     => 1,
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{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
211         $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
212         $row_data{libopac}      = CGI::escapeHTML( $data->{'libopac'} );
213         $row_data{seealso}      = CGI::escapeHTML( $data->{'seealso'} );
214         $row_data{kohafield}    = CGI::scrolling_list(
215             -name     => "kohafield",
216             -id       => "kohafield$i",
217             -values   => \@kohafields,
218             -default  => "$data->{'kohafield'}",
219             -size     => 1,
220             -multiple => 0,
221         );
222         $row_data{authorised_value} = CGI::scrolling_list(
223             -name     => "authorised_value",
224             -id       => "authorised_value$i",
225             -values   => \@authorised_values,
226             -default  => $data->{'authorised_value'},
227             -size     => 1,
228             -multiple => 0,
229         );
230         $row_data{value_builder} = CGI::scrolling_list(
231             -name     => "value_builder",
232             -id       => "value_builder$i",
233             -values   => \@value_builder,
234             -default  => $data->{'value_builder'},
235             -size     => 1,
236             -multiple => 0,
237         );
238         $row_data{authtypes} = CGI::scrolling_list(
239             -name     => "authtypecode",
240             -id       => "authtypecode$i",
241             -values   => \@authtypes,
242             -default  => $data->{'authtypecode'},
243             -size     => 1,
244             -multiple => 0,
245         );
246         $row_data{repeatable} = CGI::checkbox(
247             -name     => "repeatable$i",
248             -checked  => $data->{'repeatable'} ? 'checked' : '',
249             -value    => 1,
250             -label    => '',
251             -id       => "repeatable$i"
252         );
253         $row_data{mandatory} = CGI::checkbox(
254             -name     => "mandatory$i",
255             -checked  => $data->{'mandatory'} ? 'checked' : '',
256             -value    => 1,
257             -label    => '',
258             -id       => "mandatory$i"
259         );
260         $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
261         $row_data{isurl}  = CGI::checkbox(
262             -name     => "isurl$i",
263             -id       => "isurl$i",
264             -checked  => $data->{'isurl'} ? 'checked' : '',
265             -value    => 1,
266             -label    => ''
267         );
268         $row_data{row}    = $i;
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{row}    = $j;
364         push( @loop_data, \%row_data );
365     }
366     $template->param( 'use-heading-flags-p'      => 1 );
367     $template->param( 'heading-edit-subfields-p' => 1 );
368     $template->param(
369         action   => "Edit subfields",
370         tagfield => $tagfield,
371         loop           => \@loop_data,
372         more_subfields => $more_subfields,
373         more_tag       => $tagfield
374     );
375
376     # END $OP eq ADD_FORM
377 ################## ADD_VALIDATE ##################################
378     # called by add_form, used to insert/modify data in DB
379 }
380 elsif ( $op eq 'add_validate' ) {
381     my $dbh = C4::Context->dbh;
382     $template->param( tagfield => "$input->param('tagfield')" );
383 #     my $sth = $dbh->prepare(
384 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
385 #                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
386 #     );
387     my $sth_insert = $dbh->prepare(qq{
388         insert into marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
389         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
390     });
391     my $sth_update = $dbh->prepare(qq{
392         update marc_subfield_structure set tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, authtypecode=?, value_builder=?, hidden=?, isurl=?, frameworkcode=?,  link=?, defaultvalue=?
393         where tagfield=? and tagsubfield=? and frameworkcode=?
394     });
395     my @tagsubfield       = $input->param('tagsubfield');
396     my @liblibrarian      = $input->param('liblibrarian');
397     my @libopac           = $input->param('libopac');
398     my @kohafield         = $input->param('kohafield');
399     my @tab               = $input->param('tab');
400     my @seealso           = $input->param('seealso');
401     my @hidden            = $input->param('hidden');
402     my @authorised_values = $input->param('authorised_value');
403     my @authtypecodes     = $input->param('authtypecode');
404     my @value_builder     = $input->param('value_builder');
405     my @link              = $input->param('link');
406     my @defaultvalue      = $input->param('defaultvalue');
407     
408     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
409         my $tagfield    = $input->param('tagfield');
410         my $tagsubfield = $tagsubfield[$i];
411         $tagsubfield = "@" unless $tagsubfield ne '';
412         $tagsubfield = "@" if $tagsubfield eq '_';
413         my $liblibrarian     = $liblibrarian[$i];
414         my $libopac          = $libopac[$i];
415         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
416         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
417         my $kohafield        = $kohafield[$i];
418         my $tab              = $tab[$i];
419         my $seealso          = $seealso[$i];
420         my $authorised_value = $authorised_values[$i];
421         my $authtypecode     = $authtypecodes[$i];
422         my $value_builder    = $value_builder[$i];
423         my $hidden = $hidden[$i];                     #input->param("hidden$i");
424         my $isurl  = $input->param("isurl$i") ? 1 : 0;
425         my $link   = $link[$i];
426         my $defaultvalue = $defaultvalue[$i];
427         
428         if ($liblibrarian) {
429             unless ( C4::Context->config('demo') eq 1 ) {
430                 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
431                     $sth_update->execute(
432                         $tagfield,
433                         $tagsubfield,
434                         $liblibrarian,
435                         $libopac,
436                         $repeatable,
437                         $mandatory,
438                         $kohafield,
439                         $tab,
440                         $seealso,
441                         $authorised_value,
442                         $authtypecode,
443                         $value_builder,
444                         $hidden,
445                         $isurl,
446                         $frameworkcode,
447                         $link,
448                         $defaultvalue,
449                         (
450                             $tagfield,
451                             $tagsubfield,
452                             $frameworkcode,
453                         ),
454                     );
455                 } else {
456                     $sth_insert->execute(
457                         $tagfield,
458                         $tagsubfield,
459                         $liblibrarian,
460                         $libopac,
461                         $repeatable,
462                         $mandatory,
463                         $kohafield,
464                         $tab,
465                         $seealso,
466                         $authorised_value,
467                         $authtypecode,
468                         $value_builder,
469                         $hidden,
470                         $isurl,
471                         $frameworkcode,
472                         $link,
473                         $defaultvalue,
474                     );
475                 }
476             }
477         }
478     }
479     $sth_insert->finish;
480     $sth_update->finish;
481     print
482 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
483     exit;
484
485     # END $OP eq ADD_VALIDATE
486 ################## DELETE_CONFIRM ##################################
487     # called by default form, used to confirm deletion of data in DB
488 }
489 elsif ( $op eq 'delete_confirm' ) {
490     my $dbh = C4::Context->dbh;
491     my $sth =
492       $dbh->prepare(
493 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
494       );
495
496     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
497     my $data = $sth->fetchrow_hashref;
498     $sth->finish;
499     $template->param(
500         liblibrarian  => $data->{'liblibrarian'},
501         tagsubfield   => $data->{'tagsubfield'},
502         delete_link   => $script_name,
503         tagfield      => $tagfield,
504         tagsubfield   => $tagsubfield,
505         frameworkcode => $frameworkcode,
506     );
507
508     # END $OP eq DELETE_CONFIRM
509 ################## DELETE_CONFIRMED ##################################
510   # called by delete_confirm, used to effectively confirm deletion of data in DB
511 }
512 elsif ( $op eq 'delete_confirmed' ) {
513     my $dbh = C4::Context->dbh;
514     unless ( C4::Context->config('demo') eq 1 ) {
515         my $sth =
516           $dbh->prepare(
517 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
518           );
519         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
520         $sth->finish;
521     }
522     print
523 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
524     exit;
525     $template->param( tagfield => $tagfield );
526
527     # END $OP eq DELETE_CONFIRMED
528 ################## DEFAULT ##################################
529 }
530 else {    # DEFAULT
531     my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
532     my @loop_data = ();
533     for (
534         my $i = $offset ;
535         $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
536         $i++
537       )
538     {
539         my %row_data;    # get a fresh hash for the row data
540         $row_data{tagfield}         = $results->[$i]{'tagfield'};
541         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
542         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
543         $row_data{kohafield}        = $results->[$i]{'kohafield'};
544         $row_data{repeatable}       = $results->[$i]{'repeatable'};
545         $row_data{mandatory}        = $results->[$i]{'mandatory'};
546         $row_data{tab}              = $results->[$i]{'tab'};
547         $row_data{seealso}          = $results->[$i]{'seealso'};
548         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
549         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
550         $row_data{value_builder}    = $results->[$i]{'value_builder'};
551         $row_data{hidden}           = $results->[$i]{'hidden'};
552         $row_data{isurl}            = $results->[$i]{'isurl'};
553         $row_data{link}             = $results->[$i]{'link'};
554         $row_data{delete}           =
555 "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield="
556           . $results->[$i]{'tagsubfield'}
557           . "&amp;frameworkcode=$frameworkcode";
558
559         if ( $row_data{tab} eq -1 ) {
560             $row_data{subfield_ignored} = 1;
561         }
562
563         push( @loop_data, \%row_data );
564     }
565     $template->param( loop => \@loop_data );
566     $template->param(
567         edit_tagfield      => $tagfield,
568         edit_frameworkcode => $frameworkcode
569     );
570
571     if ( $offset > 0 ) {
572         my $prevpage = $offset - $pagesize;
573         $template->param(
574             prev => "<a href=\"$script_name?offset=$prevpage\&tagfield=$tagfield\&frameworkcode=$frameworkcode \">" );
575     }
576     if ( $offset + $pagesize < $count ) {
577         my $nextpage = $offset + $pagesize;
578         $template->param(
579             next => "<a href=\"$script_name?offset=$nextpage\&tagfield=$tagfield\&frameworkcode=$frameworkcode \">" );
580     }
581 }    #---- END $OP eq DEFAULT
582
583 output_html_with_http_headers $input, $cookie, $template->output;