Bug 25789: Don't allow past date as holds expiration date
[koha.git] / authorities / detail.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 =head1 NAME
21
22 detail.pl : script to show an authority in MARC format
23
24 =head1 SYNOPSIS
25
26 =cut
27
28 =head1 DESCRIPTION
29
30 This script needs an authid
31
32 It shows the authority in a (nice) MARC format depending on authority MARC
33 parameters tables.
34
35 =head1 FUNCTIONS
36
37 =cut
38
39 use Modern::Perl;
40
41 use C4::AuthoritiesMarc;
42 use C4::Auth;
43 use C4::Context;
44 use C4::Output;
45 use CGI qw ( -utf8 );
46 use MARC::Record;
47 use C4::Koha;
48 use Koha::Authorities;
49
50 use Koha::Authority::Types;
51 use Koha::Token;
52 use Koha::Z3950Servers;
53
54 our ($tagslib);
55
56 sub build_tabs {
57     my ( $template, $record, $dbh, $encoding,$input ) = @_;
58
59     # fill arrays
60     my @loop_data = ();
61     my $tag;
62
63     # in this array, we will push all the 10 tabs
64     # to avoid having 10 tabs in the template : they will all be in the same BIG_LOOP
65     my @BIG_LOOP;
66     my %seen;
67     my @tab_data; # all tags to display
68     
69     foreach my $used ( keys %$tagslib ){
70         push @tab_data,$used if not $seen{$used};
71         $seen{$used}++;
72     }
73         
74     my $max_num_tab=9;
75     # loop through each tab 0 through 9
76     for ( my $tabloop = 0 ; $tabloop <= $max_num_tab ; $tabloop++ ) {
77         my @loop_data = (); #innerloop in the template.
78         my $i = 0;
79         foreach my $tag (sort @tab_data) {
80             $i++;
81             next if ! $tag;
82
83             # if MARC::Record is not empty =>use it as master loop, then add missing subfields that should be in the tab.
84             # if MARC::Record is empty => use tab as master loop.
85             if ( $record != -1 && ( $record->field($tag) || $tag eq '000' ) ) {
86                 my @fields;
87                 if ( $tag ne '000' ) {
88                     @fields = $record->field($tag);
89                 }
90                 else {
91                   push @fields, MARC::Field->new('000', $record->leader()); # if tag == 000
92                 }
93                 # loop through each field
94                 foreach my $field (@fields) {
95                     my @subfields_data;
96                     if ($field->tag()<10) {
97                         next
98                         if (
99                             $tagslib->{ $field->tag() }->{ '@' }->{tab}
100                             ne $tabloop );
101                       next if ($tagslib->{$field->tag()}->{'@'}->{hidden});
102                       my %subfield_data;
103                       $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{'@'}->{lib};
104                       $subfield_data{marc_value}=$field->data();
105                       $subfield_data{marc_subfield}='@';
106                       $subfield_data{marc_tag}=$field->tag();
107                       push(@subfields_data, \%subfield_data);
108                     } else {
109                       my @subf=$field->subfields;
110                   # loop through each subfield
111                       for my $i (0..$#subf) {
112                         $subf[$i][0] = "@" unless defined $subf[$i][0];
113                         next
114                         if (
115                             $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab}
116                             ne $tabloop );
117                         next
118                         if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }
119                             ->{hidden} );
120                         my %subfield_data;
121                         $subfield_data{marc_lib}=$tagslib->{$field->tag()}->{$subf[$i][0]}->{lib};
122                         if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{isurl}) {
123                             $subfield_data{marc_value} = $subf[$i][1];
124                             $subfield_data{is_url} = 1;
125                         } else {
126                           $subfield_data{marc_value}=$subf[$i][1];
127                         }
128                               $subfield_data{short_desc} = substr(
129                                   $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib},
130                                   0, 20
131                               );
132                               $subfield_data{long_desc} =
133                                 $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
134                         $subfield_data{marc_subfield}=$subf[$i][0];
135                         $subfield_data{marc_tag}=$field->tag();
136                         push(@subfields_data, \%subfield_data);
137                       }
138                     }
139                     if ($#subfields_data>=0) {
140                       my %tag_data;
141                       $tag_data{tag_number} = $tag;
142                       $tag_data{tag_desc} = $tagslib->{$field->tag()}->{lib};
143                       $tag_data{subfield} = \@subfields_data;
144                       my $indicators = C4::Koha::display_marc_indicators($field);
145                       if ( $indicators ) {
146                           $tag_data{ind1} = substr $indicators, 0, 1;
147                           $tag_data{ind2} = substr $indicators, 1, 1;
148                       }
149
150                       push (@loop_data, \%tag_data);
151                     }
152                   }
153               }
154             }
155             if ( $#loop_data >= 0 ) {
156                 push @BIG_LOOP, {
157                     number    => $tabloop,
158                     innerloop => \@loop_data,
159                 };
160             }
161         }
162         $template->param( singletab => (scalar(@BIG_LOOP)==1), BIG_LOOP => \@BIG_LOOP );
163 }
164
165
166
167 my $query=new CGI;
168
169 my $dbh=C4::Context->dbh;
170
171 # open template
172 my ($template, $loggedinuser, $cookie) = get_template_and_user(
173     {
174         template_name   => "authorities/detail.tt",
175         query           => $query,
176         type            => "intranet",
177         authnotrequired => 0,
178         flagsrequired   => { catalogue => 1 },
179         debug           => 1,
180     }
181 );
182
183 my $authid = $query->param('authid');
184
185 my $authobj = Koha::Authorities->find($authid);
186 my $authtypecode = $authobj ? $authobj->authtypecode : q{};
187 $tagslib = &GetTagsLabels(1,$authtypecode);
188
189 # Build list of authtypes for showing them
190 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
191
192 my $record=GetAuthority($authid);
193
194 if (not defined $record) {
195     # authid invalid
196     $template->param ( errauthid => $authid,
197                        unknownauthid => 1,
198                        authority_types => $authority_types, );
199     output_html_with_http_headers $query, $cookie, $template->output;
200     exit;
201 }
202
203 if (C4::Context->preference("AuthDisplayHierarchy")){
204     $template->{VARS}->{'displayhierarchy'} = C4::Context->preference("AuthDisplayHierarchy");
205     $template->{VARS}->{'loophierarchies'} = GenerateHierarchy($authid);
206 }
207
208 my $count = $authobj ? $authobj->get_usage_count : 0;
209
210 # find the marc field/subfield used in biblio by this authority
211 my $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?");
212 $sth->execute($authtypecode);
213 my $biblio_fields;
214 while (my ($tagfield) = $sth->fetchrow) {
215         $biblio_fields.= $tagfield."9,";
216 }
217 chop $biblio_fields if $biblio_fields;
218
219 build_tabs ($template, $record, $dbh,"",$query);
220
221 my $servers = Koha::Z3950Servers->search(
222     {
223         recordtype => 'authority',
224         servertype => ['zed', 'sru'],
225     },
226 );
227
228 my $type = $authority_types->find($authtypecode);
229 $template->param(
230     authid          => $authid,
231     count           => $count,
232     biblio_fields   => $biblio_fields,
233     authtypetext    => $type ? $type->authtypetext: "",
234     authtypecode    => $authtypecode,
235     authority_types => $authority_types,
236     csrf_token      => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID') }),
237     servers => $servers,
238 );
239
240 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
241 output_html_with_http_headers $query, $cookie, $template->output;
242