]> git.koha-community.org Git - koha.git/blob - admin/marc_subfields_structure.pl
Bug 36390: Two minor OPAC CSS fixes
[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 Encode qw( encode_utf8 );
22 use C4::Output qw( output_html_with_http_headers );
23 use C4::Auth qw( get_template_and_user );
24 use CGI qw ( -utf8 );
25 use C4::Context;
26
27 use Koha::Authority::Types;
28 use Koha::AuthorisedValueCategories;
29 use Koha::Filter::MARC::ViewPolicy;
30
31 use List::MoreUtils qw( uniq );
32
33 my $input         = CGI->new;
34 my $tagfield      = $input->param('tagfield');
35 my $tagsubfield   = $input->param('tagsubfield');
36 my $frameworkcode = $input->param('frameworkcode');
37 my $pkfield       = "tagfield";
38 my $offset        = $input->param('offset');
39 $offset = 0 if not defined $offset or $offset < 0;
40 my $script_name   = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
41
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43     {
44         template_name   => "admin/marc_subfields_structure.tt",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired   => { parameters => 'manage_marc_frameworks' },
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     my @authorised_values= Koha::AuthorisedValueCategories->search->get_column('category_name');
100
101     # build thesaurus categories list
102     my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
103
104     # build value_builder list
105     my @value_builder = ('');
106
107     # read value_builder directory.
108     # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
109     # on a standard install, /cgi-bin need to be added.
110     # test one, then the other
111     my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
112     my $dir_h;
113     unless ( opendir( $dir_h, "$cgidir/cataloguing/value_builder" ) ) {
114         $cgidir = C4::Context->config('intranetdir');
115         opendir( $dir_h, "$cgidir/cataloguing/value_builder" )
116           || die "can't opendir $cgidir/value_builder: $!";
117     }
118     while ( my $line = readdir($dir_h) ) {
119         if ( $line =~ /\.pl$/ &&
120              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
121             push( @value_builder, $line );
122         }
123     }
124     @value_builder= sort {$a cmp $b} @value_builder;
125     closedir $dir_h;
126
127     # build values list
128     my $mss = Koha::MarcSubfieldStructures->search(
129         { tagfield => $tagfield, frameworkcode => $frameworkcode },
130         { order_by => 'display_order' }
131     )->unblessed;
132     my @loop_data = ();
133     my $i         = 0;
134     for my $m ( @$mss ) {
135         my %row_data = %$m;    # get a fresh hash for the row data
136         $row_data{subfieldcode}      = $m->{tagsubfield};
137         $row_data{urisubfieldcode}   = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
138         $row_data{kohafields}        = \@kohafields;
139         $row_data{authorised_values} = \@authorised_values;
140         $row_data{value_builders}    = \@value_builder;
141         $row_data{authtypes}         = \@authtypes;
142         $row_data{row}               = $i;
143
144         if ( defined $m->{kohafield}
145             and $m->{kohafield} eq 'biblio.biblionumber' )
146         {
147             my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
148                     {
149                         frameworkcode => $frameworkcode,
150                         interface     => "opac",
151                     }
152                 )->{biblionumber};
153
154             my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
155                     {
156                         frameworkcode => $frameworkcode,
157                         interface     => "intranet",
158                     }
159                 )->{biblionumber};
160
161             if ( $hidden_opac or $hidden_intranet ) {
162                 # We should allow editing for fixing it
163                 $row_data{hidden_protected} = 0;
164             }
165             else {
166                 $row_data{hidden_protected} = 1;
167             }
168         }
169
170         push( @loop_data, \%row_data );
171         $i++;
172     }
173
174     # Add a new row for the "New" tab
175     my %row_data;    # get a fresh hash for the row data
176     $row_data{'new_subfield'}    = 1;
177     $row_data{'subfieldcode'}    = '';
178     $row_data{'maxlength'}       = 9999;
179     $row_data{tab}               = -1;                    #ignore
180     $row_data{tagsubfield}       = "";
181     $row_data{liblibrarian}      = "";
182     $row_data{libopac}           = "";
183     $row_data{seealso}           = "";
184     $row_data{hidden}            = "";
185     $row_data{repeatable}        = 0;
186     $row_data{mandatory}         = 0;
187     $row_data{important}         = 0;
188     $row_data{isurl}             = 0;
189     $row_data{kohafields}        = \@kohafields;
190     $row_data{authorised_values} = \@authorised_values;
191     $row_data{value_builders}    = \@value_builder;
192     $row_data{authtypes}         = \@authtypes;
193     $row_data{link}              = "";
194     $row_data{row}               = $i;
195     push( @loop_data, \%row_data );
196
197     $template->param( 'use_heading_flags_p'      => 1 );
198     $template->param( 'heading_edit_subfields_p' => 1 );
199     $template->param(
200         action   => "Edit subfields",
201         tagfield => $tagfield,
202         tagsubfield => $tagsubfield,
203         loop           => \@loop_data,
204         more_tag       => $tagfield
205     );
206
207     # END $OP eq ADD_FORM
208 ################## ADD_VALIDATE ##################################
209     # called by add_form, used to insert/modify data in DB
210 }
211 elsif ( $op eq 'add_validate' ) {
212     my $dbh = C4::Context->dbh;
213     $template->param( tagfield => "$input->param('tagfield')" );
214     my $tagfield    = $input->param('tagfield');
215     my @tagsubfield = $input->multi_param('tagsubfield');
216     my @tab_ids     = $input->multi_param('tab_id');
217
218     my $display_order;
219     for my $tagsubfield ( @tagsubfield ) {
220         $tagsubfield = "@" unless $tagsubfield ne '';
221         my $id = shift @tab_ids;
222         my $liblibrarian     = $input->param("liblibrarian_$id");
223         my $libopac          = $input->param("libopac_$id");
224         my $repeatable       = $input->param("repeatable_$id") ? 1 : 0;
225         my $mandatory        = $input->param("mandatory_$id") ? 1 : 0;
226         my $important        = $input->param("important_$id") ? 1 : 0;
227         my $kohafield        = $input->param("kohafield_$id");
228         my $tab              = $input->param("tab_$id");
229         my $seealso          = $input->param("seealso_$id");
230         my $authorised_value = $input->param("authorised_value_$id");
231         my $authtypecode     = $input->param("authtypecode_$id");
232         my $value_builder    = $input->param("value_builder_$id");
233         my $hidden = $input->param("hidden_$id");
234         my $isurl  = $input->param("isurl_$id") ? 1 : 0;
235         my $link   = $input->param("link_$id");
236         my $defaultvalue = $input->param("defaultvalue_$id");
237         my $maxlength = $input->param("maxlength_$id") || 9999;
238
239         if (defined($liblibrarian) && $liblibrarian ne "") {
240             my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
241             if ($mss) {
242                 $mss->update(
243                     {
244                         liblibrarian     => $liblibrarian,
245                         libopac          => $libopac,
246                         repeatable       => $repeatable,
247                         mandatory        => $mandatory,
248                         important        => $important,
249                         kohafield        => $kohafield,
250                         tab              => $tab,
251                         seealso          => $seealso,
252                         authorised_value => $authorised_value,
253                         authtypecode     => $authtypecode,
254                         value_builder    => $value_builder,
255                         hidden           => $hidden,
256                         isurl            => $isurl,
257                         link             => $link,
258                         defaultvalue     => $defaultvalue,
259                         maxlength        => $maxlength,
260                         display_order    => $display_order->{$tagfield} || 0
261                     }
262                 );
263             } else {
264                 if( $frameworkcode ne q{} ) {
265                     # BZ 19096: Overwrite kohafield from Default when adding a new record
266                      my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
267                     $kohafield = $rec->kohafield if $rec;
268                 }
269                 Koha::MarcSubfieldStructure->new(
270                     {
271                         tagfield         => $tagfield,
272                         tagsubfield      => $tagsubfield,
273                         liblibrarian     => $liblibrarian,
274                         libopac          => $libopac,
275                         repeatable       => $repeatable,
276                         mandatory        => $mandatory,
277                         important        => $important,
278                         kohafield        => $kohafield,
279                         tab              => $tab,
280                         seealso          => $seealso,
281                         authorised_value => $authorised_value,
282                         authtypecode     => $authtypecode,
283                         value_builder    => $value_builder,
284                         hidden           => $hidden,
285                         isurl            => $isurl,
286                         frameworkcode    => $frameworkcode,
287                         link             => $link,
288                         defaultvalue     => $defaultvalue,
289                         maxlength        => $maxlength,
290                         display_order    => $display_order->{$tagfield} || 0,
291                     }
292                 )->store;
293             }
294             $display_order->{$tagfield}++;
295         }
296     }
297     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
298     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
299     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
300     $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
301
302     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
303     exit;
304
305     # END $OP eq ADD_VALIDATE
306 ################## DELETE_CONFIRM ##################################
307     # called by default form, used to confirm deletion of data in DB
308 }
309 elsif ( $op eq 'delete_confirm' ) {
310     my $mss = Koha::MarcSubfieldStructures->find(
311         {
312             tagfield      => $tagfield,
313             tagsubfield   => $tagsubfield,
314             frameworkcode => $frameworkcode
315         }
316     );
317     $template->param(
318         mss => $mss,
319         delete_link   => $script_name,
320     );
321
322     # END $OP eq DELETE_CONFIRM
323 ################## DELETE_CONFIRMED ##################################
324   # called by delete_confirm, used to effectively confirm deletion of data in DB
325 }
326 elsif ( $op eq 'delete_confirmed' ) {
327     Koha::MarcSubfieldStructures->find(
328         {
329             tagfield      => $tagfield,
330             tagsubfield   => $tagsubfield,
331             frameworkcode => $frameworkcode
332         }
333     )->delete;
334
335     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
336     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
337     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
338     $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
339     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
340     exit;
341
342     # END $OP eq DELETE_CONFIRMED
343 ################## DEFAULT ##################################
344 }
345 else {    # DEFAULT
346     my $mss = Koha::MarcSubfieldStructures->search(
347         {
348             tagfield      => { -like => "$tagfield%" },
349             frameworkcode => $frameworkcode
350         },
351         { order_by => [ 'tagfield', 'display_order' ] }
352     )->unblessed;
353
354     $template->param( loop => $mss );
355     $template->param(
356         edit_tagfield      => $tagfield,
357         edit_frameworkcode => $frameworkcode
358     );
359
360 }    #---- END $OP eq DEFAULT
361
362 output_html_with_http_headers $input, $cookie, $template->output;