Increment version for 3.22.21
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use C4::Output;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
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     return ( $cnt, \@results );
49 }
50
51 sub marc_subfield_structure_exists {
52     my ($tagfield, $tagsubfield, $frameworkcode) = @_;
53     my $dbh  = C4::Context->dbh;
54     my $sql  = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
55     my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
56     return @$rows > 0;
57 }
58
59 my $input         = new CGI;
60 my $tagfield      = $input->param('tagfield');
61 my $tagsubfield   = $input->param('tagsubfield');
62 my $frameworkcode = $input->param('frameworkcode');
63 my $pkfield       = "tagfield";
64 my $offset        = $input->param('offset') || 0;
65 my $script_name   = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
66
67 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
68     {
69         template_name   => "admin/marc_subfields_structure.tt",
70         query           => $input,
71         type            => "intranet",
72         authnotrequired => 0,
73         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
74         debug           => 1,
75     }
76 );
77 my $cache = Koha::Cache->get_instance();
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 $dbh            = C4::Context->dbh;
103
104     # builds kohafield tables
105     my @kohafields;
106     push @kohafields, "";
107     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
108     $sth2->execute;
109     while ( ( my $field ) = $sth2->fetchrow_array ) {
110         push @kohafields, "biblio." . $field;
111     }
112     $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
113     $sth2->execute;
114     while ( ( my $field ) = $sth2->fetchrow_array ) {
115         if ( $field eq 'notes' ) { $field = 'bnotes'; }
116         push @kohafields, "biblioitems." . $field;
117     }
118     $sth2 = $dbh->prepare("SHOW COLUMNS from items");
119     $sth2->execute;
120     while ( ( my $field ) = $sth2->fetchrow_array ) {
121         push @kohafields, "items." . $field;
122     }
123
124     # build authorised value list
125     $sth2->finish;
126     $sth2 = $dbh->prepare("select distinct category from authorised_values");
127     $sth2->execute;
128     my @authorised_values;
129     push @authorised_values, "";
130     while ( ( my $category ) = $sth2->fetchrow_array ) {
131         push @authorised_values, $category;
132     }
133     push( @authorised_values, "branches" );
134     push( @authorised_values, "itemtypes" );
135     push( @authorised_values, "cn_source" );
136
137     # build thesaurus categories list
138     $sth2->finish;
139     $sth2 = $dbh->prepare("select authtypecode from auth_types");
140     $sth2->execute;
141     my @authtypes;
142     push @authtypes, "";
143     while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
144         push @authtypes, $authtypecode;
145     }
146
147     # build value_builder list
148     my @value_builder = ('');
149
150     # read value_builder directory.
151     # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
152     # on a standard install, /cgi-bin need to be added.
153     # test one, then the other
154     my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
155     unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
156         $cgidir = C4::Context->config('intranetdir');
157         opendir( DIR, "$cgidir/cataloguing/value_builder" )
158           || die "can't opendir $cgidir/value_builder: $!";
159     }
160     while ( my $line = readdir(DIR) ) {
161         if ( $line =~ /\.pl$/ &&
162              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
163             push( @value_builder, $line );
164         }
165     }
166     @value_builder= sort {$a cmp $b} @value_builder;
167     closedir DIR;
168
169     # build values list
170     my $sth =
171       $dbh->prepare(
172 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
173       );    # and tagsubfield='$tagsubfield'");
174     $sth->execute( $tagfield, $frameworkcode );
175     my @loop_data = ();
176     my $i         = 0;
177     while ( my $data = $sth->fetchrow_hashref ) {
178         my %row_data;    # get a fresh hash for the row data
179         $row_data{defaultvalue}      = $data->{defaultvalue};
180         $row_data{maxlength}         = $data->{maxlength};
181         $row_data{tab}               = $data->{tab};
182         $row_data{tagsubfield}       = $data->{tagsubfield};
183         $row_data{subfieldcode}      = $data->{'tagsubfield'} eq '@' ? '_' : $data->{'tagsubfield'};
184         $row_data{urisubfieldcode}   = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
185         $row_data{liblibrarian}      = $data->{'liblibrarian'};
186         $row_data{libopac}           = $data->{'libopac'};
187         $row_data{seealso}           = $data->{'seealso'};
188         $row_data{kohafields}        = \@kohafields;
189         $row_data{kohafield}         = $data->{kohafield};
190         $row_data{authorised_values} = \@authorised_values;
191         $row_data{authorised_value}  = $data->{authorised_value};
192         $row_data{value_builders}    = \@value_builder;
193         $row_data{value_builder}     = $data->{'value_builder'};
194         $row_data{authtypes}         = \@authtypes;
195         $row_data{authtypecode}      = $data->{'authtypecode'};
196         $row_data{repeatable}        = $data->{repeatable};
197         $row_data{mandatory}         = $data->{mandatory};
198         $row_data{hidden}            = $data->{hidden};
199         $row_data{isurl}             = $data->{isurl};
200         $row_data{row}               = $i;
201         $row_data{link}              = $data->{'link'};
202         push( @loop_data, \%row_data );
203         $i++;
204     }
205
206     # Add a new row for the "New" tab
207     my %row_data;    # get a fresh hash for the row data
208     $row_data{'new_subfield'}    = 1;
209     $row_data{'subfieldcode'}    = '';
210     $row_data{'maxlength'}       = 9999;
211     $row_data{tab}               = -1;                    #ignore
212     $row_data{tagsubfield}       = "";
213     $row_data{liblibrarian}      = "";
214     $row_data{libopac}           = "";
215     $row_data{seealso}           = "";
216     $row_data{hidden}            = "";
217     $row_data{repeatable}        = 0;
218     $row_data{mandatory}         = 0;
219     $row_data{isurl}             = 0;
220     $row_data{kohafields}        = \@kohafields;
221     $row_data{authorised_values} = \@authorised_values;
222     $row_data{value_builders}    = \@value_builder;
223     $row_data{authtypes}         = \@authtypes;
224     $row_data{link}              = "";
225     $row_data{row}               = $i;
226     push( @loop_data, \%row_data );
227
228     $template->param( 'use_heading_flags_p'      => 1 );
229     $template->param( 'heading_edit_subfields_p' => 1 );
230     $template->param(
231         action   => "Edit subfields",
232         tagfield => $tagfield,
233         loop           => \@loop_data,
234         more_tag       => $tagfield
235     );
236
237     # END $OP eq ADD_FORM
238 ################## ADD_VALIDATE ##################################
239     # called by add_form, used to insert/modify data in DB
240 }
241 elsif ( $op eq 'add_validate' ) {
242     my $dbh = C4::Context->dbh;
243     $template->param( tagfield => "$input->param('tagfield')" );
244 #     my $sth = $dbh->prepare(
245 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
246 #                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
247 #     );
248     my $sth_insert = $dbh->prepare(qq{
249         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)
250         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
251     });
252     my $sth_update = $dbh->prepare(qq{
253         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=?
254         where tagfield=? and tagsubfield=? and frameworkcode=?
255     });
256     my @tagsubfield       = $input->multi_param('tagsubfield');
257     my @liblibrarian      = $input->multi_param('liblibrarian');
258     my @libopac           = $input->multi_param('libopac');
259     my @kohafield         = $input->multi_param('kohafield');
260     my @tab               = $input->multi_param('tab');
261     my @seealso           = $input->multi_param('seealso');
262     my @hidden            = $input->multi_param('hidden');
263     my @authorised_values = $input->multi_param('authorised_value');
264     my @authtypecodes     = $input->multi_param('authtypecode');
265     my @value_builder     = $input->multi_param('value_builder');
266     my @link              = $input->multi_param('link');
267     my @defaultvalue      = $input->multi_param('defaultvalue');
268     my @maxlength         = $input->multi_param('maxlength');
269     
270     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
271         my $tagfield    = $input->param('tagfield');
272         my $tagsubfield = $tagsubfield[$i];
273         $tagsubfield = "@" unless $tagsubfield ne '';
274         $tagsubfield = "@" if $tagsubfield eq '_';
275         my $liblibrarian     = $liblibrarian[$i];
276         my $libopac          = $libopac[$i];
277         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
278         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
279         my $kohafield        = $kohafield[$i];
280         my $tab              = $tab[$i];
281         my $seealso          = $seealso[$i];
282         my $authorised_value = $authorised_values[$i];
283         my $authtypecode     = $authtypecodes[$i];
284         my $value_builder    = $value_builder[$i];
285         my $hidden = $hidden[$i];                     #input->param("hidden$i");
286         my $isurl  = $input->param("isurl$i") ? 1 : 0;
287         my $link   = $link[$i];
288         my $defaultvalue = $defaultvalue[$i];
289         my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
290         
291         if (defined($liblibrarian) && $liblibrarian ne "") {
292             my $is_demo = C4::Context->config('demo') || '';
293             if ( $is_demo ne '1' ) {
294                 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
295                     $sth_update->execute(
296                         $tagfield,
297                         $tagsubfield,
298                         $liblibrarian,
299                         $libopac,
300                         $repeatable,
301                         $mandatory,
302                         $kohafield,
303                         $tab,
304                         $seealso,
305                         $authorised_value,
306                         $authtypecode,
307                         $value_builder,
308                         $hidden,
309                         $isurl,
310                         $frameworkcode,
311                         $link,
312                         $defaultvalue,
313                         $maxlength,
314                         (
315                             $tagfield,
316                             $tagsubfield,
317                             $frameworkcode,
318                         ),
319                     );
320                 } else {
321                     $sth_insert->execute(
322                         $tagfield,
323                         $tagsubfield,
324                         $liblibrarian,
325                         $libopac,
326                         $repeatable,
327                         $mandatory,
328                         $kohafield,
329                         $tab,
330                         $seealso,
331                         $authorised_value,
332                         $authtypecode,
333                         $value_builder,
334                         $hidden,
335                         $isurl,
336                         $frameworkcode,
337                         $link,
338                         $defaultvalue,
339                         $maxlength,
340                     );
341                 }
342             }
343         }
344     }
345     $sth_insert->finish;
346     $sth_update->finish;
347     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
348     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
349     $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
350
351     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
352     exit;
353
354     # END $OP eq ADD_VALIDATE
355 ################## DELETE_CONFIRM ##################################
356     # called by default form, used to confirm deletion of data in DB
357 }
358 elsif ( $op eq 'delete_confirm' ) {
359     my $dbh = C4::Context->dbh;
360     my $sth =
361       $dbh->prepare(
362 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
363       );
364
365     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
366     my $data = $sth->fetchrow_hashref;
367     $sth->finish;
368     $template->param(
369         liblibrarian  => $data->{'liblibrarian'},
370         tagsubfield   => $data->{'tagsubfield'},
371         delete_link   => $script_name,
372         tagfield      => $tagfield,
373         tagsubfield   => $tagsubfield,
374         frameworkcode => $frameworkcode,
375     );
376
377     # END $OP eq DELETE_CONFIRM
378 ################## DELETE_CONFIRMED ##################################
379   # called by delete_confirm, used to effectively confirm deletion of data in DB
380 }
381 elsif ( $op eq 'delete_confirmed' ) {
382     my $dbh = C4::Context->dbh;
383     my $is_demo = C4::Context->config('demo') || '';
384     if ( $is_demo ne '1' ) {
385         my $sth =
386           $dbh->prepare(
387 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
388           );
389         $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
390         $sth->finish;
391     }
392     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
393     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
394     $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
395     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
396     exit;
397
398     # END $OP eq DELETE_CONFIRMED
399 ################## DEFAULT ##################################
400 }
401 else {    # DEFAULT
402     my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
403     my @loop_data = ();
404     for ( my $i = 0; $i < $count; $i++ ) {
405         my %row_data;    # get a fresh hash for the row data
406         $row_data{tagfield}         = $results->[$i]{'tagfield'};
407         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
408         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
409         $row_data{kohafield}        = $results->[$i]{'kohafield'};
410         $row_data{repeatable}       = $results->[$i]{'repeatable'};
411         $row_data{mandatory}        = $results->[$i]{'mandatory'};
412         $row_data{tab}              = $results->[$i]{'tab'};
413         $row_data{seealso}          = $results->[$i]{'seealso'};
414         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
415         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
416         $row_data{value_builder}    = $results->[$i]{'value_builder'};
417         $row_data{hidden}           = $results->[$i]{'hidden'};
418         $row_data{isurl}            = $results->[$i]{'isurl'};
419         $row_data{link}             = $results->[$i]{'link'};
420
421         if ( $row_data{tab} eq -1 ) {
422             $row_data{subfield_ignored} = 1;
423         }
424
425         push( @loop_data, \%row_data );
426     }
427     $template->param( loop => \@loop_data );
428     $template->param(
429         edit_tagfield      => $tagfield,
430         edit_frameworkcode => $frameworkcode
431     );
432
433 }    #---- END $OP eq DEFAULT
434
435 output_html_with_http_headers $input, $cookie, $template->output;