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