Bug 16996: (follow-up) Do not explode if mandatory fields are missing
[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::Review;
30 use C4::Biblio;
31 use C4::Members qw/GetMemberDetails/;
32 use Koha::DateUtils;
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.tt";
46 } else {
47     $template_name = "opac-showreviews.tt",
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 = dt_from_string;
61     my $lastbuilddate_output = $lastbuilddate->strftime("%a, %d %b %Y %H:%M:%S %z");
62     $template->param(
63         rss => 1,
64         timestamp => $lastbuilddate_output
65         );
66 }
67
68 my $libravatar_enabled = 0;
69 if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto')) {
70     eval {
71         require Libravatar::URL;
72         Libravatar::URL->import();
73     };
74     if ( !$@ ) {
75         $libravatar_enabled = 1;
76     }
77 }
78
79 my $reviews = getallreviews(1,$offset,$results_per_page);
80 my $marcflavour      = C4::Context->preference("marcflavour");
81 my $hits = numberofreviews(1);
82 my $i = 0;
83 my $latest_comment_date;
84 for my $result (@$reviews){
85     my $biblionumber = $result->{biblionumber};
86         my $bib = &GetBiblioData($biblionumber);
87     my $record = GetMarcBiblio($biblionumber);
88     my $frameworkcode = GetFrameworkCode($biblionumber);
89         my ( $borr ) = GetMemberDetails( $result->{borrowernumber} );
90         $result->{normalized_upc} = GetNormalizedUPC($record,$marcflavour);
91         $result->{normalized_ean} = GetNormalizedEAN($record,$marcflavour);
92         $result->{normalized_oclc} = GetNormalizedOCLCNumber($record,$marcflavour);
93         $result->{normalized_isbn} = GetNormalizedISBN(undef,$record,$marcflavour);
94         $result->{title} = $bib->{'title'};
95         $result->{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
96         $result->{author} = $bib->{'author'};
97         $result->{place} = $bib->{'place'};
98         $result->{publishercode} = $bib->{'publishercode'};
99         $result->{copyrightdate} = $bib->{'copyrightdate'};
100         $result->{pages} = $bib->{'pages'};
101         $result->{size} = $bib->{'size'};
102         $result->{notes} = $bib->{'notes'};
103         $result->{timestamp} = $bib->{'timestamp'};
104     $result->{borrtitle} = $borr->{'title'};
105         $result->{firstname} = $borr->{'firstname'};
106         $result->{surname} = $borr->{'surname'};
107     $result->{userid} = $borr->{'userid'};
108         if ($libravatar_enabled and $borr->{'email'}) {
109             $result->{avatarurl} = libravatar_url(email => $borr->{'email'}, size => 40, https => $ENV{HTTPS});
110         }
111
112     if ($result->{borrowernumber} eq $borrowernumber) {
113                 $result->{your_comment} = 1;
114         }
115
116     if($format eq "rss"){
117         my $rsstimestamp = eval { dt_from_string( $result->{datereviewed} ); };
118         $rsstimestamp = dt_from_string unless ( $rsstimestamp ); #default to today if something went wrong
119         my $rsstimestamp_output = $rsstimestamp->strftime("%a, %d %b %Y %H:%M:%S %z");
120         $result->{timestamp} = $rsstimestamp_output;
121     }
122 }
123 ## Build the page numbers on the bottom of the page
124             my @page_numbers;
125             my $previous_page_first;
126             my $previous_page_offset;
127             # total number of pages there will be
128             my $pages = ceil($hits / $results_per_page);
129             # default page number
130             my $current_page_number = 1;
131             $current_page_number = ($offset / $results_per_page + 1) if $offset;
132             if($offset - $results_per_page == 0){
133                 $previous_page_first = 1;
134             } elsif ($offset - $results_per_page > 0){
135                 $previous_page_offset = $offset - $results_per_page;
136             }
137             my $next_page_offset = $offset + $results_per_page;
138             # If we're within the first 10 pages, keep it simple
139             if ($current_page_number < 10) {
140                 # just show the first 10 pages
141                 # Loop through the pages
142                 my $pages_to_show = 10;
143                 $pages_to_show = $pages if $pages<10;
144                 for ($i=1; $i<=$pages_to_show;$i++) {
145                     # the offset for this page
146                     my $this_offset = (($i*$results_per_page)-$results_per_page);
147                     # the page number for this page
148                     my $this_page_number = $i;
149                     # it should only be highlighted if it's the current page
150                     my $highlight;
151             $highlight = 1 if ($this_page_number == $current_page_number);
152                     # put it in the array
153                     push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight };
154
155                 }
156
157             }
158             # now, show twenty pages, with the current one smack in the middle
159             else {
160                 for ($i=$current_page_number; $i<=($current_page_number + 20 );$i++) {
161                     my $this_offset = ((($i-9)*$results_per_page)-$results_per_page);
162                     my $this_page_number = $i-9;
163                     my $highlight;
164             $highlight = 1 if ($this_page_number == $current_page_number);
165                     if ($this_page_number <= $pages) {
166                         push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight };
167                     }
168                 }
169             }
170 $template->param(   PAGE_NUMBERS => \@page_numbers,
171                     previous_page_first => $previous_page_first,
172                     previous_page_offset => $previous_page_offset) unless $pages < 2;
173 $template->param(next_page_offset => $next_page_offset) unless $pages eq $current_page_number;
174
175 $template->param(
176     reviews => $reviews,
177 );
178
179 output_html_with_http_headers $query, $cookie, $template->output;