Bug 22696: Simplify visibility logic in opac-ISBDdetail.pl
[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 Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Output;
27 use C4::Circulation;
28 use C4::Biblio;
29 use Koha::DateUtils;
30 use Koha::Patrons;
31 use Koha::Reviews;
32 use POSIX qw(ceil floor strftime);
33
34 my $template_name;
35 my $query = new CGI;
36 my $format = $query->param("format") || '';
37 my $count = C4::Context->preference('OPACnumSearchResults') || 20;
38 my $results_per_page = $query->param('count') || $count;
39 my $offset = $query->param('offset') || 0;
40 my $page = floor( $offset / $results_per_page ) + 1;
41
42 if ($format eq "rss") {
43     $template_name = "opac-showreviews-rss.tt";
44 } else {
45     $template_name = "opac-showreviews.tt",
46 }
47
48 my ( $template, $borrowernumber, $cookie ) = &get_template_and_user(
49     {
50         template_name   => $template_name,
51         query           => $query,
52         type            => "opac",
53         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
54     }
55 );
56
57 if($format eq "rss"){
58     my $lastbuilddate = dt_from_string;
59     my $lastbuilddate_output = $lastbuilddate->strftime("%a, %d %b %Y %H:%M:%S %z");
60     $template->param(
61         rss => 1,
62         timestamp => $lastbuilddate_output
63         );
64 }
65
66 my $libravatar_enabled = 0;
67 if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto')) {
68     eval {
69         require Libravatar::URL;
70         Libravatar::URL->import();
71     };
72     if ( !$@ ) {
73         $libravatar_enabled = 1;
74     }
75 }
76
77 my $reviews = Koha::Reviews->search(
78     { approved => 1 },
79     {
80         rows => $results_per_page,
81         page => $page,
82         order_by => { -desc => 'datereviewed' },
83     }
84 )->unblessed;
85 my $marcflavour      = C4::Context->preference("marcflavour");
86 my $hits = Koha::Reviews->search({ approved => 1 })->count;
87 my $i = 0;
88 my $latest_comment_date;
89 for my $result (@$reviews){
90     my $biblionumber = $result->{biblionumber};
91     my $biblio = Koha::Biblios->find( $biblionumber );
92     my $biblioitem = $biblio->biblioitem;
93     my $record = GetMarcBiblio({ biblionumber => $biblionumber });
94     my $frameworkcode = GetFrameworkCode($biblionumber);
95         $result->{normalized_upc} = GetNormalizedUPC($record,$marcflavour);
96         $result->{normalized_ean} = GetNormalizedEAN($record,$marcflavour);
97         $result->{normalized_oclc} = GetNormalizedOCLCNumber($record,$marcflavour);
98         $result->{normalized_isbn} = GetNormalizedISBN(undef,$record,$marcflavour);
99     $result->{title} = $biblio->title;
100         $result->{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
101     $result->{author} = $biblio->author;
102     $result->{place} = $biblioitem->place;
103     $result->{publishercode} = $biblioitem->publishercode;
104     $result->{copyrightdate} = $biblio->copyrightdate;
105     $result->{pages} = $biblioitem->pages;
106     $result->{size} = $biblioitem->size;
107     $result->{notes} = $biblioitem->notes;
108     $result->{timestamp} = $biblioitem->timestamp;
109
110     my $patron = Koha::Patrons->find( $result->{borrowernumber} );
111     if ( $patron ) {
112         $result->{borrtitle} = $patron->title;
113         $result->{firstname} = $patron->firstname;
114         $result->{surname} = $patron->surname;
115         $result->{userid} = $patron->userid;
116             if ($libravatar_enabled and $patron->email) {
117                 $result->{avatarurl} = libravatar_url(email => $patron->email, size => 40, https => $ENV{HTTPS});
118             }
119
120         if ($result->{borrowernumber} eq $borrowernumber) {
121             $result->{your_comment} = 1;
122         }
123     }
124
125     if($format eq "rss"){
126         my $rsstimestamp = eval { dt_from_string( $result->{datereviewed} ); };
127         $rsstimestamp = dt_from_string unless ( $rsstimestamp ); #default to today if something went wrong
128         my $rsstimestamp_output = $rsstimestamp->strftime("%a, %d %b %Y %H:%M:%S %z");
129         $result->{timestamp} = $rsstimestamp_output;
130     }
131 }
132 ## Build the page numbers on the bottom of the page
133             my @page_numbers;
134             my $previous_page_first;
135             my $previous_page_offset;
136             # total number of pages there will be
137             my $pages = ceil($hits / $results_per_page);
138             # default page number
139             my $current_page_number = 1;
140             $current_page_number = ($offset / $results_per_page + 1) if $offset;
141             if($offset - $results_per_page == 0){
142                 $previous_page_first = 1;
143             } elsif ($offset - $results_per_page > 0){
144                 $previous_page_offset = $offset - $results_per_page;
145             }
146             my $next_page_offset = $offset + $results_per_page;
147             # If we're within the first 10 pages, keep it simple
148             if ($current_page_number < 10) {
149                 # just show the first 10 pages
150                 # Loop through the pages
151                 my $pages_to_show = 10;
152                 $pages_to_show = $pages if $pages<10;
153                 for ($i=1; $i<=$pages_to_show;$i++) {
154                     # the offset for this page
155                     my $this_offset = (($i*$results_per_page)-$results_per_page);
156                     # the page number for this page
157                     my $this_page_number = $i;
158                     # it should only be highlighted if it's the current page
159                     my $highlight;
160             $highlight = 1 if ($this_page_number == $current_page_number);
161                     # put it in the array
162                     push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight };
163
164                 }
165
166             }
167             # now, show twenty pages, with the current one smack in the middle
168             else {
169                 for ($i=$current_page_number; $i<=($current_page_number + 20 );$i++) {
170                     my $this_offset = ((($i-9)*$results_per_page)-$results_per_page);
171                     my $this_page_number = $i-9;
172                     my $highlight;
173             $highlight = 1 if ($this_page_number == $current_page_number);
174                     if ($this_page_number <= $pages) {
175                         push @page_numbers, { offset => $this_offset, pg => $this_page_number, highlight => $highlight };
176                     }
177                 }
178             }
179 $template->param(   PAGE_NUMBERS => \@page_numbers,
180                     previous_page_first => $previous_page_first,
181                     previous_page_offset => $previous_page_offset) unless $pages < 2;
182 $template->param(next_page_offset => $next_page_offset) unless $pages eq $current_page_number;
183
184 $template->param(
185     reviews => $reviews,
186     results_per_page => $results_per_page,
187 );
188
189 output_html_with_http_headers $query, $cookie, $template->output;