Bug 18508: Fix t/db_dependent/api/v1/swagger/definitions.t (follow-up of 6758)
[koha.git] / opac / opac-showreviews.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Parts copyright 2010 Nelsonville Public Library
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23
24 use CGI qw ( -utf8 );
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Output;
28 use C4::Circulation;
29 use C4::Biblio;
30 use Koha::DateUtils;
31 use Koha::Patrons;
32 use Koha::Reviews;
33 use POSIX qw(ceil floor strftime);
34
35 my $template_name;
36 my $query = new CGI;
37 my $format = $query->param("format") || '';
38 my $count = C4::Context->preference('OPACnumSearchResults') || 20;
39 my $results_per_page = $query->param('count') || $count;
40 my $offset = $query->param('offset') || 0;
41 my $page = floor( $offset / $results_per_page ) + 1;
42
43 if ($format eq "rss") {
44     $template_name = "opac-showreviews-rss.tt";
45 } else {
46     $template_name = "opac-showreviews.tt",
47 }
48
49 my ( $template, $borrowernumber, $cookie ) = &get_template_and_user(
50     {
51         template_name   => $template_name,
52         query           => $query,
53         type            => "opac",
54         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
55     }
56 );
57
58 if($format eq "rss"){
59     my $lastbuilddate = dt_from_string;
60     my $lastbuilddate_output = $lastbuilddate->strftime("%a, %d %b %Y %H:%M:%S %z");
61     $template->param(
62         rss => 1,
63         timestamp => $lastbuilddate_output
64         );
65 }
66
67 my $libravatar_enabled = 0;
68 if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto')) {
69     eval {
70         require Libravatar::URL;
71         Libravatar::URL->import();
72     };
73     if ( !$@ ) {
74         $libravatar_enabled = 1;
75     }
76 }
77
78 my $reviews = Koha::Reviews->search(
79     { approved => 1 },
80     {
81         rows => $results_per_page,
82         page => $page,
83         order_by => { -desc => 'datereviewed' },
84     }
85 )->unblessed;
86 my $marcflavour      = C4::Context->preference("marcflavour");
87 my $hits = Koha::Reviews->search({ approved => 1 })->count;
88 my $i = 0;
89 my $latest_comment_date;
90 for my $result (@$reviews){
91     my $biblionumber = $result->{biblionumber};
92     my $biblio = Koha::Biblios->find( $biblionumber );
93     my $biblioitem = $biblio->biblioitem;
94     my $record = GetMarcBiblio({ biblionumber => $biblionumber });
95     my $frameworkcode = GetFrameworkCode($biblionumber);
96     my $borr = Koha::Patrons->find( $result->{borrowernumber} )->unblessed;
97         $result->{normalized_upc} = GetNormalizedUPC($record,$marcflavour);
98         $result->{normalized_ean} = GetNormalizedEAN($record,$marcflavour);
99         $result->{normalized_oclc} = GetNormalizedOCLCNumber($record,$marcflavour);
100         $result->{normalized_isbn} = GetNormalizedISBN(undef,$record,$marcflavour);
101     $result->{title} = $biblio->title;
102         $result->{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
103     $result->{author} = $biblio->author;
104     $result->{place} = $biblioitem->place;
105     $result->{publishercode} = $biblioitem->publishercode;
106     $result->{copyrightdate} = $biblio->copyrightdate;
107     $result->{pages} = $biblioitem->pages;
108     $result->{size} = $biblioitem->size;
109     $result->{notes} = $biblioitem->notes;
110     $result->{timestamp} = $biblioitem->timestamp;
111     $result->{borrtitle} = $borr->{'title'};
112         $result->{firstname} = $borr->{'firstname'};
113         $result->{surname} = $borr->{'surname'};
114     $result->{userid} = $borr->{'userid'};
115         if ($libravatar_enabled and $borr->{'email'}) {
116             $result->{avatarurl} = libravatar_url(email => $borr->{'email'}, size => 40, https => $ENV{HTTPS});
117         }
118
119     if ($result->{borrowernumber} eq $borrowernumber) {
120                 $result->{your_comment} = 1;
121         }
122
123     if($format eq "rss"){
124         my $rsstimestamp = eval { dt_from_string( $result->{datereviewed} ); };
125         $rsstimestamp = dt_from_string unless ( $rsstimestamp ); #default to today if something went wrong
126         my $rsstimestamp_output = $rsstimestamp->strftime("%a, %d %b %Y %H:%M:%S %z");
127         $result->{timestamp} = $rsstimestamp_output;
128     }
129 }
130 ## Build the page numbers on the bottom of the page
131             my @page_numbers;
132             my $previous_page_first;
133             my $previous_page_offset;
134             # total number of pages there will be
135             my $pages = ceil($hits / $results_per_page);
136             # default page number
137             my $current_page_number = 1;
138             $current_page_number = ($offset / $results_per_page + 1) if $offset;
139             if($offset - $results_per_page == 0){
140                 $previous_page_first = 1;
141             } elsif ($offset - $results_per_page > 0){
142                 $previous_page_offset = $offset - $results_per_page;
143             }
144             my $next_page_offset = $offset + $results_per_page;
145             # If we're within the first 10 pages, keep it simple
146             if ($current_page_number < 10) {
147                 # just show the first 10 pages
148                 # Loop through the pages
149                 my $pages_to_show = 10;
150                 $pages_to_show = $pages if $pages<10;
151                 for ($i=1; $i<=$pages_to_show;$i++) {
152                     # the offset for this page
153                     my $this_offset = (($i*$results_per_page)-$results_per_page);
154                     # the page number for this page
155                     my $this_page_number = $i;
156                     # it should only be highlighted if it's the current page
157                     my $highlight;
158             $highlight = 1 if ($this_page_number == $current_page_number);
159                     # put it in the array
160                     push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight };
161
162                 }
163
164             }
165             # now, show twenty pages, with the current one smack in the middle
166             else {
167                 for ($i=$current_page_number; $i<=($current_page_number + 20 );$i++) {
168                     my $this_offset = ((($i-9)*$results_per_page)-$results_per_page);
169                     my $this_page_number = $i-9;
170                     my $highlight;
171             $highlight = 1 if ($this_page_number == $current_page_number);
172                     if ($this_page_number <= $pages) {
173                         push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight };
174                     }
175                 }
176             }
177 $template->param(   PAGE_NUMBERS => \@page_numbers,
178                     previous_page_first => $previous_page_first,
179                     previous_page_offset => $previous_page_offset) unless $pages < 2;
180 $template->param(next_page_offset => $next_page_offset) unless $pages eq $current_page_number;
181
182 $template->param(
183     reviews => $reviews,
184     results_per_page => $results_per_page,
185 );
186
187 output_html_with_http_headers $query, $cookie, $template->output;