Bug 18111: Fix import of default framework
[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') || 0;
69 my $script_name   = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
70
71 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
72     {
73         template_name   => "admin/marc_subfields_structure.tt",
74         query           => $input,
75         type            => "intranet",
76         authnotrequired => 0,
77         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
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'} eq '@' ? '_' : $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 = $dbh->prepare(
236 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
237 #                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
238 #     );
239     my $sth_insert = $dbh->prepare(qq{
240         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)
241         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
242     });
243     my $sth_update = $dbh->prepare(qq{
244         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=?
245         where tagfield=? and tagsubfield=? and frameworkcode=?
246     });
247     my @tagsubfield       = $input->multi_param('tagsubfield');
248     my @liblibrarian      = $input->multi_param('liblibrarian');
249     my @libopac           = $input->multi_param('libopac');
250     my @kohafield         = $input->multi_param('kohafield');
251     my @tab               = $input->multi_param('tab');
252     my @seealso           = $input->multi_param('seealso');
253     my @hidden            = $input->multi_param('hidden');
254     my @authorised_values = $input->multi_param('authorised_value');
255     my @authtypecodes     = $input->multi_param('authtypecode');
256     my @value_builder     = $input->multi_param('value_builder');
257     my @link              = $input->multi_param('link');
258     my @defaultvalue      = $input->multi_param('defaultvalue');
259     my @maxlength         = $input->multi_param('maxlength');
260     
261     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
262         my $tagfield    = $input->param('tagfield');
263         my $tagsubfield = $tagsubfield[$i];
264         $tagsubfield = "@" unless $tagsubfield ne '';
265         $tagsubfield = "@" if $tagsubfield eq '_';
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             my $is_demo = C4::Context->config('demo') || '';
284             if ( $is_demo ne '1' ) {
285                 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
286                     $sth_update->execute(
287                         $tagfield,
288                         $tagsubfield,
289                         $liblibrarian,
290                         $libopac,
291                         $repeatable,
292                         $mandatory,
293                         $kohafield,
294                         $tab,
295                         $seealso,
296                         $authorised_value,
297                         $authtypecode,
298                         $value_builder,
299                         $hidden,
300                         $isurl,
301                         $frameworkcode,
302                         $link,
303                         $defaultvalue,
304                         $maxlength,
305                         (
306                             $tagfield,
307                             $tagsubfield,
308                             $frameworkcode,
309                         ),
310                     );
311                 } else {
312                     $sth_insert->execute(
313                         $tagfield,
314                         $tagsubfield,
315                         $liblibrarian,
316                         $libopac,
317                         $repeatable,
318                         $mandatory,
319                         $kohafield,
320                         $tab,
321                         $seealso,
322                         $authorised_value,
323                         $authtypecode,
324                         $value_builder,
325                         $hidden,
326                         $isurl,
327                         $frameworkcode,
328                         $link,
329                         $defaultvalue,
330                         $maxlength,
331                     );
332                 }
333             }
334         }
335     }
336     $sth_insert->finish;
337     $sth_update->finish;
338     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
339     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
340     $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
341     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
342
343     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
344     exit;
345
346     # END $OP eq ADD_VALIDATE
347 ################## DELETE_CONFIRM ##################################
348     # called by default form, used to confirm deletion of data in DB
349 }
350 elsif ( $op eq 'delete_confirm' ) {
351     my $dbh = C4::Context->dbh;
352     my $sth =
353       $dbh->prepare(
354 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
355       );
356
357     $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
358     my $data = $sth->fetchrow_hashref;
359     $sth->finish;
360     $template->param(
361         liblibrarian  => $data->{'liblibrarian'},
362         tagsubfield   => $data->{'tagsubfield'},
363         delete_link   => $script_name,
364         tagfield      => $tagfield,
365         tagsubfield   => $tagsubfield,
366         frameworkcode => $frameworkcode,
367     );
368
369     # END $OP eq DELETE_CONFIRM
370 ################## DELETE_CONFIRMED ##################################
371   # called by delete_confirm, used to effectively confirm deletion of data in DB
372 }
373 elsif ( $op eq 'delete_confirmed' ) {
374     my $dbh = C4::Context->dbh;
375     my $is_demo = C4::Context->config('demo') || '';
376     if ( $is_demo ne '1' ) {
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     }
384     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
385     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
386     $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
387     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
388     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
389     exit;
390
391     # END $OP eq DELETE_CONFIRMED
392 ################## DEFAULT ##################################
393 }
394 else {    # DEFAULT
395     my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
396     my @loop_data = ();
397     for ( my $i = 0; $i < $count; $i++ ) {
398         my %row_data;    # get a fresh hash for the row data
399         $row_data{tagfield}         = $results->[$i]{'tagfield'};
400         $row_data{tagsubfield}      = $results->[$i]{'tagsubfield'};
401         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
402         $row_data{kohafield}        = $results->[$i]{'kohafield'};
403         $row_data{repeatable}       = $results->[$i]{'repeatable'};
404         $row_data{mandatory}        = $results->[$i]{'mandatory'};
405         $row_data{tab}              = $results->[$i]{'tab'};
406         $row_data{seealso}          = $results->[$i]{'seealso'};
407         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
408         $row_data{authtypecode}     = $results->[$i]{'authtypecode'};
409         $row_data{value_builder}    = $results->[$i]{'value_builder'};
410         $row_data{hidden}           = $results->[$i]{'hidden'};
411         $row_data{isurl}            = $results->[$i]{'isurl'};
412         $row_data{link}             = $results->[$i]{'link'};
413
414         if ( $row_data{tab} eq -1 ) {
415             $row_data{subfield_ignored} = 1;
416         }
417
418         push( @loop_data, \%row_data );
419     }
420     $template->param( loop => \@loop_data );
421     $template->param(
422         edit_tagfield      => $tagfield,
423         edit_frameworkcode => $frameworkcode
424     );
425
426 }    #---- END $OP eq DEFAULT
427
428 output_html_with_http_headers $input, $cookie, $template->output;