View | Details | Raw Unified | Return to bug 28514
Collapse All | Expand All

(-)a/C4/Letters.pm (-34 / +15 lines)
Lines 205-239 sub GetLettersAvailableForALibrary { Link Here
205
205
206
}
206
}
207
207
208
sub getletter {
209
    my ( $module, $code, $branchcode, $message_transport_type, $lang) = @_;
210
    $message_transport_type //= '%';
211
    $lang = 'default' unless( $lang && C4::Context->preference('TranslateNotices') );
212
213
214
    my $only_my_library = C4::Context->only_my_library;
215
    if ( $only_my_library and $branchcode ) {
216
        $branchcode = C4::Context::mybranch();
217
    }
218
    $branchcode //= '';
219
220
    my $dbh = C4::Context->dbh;
221
    my $sth = $dbh->prepare(q{
222
        SELECT *
223
        FROM letter
224
        WHERE module=? AND code=? AND (branchcode = ? OR branchcode = '')
225
        AND message_transport_type LIKE ?
226
        AND lang =?
227
        ORDER BY branchcode DESC LIMIT 1
228
    });
229
    $sth->execute( $module, $code, $branchcode, $message_transport_type, $lang );
230
    my $line = $sth->fetchrow_hashref
231
      or return;
232
    $line->{'content-type'} = 'text/html; charset="UTF-8"' if $line->{is_html};
233
    return { %$line };
234
}
235
236
237
=head2 DelLetter
208
=head2 DelLetter
238
209
239
    DelLetter(
210
    DelLetter(
Lines 637-649 sub GetPreparedLetter { Link Here
637
        my $branchcode  = $params{branchcode} || '';
608
        my $branchcode  = $params{branchcode} || '';
638
        my $mtt         = $params{message_transport_type} || 'email';
609
        my $mtt         = $params{message_transport_type} || 'email';
639
610
640
        $letter = getletter( $module, $letter_code, $branchcode, $mtt, $lang );
611
        my $template = Koha::Notice::Templates->find_effective_template(
612
            {
613
                module                 => $module,
614
                code                   => $letter_code,
615
                branchcode             => $branchcode,
616
                message_transport_type => $mtt,
617
                lang                   => $lang
618
            }
619
        );
641
620
642
        unless ( $letter ) {
621
        unless ( $template ) {
643
            $letter = getletter( $module, $letter_code, $branchcode, $mtt, 'default' )
622
            warn( "No $module $letter_code letter transported by " . $mtt );
644
                or warn( "No $module $letter_code letter transported by " . $mtt ),
623
            return;
645
                    return;
646
        }
624
        }
625
626
        $letter = $template->unblessed;
627
        $letter->{'content-type'} = 'text/html; charset="UTF-8"' if $letter->{is_html};
647
    }
628
    }
648
629
649
    my $tables = $params{tables} || {};
630
    my $tables = $params{tables} || {};
(-)a/Koha/Notice/Templates.pm (+46 lines)
Lines 35-40 Koha::Notice::Templates - Koha notice template Object set class, related to the Link Here
35
35
36
=cut
36
=cut
37
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
38
=head3 type
84
=head3 type
39
85
40
=cut
86
=cut
(-)a/misc/cronjobs/holds/holds_reminder.pl (-2 / +13 lines)
Lines 37-42 use C4::Log; Link Here
37
use Koha::DateUtils;
37
use Koha::DateUtils;
38
use Koha::Calendar;
38
use Koha::Calendar;
39
use Koha::Libraries;
39
use Koha::Libraries;
40
use Koha::Notice::Templates;
41
use Koha::Patrons;
40
use Koha::Script -cron;
42
use Koha::Script -cron;
41
43
42
=head1 NAME
44
=head1 NAME
Lines 223-230 else { Link Here
223
# Loop through each branch
225
# Loop through each branch
224
foreach my $branchcode (@branchcodes) { #BEGIN BRANCH LOOP
226
foreach my $branchcode (@branchcodes) { #BEGIN BRANCH LOOP
225
    # Check that this branch has the letter code specified or skip this branch
227
    # Check that this branch has the letter code specified or skip this branch
226
    my $letter = C4::Letters::getletter( 'reserves', $lettercode , $branchcode );
228
227
    unless ($letter) {
229
    # FIXME What if we don't want to default if the translated template does not exist?
230
    my $template_exists = Koha::Notice::Templates->search(
231
        {
232
            module     => 'reserves',
233
            code       => $lettercode,
234
            branchcode => $branchcode,
235
            lang       => 'default',
236
        }
237
    )->count;
238
    unless ($template_exists) {
228
        $verbose and print qq|Message '$lettercode' content not found for $branchcode\n|;
239
        $verbose and print qq|Message '$lettercode' content not found for $branchcode\n|;
229
        next;
240
        next;
230
    }
241
    }
(-)a/misc/cronjobs/overdue_notices.pl (-4 / +18 lines)
Lines 624-631 END_SQL Link Here
624
                    }
624
                    }
625
                }
625
                }
