Bug 24606: (QA follow-up) Fix Template.t
[koha-ffzg.git] / opac / opac-showreviews.pl
index a88a74e..7a72214 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
-use C4::Auth;
-use C4::Koha;
-use C4::Output;
-use C4::Circulation;
-use C4::Review;
-use C4::Biblio;
-use C4::Members qw/GetMemberDetails/;
-use Koha::DateUtils;
+use C4::Auth qw( get_template_and_user );
+use C4::Koha qw(
+    GetNormalizedEAN
+    GetNormalizedISBN
+    GetNormalizedOCLCNumber
+    GetNormalizedUPC
+);
+use C4::Output qw( output_html_with_http_headers );
+use Koha::DateUtils qw( dt_from_string );
+use Koha::Biblios;
+use Koha::Patrons;
 use Koha::Reviews;
-use POSIX qw(ceil strftime);
+use POSIX qw( ceil floor );
 
 my $template_name;
-my $query = new CGI;
+my $query = CGI->new;
 my $format = $query->param("format") || '';
 my $count = C4::Context->preference('OPACnumSearchResults') || 20;
 my $results_per_page = $query->param('count') || $count;
 my $offset = $query->param('offset') || 0;
-my $page = $offset / $results_per_page + 1;
+my $page = floor( $offset / $results_per_page ) + 1;
 
 if ($format eq "rss") {
     $template_name = "opac-showreviews-rss.tt";
@@ -87,38 +89,45 @@ my $reviews = Koha::Reviews->search(
 my $marcflavour      = C4::Context->preference("marcflavour");
 my $hits = Koha::Reviews->search({ approved => 1 })->count;
 my $i = 0;
-my $latest_comment_date;
 for my $result (@$reviews){
     my $biblionumber = $result->{biblionumber};
-       my $bib = &GetBiblioData($biblionumber);
-    my $record = GetMarcBiblio($biblionumber);
-    my $frameworkcode = GetFrameworkCode($biblionumber);
-       my ( $borr ) = GetMemberDetails( $result->{borrowernumber} );
+    my $biblio = Koha::Biblios->find( $biblionumber );
+    my $biblioitem = $biblio->biblioitem;
+    my $record = $biblio->metadata->record;
        $result->{normalized_upc} = GetNormalizedUPC($record,$marcflavour);
        $result->{normalized_ean} = GetNormalizedEAN($record,$marcflavour);
        $result->{normalized_oclc} = GetNormalizedOCLCNumber($record,$marcflavour);
        $result->{normalized_isbn} = GetNormalizedISBN(undef,$record,$marcflavour);
-       $result->{title} = $bib->{'title'};
-       $result->{subtitle} = GetRecordValue('subtitle', $record, $frameworkcode);
-       $result->{author} = $bib->{'author'};
-       $result->{place} = $bib->{'place'};
-       $result->{publishercode} = $bib->{'publishercode'};
-       $result->{copyrightdate} = $bib->{'copyrightdate'};
-       $result->{pages} = $bib->{'pages'};
-       $result->{size} = $bib->{'size'};
-       $result->{notes} = $bib->{'notes'};
-       $result->{timestamp} = $bib->{'timestamp'};
-    $result->{borrtitle} = $borr->{'title'};
-       $result->{firstname} = $borr->{'firstname'};
-       $result->{surname} = $borr->{'surname'};
-    $result->{userid} = $borr->{'userid'};
-        if ($libravatar_enabled and $borr->{'email'}) {
-            $result->{avatarurl} = libravatar_url(email => $borr->{'email'}, size => 40, https => $ENV{HTTPS});
-        }
+    $result->{title} = $biblio->title;
+    $result->{subtitle} = $biblio->subtitle;
+    $result->{medium} = $biblio->medium;
+    $result->{part_number} = $biblio->part_number;
+    $result->{part_name} = $biblio->part_name;
+    $result->{author} = $biblio->author;
+    $result->{place} = $biblioitem->place;
+    $result->{publishercode} = $biblioitem->publishercode;
+    $result->{copyrightdate} = $biblio->copyrightdate;
+    $result->{pages} = $biblioitem->pages;
+    $result->{size} = $biblioitem->size;
+    $result->{notes} = $biblioitem->notes;
+    $result->{timestamp} = $biblioitem->timestamp;
 
-    if ($result->{borrowernumber} eq $borrowernumber) {
-               $result->{your_comment} = 1;
-       }
+    $result->{biblio_object} = $biblio; # TODO Use this variable directly in the template
+
+    my $patron = Koha::Patrons->find( $result->{borrowernumber} );
+    if ( $patron ) {
+        $result->{borrtitle} = $patron->title;
+        $result->{firstname} = $patron->firstname;
+        $result->{surname} = $patron->surname;
+        $result->{userid} = $patron->userid;
+            if ($libravatar_enabled and $patron->email) {
+                $result->{avatarurl} = libravatar_url(email => $patron->email, size => 40, https => $ENV{HTTPS});
+            }
+
+        if ($result->{borrowernumber} eq $borrowernumber) {
+            $result->{your_comment} = 1;
+        }
+    }
 
     if($format eq "rss"){
         my $rsstimestamp = eval { dt_from_string( $result->{datereviewed} ); };
@@ -181,6 +190,7 @@ $template->param(next_page_offset => $next_page_offset) unless $pages eq $curren
 
 $template->param(
     reviews => $reviews,
+    results_per_page => $results_per_page,
 );
 
 output_html_with_http_headers $query, $cookie, $template->output;