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