Bug 30920: (follow-up) Cache key fixes
[koha.git] / Koha / Library.pm
1 package Koha::Library;
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22
23 use C4::Context;
24
25 use Koha::Caches;
26 use Koha::Database;
27 use Koha::StockRotationStages;
28 use Koha::SMTP::Servers;
29
30 use base qw(Koha::Object);
31
32 =head1 NAME
33
34 Koha::Library - Koha Library Object class
35
36 =head1 API
37
38 =head2 Class methods
39
40 =head3 store
41
42 Library specific store to ensure relevant caches are flushed on change
43
44 =cut
45
46 sub store {
47     my ($self) = @_;
48
49     my $flush = 0;
50
51     if ( !$self->in_storage ) {
52         $flush = 1;
53     }
54     else {
55         my $self_from_storage = $self->get_from_storage;
56         $flush = 1 if ( $self_from_storage->branchname ne $self->branchname );
57     }
58
59     $self = $self->SUPER::store;
60
61     if ($flush) {
62         my $cache = Koha::Caches->get_instance();
63         $cache->clear_from_cache('libraries:name');
64     }
65
66     return $self;
67 }
68
69 =head3 stockrotationstages
70
71   my $stages = Koha::Library->stockrotationstages;
72
73 Returns the stockrotation stages associated with this Library.
74
75 =cut
76
77 sub stockrotationstages {
78     my ( $self ) = @_;
79     my $rs = $self->_result->stockrotationstages;
80     return Koha::StockRotationStages->_new_from_dbic( $rs );
81 }
82
83 =head3 outgoing_transfers
84
85   my $outgoing_transfers = Koha::Library->outgoing_transfers;
86
87 Returns the outgoing item transfers associated with this Library.
88
89 =cut
90
91 sub outgoing_transfers {
92     my ( $self ) = @_;
93     my $rs = $self->_result->branchtransfers_frombranches;
94     return Koha::Item::Transfers->_new_from_dbic( $rs );
95 }
96
97 =head3 inbound_transfers
98
99   my $inbound_transfers = Koha::Library->inbound_transfers;
100
101 Returns the inbound item transfers associated with this Library.
102
103 =cut
104
105 sub inbound_transfers {
106     my ( $self ) = @_;
107     my $rs = $self->_result->branchtransfers_tobranches;
108     return Koha::Item::Transfers->_new_from_dbic( $rs );
109 }
110
111 =head3 get_effective_marcorgcode
112
113     my $marcorgcode = Koha::Libraries->find( $library_id )->get_effective_marcorgcode();
114
115 Returns the effective MARC organization code of the library. It falls back to the value
116 from the I<MARCOrgCode> syspref if undefined for the library.
117
118 =cut
119
120 sub get_effective_marcorgcode {
121     my ( $self )  = @_;
122
123     return $self->marcorgcode || C4::Context->preference("MARCOrgCode");
124 }
125
126 =head3 smtp_server
127
128     my $smtp_server = $library->smtp_server;
129     $library->smtp_server({ smtp_server => $smtp_server });
130     $library->smtp_server({ smtp_server => undef });
131
132 Accessor for getting and setting the library's SMTP server.
133
134 Returns the effective SMTP server configuration to be used on the library. The returned
135 value is always a I<Koha::SMTP::Server> object.
136
137 Setting it to undef will remove the link to a specific SMTP server and effectively
138 make the library use the default setting
139
140 =cut
141
142 sub smtp_server {
143     my ( $self, $params ) = @_;
144
145     my $library_smtp_server_rs = $self->_result->library_smtp_server;
146
147     if ( exists $params->{smtp_server} ) {
148
149         $self->_result->result_source->schema->txn_do( sub {
150             $library_smtp_server_rs->delete
151                 if $library_smtp_server_rs;
152
153             if ( defined $params->{smtp_server} ) {
154                 # Set the new server
155                 # Remove any already set SMTP server
156
157                 my $smtp_server = $params->{smtp_server};
158                 $smtp_server->_result->add_to_library_smtp_servers({ library_id => $self->id });
159             }
160         });
161     } # else => reset to default
162     else {
163         # Getter
164         if ( $library_smtp_server_rs ) {
165             return Koha::SMTP::Servers->find(
166                 $library_smtp_server_rs->smtp_server_id );
167         }
168
169         return Koha::SMTP::Servers->get_default;
170     }
171
172     return $self;
173 }
174
175 =head3 from_email_address
176
177   my $from_email = Koha::Library->from_email_address;
178
179 Returns the official 'from' email address for the branch.
180
181 It may well be a 'noreply' or other inaccessible local domain
182 address that is being used to satisfy spam protection filters.
183
184 =cut
185
186 sub from_email_address {
187     my ($self) = @_;
188
189     return
190          $self->branchemail
191       || C4::Context->preference('KohaAdminEmailAddress')
192       || undef;
193 }
194
195 =head3 inbound_email_address
196
197   my $to_email = Koha::Library->inbound_email_address;
198
199 Returns an effective email address which should be accessible to librarians at the branch.
200
201 NOTE: This is the address to use for 'reply_to' or 'to' fields; It should not usually be
202 used as the 'from' address for emails as it may lead to mail being caught by spam filters.
203
204 =cut
205
206 sub inbound_email_address {
207     my ($self) = @_;
208
209     return
210          $self->branchreplyto
211       || $self->branchemail
212       || C4::Context->preference('ReplytoDefault')
213       || C4::Context->preference('KohaAdminEmailAddress')
214       || undef;
215 }
216
217 =head3 inbound_ill_address
218
219   my $to_email = Koha::Library->inbound_ill_address;
220
221 Returns an effective email address which should be accessible to librarians at the branch
222 for inter library loans communication.
223
224 =cut
225
226 sub inbound_ill_address {
227     my ($self) = @_;
228
229     return
230          $self->branchillemail
231       || C4::Context->preference('ILLDefaultStaffEmail')
232       || $self->inbound_email_address;
233 }
234
235 =head3 library_groups
236
237 Return the Library groups of this library
238
239 =cut
240
241 sub library_groups {
242     my ( $self ) = @_;
243     my $rs = $self->_result->library_groups;
244     return Koha::Library::Groups->_new_from_dbic( $rs );
245 }
246
247 =head3 cash_registers
248
249 Return Cash::Registers associated with this Library
250
251 =cut
252
253 sub cash_registers {
254     my ( $self ) = @_;
255     my $rs = $self->_result->cash_registers;
256     return Koha::Cash::Registers->_new_from_dbic( $rs );
257 }
258
259 =head3 get_hold_libraries
260
261 Return all libraries (including self) that belong to the same hold groups
262
263 =cut
264
265 sub get_hold_libraries {
266     my ( $self ) = @_;
267     my $library_groups = $self->library_groups;
268     my @hold_libraries;
269     while ( my $library_group = $library_groups->next ) {
270         my $root = Koha::Library::Groups->get_root_ancestor({id => $library_group->id});
271         if($root->ft_local_hold_group) {
272             push @hold_libraries, $root->all_libraries;
273         }
274     }
275
276     my %seen;
277     @hold_libraries =
278       grep { !$seen{ $_->id }++ } @hold_libraries;
279
280     return Koha::Libraries->search({ branchcode => { '-in' => [ keys %seen ] } });
281 }
282
283 =head3 validate_hold_sibling
284
285 Return if given library is a valid hold group member
286
287 =cut
288
289 sub validate_hold_sibling {
290     my ( $self, $params ) = @_;
291
292     return 1 if $params->{branchcode} eq $self->id;
293
294     my $branchcode = $params->{branchcode};
295     return $self->get_hold_libraries->search( { branchcode => $branchcode } )
296       ->count > 0;
297 }
298
299 =head3 public_read_list
300
301 This method returns the list of publicly readable database fields for both API and UI output purposes
302
303 =cut
304
305 sub public_read_list {
306     return [
307         'branchcode',     'branchname',     'branchaddress1',
308         'branchaddress2', 'branchaddress3', 'branchzip',
309         'branchcity',     'branchstate',    'branchcountry',
310         'branchfax',      'branchemail',    'branchurl'
311     ];
312 }
313
314 =head3 to_api_mapping
315
316 This method returns the mapping for representing a Koha::Library object
317 on the API.
318
319 =cut
320
321 sub to_api_mapping {
322     return {
323         branchcode       => 'library_id',
324         branchname       => 'name',
325         branchaddress1   => 'address1',
326         branchaddress2   => 'address2',
327         branchaddress3   => 'address3',
328         branchzip        => 'postal_code',
329         branchcity       => 'city',
330         branchstate      => 'state',
331         branchcountry    => 'country',
332         branchphone      => 'phone',
333         branchfax        => 'fax',
334         branchemail      => 'email',
335         branchillemail   => 'illemail',
336         branchreplyto    => 'reply_to_email',
337         branchreturnpath => 'return_path_email',
338         branchurl        => 'url',
339         issuing          => undef,
340         branchip         => 'ip',
341         branchnotes      => 'notes',
342         marcorgcode      => 'marc_org_code',
343     };
344 }
345
346 =head3 opac_info
347
348     $library->opac_info({ lang => $lang });
349
350 Returns additional contents block OpacLibraryInfo for $lang or 'default'.
351
352 Note: This replaces the former branches.opac_info column.
353
354 =cut
355
356 sub opac_info {
357     my ( $self, $params ) = @_;
358     return Koha::AdditionalContents->find_best_match({
359         category => 'html_customizations',
360         location => 'OpacLibraryInfo',
361         lang => $params->{lang},
362         library_id => $self->branchcode,
363     });
364 }
365
366 =head2 Internal methods
367
368 =head3 _type
369
370 =cut
371
372 sub _type {
373     return 'Branch';
374 }
375
376 1;