Bug 12615: Remove CGI::scrolling_list from marc_subfields_structure.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
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     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.tt",
71         query           => $input,
72         type            => "intranet",
73         authnotrequired => 0,
74         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
75         debug           => 1,
76     }
77 );
78
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{maxlength} = $data->{maxlength};
182         $row_data{tab} = {
183                     id      => "tab$i",
184                     default => $data->{'tab'},
185                     };
186
187         $row_data{tagsubfield} =
188             $data->{'tagsubfield'}
189           . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
190           . $data->{'tagsubfield'}
191           . "\" id=\"tagsubfield\" />";
192         $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
193         $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
194         $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
195         $row_data{libopac}      = CGI::escapeHTML( $data->{'libopac'} );
196         $row_data{seealso}      = CGI::escapeHTML( $data->{'seealso'} );
197         $row_data{kohafield} = {
198                     id      => "kohafield$i",
199                     values  => \@kohafields,
200                     default => "$data->{'kohafield'}",
201                     };
202         $row_data{authorised_value} = {
203                     id      => "authorised_value$i",
204                     values  => \@authorised_values,
205                     default => $data->{'authorised_value'},
206         };
207         $row_data{value_builder} = {
208                     id      => "value_builder$i",
209                     values  => \@value_builder,
210                     default => $data->{'value_builder'},
211         };
212         $row_data{authtypes} = {
213                     id      => "authtypecode$i",
214                     values  => \@authtypes,
215                     default => $data->{'authtypecode'},
216         };
217         $row_data{repeatable} = CGI::checkbox(
218             -name     => "repeatable$i",
219             -checked  => $data->{'repeatable'} ? 'checked' : '',
220             -value    => 1,
221             -label    => '',
222             -id       => "repeatable$i"
223         );
224         $row_data{mandatory} = CGI::checkbox(
225             -name     => "mandatory$i",
226             -checked  => $data->{'mandatory'} ? 'checked' : '',
227             -value    => 1,
228             -label    => '',
229             -id       => "mandatory$i"
230         );
231         $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
232         $row_data{isurl}  = CGI::checkbox(
233             -name     => "isurl$i",
234             -id       => "isurl$i",
235             -checked  => $data->{'isurl'} ? 'checked' : '',
236             -value    => 1,
237             -label    => ''
238         );
239         $row_data{row}    = $i;
240         $row_data{link}   = CGI::escapeHTML( $data->{'link'} ); 
241         push( @loop_data, \%row_data );
242         $i++;
243     }
244
245     # add more_subfields empty lines for add if needed
246         my %row_data;    # get a fresh hash for the row data
247         $row_data{'new_subfield'} = 1;
248         $row_data{'subfieldcode'} = '';
249         $row_data{'maxlength'} = 9999;
250
251         $row_data{tab} = {
252                     id      => "tab$i",
253                     default => $data->{'tab'},
254                     };
255         $row_data{tagsubfield} =
256             "<input type=\"text\" name=\"tagsubfield\" value=\""
257           . $data->{'tagsubfield'}
258           . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
259         $row_data{liblibrarian} = "";
260         $row_data{libopac}      = "";
261         $row_data{seealso}      = "";
262         $row_data{kohafield} = {
263                     id      => "kohafield$i",
264                     values  => \@kohafields,
265                     default => "$data->{'kohafield'}",
266                     };
267         $row_data{hidden}     = "";
268         $row_data{repeatable} = CGI::checkbox(
269             -name     => "repeatable$i",
270             -id       => "repeatable$i",
271             -checked  => '',
272             -value    => 1,
273             -label    => ''
274         );
275         $row_data{mandatory} = CGI::checkbox(
276             -name     => "mandatory$i",
277             -id       => "mandatory$i",
278             -checked  => '',
279             -value    => 1,
280             -label    => ''
281         );
282         $row_data{isurl} = CGI::checkbox(
283             -name     => "isurl$i",
284             -id       => "isurl$i",
285             -checked  => '',
286             -value    => 1,
287             -label    => ''
288         );
289         $row_data{value_builder} = {
290                     id      => "value_builder$i",
291                     values  => \@value_builder,
292                     default => $data->{'value_builder'},
293         };
294         $row_data{authorised_value} = {
295                     id      => "authorised_value$i",
296                     values  => \@authorised_values,
297                     default => $data->{'authorised_value'},
298         };
299         $row_data{authtypes} = {
300                     id      => "authtypecode$i",
301                     values  => \@authtypes,
302                     default => $data->{'authtypecode'},
303         };
304         $row_data{link}   = CGI::escapeHTML( $data->{'link'} );
305         $row_data{row}    = $i;
306         push( @loop_data, \%row_data );
307
308     $template->param( 'use_heading_flags_p'      => 1 );
309     $template->param( 'heading_edit_subfields_p' => 1 );
310     $template->param(
311         action   => "Edit subfields",
312         tagfield => $tagfield,
313         loop           => \@loop_data,
314         more_subfields => $more_subfields,
315         more_tag       => $tagfield
316     );
317
318     # END $OP eq ADD_FORM
319 ################## ADD_VALIDATE ##################################
320     # called by add_form, used to insert/modify data in DB
321 }
322 elsif ( $op eq 'add_validate' ) {
323     my $dbh = C4::Context->dbh;
324     $template->param( tagfield => "$input->param('tagfield')" );
325 #     my $sth = $dbh->prepare(
326 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
327 #                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
328 #     );
329     my $sth_insert = $dbh->prepare(qq{
330         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)
331         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
332     });
333     my $sth_update = $dbh->prepare(qq{
334         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=?
335         where tagfield=? and tagsubfield=? and frameworkcode=?
336     });
337     my @tagsubfield       = $input->param('tagsubfield');
338     my @liblibrarian      = $input->param('liblibrarian');
339     my @libopac           = $input->param('libopac');
340     my @kohafield         = $input->param('kohafield');
341     my @tab               = $input->param('tab');
342     my @seealso           = $input->param('seealso');
343     my @hidden            = $input->param('hidden');
344     my @authorised_values = $input->param('authorised_value');
345     my @authtypecodes     = $input->param('authtypecode');
346     my @value_builder     = $input->param('value_builder');
347     my @link              = $input->param('link');
348     my @defaultvalue      = $input->param('defaultvalue');
349     my @maxlength         = $input->param('maxlength');
350     
351     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
352         my $tagfield    = $input->param('tagfield');
353         my $tagsubfield = $tagsubfield[$i];
354         $tagsubfield = "@" unless $tagsubfield ne '';
355         $tagsubfield = "@" if $tagsubfield eq '_';
356         my $liblibrarian     = $liblibrarian[$i];
357         my $libopac          = $libopac[$i];
358         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
359         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
360         my $kohafield        = $kohafield[$i];
361         my $tab              = $tab[$i];
362         my $seealso          = $seealso[$i];
363         my $authorised_value = $authorised_values[$i];
364         my $authtypecode     = $authtypecodes[$i];
365         my $value_builder    = $value_builder[$i];
366         my $hidden = $hidden[$i];                     #input->param("hidden$i");
367         my $isurl  = $input->param("isurl$i") ? 1 : 0;
368         my $link   = $link[$i];
369         my $defaultvalue = $defaultvalue[$i];
370         my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
371         
372         if (defined($liblibrarian) && $liblibrarian ne "") {
373             unless ( C4::Context->config('demo') eq 1 ) {
374                 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
375                     $sth_update->execute(
376                         $tagfield,
377                         $tagsubfield,
378                         $liblibrarian,
379                         $libopac,
380                         $repeatable,
381                         $mandatory,
382                         $kohafield,
383                         $tab,
384                         $seealso,
385                         $authorised_value,
386                         $authtypecode,
387                         $value_builder,
388                         $hidden,
389                         $isurl,
390                         $frameworkcode,
391                         $link,
392                         $defaultvalue,
393                         $maxlength,
394                         (
395                             $tagfield,
396                             $tagsubfield,
397                             $frameworkcode,
398                         ),
399                     );
400                 } else {
401                     $sth_insert->execute(
402                         $tagfield,
403                         $tagsubfield,
404                         $liblibrarian,
405                         $libopac,
406                         $repeatable,
407                         $mandatory,
408                         $kohafield,
409                         $tab,
410                         $seealso,
411                         $authorised_value,
412                         $authtypecode,
413                         $value_builder,
414                         $hidden,
415                         $isurl,
416                         $frameworkcode,
417                         $link,
418                         $defaultvalue,
419                         $maxlength,
420                     );
421                 }
422             }
423         }
424     }
425     $sth_insert->finish;
426     $sth_update->finish;
427     print
428 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
429     exit;
430
431     # END $OP eq ADD_VALIDATE
432 ################## DELETE_CONFIRM ##################################
433     # called by default form, used to confirm deletion of data in DB
434 }
435 elsif ( $op eq 'delete_confirm' ) {
436     my $dbh = C4::Context->dbh;
437     my $sth =
438       $dbh->prepare(
439 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
440       );
441
442     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
443     my $data = $sth->fetchrow_hashref;
444     $sth->finish;
445     $template->param(
446         liblibrarian  => $data->{'liblibrarian'},
447         tagsubfield   => $data->{'tagsubfield'},
448         delete_link   => $script_name,
449         tagfield      => $tagfield,
450         tagsubfield   => $tagsubfield,
451         frameworkcode => $frameworkcode,
452     );
453
454     # END $OP eq DELETE_CONFIRM
455 ################## DELETE_CONFIRMED ##################################
456   # called by delete_confirm, used to effectively confirm deletion of data in DB
457 }
458 elsif ( $op eq 'delete_confirmed' ) {
459     my $dbh = C4::Context->dbh;
460     unless ( C4::Context->config('demo') eq 1 ) {
461         my $sth =
462           $dbh->prepare(
463 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
464           );
465         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
466         $sth->finish;
467     }
468     print
469 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
470     exit;
471     $template->param( tagfield => $tagfield );
472
473     # END $OP eq DELETE_CONFIRMED
474 ################## DEFAULT ##################################
475 }
476 else {    # DEFAULT
477     my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
478     my @loop_data = ();
479     for ( my $i = 0; $i < $count; $i++ ) {
480         my %row_data;    # get a fresh hash for the row data
481         $row_data{tagfield}         = $results->[$i]{'tagfield'};
482         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
483         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
484         $row_data{kohafield}        = $results->[$i]{'kohafield'};
485         $row_data{repeatable}       = $results->[$i]{'repeatable'};
486         $row_data{mandatory}        = $results->[$i]{'mandatory'};
487         $row_data{tab}              = $results->[$i]{'tab'};
488         $row_data{seealso}          = $results->[$i]{'seealso'};
489         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
490         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
491         $row_data{value_builder}    = $results->[$i]{'value_builder'};
492         $row_data{hidden}           = $results->[$i]{'hidden'};
493         $row_data{isurl}            = $results->[$i]{'isurl'};
494         $row_data{link}             = $results->[$i]{'link'};
495
496         if ( $row_data{tab} eq -1 ) {
497             $row_data{subfield_ignored} = 1;
498         }
499
500         push( @loop_data, \%row_data );
501     }
502     $template->param( loop => \@loop_data );
503     $template->param(
504         edit_tagfield      => $tagfield,
505         edit_frameworkcode => $frameworkcode
506     );
507
508 }    #---- END $OP eq DEFAULT
509
510 output_html_with_http_headers $input, $cookie, $template->output;