Koha 3.0 nozebra 1st commit : the script misc/migration_tools/rebuild_nozebra.pl...
[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 = $i ; $j <= $more_subfields + $i ; $j++ ) {
281         my %row_data;    # get a fresh hash for the row data
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             -tabindex => '',
304             -multiple => 0,
305         );
306         $row_data{tagsubfield} =
307             "<input type=\"text\" name=\"tagsubfield\" value=\""
308           . $data->{'tagsubfield'}
309           . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
310         $row_data{liblibrarian} = "";
311         $row_data{libopac}      = "";
312         $row_data{seealso}      = "";
313         $row_data{kohafield}    = CGI::scrolling_list(
314             -name     => 'kohafield',
315             -id       => "kohafield$j",
316             -values   => \@kohafields,
317             -default  => "",
318             -size     => 1,
319             -tabindex => '',
320             -multiple => 0,
321         );
322         $row_data{hidden}     = "";
323         $row_data{repeatable} = CGI::checkbox(
324             -name     => "repeatable$j",
325             -id       => "repeatable$j",
326             -checked  => '',
327             -value    => 1,
328             -tabindex => '',
329             -label    => ''
330         );
331         $row_data{mandatory} = CGI::checkbox(
332             -name     => "mandatory$j",
333             -id       => "mandatory$j",
334             -checked  => '',
335             -value    => 1,
336             -tabindex => '',
337             -label    => ''
338         );
339         $row_data{isurl} = CGI::checkbox(
340             -name     => "isurl$j",
341             -id       => "isurl$j",
342             -checked  => '',
343             -value    => 1,
344             -tabindex => '',
345             -label    => ''
346         );
347         $row_data{value_builder} = CGI::scrolling_list(
348             -name     => 'value_builder',
349             -id       => 'value_builder',
350             -values   => \@value_builder,
351             -default  => $data->{'value_builder'},
352             -size     => 1,
353             -tabindex => '',
354             -multiple => 0,
355         );
356         $row_data{authorised_value} = CGI::scrolling_list(
357             -name     => 'authorised_value',
358             -id       => 'authorised_value',
359             -values   => \@authorised_values,
360             -size     => 1,
361             -tabindex => '',
362             -multiple => 0,
363         );
364         $row_data{authtypes} = CGI::scrolling_list(
365             -name     => 'authtypecode',
366             -id       => 'authtypecode',
367             -values   => \@authtypes,
368             -size     => 1,
369             -tabindex => '',
370             -multiple => 0,
371         );
372         $row_data{link}   = CGI::escapeHTML( $data->{'link'} );
373         $row_data{toggle} = $toggle;
374         $row_data{row}    = $j;
375         push( @loop_data, \%row_data );
376     }
377     $template->param( 'use-heading-flags-p'      => 1 );
378     $template->param( 'heading-edit-subfields-p' => 1 );
379     $template->param(
380         action   => "Edit subfields",
381         tagfield =>
382 "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
383         loop           => \@loop_data,
384         more_subfields => $more_subfields,
385         more_tag       => $tagfield
386     );
387
388     # END $OP eq ADD_FORM
389 ################## ADD_VALIDATE ##################################
390     # called by add_form, used to insert/modify data in DB
391 }
392 elsif ( $op eq 'add_validate' ) {
393     my $dbh = C4::Context->dbh;
394     $template->param( tagfield => "$input->param('tagfield')" );
395     my $sth = $dbh->prepare(
396 "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
397                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
398     );
399     my @tagsubfield       = $input->param('tagsubfield');
400     my @liblibrarian      = $input->param('liblibrarian');
401     my @libopac           = $input->param('libopac');
402     my @kohafield         = $input->param('kohafield');
403     my @tab               = $input->param('tab');
404     my @seealso           = $input->param('seealso');
405     my @hidden            = $input->param('hidden');
406     my @authorised_values = $input->param('authorised_value');
407     my @authtypecodes     = $input->param('authtypecode');
408     my @value_builder     = $input->param('value_builder');
409     my @link              = $input->param('link');
410     my @defaultvalue      = $input->param('defaultvalue');
411     
412     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
413         my $tagfield    = $input->param('tagfield');
414         my $tagsubfield = $tagsubfield[$i];
415         $tagsubfield = "@" unless $tagsubfield ne '';
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         
431         if ($liblibrarian) {
432             unless ( C4::Context->config('demo') eq 1 ) {
433                 $sth->execute(
434                     $tagfield,
435                     $tagsubfield,
436                     $liblibrarian,
437                     $libopac,
438                     $repeatable,
439                     $mandatory,
440                     $kohafield,
441                     $tab,
442                     $seealso,
443                     $authorised_value,
444                     $authtypecode,
445                     $value_builder,
446                     $hidden,
447                     $isurl,
448                     $frameworkcode,
449                     $link,
450                     $defaultvalue,
451                 );
452             }
453         }
454     }
455     $sth->finish;
456     print
457 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
458     exit;
459
460     # END $OP eq ADD_VALIDATE
461 ################## DELETE_CONFIRM ##################################
462     # called by default form, used to confirm deletion of data in DB
463 }
464 elsif ( $op eq 'delete_confirm' ) {
465     my $dbh = C4::Context->dbh;
466     my $sth =
467       $dbh->prepare(
468 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
469       );
470
471     #FIXME : called with 2 bind variables when 3 are needed
472     $sth->execute( $tagfield, $tagsubfield );
473     my $data = $sth->fetchrow_hashref;
474     $sth->finish;
475     $template->param(
476         liblibrarian  => $data->{'liblibrarian'},
477         tagsubfield   => $data->{'tagsubfield'},
478         delete_link   => $script_name,
479         tagfield      => $tagfield,
480         tagsubfield   => $tagsubfield,
481         frameworkcode => $frameworkcode,
482     );
483
484     # END $OP eq DELETE_CONFIRM
485 ################## DELETE_CONFIRMED ##################################
486   # called by delete_confirm, used to effectively confirm deletion of data in DB
487 }
488 elsif ( $op eq 'delete_confirmed' ) {
489     my $dbh = C4::Context->dbh;
490     unless ( C4::Context->config('demo') eq 1 ) {
491         my $sth =
492           $dbh->prepare(
493 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
494           );
495         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
496         $sth->finish;
497     }
498     print
499 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
500     exit;
501     $template->param( tagfield => $tagfield );
502
503     # END $OP eq DELETE_CONFIRMED
504 ################## DEFAULT ##################################
505 }
506 else {    # DEFAULT
507     my ( $count, $results ) = StringSearch( $tagfield, $frameworkcode );
508     my $toggle    = 1;
509     my @loop_data = ();
510     for (
511         my $i = $offset ;
512         $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
513         $i++
514       )
515     {
516         if ( $toggle eq 1 ) {
517             $toggle = 0;
518         }
519         else {
520             $toggle = 1;
521         }
522         my %row_data;    # get a fresh hash for the row data
523         $row_data{tagfield}         = $results->[$i]{'tagfield'};
524         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
525         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
526         $row_data{kohafield}        = $results->[$i]{'kohafield'};
527         $row_data{repeatable}       = $results->[$i]{'repeatable'};
528         $row_data{mandatory}        = $results->[$i]{'mandatory'};
529         $row_data{tab}              = $results->[$i]{'tab'};
530         $row_data{seealso}          = $results->[$i]{'seealso'};
531         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
532         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
533         $row_data{value_builder}    = $results->[$i]{'value_builder'};
534         $row_data{hidden}           = $results->[$i]{'hidden'};
535         $row_data{isurl}            = $results->[$i]{'isurl'};
536         $row_data{link}             = $results->[$i]{'link'};
537         $row_data{delete}           =
538 "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield="
539           . $results->[$i]{'tagsubfield'}
540           . "&frameworkcode=$frameworkcode";
541         $row_data{toggle} = $toggle;
542
543         if ( $row_data{tab} eq -1 ) {
544             $row_data{subfield_ignored} = 1;
545         }
546
547         push( @loop_data, \%row_data );
548     }
549     $template->param( loop => \@loop_data );
550     $template->param(
551         edit_tagfield      => $tagfield,
552         edit_frameworkcode => $frameworkcode
553     );
554
555     if ( $offset > 0 ) {
556         my $prevpage = $offset - $pagesize;
557         $template->param(
558             prev => "<a href=\"$script_name?offset=$prevpage\">" );
559     }
560     if ( $offset + $pagesize < $count ) {
561         my $nextpage = $offset + $pagesize;
562         $template->param(
563             next => "<a href=\"$script_name?offset=$nextpage\">" );
564     }
565 }    #---- END $OP eq DEFAULT
566
567 output_html_with_http_headers $input, $cookie, $template->output;