3 # Copyright 2015 Koha Development team
5 # This file is part of Koha.
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.
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.
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>.
28 use Koha::StockRotationStages;
29 use Koha::SMTP::Servers;
30 use Koha::Library::Hours;
32 use base qw(Koha::Object);
34 my $cache = Koha::Caches->get_instance();
38 Koha::Library - Koha Library Object class
46 Library specific store to ensure relevant caches are flushed on change
55 if ( !$self->in_storage ) {
59 my $self_from_storage = $self->get_from_storage;
60 $flush = 1 if ( $self_from_storage->branchname ne $self->branchname );
63 $self = $self->SUPER::store;
66 $cache->clear_from_cache('libraries:name');
74 Library specific C<delete> to clear relevant caches on delete.
80 $cache->clear_from_cache('libraries:name');
81 $self->SUPER::delete(@_);
84 =head3 stockrotationstages
86 my $stages = Koha::Library->stockrotationstages;
88 Returns the stockrotation stages associated with this Library.
92 sub stockrotationstages {
94 my $rs = $self->_result->stockrotationstages;
95 return Koha::StockRotationStages->_new_from_dbic( $rs );
98 =head3 outgoing_transfers
100 my $outgoing_transfers = Koha::Library->outgoing_transfers;
102 Returns the outgoing item transfers associated with this Library.
106 sub outgoing_transfers {
108 my $rs = $self->_result->branchtransfers_frombranches;
109 return Koha::Item::Transfers->_new_from_dbic( $rs );
112 =head3 inbound_transfers
114 my $inbound_transfers = Koha::Library->inbound_transfers;
116 Returns the inbound item transfers associated with this Library.
120 sub inbound_transfers {
122 my $rs = $self->_result->branchtransfers_tobranches;
123 return Koha::Item::Transfers->_new_from_dbic( $rs );
126 =head3 get_effective_marcorgcode
128 my $marcorgcode = Koha::Libraries->find( $library_id )->get_effective_marcorgcode();
130 Returns the effective MARC organization code of the library. It falls back to the value
131 from the I<MARCOrgCode> syspref if undefined for the library.
135 sub get_effective_marcorgcode {
138 return $self->marcorgcode || C4::Context->preference("MARCOrgCode");
143 my $smtp_server = $library->smtp_server;
144 $library->smtp_server({ smtp_server => $smtp_server });
145 $library->smtp_server({ smtp_server => undef });
147 Accessor for getting and setting the library's SMTP server.
149 Returns the effective SMTP server configuration to be used on the library. The returned
150 value is always a I<Koha::SMTP::Server> object.
152 Setting it to undef will remove the link to a specific SMTP server and effectively
153 make the library use the default setting
158 my ( $self, $params ) = @_;
160 my $library_smtp_server_rs = $self->_result->library_smtp_server;
162 if ( exists $params->{smtp_server} ) {
164 $self->_result->result_source->schema->txn_do( sub {
165 $library_smtp_server_rs->delete
166 if $library_smtp_server_rs;
168 if ( defined $params->{smtp_server} ) {
170 # Remove any already set SMTP server
172 my $smtp_server = $params->{smtp_server};
173 $smtp_server->_result->add_to_library_smtp_servers({ library_id => $self->id });
176 } # else => reset to default
179 if ( $library_smtp_server_rs ) {
180 return Koha::SMTP::Servers->find(
181 $library_smtp_server_rs->smtp_server_id );
184 return Koha::SMTP::Servers->get_default;
190 =head3 from_email_address
192 my $from_email = Koha::Library->from_email_address;
194 Returns the official 'from' email address for the branch.
196 It may well be a 'noreply' or other inaccessible local domain
197 address that is being used to satisfy spam protection filters.
201 sub from_email_address {
206 || C4::Context->preference('KohaAdminEmailAddress')
210 =head3 inbound_email_address
212 my $to_email = Koha::Library->inbound_email_address;
214 Returns an effective email address which should be accessible to librarians at the branch.
216 NOTE: This is the address to use for 'reply_to' or 'to' fields; It should not usually be
217 used as the 'from' address for emails as it may lead to mail being caught by spam filters.
221 sub inbound_email_address {
226 || $self->branchemail
227 || C4::Context->preference('ReplytoDefault')
228 || C4::Context->preference('KohaAdminEmailAddress')
232 =head3 inbound_ill_address
234 my $to_email = Koha::Library->inbound_ill_address;
236 Returns an effective email address which should be accessible to librarians at the branch
237 for inter library loans communication.
241 sub inbound_ill_address {
245 $self->branchillemail
246 || C4::Context->preference('ILLDefaultStaffEmail')
247 || $self->inbound_email_address;
250 =head3 library_groups
252 Return the Library groups of this library
258 my $rs = $self->_result->library_groups;
259 return Koha::Library::Groups->_new_from_dbic( $rs );
262 =head3 cash_registers
264 Return Cash::Registers associated with this Library
270 my $rs = $self->_result->cash_registers;
271 return Koha::Cash::Registers->_new_from_dbic( $rs );
276 my $desks = $library->desks;
278 Returns Koha::Desks associated with this library.
284 return Koha::Desks->_new_from_dbic( scalar $self->_result->desks );
287 =head3 get_hold_libraries
289 Return all libraries (including self) that belong to the same hold groups
293 sub get_hold_libraries {
295 my $library_groups = $self->library_groups;
297 while ( my $library_group = $library_groups->next ) {
298 my $root = Koha::Library::Groups->get_root_ancestor({id => $library_group->id});
299 if($root->ft_local_hold_group) {
300 push @hold_libraries, $root->all_libraries;
306 grep { !$seen{ $_->id }++ } @hold_libraries;
308 return Koha::Libraries->search({ branchcode => { '-in' => [ keys %seen ] } });
311 =head3 validate_hold_sibling
313 Return if given library is a valid hold group member
317 sub validate_hold_sibling {
318 my ( $self, $params ) = @_;
320 return 1 if $params->{branchcode} eq $self->id;
322 my $branchcode = $params->{branchcode};
323 return $self->get_hold_libraries->search( { branchcode => $branchcode } )
327 =head3 public_read_list
329 This method returns the list of publicly readable database fields for both API and UI output purposes
333 sub public_read_list {
335 'branchcode', 'branchname', 'branchaddress1',
336 'branchaddress2', 'branchaddress3', 'branchzip',
337 'branchcity', 'branchstate', 'branchcountry',
338 'branchfax', 'branchemail', 'branchurl'
342 =head3 to_api_mapping
344 This method returns the mapping for representing a Koha::Library object
351 branchcode => 'library_id',
352 branchname => 'name',
353 branchaddress1 => 'address1',
354 branchaddress2 => 'address2',
355 branchaddress3 => 'address3',
356 branchzip => 'postal_code',
357 branchcity => 'city',
358 branchstate => 'state',
359 branchcountry => 'country',
360 branchphone => 'phone',
362 branchemail => 'email',
363 branchillemail => 'illemail',
364 branchreplyto => 'reply_to_email',
365 branchreturnpath => 'return_path_email',
369 branchnotes => 'notes',
370 marcorgcode => 'marc_org_code',
371 opacusercss => undef,
378 $library->opac_info({ lang => $lang });
380 Returns additional contents block OpacLibraryInfo for $lang or 'default'.
382 Note: This replaces the former branches.opac_info column.
387 my ( $self, $params ) = @_;
388 return Koha::AdditionalContents->find_best_match({
389 category => 'html_customizations',
390 location => 'OpacLibraryInfo',
391 lang => $params->{lang},
392 library_id => $self->branchcode,
397 =head3 get_float_libraries
399 Return all libraries belonging to the same float group
403 sub get_float_libraries {
406 my $library_groups = $self->library_groups;
409 while ( my $library_group = $library_groups->next ) {
410 my $root = Koha::Library::Groups->get_root_ancestor( { id => $library_group->id } );
411 if ( $root->ft_local_float_group ) {
412 push @float_libraries, $root->all_libraries;
418 grep { !$seen{ $_->id }++ } @float_libraries;
420 return Koha::Libraries->search( { branchcode => { '-in' => [ keys %seen ] } } );
423 =head3 validate_float_sibling
425 Return if given library is a valid float group member
429 sub validate_float_sibling {
430 my ( $self, $params ) = @_;
432 return 1 if $params->{branchcode} eq $self->id;
434 my $branchcode = $params->{branchcode};
435 return $self->get_float_libraries->search( { branchcode => $branchcode } )->count > 0;
440 Returns the open and close times for a library.
446 my $library_hours_rs = $self->_result->library_hours;
447 return Koha::Library::Hours->_new_from_dbic($library_hours_rs);
450 =head2 Internal methods