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