Bug 28514: Remove getletter
[koha-ffzg.git] / Koha / Notice / Templates.pm
1 package Koha::Notice::Templates;
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 use Modern::Perl;
19
20 use Carp;
21
22 use Koha::Database;
23
24 use Koha::Notice::Template;
25
26 use base qw(Koha::Objects);
27
28 =head1 NAME
29
30 Koha::Notice::Templates - Koha notice template Object set class, related to the letter table
31
32 =head1 API
33
34 =head2 Class Methods
35
36 =cut
37
38 =head3
39
40 my $template = Koha::Notice::Templates->find_effective_template(
41     {
42         module     => $module,
43         code       => $code,
44         branchcode => $branchcode,
45         lang       => $lang,
46     }
47 );
48
49 Return the notice template that must be used for a given primary key (module, code, branchcode, lang).
50
51 For instance if lang="es-ES" but there is no "es-ES" template defined for this language,
52 the default template will be returned.
53
54 lang will default to "default" if not passed.
55
56 =cut
57
58 sub find_effective_template {
59     my ( $self, $params ) = @_;
60
61     $params = { %$params }; # don't modify original
62     $params->{lang} = 'default' unless C4::Context->preference('TranslateNotices');
63
64     my $only_my_library = C4::Context->only_my_library;
65     if ( $only_my_library and $params->{branchcode} ) {
66         $params->{branchcode} = C4::Context::mybranch();
67     }
68     $params->{branchcode} //= '';
69     $params->{branchcode} = [$params->{branchcode}, ''];
70
71     my $template = $self->SUPER::search( $params, { order_by => { -desc => 'branchcode' } } );
72
73     if (   !$template->count
74         && C4::Context->preference('TranslateNotices')
75         && $params->{lang} ne 'default' )
76     {
77         $params->{lang} = 'default';
78         $template = $self->SUPER::search( $params, { order_by => { -desc => 'branchcode' } } );
79     }
80
81     return $template->next if $template->count;
82 }
83
84 =head3 type
85
86 =cut
87
88 sub _type {
89     return 'Letter';
90 }
91
92 sub object_class {
93     return 'Koha::Notice::Template';
94 }
95
96 1;