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