From da99c9f72b7db472f9ed1efe16af5394c0c882b8 Mon Sep 17 00:00:00 2001
From: Katrin Fischer <katrin.fischer.83@web.de>
Date: Thu, 15 Mar 2018 15:26:35 +0000
Subject: [PATCH] Bug 20400: Rewrite using Koha::Objects

Adds
- Koha::Subscription::Routinglist
- Koha::Subscription::Routinglists

Adds 2 methods
- Koha::Patron::get_routinglists
- Koha::Routinglist::subscription
---
 Koha/Patron.pm                                     | 17 +++++-
 Koha/Subscription/Routinglist.pm                   | 67 ++++++++++++++++++++++
 Koha/Subscription/Routinglists.pm                  | 62 ++++++++++++++++++++
 .../bootstrap/en/modules/opac-routing-lists.tt     | 10 ++--
 opac/opac-routing-lists.pl                         | 23 +-------
 5 files changed, 153 insertions(+), 26 deletions(-)
 create mode 100644 Koha/Subscription/Routinglist.pm
 create mode 100644 Koha/Subscription/Routinglists.pm

diff --git a/Koha/Patron.pm b/Koha/Patron.pm
index d0be998..af2f7ae 100644
--- a/Koha/Patron.pm
+++ b/Koha/Patron.pm
@@ -38,6 +38,7 @@ use Koha::Patrons;
 use Koha::Virtualshelves;
 use Koha::Club::Enrollments;
 use Koha::Account;
+use Koha::Subscription::Routinglists;
 
 use base qw(Koha::Object);
 
@@ -539,7 +540,7 @@ sub old_checkouts {
 
 my $overdue_items = $patron->get_overdues
 
-Return the overdued items
+Return the overdue items
 
 =cut
 
@@ -556,6 +557,20 @@ sub get_overdues {
     );
 }
 
+=head3 get_routinglists
+
+my @routinglists = $patron->get_routinglists
+
+Returns the routing lists a patron is subscribed to.
+
+=cut
+
+sub get_routinglists {
+    my ($self) = @_;
+    my @subscribed_routings = Koha::Subscription::Routinglists->search({ borrowernumber => $self->borrowernumber });
+    return @subscribed_routings;
+}
+
 =head3 get_age
 
 my $age = $patron->get_age
diff --git a/Koha/Subscription/Routinglist.pm b/Koha/Subscription/Routinglist.pm
new file mode 100644
index 0000000..4c081e5
--- /dev/null
+++ b/Koha/Subscription/Routinglist.pm
@@ -0,0 +1,67 @@
+package Koha::Subscription::Routinglist;
+
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Carp;
+
+use Koha::Database;
+use Koha::Subscriptions;
+
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::Subscription::Routinglist - Koha subscription routing list object class
+
+=head1 API
+
+=head2 Class methods
+
+=cut
+
+=head3 subscription
+
+my $subscription = $routinglist->subscription
+
+Returns the subscription for a routing list.
+
+=cut
+
+sub subscription {
+    my ( $self ) = @_;
+    return scalar Koha::Subscriptions->find( $self->subscriptionid );
+}
+
+=head2 Internal methods
+
+=head3 _type
+
+=cut
+
+sub _type {
+    return 'Subscriptionroutinglist';
+}
+
+=head1 AUTHOR
+
+Katrin Fischer <katrin.fischer@bsz-bw.de>
+
+=cut
+
+1;
diff --git a/Koha/Subscription/Routinglists.pm b/Koha/Subscription/Routinglists.pm
new file mode 100644
index 0000000..198118e
--- /dev/null
+++ b/Koha/Subscription/Routinglists.pm
@@ -0,0 +1,62 @@
+package Koha::Subscription::Routinglists;
+
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Carp;
+
+use Koha::Database;
+use Koha::Subscription::Routinglist;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::Subscription::Routinglists - Koha subscription routing lists object class
+
+=head1 API
+
+=head2 Class methods
+
+=cut
+
+=head2 Internal methods
+
+=head3 _type
+
+=cut
+
+sub _type {
+    return 'Subscriptionroutinglist';
+}
+
+=head3 object_class
+
+=cut
+
+sub object_class {
+    return 'Koha::Subscription::Routinglist';
+}
+
+=head1 AUTHOR
+
+Katrin Fischer <katrin.fischer@bsz-bw.de>
+
+=cut
+
+1;
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-routing-lists.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-routing-lists.tt
index 0384c93..b7358fa 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-routing-lists.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-routing-lists.tt
@@ -31,7 +31,7 @@
 
                     <h3>Routing lists</h3>
 
-                    [% IF ( subscriptionLoop ) %]
+                    [% IF ( routinglists ) %]
                         <p id="routing-list-intro">You are subscribed to the routing lists for folloging serial titles. If you wish to make changes, please contact the library.</p>
                         <table class="table table-bordered table-striped" id="routingtable">
                             <thead>
@@ -41,11 +41,11 @@
                             </thead>
 
                             <tbody>
-                            [% FOREACH subscription IN subscriptionLoop %]
-                                [% IF ( subscripLoop.odd ) %]<tr class="highlight">[% ELSE %]<tr>[% END %]
+                            [% FOREACH routinglist IN routinglists %]
+                                [% IF ( titles_loop.odd ) %]<tr class="highlight">[% ELSE %]<tr>[% END %]
                                     <td>
-                                        <a href="/cgi-bin/koha/opac-detail.pl?biblionumber=[% subscription.biblionumber %]">
-                                            [% subscription.title %]
+                                        <a href="/cgi-bin/koha/opac-detail.pl?biblionumber=[% routinglist.subscription.biblio.biblionumber %]">
+                                            [% routinglist.subscription.biblio.title %]
                                         </a>
                                     </td>
                                 </tr>
diff --git a/opac/opac-routing-lists.pl b/opac/opac-routing-lists.pl
index 55e3878..2dc09c6 100644
--- a/opac/opac-routing-lists.pl
+++ b/opac/opac-routing-lists.pl
@@ -21,9 +21,7 @@ use CGI qw ( -utf8 );
 use C4::Members;
 use C4::Auth;
 use C4::Output;
-use C4::Serials;
 use Koha::Patrons;
-use Koha::Subscriptions;
 
 my $query = new CGI;
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
@@ -43,26 +41,11 @@ $borrower->{description} = $category->description;
 $borrower->{category_type} = $category->category_type;
 $template->param( BORROWER_INFO => $borrower );
 
-
-my $count;
-my @borrowerSubscriptions;
-($count, @borrowerSubscriptions) = GetSubscriptionsFromBorrower($borrowernumber );
-my @subscripLoop;
-
-foreach my $num_res (@borrowerSubscriptions) {
-    my %getSubscrip;
-    $getSubscrip{subscriptionid} = $num_res->{'subscriptionid'};
-    $getSubscrip{title}          = $num_res->{'title'};
-    $getSubscrip{borrowernumber} = $num_res->{'borrowernumber'};
-    my $subscription = Koha::Subscriptions->find( $num_res->{'subscriptionid'} );
-    $getSubscrip{biblionumber}   = $subscription->biblionumber();
-    push( @subscripLoop, \%getSubscrip );
-}
+my @routinglists = $patron->get_routinglists();
 
 $template->param(
-    countSubscrip => scalar @subscripLoop,
-    subscriptionLoop  => \@subscripLoop,
-    routinglistview => 1
+    routinglists  => \@routinglists,
+    routinglistview => 1,
 );
 
 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
-- 
2.1.4