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