Bug 35496: (QA follow-up): tidy up code
[koha.git] / opac / opac-readingrecord.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
19 use Modern::Perl;
20
21 use CGI qw ( -utf8 );
22
23 use C4::Auth qw( get_template_and_user );
24 use C4::Koha qw(
25     getitemtypeimagelocation
26     GetNormalizedISBN
27     GetNormalizedUPC
28     GetNormalizedOCLCNumber
29 );
30 use C4::Biblio;
31 use C4::Members qw( GetAllIssues );
32 use C4::External::BakerTaylor qw( image_url link_url );
33 use MARC::Record;
34
35 use C4::Output qw( output_html_with_http_headers );
36 use C4::Charset qw( StripNonXmlChars );
37 use Koha::Patrons;
38
39 use Koha::ItemTypes;
40 use Koha::Ratings;
41
42 my $query = CGI->new;
43
44 # if opacreadinghistory is disabled, leave immediately
45 if ( ! C4::Context->preference('opacreadinghistory') ) {
46     print $query->redirect("/cgi-bin/koha/errors/404.pl");
47     exit;
48 }
49
50 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
51     {
52         template_name   => "opac-readingrecord.tt",
53         query           => $query,
54         type            => "opac",
55     }
56 );
57
58 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
59
60 # get the record
61 my $order = $query->param('order') || '';
62 if ( $order eq 'title' ) {
63     $template->param( orderbytitle => 1 );
64 }
65 elsif ( $order eq 'author' ) {
66     $template->param( orderbyauthor => 1 );
67 }
68 else {
69     $order = "date_due desc";
70     $template->param( orderbydate => 1 );
71 }
72
73
74 my $limit = $query->param('limit');
75 $limit //= '';
76 $limit = ( $limit eq 'full' ) ? 0 : 50;
77
78 my $issues = GetAllIssues( $borrowernumber, $order, $limit );
79
80 my $itype_attribute =
81   ( C4::Context->preference('item-level_itypes') ) ? 'itype' : 'itemtype';
82
83 my $opac_summary_html = C4::Context->preference('OPACMySummaryHTML');
84 foreach my $issue ( @{$issues} ) {
85     $issue->{normalized_isbn} = GetNormalizedISBN( $issue->{isbn} );
86     if ( $issue->{$itype_attribute} ) {
87         $issue->{translated_description} =
88           $itemtypes->{ $issue->{$itype_attribute} }->{translated_description};
89         $issue->{imageurl} =
90           getitemtypeimagelocation( 'opac',
91             $itemtypes->{ $issue->{$itype_attribute} }->{imageurl} );
92     }
93
94     if (   C4::Context->preference('BakerTaylorEnabled')
95         || C4::Context->preference('SyndeticsEnabled')
96         || C4::Context->preference('SyndeticsCoverImages') )
97     {
98         my $marcxml = C4::Biblio::GetXmlBiblio( $issue->{biblionumber} );
99         if ( $marcxml ) {
100             $marcxml = StripNonXmlChars( $marcxml );
101             my $marc_rec =
102               MARC::Record::new_from_xml( $marcxml, 'UTF-8',
103                 C4::Context->preference('marcflavour') );
104             $issue->{normalized_upc} = GetNormalizedUPC( $marc_rec, C4::Context->preference('marcflavour') );
105             $issue->{normalized_oclc} = GetNormalizedOCLCNumber($marc_rec, C4::Context->preference('marcflavour'));
106         }
107
108     }
109     # My Summary HTML
110     if ($opac_summary_html) {
111         my $my_summary_html = $opac_summary_html;
112         $issue->{author}
113           ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g
114           : $my_summary_html =~ s/{AUTHOR}//g;
115         my $title = $issue->{title};
116         $title =~ s/\/+$//;    # remove trailing slash
117         $title =~ s/\s+$//;    # remove trailing space
118         $title
119           ? $my_summary_html =~ s/{TITLE}/$title/g
120           : $my_summary_html =~ s/{TITLE}//g;
121         $issue->{normalized_isbn}
122           ? $my_summary_html =~ s/{ISBN}/$issue->{normalized_isbn}/g
123           : $my_summary_html =~ s/{ISBN}//g;
124         $issue->{biblionumber}
125           ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g
126           : $my_summary_html =~ s/{BIBLIONUMBER}//g;
127         $issue->{MySummaryHTML} = $my_summary_html;
128     }
129     # Star ratings
130     if ( C4::Context->preference('OpacStarRatings') eq 'all' ) {
131         my $ratings = Koha::Ratings->search({ biblionumber => $issue->{biblionumber} });
132         $issue->{ratings} = $ratings;
133         $issue->{my_rating} = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
134     }
135 }
136
137 if (C4::Context->preference('BakerTaylorEnabled')) {
138         $template->param(
139                 JacketImages=>1,
140                 BakerTaylorEnabled  => 1,
141                 BakerTaylorImageURL => &image_url(),
142                 BakerTaylorLinkURL  => &link_url(),
143                 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
144         );
145 }
146
147 for(qw(AmazonCoverImages GoogleJackets)) { # BakerTaylorEnabled handled above
148         C4::Context->preference($_) or next;
149         $template->param($_=>1);
150         $template->param(JacketImages=>1);
151 }
152
153 my $saving_display = C4::Context->preference('OPACShowSavings');
154 if ( $saving_display =~ /checkouthistory/ ) {
155     my $patron = Koha::Patrons->find( $borrowernumber );
156     $template->param( savings => $patron->get_savings );
157 }
158
159 $template->param(
160     READING_RECORD => $issues,
161     limit          => $limit,
162     readingrecview => 1,
163     OPACMySummaryHTML => $opac_summary_html ? 1 : 0,
164 );
165
166 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };