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