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