8105f97568003ef4fa229c57f40011c5cd7c6c8a
[srvgit] / 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
63     $params->{lang} = 'default'
64       unless C4::Context->preference('TranslateNotices') && $params->{lang};
65
66     my $only_my_library = C4::Context->only_my_library;
67     if ( $only_my_library and $params->{branchcode} ) {
68         $params->{branchcode} = C4::Context::mybranch();
69     }
70     $params->{branchcode} //= '';
71     $params->{branchcode} = [$params->{branchcode}, ''];
72
73     my $template = $self->SUPER::search( $params, { order_by => { -desc => 'branchcode' } } );
74
75     if (   !$template->count
76         && C4::Context->preference('TranslateNotices')
77         && $params->{lang} ne 'default' )
78     {
79         $params->{lang} = 'default';
80         $template = $self->SUPER::search( $params, { order_by => { -desc => 'branchcode' } } );
81     }
82
83     return $template->next if $template->count;
84 }
85
86 =head3 type
87
88 =cut
89
90 sub _type {
91     return 'Letter';
92 }
93
94 sub object_class {
95     return 'Koha::Notice::Template';
96 }
97
98 1;