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