626
626
627
                my $letter = C4::Letters::getletter( 'circulation', $overdue_rules->{"letter$i"}, $branchcode, undef, $patron->lang )
627
                my $letter = Koha::Notice::Templates->find_effective_template(
628
                          || C4::Letters::getletter( 'circulation', $overdue_rules->{"letter$i"}, $branchcode, undef, "default");
628
                    {
629
                        module     => 'circulation',
630
                        code       => $overdue_rules->{"letter$i"},
631
                        branchcode => $branchcode,
632
                        lang       => $patron->lang
633
                    }
634
                );
629
635
630
                unless ($letter) {
636
                unless ($letter) {
631
                    $verbose and warn qq|Message '$overdue_rules->{"letter$i"}' content not found|;
637
                    $verbose and warn qq|Message '$overdue_rules->{"letter$i"}' content not found|;
Lines 716-723 END_SQL Link Here
716
                    splice @items, $PrintNoticesMaxLines if $effective_mtt eq 'print' && $PrintNoticesMaxLines && scalar @items > $PrintNoticesMaxLines;
722
                    splice @items, $PrintNoticesMaxLines if $effective_mtt eq 'print' && $PrintNoticesMaxLines && scalar @items > $PrintNoticesMaxLines;
717
                    #catch the case where we are sending a print to someone with an email
723
                    #catch the case where we are sending a print to someone with an email
718
724
719
                    my $letter_exists = ( C4::Letters::getletter( 'circulation', $overdue_rules->{"letter$i"}, $branchcode, $effective_mtt, $patron->lang )
725
                    my $letter_exists = Koha::Notice::Templates->find_effective_template(
720
                                       || C4::Letters::getletter( 'circulation', $overdue_rules->{"letter$i"}, $branchcode, $effective_mtt, "default") ) ? 1 : 0;
726
                        {
727
                            module     => 'circulation',
728
                            code       => $overdue_rules->{"letter$i"},
729
                            message_transport_type => $effective_mtt,
730
                            branchcode => $branchcode,
731
                            lang       => $patron->lang
732
                        }
733
                    );
734
721
                    my $letter = parse_overdues_letter(
735
                    my $letter = parse_overdues_letter(
722
                        {   letter_code     => $overdue_rules->{"letter$i"},
736
                        {   letter_code     => $overdue_rules->{"letter$i"},
723
                            borrowernumber  => $borrowernumber,
737
                            borrowernumber  => $borrowernumber,
(-)a/pos/printreceipt.pl (-2 / +10 lines)
Lines 25-30 use CGI qw ( -utf8 ); Link Here
25
use C4::Letters;
25
use C4::Letters;
26
use Koha::Account::Lines;
26
use Koha::Account::Lines;
27
use Koha::DateUtils;
27
use Koha::DateUtils;
28
use Koha::Notice::Templates;
28
29
29
my $input = CGI->new;
30
my $input = CGI->new;
30
31
Lines 52-59 output_and_exit_if_error( Link Here
52
) if $patron;    # Payment could have been anonymous
53
) if $patron;    # Payment could have been anonymous
53
54
54
my $lang = $patron ? $patron->lang : $template->lang;
55
my $lang = $patron ? $patron->lang : $template->lang;
55
my $letter = C4::Letters::getletter( 'pos', 'RECEIPT',
56
my $letter = Koha::Notice::Templates->find_effective_template(
56
    C4::Context::mybranch, 'print', $lang );
57
    {
58
        module                 => 'pos',
59
        code                   => 'RECEIPT',
60
        branchcode             => C4::Context::mybranch,
61
        message_transport_type => 'print',
62
        lang                   => $lang
63
    }
64
);
57
65
58
$template->param(
66
$template->param(
59
    letter  => $letter,
67
    letter  => $letter,
(-)a/t/db_dependent/Koha/Notices.t (-1 / +69 lines)
Lines 19-30 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 3;
22
use Test::More tests => 4;
23
23
24
use Koha::Notice::Templates;
24
use Koha::Notice::Templates;
25
use Koha::Database;
25
use Koha::Database;
26
26
27
use t::lib::TestBuilder;
27
use t::lib::TestBuilder;
28
use t::lib::Mocks;
28
29
29
my $schema = Koha::Database->new->schema;
30
my $schema = Koha::Database->new->schema;
30
$schema->storage->txn_begin;
31
$schema->storage->txn_begin;
Lines 66-70 $retrieved_template->delete; Link Here
66
is( Koha::Notice::Templates->search->count,
67
is( Koha::Notice::Templates->search->count,
67
    $nb_of_templates, 'Delete should have deleted the template' );
68
    $nb_of_templates, 'Delete should have deleted the template' );
68
69
70
subtest 'find_effective_template' => sub {
71
    plan tests => 7;
72
73
    my $default_template = $builder->build_object(
74
        { class => 'Koha::Notice::Templates', value => { branchcode => '', lang => 'default' } }
75
    );
76
    my $key = {
77
        module                 => $default_template->module,
78
        code                   => $default_template->code,
79
        message_transport_type => $default_template->message_transport_type,
80
    };
81
82
    my $library_specific_template = $builder->build_object(
83
        { class => 'Koha::Notice::Templates', value => { %$key, lang => 'default' } }
84
    );
85
86
    my $es_template = $builder->build_object(
87
        {
88
            class => 'Koha::Notice::Templates',
89
            value => { %$key, lang => 'es-ES' },
90
        }
91
    );
92
93
    $key->{branchcode} = $es_template->branchcode;
94
95
    t::lib::Mocks::mock_preference( 'TranslateNotices', 0 );
96
97
    my $template = Koha::Notice::Templates->find_effective_template($key);
98
    is( $template->lang, 'default', 'no lang passed, default is returned' );
99
    $template = Koha::Notice::Templates->find_effective_template( { %$key, lang => 'es-ES' } );
100
    is( $template->lang, 'default',
101
        'TranslateNotices is off, default is returned' );
102
103
    t::lib::Mocks::mock_preference( 'TranslateNotices', 1 );
104
    $template = Koha::Notice::Templates->find_effective_template($key);
105
    is( $template->lang, 'default', 'no lang passed, default is returned' );
106
    $template = Koha::Notice::Templates->find_effective_template( { %$key, lang => 'es-ES' } );
107
    is( $template->lang, 'es-ES',
108
        'TranslateNotices is on and es-ES is requested, es-ES is returned' );
109
110
111
    {    # IndependentBranches => 1
112
        t::lib::Mocks::mock_userenv( { branchcode => $library_specific_template->branchcode, flag => 0 } );
113
        t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
114
        $template = Koha::Notice::Templates->find_effective_template( { %$key, branchcode => $library_specific_template->branchcode } );
115
        is( $template->content, $library_specific_template->content,
116
            'IndependentBranches is on, logged in patron is not superlibrarian but asks for their specific template, it is returned'
117
        );
118
119
        my $another_library = $builder->build_object( { class => 'Koha::Libraries' } );
120
        t::lib::Mocks::mock_userenv( { branchcode => $another_library->branchcode, flag => 0 } );
121
        $template = Koha::Notice::Templates->find_effective_template($key);
122
        is( $template->content, $default_template->content,
123
'IndependentBranches is on, logged in patron is not superlibrarian, default is returned'
124
        );
125
    }
126
127
    t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
128
    $es_template->delete;
129
130
    $template = Koha::Notice::Templates->find_effective_template( { %$key, lang => 'es-ES' } );
131
    is( $template->lang, 'default',
132
        'TranslateNotices is on and es-ES is requested but does not exist, default is returned'
133
    );
134
135
};
136
69
$schema->storage->txn_rollback;
137
$schema->storage->txn_rollback;
70
138
(-)a/t/db_dependent/Letters.t (-48 / +1 lines)
Lines 18-24 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Test::More tests => 86;
21
use Test::More tests => 82;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
Lines 209-261 is( $letters->[0]->{module}, 'my module', 'GetLetters gets the module correctly' Link Here
209
is( $letters->[0]->{code}, 'my code', 'GetLetters gets the code correctly' );
209
is( $letters->[0]->{code}, 'my code', 'GetLetters gets the code correctly' );
210
is( $letters->[0]->{name}, 'my name', 'GetLetters gets the name correctly' );
210
is( $letters->[0]->{name}, 'my name', 'GetLetters gets the name correctly' );
211
211
212
213
# getletter
214
subtest 'getletter' => sub {
215
    plan tests => 16;
216
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
217
    my $letter = C4::Letters::getletter('my module', 'my code', $library->{branchcode}, 'email');
218
    is( $letter->{branchcode}, $library->{branchcode}, 'GetLetters gets the branch code correctly' );
219
    is( $letter->{module}, 'my module', 'GetLetters gets the module correctly' );
220
    is( $letter->{code}, 'my code', 'GetLetters gets the code correctly' );
221
    is( $letter->{name}, 'my name', 'GetLetters gets the name correctly' );
222
    is( $letter->{is_html}, 1, 'GetLetters gets the boolean is_html correctly' );
223
    is( $letter->{title}, $title, 'GetLetters gets the title correctly' );
224
    is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
225
    is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
226
227
    t::lib::Mocks::mock_userenv({ branchcode => "anotherlib", flags => 1 });
228
229
    t::lib::Mocks::mock_preference('IndependentBranches', 1);
230
    $letter = C4::Letters::getletter('my module', 'my code', $library->{branchcode}, 'email');
231
    is( $letter->{branchcode}, $library->{branchcode}, 'GetLetters gets the branch code correctly' );
232
    is( $letter->{module}, 'my module', 'GetLetters gets the module correctly' );
233
    is( $letter->{code}, 'my code', 'GetLetters gets the code correctly' );
234
    is( $letter->{name}, 'my name', 'GetLetters gets the name correctly' );
235
    is( $letter->{is_html}, 1, 'GetLetters gets the boolean is_html correctly' );
236
    is( $letter->{title}, $title, 'GetLetters gets the title correctly' );
237
    is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
238
    is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
239
};
240
241
242
243
# Regression test for Bug 14206
244
$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES ('FFL','my module','my code','my name',1,?,?,'print')|, undef, $title, $content );
245
my $letter14206_a = C4::Letters::getletter('my module', 'my code', 'FFL' );
246
is( $letter14206_a->{message_transport_type}, 'print', 'Bug 14206 - message_transport_type not passed, correct mtt detected' );
247
my $letter14206_b = C4::Letters::getletter('my module', 'my code', 'FFL', 'print');
248
is( $letter14206_b->{message_transport_type}, 'print', 'Bug 14206 - message_transport_type passed, correct mtt detected'  );
249
250
# test for overdue_notices.pl
251
my $overdue_rules = {
252
    letter1         => 'my code',
253
};
254
my $i = 1;
255
my $branchcode = 'FFL';
256
my $letter14206_c = C4::Letters::getletter('my module', $overdue_rules->{"letter$i"}, $branchcode);
257
is( $letter14206_c->{message_transport_type}, 'print', 'Bug 14206 - correct mtt detected for call from overdue_notices.pl' );
258
259
# GetPreparedLetter
212
# GetPreparedLetter
260
t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com');
213
t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com');
261
t::lib::Mocks::mock_preference( 'SendAllEmailsTo', '' );
214
t::lib::Mocks::mock_preference( 'SendAllEmailsTo', '' );
(-)a/tools/letter.pl (-23 / +29 lines)
Lines 48-53 use C4::Output; Link Here
48
use C4::Letters;
48
use C4::Letters;
49
use C4::Log;
49
use C4::Log;
50
50
51
use Koha::Notice::Templates;
51
use Koha::Patron::Attribute::Types;
52
use Koha::Patron::Attribute::Types;
52
53
53
# $protected_letters = protected_letters()
54
# $protected_letters = protected_letters()
Lines 303-335 sub add_validate { Link Here
303
        my $is_html = $input->param("is_html_$mtt\_$lang");
304
        my $is_html = $input->param("is_html_$mtt\_$lang");
304
        my $title   = shift @title;
305
        my $title   = shift @title;
305
        my $content = shift @content;
306
        my $content = shift @content;
306
        my $letter = C4::Letters::getletter( $oldmodule, $code, $branchcode, $mtt, $lang );
307
        my $letter = Koha::Notice::Templates->find(
308
            {
309
                module                 => $oldmodule,
310
                code                   => $code,
311
                branchcode             => $branchcode,
312
                message_transport_type => $mtt,
313
                lang                   => $lang
314
            }
315
        );
307
316
308
        # getletter can return the default letter even if we pass a branchcode
309
        # If we got the default one and we needed the specific one, we didn't get the one we needed!
310
        if ( $letter and $branchcode and $branchcode ne $letter->{branchcode} ) {
311
            $letter = undef;
312
        }
313
        unless ( $title and $content ) {
317
        unless ( $title and $content ) {
314
            # Delete this mtt if no title or content given
318
            # Delete this mtt if no title or content given
315
            delete_confirmed( $branchcode, $oldmodule, $code, $mtt, $lang );
319
            delete_confirmed( $branchcode, $oldmodule, $code, $mtt, $lang );
316
            next;
320
            next;
317
        }
321
        }
318
        elsif ( $letter and $letter->{message_transport_type} eq $mtt and $letter->{lang} eq $lang ) {
322
        elsif ( $letter ) {
319
            logaction( 'NOTICES', 'MODIFY', $letter->{id}, $content,
323
            logaction( 'NOTICES', 'MODIFY', $letter->id, $content,
320
                'Intranet' )
324
                'Intranet' )
321
              if ( C4::Context->preference("NoticesLog")
325
              if ( C4::Context->preference("NoticesLog")
322
                && $content ne $letter->{content} );
326
                && $content ne $letter->content );
323
            $dbh->do(
327
324
                q{
328
            $letter->set(
325
                    UPDATE letter
329
                {
326
                    SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ?, lang = ?
330
                    branchcode => $branchcode || '',
327
                    WHERE branchcode = ? AND module = ? AND code = ? AND message_transport_type = ? AND lang = ?
331
                    module     => $module,
328
                },
332
                    name       => $name,
329
                undef,
333
                    is_html    => $is_html || 0,
330
                $branchcode || '', $module, $name, $is_html || 0, $title, $content, $lang,
334
                    title      => $title,
331
                $branchcode, $oldmodule, $code, $mtt, $lang
335
                    content    => $content,
332
            );
336
                    lang       => $lang
337
                }
338
            )->store;
339
333
        } else {
340
        } else {
334
            my $letter = Koha::Notice::Template->new(
341
            my $letter = Koha::Notice::Template->new(
335
                {
342
                {
Lines 357-366 sub add_validate { Link Here
357
sub delete_confirm {
364
sub delete_confirm {
358
    my ($branchcode, $module, $code) = @_;
365
    my ($branchcode, $module, $code) = @_;
359
    my $dbh = C4::Context->dbh;
366
    my $dbh = C4::Context->dbh;
360
    my $letter = C4::Letters::getletter($module, $code, $branchcode);
367
    my $letter = Koha::Notice::Templates->search(
361
    my @values = values %$letter;
368
        { module => $module, code => $code, branchcode => $branchcode } );
362
    $template->param(
369
    $template->param(
363
        letter => $letter,
370
        letter => $letter ? $letter->next : undef,
364
    );
371
    );
365
    return;
372
    return;
366
}
373
}
367
- 

Return to bug 28514