Bug 11592: (QA followup) Simplify code
[koha.git] / opac / opac-basket.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use C4::Koha;
22 use C4::Biblio;
23 use C4::Branch;
24 use C4::Items;
25 use C4::Circulation;
26 use C4::Auth;
27 use C4::Output;
28 use Koha::RecordProcessor;
29
30 my $query = new CGI;
31
32 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
33     {
34         template_name   => "opac-basket.tt",
35         query           => $query,
36         type            => "opac",
37         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
38     }
39 );
40
41 my $bib_list     = $query->param('bib_list');
42 my $print_basket = $query->param('print');
43 my $verbose      = $query->param('verbose');
44
45 if ($verbose)      { $template->param( verbose      => 1 ); }
46 if ($print_basket) { $template->param( print_basket => 1 ); }
47
48 my @bibs = split( /\//, $bib_list );
49 my @results;
50
51 my $num = 1;
52 my $marcflavour = C4::Context->preference('marcflavour');
53 if (C4::Context->preference('TagsEnabled')) {
54         $template->param(TagsEnabled => 1);
55         foreach (qw(TagsShowOnList TagsInputOnList)) {
56                 C4::Context->preference($_) and $template->param($_ => 1);
57         }
58 }
59
60 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
61 foreach my $biblionumber ( @bibs ) {
62     $template->param( biblionumber => $biblionumber );
63
64     my $dat              = &GetBiblioData($biblionumber);
65     next unless $dat;
66     my $record = &GetMarcBiblio($biblionumber);
67     $record_processor->process($record);
68     next unless $record;
69     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
70     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
71     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
72     my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
73     my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
74     my @items            = &GetItemsInfo( $biblionumber );
75     my $subtitle         = GetRecordValue('subtitle', $record, GetFrameworkCode($biblionumber));
76
77     my $hasauthors = 0;
78     if($dat->{'author'} || @$marcauthorsarray) {
79       $hasauthors = 1;
80     }
81     my $collections =  GetKohaAuthorisedValues('items.ccode',$dat->{'frameworkcode'}, 'opac');
82     my $shelflocations =GetKohaAuthorisedValues('items.location',$dat->{'frameworkcode'}, 'opac');
83
84         # COinS format FIXME: for books Only
85         my $coins_format;
86         my $fmt = substr $record->leader(), 6,2;
87         my $fmts;
88         $fmts->{'am'} = 'book';
89         $dat->{ocoins_format} = $fmts->{$fmt};
90
91     if ( $num % 2 == 1 ) {
92         $dat->{'even'} = 1;
93     }
94
95 my $branches = GetBranches();
96     for my $itm (@items) {
97         if ($itm->{'location'}){
98             $itm->{'location_opac'} = $shelflocations->{$itm->{'location'} };
99         }
100         my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
101         if ( defined( $transfertwhen ) && $transfertwhen ne '' ) {
102              $itm->{transfertwhen} = $transfertwhen;
103              $itm->{transfertfrom} = $branches->{$transfertfrom}{branchname};
104              $itm->{transfertto}   = $branches->{$transfertto}{branchname};
105         }
106     }
107     $num++;
108     $dat->{biblionumber} = $biblionumber;
109     $dat->{ITEM_RESULTS}   = \@items;
110     $dat->{MARCNOTES}      = $marcnotesarray;
111     $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
112     $dat->{MARCAUTHORS}    = $marcauthorsarray;
113     $dat->{MARCSERIES}  = $marcseriesarray;
114     $dat->{MARCURLS}    = $marcurlsarray;
115     $dat->{HASAUTHORS}  = $hasauthors;
116     $dat->{subtitle} = $subtitle;
117
118     if ( C4::Context->preference("BiblioDefaultView") eq "normal" ) {
119         $dat->{dest} = "opac-detail.pl";
120     }
121     elsif ( C4::Context->preference("BiblioDefaultView") eq "marc" ) {
122         $dat->{dest} = "opac-MARCdetail.pl";
123     }
124     else {
125         $dat->{dest} = "opac-ISBDdetail.pl";
126     }
127     push( @results, $dat );
128 }
129
130 my $resultsarray = \@results;
131
132 # my $itemsarray=\@items;
133
134 $template->param(
135     bib_list => $bib_list,
136     BIBLIO_RESULTS => $resultsarray,
137 );
138
139 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };