Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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 use Koha::Authority::Types;
27 use Koha::AuthorisedValueCategories;
28
29 use List::MoreUtils qw( uniq );
30
31 sub string_search {
32     my ( $searchstring, $frameworkcode ) = @_;
33     my $dbh = C4::Context->dbh;
34     $searchstring =~ s/\'/\\\'/g;
35     my @data  = split( ' ', $searchstring );
36     my $count = @data;
37     my $sth   =
38       $dbh->prepare(
39 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
40       );
41     $sth->execute( "$searchstring%", $frameworkcode );
42     my @results;
43     my $cnt = 0;
44     my $u   = 1;
45
46     while ( my $data = $sth->fetchrow_hashref ) {
47         push( @results, $data );
48         $cnt++;
49         $u++;
50     }
51     $sth->finish;
52     return ( $cnt, \@results );
53 }
54
55 sub marc_subfield_structure_exists {
56     my ($tagfield, $tagsubfield, $frameworkcode) = @_;
57     my $dbh  = C4::Context->dbh;
58     my $sql  = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
59     my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
60     return @$rows > 0;
61 }
62
63 my $input         = new CGI;
64 my $tagfield      = $input->param('tagfield');
65 my $tagsubfield   = $input->param('tagsubfield');
66 my $frameworkcode = $input->param('frameworkcode');
67 my $pkfield       = "tagfield";
68 my $offset        = $input->param('offset');
69 $offset = 0 if not defined $offset or $offset < 0;
70 my $script_name   = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
71
72 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
73     {
74         template_name   => "admin/marc_subfields_structure.tt",
75         query           => $input,
76         type            => "intranet",
77         authnotrequired => 0,
78         flagsrequired   => { parameters => 'manage_marc_frameworks' },
79         debug           => 1,
80     }
81 );
82 my $cache = Koha::Caches->get_instance();
83
84 my $op       = $input->param('op') || "";
85 $tagfield =~ s/\,//g;
86
87 if ($op) {
88     $template->param(
89         script_name   => $script_name,
90         tagfield      => $tagfield,
91         frameworkcode => $frameworkcode,
92         $op           => 1
93     );    # we show only the TMPL_VAR names $op
94 }
95 else {
96     $template->param(
97         script_name   => $script_name,
98         tagfield      => $tagfield,
99         frameworkcode => $frameworkcode,
100         else          => 1
101     );    # we show only the TMPL_VAR names $op
102 }
103
104 ################## ADD_FORM ##################################
105 # called by default. Used to create form to add or  modify a record
106 if ( $op eq 'add_form' ) {
107     my $dbh            = C4::Context->dbh;
108
109     # builds kohafield tables
110     my @kohafields;
111     push @kohafields, "";
112     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
113     $sth2->execute;
114     while ( ( my $field ) = $sth2->fetchrow_array ) {
115         push @kohafields, "biblio." . $field;
116     }
117     $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
118     $sth2->execute;
119     while ( ( my $field ) = $sth2->fetchrow_array ) {
120         if ( $field eq 'notes' ) { $field = 'bnotes'; }
121         push @kohafields, "biblioitems." . $field;
122     }
123     $sth2 = $dbh->prepare("SHOW COLUMNS from items");
124     $sth2->execute;
125     while ( ( my $field ) = $sth2->fetchrow_array ) {
126         push @kohafields, "items." . $field;
127     }
128
129     # build authorised value list
130     $sth2->finish;
131     $sth2 = $dbh->prepare("select distinct category from authorised_values");
132     $sth2->execute;
133     my @av_cat = Koha::AuthorisedValueCategories->search;
134     my @authorised_values = map { $_->category_name } @av_cat;
135
136     # build thesaurus categories list
137     my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
138
139     # build value_builder list
140     my @value_builder = ('');
141
142     # read value_builder directory.
143     # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
144     # on a standard install, /cgi-bin need to be added.
145     # test one, then the other
146     my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
147     unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
148         $cgidir = C4::Context->config('intranetdir');
149         opendir( DIR, "$cgidir/cataloguing/value_builder" )
150           || die "can't opendir $cgidir/value_builder: $!";
151     }
152     while ( my $line = readdir(DIR) ) {
153         if ( $line =~ /\.pl$/ &&
154              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
155             push( @value_builder, $line );
156         }
157     }
158     @value_builder= sort {$a cmp $b} @value_builder;
159     closedir DIR;
160
161     # build values list
162     my $sth =
163       $dbh->prepare(
164 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
165       );    # and tagsubfield='$tagsubfield'");
166     $sth->execute( $tagfield, $frameworkcode );
167     my @loop_data = ();
168     my $i         = 0;
169     while ( my $data = $sth->fetchrow_hashref ) {
170         my %row_data;    # get a fresh hash for the row data
171         $row_data{defaultvalue}      = $data->{defaultvalue};
172         $row_data{maxlength}         = $data->{maxlength};
173         $row_data{tab}               = $data->{tab};
174         $row_data{tagsubfield}       = $data->{tagsubfield};
175         $row_data{subfieldcode}      = $data->{'tagsubfield'};
176         $row_data{urisubfieldcode}   = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
177         $row_data{liblibrarian}      = $data->{'liblibrarian'};
178         $row_data{libopac}           = $data->{'libopac'};
179         $row_data{seealso}           = $data->{'seealso'};
180         $row_data{kohafields}        = \@kohafields;
181         $row_data{kohafield}         = $data->{kohafield};
182         $row_data{authorised_values} = \@authorised_values;
183         $row_data{authorised_value}  = $data->{authorised_value};
184         $row_data{value_builders}    = \@value_builder;
185         $row_data{value_builder}     = $data->{'value_builder'};
186         $row_data{authtypes}         = \@authtypes;
187         $row_data{authtypecode}      = $data->{'authtypecode'};
188         $row_data{repeatable}        = $data->{repeatable};
189         $row_data{mandatory}         = $data->{mandatory};
190         $row_data{hidden}            = $data->{hidden};
191         $row_data{isurl}             = $data->{isurl};
192         $row_data{row}               = $i;
193         $row_data{link}              = $data->{'link'};
194         push( @loop_data, \%row_data );
195         $i++;
196     }
197
198     # Add a new row for the "New" tab
199     my %row_data;    # get a fresh hash for the row data
200     $row_data{'new_subfield'}    = 1;
201     $row_data{'subfieldcode'}    = '';
202     $row_data{'maxlength'}       = 9999;
203     $row_data{tab}               = -1;                    #ignore
204     $row_data{tagsubfield}       = "";
205     $row_data{liblibrarian}      = "";
206     $row_data{libopac}           = "";
207     $row_data{seealso}           = "";
208     $row_data{hidden}            = "";
209     $row_data{repeatable}        = 0;
210     $row_data{mandatory}         = 0;
211     $row_data{isurl}             = 0;
212     $row_data{kohafields}        = \@kohafields;
213     $row_data{authorised_values} = \@authorised_values;
214     $row_data{value_builders}    = \@value_builder;
215     $row_data{authtypes}         = \@authtypes;
216     $row_data{link}              = "";
217     $row_data{row}               = $i;
218     push( @loop_data, \%row_data );
219
220     $template->param( 'use_heading_flags_p'      => 1 );
221     $template->param( 'heading_edit_subfields_p' => 1 );
222     $template->param(
223         action   => "Edit subfields",
224         tagfield => $tagfield,
225         loop           => \@loop_data,
226         more_tag       => $tagfield
227     );
228
229     # END $OP eq ADD_FORM
230 ################## ADD_VALIDATE ##################################
231     # called by add_form, used to insert/modify data in DB
232 }
233 elsif ( $op eq 'add_validate' ) {
234     my $dbh = C4::Context->dbh;
235     $template->param( tagfield => "$input->param('tagfield')" );
236 #     my $sth = $dbh->prepare(
237 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
238 #                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
239 #     );
240     my $sth_insert = $dbh->prepare(qq{
241         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)
242         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
243     });
244     my $sth_update = $dbh->prepare(qq{
245         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=?
246         where tagfield=? and tagsubfield=? and frameworkcode=?
247     });
248     my @tagsubfield       = $input->multi_param('tagsubfield');
249     my @liblibrarian      = $input->multi_param('liblibrarian');
250     my @libopac           = $input->multi_param('libopac');
251     my @kohafield         = $input->multi_param('kohafield');
252     my @tab               = $input->multi_param('tab');
253     my @seealso           = $input->multi_param('seealso');
254     my @hidden            = $input->multi_param('hidden');
255     my @authorised_values = $input->multi_param('authorised_value');
256     my @authtypecodes     = $input->multi_param('authtypecode');
257     my @value_builder     = $input->multi_param('value_builder');
258     my @link              = $input->multi_param('link');
259     my @defaultvalue      = $input->multi_param('defaultvalue');
260     my @maxlength         = $input->multi_param('maxlength');
261     
262     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
263         my $tagfield    = $input->param('tagfield');
264         my $tagsubfield = $tagsubfield[$i];
265         $tagsubfield = "@" unless $tagsubfield ne '';
266         my $liblibrarian     = $liblibrarian[$i];
267         my $libopac          = $libopac[$i];
268         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
269         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
270         my $kohafield        = $kohafield[$i];
271         my $tab              = $tab[$i];
272         my $seealso          = $seealso[$i];
273         my $authorised_value = $authorised_values[$i];
274         my $authtypecode     = $authtypecodes[$i];
275         my $value_builder    = $value_builder[$i];
276         my $hidden = $hidden[$i];                     #input->param("hidden$i");
277         my $isurl  = $input->param("isurl$i") ? 1 : 0;
278         my $link   = $link[$i];
279         my $defaultvalue = $defaultvalue[$i];
280         my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
281         
282         if (defined($liblibrarian) && $liblibrarian ne "") {
283             if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
284                 $sth_update->execute(
285                     $tagfield,
286                     $tagsubfield,
287                     $liblibrarian,
288                     $libopac,
289                     $repeatable,
290                     $mandatory,
291                     $kohafield,
292                     $tab,
293                     $seealso,
294                     $authorised_value,
295                     $authtypecode,
296                     $value_builder,
297                     $hidden,
298                     $isurl,
299                     $frameworkcode,
300                     $link,
301                     $defaultvalue,
302                     $maxlength,
303                     (
304                         $tagfield,
305                         $tagsubfield,
306                         $frameworkcode,
307                     )
308                 );
309             } else {
310                 if( $frameworkcode ne q{} ) {
311                     # BZ 19096: Overwrite kohafield from Default when adding a new record
312                      my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
313                     $kohafield = $rec->kohafield if $rec;
314                 }
315                 $sth_insert->execute(
316                     $tagfield,
317                     $tagsubfield,
318                     $liblibrarian,
319                     $libopac,
320                     $repeatable,
321                     $mandatory,
322                     $kohafield,
323                     $tab,
324                     $seealso,
325                     $authorised_value,
326                     $authtypecode,
327                     $value_builder,
328                     $hidden,
329                     $isurl,
330                     $frameworkcode,
331                     $link,
332                     $defaultvalue,
333                     $maxlength,
334                 );
335             }
336         }
337     }
338     $sth_insert->finish;
339     $sth_update->finish;
340     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
341     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
342     $cache->clear_from_cache("default_value_for_mod_marc-");
343     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
344
345     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
346     exit;
347
348     # END $OP eq ADD_VALIDATE
349 ################## DELETE_CONFIRM ##################################
350     # called by default form, used to confirm deletion of data in DB
351 }
352 elsif ( $op eq 'delete_confirm' ) {
353     my $dbh = C4::Context->dbh;
354     my $sth =
355       $dbh->prepare(
356 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
357       );
358
359     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
360     my $data = $sth->fetchrow_hashref;
361     $sth->finish;
362     $template->param(
363         liblibrarian  => $data->{'liblibrarian'},
364         tagsubfield   => $data->{'tagsubfield'},
365         delete_link   => $script_name,
366         tagfield      => $tagfield,
367         tagsubfield   => $tagsubfield,
368         frameworkcode => $frameworkcode,
369     );
370
371     # END $OP eq DELETE_CONFIRM
372 ################## DELETE_CONFIRMED ##################################
373   # called by delete_confirm, used to effectively confirm deletion of data in DB
374 }
375 elsif ( $op eq 'delete_confirmed' ) {
376     my $dbh = C4::Context->dbh;
377     my $sth =
378       $dbh->prepare(
379 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
380       );
381     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
382     $sth->finish;
383     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
384     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
385     $cache->clear_from_cache("default_value_for_mod_marc-");
386     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
387     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
388     exit;
389
390     # END $OP eq DELETE_CONFIRMED
391 ################## DEFAULT ##################################
392 }
393 else {    # DEFAULT
394     my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
395     my @loop_data = ();
396     for ( my $i = 0; $i < $count; $i++ ) {
397         my %row_data;    # get a fresh hash for the row data
398         $row_data{tagfield}         = $results->[$i]{'tagfield'};
399         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
400         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
401         $row_data{kohafield}        = $results->[$i]{'kohafield'};
402         $row_data{repeatable}       = $results->[$i]{'repeatable'};
403         $row_data{mandatory}        = $results->[$i]{'mandatory'};
404         $row_data{tab}              = $results->[$i]{'tab'};
405         $row_data{seealso}          = $results->[$i]{'seealso'};
406         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
407         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
408         $row_data{value_builder}    = $results->[$i]{'value_builder'};
409         $row_data{hidden}           = $results->[$i]{'hidden'};
410         $row_data{isurl}            = $results->[$i]{'isurl'};
411         $row_data{link}             = $results->[$i]{'link'};
412
413         if ( $row_data{tab} eq -1 ) {
414             $row_data{subfield_ignored} = 1;
415         }
416
417         push( @loop_data, \%row_data );
418     }
419     $template->param( loop => \@loop_data );
420     $template->param(
421         edit_tagfield      => $tagfield,
422         edit_frameworkcode => $frameworkcode
423     );
424
425 }    #---- END $OP eq DEFAULT
426
427 output_html_with_http_headers $input, $cookie, $template->output;