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