Enh 6165: Add OPACResultsSidebar system preference
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Output;
28 use C4::Circulation;
29 use C4::Review;
30 use C4::Biblio;
31 use C4::Dates qw/format_date/;
32 use C4::Members qw/GetMemberDetails/;
33 use POSIX qw(ceil 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 = $query->param('page') || 1;
42 $offset = ($page-1)*$results_per_page if $page>1;
43
44 if ($format eq "rss") {
45     $template_name = "opac-showreviews-rss.tmpl";
46 } else {
47     $template_name = "opac-showreviews.tmpl",
48 }
49
50 my ( $template, $borrowernumber, $cookie ) = &get_template_and_user(
51     {
52         template_name   => $template_name,
53         query           => $query,
54         type            => "opac",
55         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
56     }
57 );
58
59 if($format eq "rss"){
60     my $lastbuilddate = C4::Dates->new();
61     my $lastbuilddate_output = $lastbuilddate->output("rfc822");
62     $template->param(
63         rss => 1,
64         timestamp => $lastbuilddate_output
65         );
66 }
67
68 my $libravatar_enabled = 0;
69 eval 'use Libravatar::URL';
70 if (!$@ and C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto')) {
71     $libravatar_enabled = 1;
72 }
73
74 my $reviews = getallreviews(1,$offset,$results_per_page);
75 my $marcflavour      = C4::Context->preference("marcflavour");
76 my $hits = numberofreviews();
77 my $i = 0;
78 my $latest_comment_date;
79 for my $result (@$reviews){
80     my $biblionumber = $result->{biblionumber};
81         my $bib = &GetBiblioData($biblionumber);
82     my $record = GetMarcBiblio($biblionumber);
83     my $frameworkcode = GetFrameworkCode($biblionumber);
84         my ( $borr ) = GetMemberDetails( $result->{borrowernumber} );
85         $result->{normalized_upc} = GetNormalizedUPC($record,$marcflavour);
86         $result->{normalized_ean} = GetNormalizedEAN($record,$marcflavour);
87         $result->{normalized_oclc} = GetNormalizedOCLCNumber($record,$marcflavour);
88         $result->{normalized_isbn} = GetNormalizedISBN(undef,$record,$marcflavour);
89         $result->{title} = $bib->{'title'};
90         $result->{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
91         $result->{author} = $bib->{'author'};
92         $result->{place} = $bib->{'place'};
93         $result->{publishercode} = $bib->{'publishercode'};
94         $result->{copyrightdate} = $bib->{'copyrightdate'};
95         $result->{pages} = $bib->{'pages'};
96         $result->{size} = $bib->{'size'};
97         $result->{notes} = $bib->{'notes'};
98         $result->{timestamp} = $bib->{'timestamp'};
99         $result->{firstname} = $borr->{'firstname'};
100         $result->{surname} = $borr->{'surname'};
101         if ($libravatar_enabled and $borr->{'email'}) {
102             $result->{avatarurl} = libravatar_url(email => $borr->{'email'}, size => 40, https => $ENV{HTTPS});
103         }
104
105     if ($result->{borrowernumber} eq $borrowernumber) {
106                 $result->{your_comment} = 1;
107         }
108
109     if($format eq "rss"){
110         my $rsstimestamp = C4::Dates->new($result->{datereviewed},"iso");
111         my $rsstimestamp_output = $rsstimestamp->output("rfc822");
112         $result->{timestamp} = $rsstimestamp_output;
113         $result->{datereviewed} = format_date($result->{datereviewed});
114     } else {
115         $result->{datereviewed} = format_date($result->{datereviewed});
116     }
117 }
118 ## Build the page numbers on the bottom of the page
119             my @page_numbers;
120             my $previous_page_first;
121             my $previous_page_offset;
122             # total number of pages there will be
123             my $pages = ceil($hits / $results_per_page);
124             # default page number
125             my $current_page_number = 1;
126             $current_page_number = ($offset / $results_per_page + 1) if $offset;
127             if($offset - $results_per_page == 0){
128                 $previous_page_first = 1;
129             } elsif ($offset - $results_per_page > 0){
130                 $previous_page_offset = $offset - $results_per_page;
131             }
132             my $next_page_offset = $offset + $results_per_page;
133             # If we're within the first 10 pages, keep it simple
134             if ($current_page_number < 10) {
135                 # just show the first 10 pages
136                 # Loop through the pages
137                 my $pages_to_show = 10;
138                 $pages_to_show = $pages if $pages<10;
139                 for ($i=1; $i<=$pages_to_show;$i++) {
140                     # the offset for this page
141                     my $this_offset = (($i*$results_per_page)-$results_per_page);
142                     # the page number for this page
143                     my $this_page_number = $i;
144                     # it should only be highlighted if it's the current page
145                     my $highlight = 1 if ($this_page_number == $current_page_number);
146                     # put it in the array
147                     push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight };
148
149                 }
150
151             }
152             # now, show twenty pages, with the current one smack in the middle
153             else {
154                 for ($i=$current_page_number; $i<=($current_page_number + 20 );$i++) {
155                     my $this_offset = ((($i-9)*$results_per_page)-$results_per_page);
156                     my $this_page_number = $i-9;
157                     my $highlight = 1 if ($this_page_number == $current_page_number);
158                     if ($this_page_number <= $pages) {
159                         push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight };
160                     }
161                 }
162             }
163 $template->param(   PAGE_NUMBERS => \@page_numbers,
164                     previous_page_first => $previous_page_first,
165                     previous_page_offset => $previous_page_offset) unless $pages < 2;
166 $template->param(next_page_offset => $next_page_offset) unless $pages eq $current_page_number;
167
168 $template->param(
169     reviews => $reviews,
170 );
171
172 output_html_with_http_headers $query, $cookie, $template->output;