Bug 28959: Add virtualshelves.public as a boolean
[koha.git] / Koha / ExternalContent.pm
1 # Copyright 2014 Catalyst
2 #
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 package Koha::ExternalContent;
19
20 use Modern::Perl;
21 use Carp qw( croak );
22 use base qw(Class::Accessor);
23
24 use Koha;
25 use Koha::Logger;
26 use Koha::Patrons;
27 use C4::Auth;
28
29 __PACKAGE__->mk_accessors(qw(client koha_session_id koha_patron logger));
30
31 =head1 NAME
32
33 Koha::ExternalContent
34
35 =head1 SYNOPSIS
36
37  use Koha::ExternalContent;
38  my $externalcontent = Koha::ExternalContent->new();
39
40 =head1 DESCRIPTION
41
42 Base class for interfacing with external content providers.
43
44 Subclasses provide clients for particular systems. This class provides
45 common methods for getting Koha patron.
46
47 =head1 METHODS
48
49 =cut
50
51 sub agent_string {
52     return 'Koha/'.Koha::version();
53 }
54
55 sub new {
56     my $class     = shift;
57     my $params    = shift || {};
58
59     $params->{logger} = Koha::Logger->get();
60
61     return bless $params, $class;
62 }
63
64 sub _koha_session {
65     my $self = shift;
66     my $session_id = $self->koha_session_id or return;
67     return C4::Auth::get_session($session_id);
68 }
69
70 sub get_from_koha_session {
71     my $self = shift;
72     my $key = shift or croak "No key";
73     my $session = $self->_koha_session or return;
74     return $session->param($key);
75 }
76
77 sub set_in_koha_session {
78     my $self = shift;
79     my $key = shift or croak "No key";
80     my $value = shift;
81     my $session = $self->_koha_session or croak "No Koha session";
82     return $session->param($key, $value);
83 }
84
85 sub koha_patron {
86     my $self = shift;
87
88     if (my $patron = $self->_koha_patron_accessor) {
89         return $patron;
90     }
91
92     my $id = $self->get_from_koha_session('number')
93       or return;
94     my $patron = Koha::Patrons->find($id)
95       or die "Invalid patron number in session";
96     return $self->_koha_patron_accessor($patron);
97 }
98
99 =head1 AUTHOR
100
101 CatalystIT
102
103 =cut
104
105 1;