Bug 36815: (follow-up) Use correct language for default
[koha.git] / Koha / Notice / Template.pm
1 package Koha::Notice::Template;
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 YAML::XS qw(LoadFile);
21
22 use C4::Context;
23 use Koha::Database;
24
25 use base qw(Koha::Object);
26
27 =head1 NAME
28
29 Koha::Notice::Template - Koha notice template Object class, related to the letter table
30
31 =head1 API
32
33 =head2 Class Methods
34
35 =head3 get_default
36
37     my $default = $template->get_default;
38
39 Returns the default notice template content.
40
41 =cut
42
43 sub get_default {
44     my $self = shift;
45     my $lang = $self->lang;
46     if ( $lang eq 'default' ) {
47         my $translated_languages = C4::Languages::getTranslatedLanguages(
48             'opac',
49             C4::Context->preference('template')
50         );
51         $lang = @{ @{$translated_languages}[0]->{sublanguages_loop} }[0]->{rfc4646_subtag};
52     }
53
54     my $defaulted_to_en = 0;
55
56     my $file = C4::Context->config('intranetdir') . "/installer/data/mysql/$lang/mandatory/sample_notices.yml";
57     if ( !-e $file ) {
58         if ( $lang eq 'en' ) {
59             warn "cannot open sample data $file";
60         } else {
61
62             # if no localised sample data is available,
63             # default to English
64             $file = C4::Context->config('intranetdir') . "/installer/data/mysql/en/mandatory/sample_notices.yml";
65             die "cannot open English sample data directory $file" unless ( -e $file );
66             $defaulted_to_en = 1;
67         }
68     }
69
70     my $data = YAML::XS::LoadFile("$file");
71
72     my $module = $self->module;
73     my $code   = $self->code;
74     my $mtt    = $self->message_transport_type;
75
76     my $content;
77     for my $table ( @{ $data->{tables} } ) {
78         if ( $table->{letter}->{rows} ) {
79             for my $template ( @{ $table->{letter}->{rows} } ) {
80                 if (   $template->{module} eq $module
81                     && $template->{code} eq $code
82                     && $template->{message_transport_type} eq $mtt )
83                 {
84                     $content = join "\r\n", @{ $template->{content} };
85                     last;
86                 }
87             }
88         }
89     }
90
91     return $content;
92 }
93
94 =head3 type
95
96 =cut
97
98 sub _type {
99     return 'Letter';
100 }
101
102 1;