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