Merge remote-tracking branch 'origin/new/bug_7876'
[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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Output;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26
27
28 sub string_search {
29     my ( $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 sub marc_subfield_structure_exists {
54     my ($tagfield, $tagsubfield, $frameworkcode) = @_;
55     my $dbh  = C4::Context->dbh;
56     my $sql  = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
57     my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
58     return @$rows > 0;
59 }
60
61 my $input         = new CGI;
62 my $tagfield      = $input->param('tagfield');
63 my $tagsubfield   = $input->param('tagsubfield');
64 my $frameworkcode = $input->param('frameworkcode');
65 my $pkfield       = "tagfield";
66 my $offset        = $input->param('offset');
67 my $script_name   = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
68
69 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
70     {
71         template_name   => "admin/marc_subfields_structure.tmpl",
72         query           => $input,
73         type            => "intranet",
74         authnotrequired => 0,
75         flagsrequired   => { parameters => 1 },
76         debug           => 1,
77     }
78 );
79
80 my $op       = $input->param('op');
81 $tagfield =~ s/\,//g;
82
83 if ($op) {
84     $template->param(
85         script_name   => $script_name,
86         tagfield      => $tagfield,
87         frameworkcode => $frameworkcode,
88         $op           => 1
89     );    # we show only the TMPL_VAR names $op
90 }
91 else {
92     $template->param(
93         script_name   => $script_name,
94         tagfield      => $tagfield,
95         frameworkcode => $frameworkcode,
96         else          => 1
97     );    # we show only the TMPL_VAR names $op
98 }
99
100 ################## ADD_FORM ##################################
101 # called by default. Used to create form to add or  modify a record
102 if ( $op eq 'add_form' ) {
103     my $data;
104     my $dbh            = C4::Context->dbh;
105     my $more_subfields = $input->param("more_subfields") + 1;
106
107     # builds kohafield tables
108     my @kohafields;
109     push @kohafields, "";
110     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
111     $sth2->execute;
112     while ( ( my $field ) = $sth2->fetchrow_array ) {
113         push @kohafields, "biblio." . $field;
114     }
115     $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
116     $sth2->execute;
117     while ( ( my $field ) = $sth2->fetchrow_array ) {
118         if ( $field eq 'notes' ) { $field = 'bnotes'; }
119         push @kohafields, "biblioitems." . $field;
120     }
121     $sth2 = $dbh->prepare("SHOW COLUMNS from items");
122     $sth2->execute;
123     while ( ( my $field ) = $sth2->fetchrow_array ) {
124         push @kohafields, "items." . $field;
125     }
126
127     # build authorised value list
128     $sth2->finish;
129     $sth2 = $dbh->prepare("select distinct category from authorised_values");
130     $sth2->execute;
131     my @authorised_values;
132     push @authorised_values, "";
133     while ( ( my $category ) = $sth2->fetchrow_array ) {
134         push @authorised_values, $category;
135     }
136     push( @authorised_values, "branches" );
137     push( @authorised_values, "itemtypes" );
138     push( @authorised_values, "cn_source" );
139
140     # build thesaurus categories list
141     $sth2->finish;
142     $sth2 = $dbh->prepare("select authtypecode from auth_types");
143     $sth2->execute;
144     my @authtypes;
145     push @authtypes, "";
146     while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
147         push @authtypes, $authtypecode;
148     }
149
150     # build value_builder list
151     my @value_builder = ('');
152
153     # read value_builder directory.
154     # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
155     # on a standard install, /cgi-bin need to be added.
156     # test one, then the other
157     my $cgidir = C4::Context->intranetdir . "/cgi-bin";
158     unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
159         $cgidir = C4::Context->intranetdir;
160         opendir( DIR, "$cgidir/cataloguing/value_builder" )
161           || die "can't opendir $cgidir/value_builder: $!";
162     }
163     while ( my $line = readdir(DIR) ) {
164         if ( $line =~ /\.pl$/ ) {
165             push( @value_builder, $line );
166         }
167     }
168     @value_builder= sort {$a cmp $b} @value_builder;
169     closedir DIR;
170
171     # build values list
172     my $sth =
173       $dbh->prepare(
174 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
175       );    # and tagsubfield='$tagsubfield'");
176     $sth->execute( $tagfield, $frameworkcode );
177     my @loop_data = ();
178     my $i         = 0;
179     while ( $data = $sth->fetchrow_hashref ) {
180         my %row_data;    # get a fresh hash for the row data
181         $row_data{defaultvalue} = $data->{defaultvalue};
182         $row_data{maxlength} = $data->{maxlength};
183         $row_data{tab} = CGI::scrolling_list(
184             -name   => 'tab',
185             -id     => "tab$i",
186             -values =>
187               [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
188             -labels => {
189                 '-1' => 'ignore',
190                 '0'  => '0',
191                 '1'  => '1',
192                 '2'  => '2',
193                 '3'  => '3',
194                 '4'  => '4',
195                 '5'  => '5',
196                 '6'  => '6',
197                 '7'  => '7',
198                 '8'  => '8',
199                 '9'  => '9',
200                 '10' => 'items (10)',
201             },
202             -default  => $data->{'tab'},
203             -size     => 1,
204             -multiple => 0,
205         );
206         $row_data{tagsubfield} =
207             $data->{'tagsubfield'}
208           . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
209           . $data->{'tagsubfield'}
210           . "\" id=\"tagsubfield\" />";
211         $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
212         $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
213         $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
214         $row_data{libopac}      = CGI::escapeHTML( $data->{'libopac'} );
215         $row_data{seealso}      = CGI::escapeHTML( $data->{'seealso'} );
216         $row_data{kohafield}    = CGI::scrolling_list(
217             -name     => "kohafield",
218             -id       => "kohafield$i",
219             -values   => \@kohafields,
220             -default  => "$data->{'kohafield'}",
221             -size     => 1,
222             -multiple => 0,
223         );
224         $row_data{authorised_value} = CGI::scrolling_list(
225             -name     => "authorised_value",
226             -id       => "authorised_value$i",
227             -values   => \@authorised_values,
228             -default  => $data->{'authorised_value'},
229             -size     => 1,
230             -multiple => 0,
231         );
232         $row_data{value_builder} = CGI::scrolling_list(
233             -name     => "value_builder",
234             -id       => "value_builder$i",
235             -values   => \@value_builder,
236             -default  => $data->{'value_builder'},
237             -size     => 1,
238             -multiple => 0,
239         );
240         $row_data{authtypes} = CGI::scrolling_list(
241             -name     => "authtypecode",
242             -id       => "authtypecode$i",
243             -values   => \@authtypes,
244             -default  => $data->{'authtypecode'},
245             -size     => 1,
246             -multiple => 0,
247         );
248         $row_data{repeatable} = CGI::checkbox(
249             -name     => "repeatable$i",
250             -checked  => $data->{'repeatable'} ? 'checked' : '',
251             -value    => 1,
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             -label    => '',
260             -id       => "mandatory$i"
261         );
262         $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
263         $row_data{isurl}  = CGI::checkbox(
264             -name     => "isurl$i",
265             -id       => "isurl$i",
266             -checked  => $data->{'isurl'} ? 'checked' : '',
267             -value    => 1,
268             -label    => ''
269         );
270         $row_data{row}    = $i;
271         $row_data{link}   = CGI::escapeHTML( $data->{'link'} ); 
272         push( @loop_data, \%row_data );
273         $i++;
274     }
275
276     # add more_subfields empty lines for add if needed
277     for ( my $j = 1 ; $j <= 1 ; $j++ ) {
278         my %row_data;    # get a fresh hash for the row data
279         $row_data{'new_subfield'} = 1;
280         $row_data{'subfieldcode'} = '';
281
282         $row_data{tab} = CGI::scrolling_list(
283             -name   => 'tab',
284             -id     => "tab$j",
285             -values =>
286               [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
287             -labels => {
288                 '-1' => 'ignore',
289                 '0'  => '0',
290                 '1'  => '1',
291                 '2'  => '2',
292                 '3'  => '3',
293                 '4'  => '4',
294                 '5'  => '5',
295                 '6'  => '6',
296                 '7'  => '7',
297                 '8'  => '8',
298                 '9'  => '9',
299                 '10' => 'items (10)',
300             },
301             -default  => "",
302             -size     => 1,
303             -multiple => 0,
304         );
305         $row_data{tagsubfield} =
306             "<input type=\"text\" name=\"tagsubfield\" value=\""
307           . $data->{'tagsubfield'}
308           . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
309         $row_data{liblibrarian} = "";
310         $row_data{libopac}      = "";
311         $row_data{seealso}      = "";
312         $row_data{kohafield}    = CGI::scrolling_list(
313             -name     => 'kohafield',
314             -id       => "kohafield$j",
315             -values   => \@kohafields,
316             -default  => "",
317             -size     => 1,
318             -multiple => 0,
319         );
320         $row_data{hidden}     = "";
321         $row_data{repeatable} = CGI::checkbox(
322             -name     => "repeatable$j",
323             -id       => "repeatable$j",
324             -checked  => '',
325             -value    => 1,
326             -label    => ''
327         );
328         $row_data{mandatory} = CGI::checkbox(
329             -name     => "mandatory$j",
330             -id       => "mandatory$j",
331             -checked  => '',
332             -value    => 1,
333             -label    => ''
334         );
335         $row_data{isurl} = CGI::checkbox(
336             -name     => "isurl$j",
337             -id       => "isurl$j",
338             -checked  => '',
339             -value    => 1,
340             -label    => ''
341         );
342         $row_data{value_builder} = CGI::scrolling_list(
343             -name     => "value_builder",
344             -id       => "value_builder$j",
345             -values   => \@value_builder,
346             -default  => $data->{'value_builder'},
347             -size     => 1,
348             -multiple => 0,
349         );
350         $row_data{authorised_value} = CGI::scrolling_list(
351             -name     => "authorised_value",
352             -id       => "authorised_value$j",
353             -values   => \@authorised_values,
354             -size     => 1,
355             -multiple => 0,
356         );
357         $row_data{authtypes} = CGI::scrolling_list(
358             -name     => "authtypecode",
359             -id       => "authtypecode$j",
360             -values   => \@authtypes,
361             -size     => 1,
362             -multiple => 0,
363         );
364         $row_data{link}   = CGI::escapeHTML( $data->{'link'} );
365         $row_data{row}    = $j;
366         push( @loop_data, \%row_data );
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 $sth_insert = $dbh->prepare(qq{
390         insert into marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue,maxlength)
391         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
392     });
393     my $sth_update = $dbh->prepare(qq{
394         update marc_subfield_structure set tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, authtypecode=?, value_builder=?, hidden=?, isurl=?, frameworkcode=?,  link=?, defaultvalue=?, maxlength=?
395         where tagfield=? and tagsubfield=? and frameworkcode=?
396     });
397     my @tagsubfield       = $input->param('tagsubfield');
398     my @liblibrarian      = $input->param('liblibrarian');
399     my @libopac           = $input->param('libopac');
400     my @kohafield         = $input->param('kohafield');
401     my @tab               = $input->param('tab');
402     my @seealso           = $input->param('seealso');
403     my @hidden            = $input->param('hidden');
404     my @authorised_values = $input->param('authorised_value');
405     my @authtypecodes     = $input->param('authtypecode');
406     my @value_builder     = $input->param('value_builder');
407     my @link              = $input->param('link');
408     my @defaultvalue      = $input->param('defaultvalue');
409     my @maxlength         = $input->param('maxlength');
410     
411     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
412         my $tagfield    = $input->param('tagfield');
413         my $tagsubfield = $tagsubfield[$i];
414         $tagsubfield = "@" unless $tagsubfield ne '';
415         $tagsubfield = "@" if $tagsubfield eq '_';
416         my $liblibrarian     = $liblibrarian[$i];
417         my $libopac          = $libopac[$i];
418         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
419         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
420         my $kohafield        = $kohafield[$i];
421         my $tab              = $tab[$i];
422         my $seealso          = $seealso[$i];
423         my $authorised_value = $authorised_values[$i];
424         my $authtypecode     = $authtypecodes[$i];
425         my $value_builder    = $value_builder[$i];
426         my $hidden = $hidden[$i];                     #input->param("hidden$i");
427         my $isurl  = $input->param("isurl$i") ? 1 : 0;
428         my $link   = $link[$i];
429         my $defaultvalue = $defaultvalue[$i];
430         my $maxlength = $maxlength[$i];
431         
432         if (defined($liblibrarian) && $liblibrarian ne "") {
433             unless ( C4::Context->config('demo') eq 1 ) {
434                 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
435                     $sth_update->execute(
436                         $tagfield,
437                         $tagsubfield,
438                         $liblibrarian,
439                         $libopac,
440                         $repeatable,
441                         $mandatory,
442                         $kohafield,
443                         $tab,
444                         $seealso,
445                         $authorised_value,
446                         $authtypecode,
447                         $value_builder,
448                         $hidden,
449                         $isurl,
450                         $frameworkcode,
451                         $link,
452                         $defaultvalue,
453                         $maxlength,
454                         (
455                             $tagfield,
456                             $tagsubfield,
457                             $frameworkcode,
458                         ),
459                     );
460                 } else {
461                     $sth_insert->execute(
462                         $tagfield,
463                         $tagsubfield,
464                         $liblibrarian,
465                         $libopac,
466                         $repeatable,
467                         $mandatory,
468                         $kohafield,
469                         $tab,
470                         $seealso,
471                         $authorised_value,
472                         $authtypecode,
473                         $value_builder,
474                         $hidden,
475                         $isurl,
476                         $frameworkcode,
477                         $link,
478                         $defaultvalue,
479                         $maxlength,
480                     );
481                 }
482             }
483         }
484     }
485     $sth_insert->finish;
486     $sth_update->finish;
487     print
488 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
489     exit;
490
491     # END $OP eq ADD_VALIDATE
492 ################## DELETE_CONFIRM ##################################
493     # called by default form, used to confirm deletion of data in DB
494 }
495 elsif ( $op eq 'delete_confirm' ) {
496     my $dbh = C4::Context->dbh;
497     my $sth =
498       $dbh->prepare(
499 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
500       );
501
502     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
503     my $data = $sth->fetchrow_hashref;
504     $sth->finish;
505     $template->param(
506         liblibrarian  => $data->{'liblibrarian'},
507         tagsubfield   => $data->{'tagsubfield'},
508         delete_link   => $script_name,
509         tagfield      => $tagfield,
510         tagsubfield   => $tagsubfield,
511         frameworkcode => $frameworkcode,
512     );
513
514     # END $OP eq DELETE_CONFIRM
515 ################## DELETE_CONFIRMED ##################################
516   # called by delete_confirm, used to effectively confirm deletion of data in DB
517 }
518 elsif ( $op eq 'delete_confirmed' ) {
519     my $dbh = C4::Context->dbh;
520     unless ( C4::Context->config('demo') eq 1 ) {
521         my $sth =
522           $dbh->prepare(
523 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
524           );
525         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
526         $sth->finish;
527     }
528     print
529 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
530     exit;
531     $template->param( tagfield => $tagfield );
532
533     # END $OP eq DELETE_CONFIRMED
534 ################## DEFAULT ##################################
535 }
536 else {    # DEFAULT
537     my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
538     my @loop_data = ();
539     for ( my $i = 0; $i < $count; $i++ ) {
540         my %row_data;    # get a fresh hash for the row data
541         $row_data{tagfield}         = $results->[$i]{'tagfield'};
542         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
543         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
544         $row_data{kohafield}        = $results->[$i]{'kohafield'};
545         $row_data{repeatable}       = $results->[$i]{'repeatable'};
546         $row_data{mandatory}        = $results->[$i]{'mandatory'};
547         $row_data{tab}              = $results->[$i]{'tab'};
548         $row_data{seealso}          = $results->[$i]{'seealso'};
549         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
550         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
551         $row_data{value_builder}    = $results->[$i]{'value_builder'};
552         $row_data{hidden}           = $results->[$i]{'hidden'};
553         $row_data{isurl}            = $results->[$i]{'isurl'};
554         $row_data{link}             = $results->[$i]{'link'};
555
556         if ( $row_data{tab} eq -1 ) {
557             $row_data{subfield_ignored} = 1;
558         }
559
560         push( @loop_data, \%row_data );
561     }
562     $template->param( loop => \@loop_data );
563     $template->param(
564         edit_tagfield      => $tagfield,
565         edit_frameworkcode => $frameworkcode
566     );
567
568 }    #---- END $OP eq DEFAULT
569
570 output_html_with_http_headers $input, $cookie, $template->output;