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

(-)a/C4/Form/MessagingPreferences.pm (-4 / +23 lines)
Lines 20-26 package C4::Form::MessagingPreferences; Link Here
20
use strict;
20
use strict;
21
use warnings;
21
use warnings;
22
22
23
use Data::Dumper;
24
use CGI qw ( -utf8 );
23
use CGI qw ( -utf8 );
25
use C4::Context;
24
use C4::Context;
26
use C4::Debug;
25
use C4::Debug;
Lines 109-120 sub handle_form_action { Link Here
109
            categorycode => $categorycode || $updater->{'categorycode'},
108
            categorycode => $categorycode || $updater->{'categorycode'},
110
        });
109
        });
111
        unless ($preference) {
110
        unless ($preference) {
112
            Koha::Patron::Message::Preference->new($updater)->store;
111
            $preference = Koha::Patron::Message::Preference->new($updater)->store;
113
        } else {
112
        } else {
114
            $preference->set($updater)->store;
113
            $preference->set($updater)->store;
115
        }
114
        }
116
115
117
        _pushToActionLogBuffer($logEntries, $updater, $option);
116
        $preference->_push_to_action_buffer($logEntries);
118
117
119
	if ($query->param( $option->{'message_attribute_id'})){
118
	if ($query->param( $option->{'message_attribute_id'})){
120
	    $prefs_set = 1;
119
	    $prefs_set = 1;
Lines 129-135 sub handle_form_action { Link Here
129
    # show the success message
128
    # show the success message
130
    $template->param( settings_updated => 1 );
129
    $template->param( settings_updated => 1 );
131
130
132
    _writeActionLogBuffer($logEntries, $borrowernumber);
131
    Koha::Patron::Message::Preferences->_log_action_buffer($logEntries, $borrowernumber);
133
}
132
}
134
133
135
=head2 set_form_values
134
=head2 set_form_values
Lines 202-207 sub _writeActionLogBuffer { Link Here
202
    }
201
    }
203
}
202
}
204
203
204
sub _no_contact_set {
205
    my ($param, $target_params, $target_param_name) = @_;
206
207
    if (defined $param && !$param) {
208
        return 1;
209
    }
210
    elsif (!defined $param && exists $target_params->{$target_param_name}
211
                           && !$target_params->{$target_param_name}) {
212
        return 1;
213
    } else {
214
        return 0;
215
    }
216
}
217
218
sub _transport_set {
219
    my ($name, @transport_methods) = @_;
220
221
    return List::MoreUtils::firstidx { $_ eq $name } @transport_methods;
222
}
223
205
=head1 TODO
224
=head1 TODO
206
225
207
=over 4
226
=over 4
(-)a/Koha/Patron/Message/Preference.pm (+35 lines)
Lines 456-461 sub _validate_message_transport_types { Link Here
456
    }
456
    }
457
}
457
}
458
458
459
sub _push_to_action_buffer {
460
    return unless C4::Context->preference("BorrowersLog");
461
    my $self = shift;
462
    my ($logEntries) = @_;
463
    return unless $logEntries;
464
    Koha::Exceptions::MissingParameter->throw(
465
        error => 'You must pass an array reference to '.
466
                 'Koha::Patron::Message::Preference::_push_to_action_buffer'
467
    ) if ref($logEntries) ne 'ARRAY';
468
469
    my @mtts = keys %{$self->message_transport_types};
470
    if (@mtts) {
471
        my $entry = {};
472
        $entry->{cc}   = $self->categorycode    if $self->categorycode;
473
        $entry->{dig}  = $self->wants_digest    if $self->wants_digest;
474
        $entry->{da}   = $self->days_in_advance if $self->days_in_advance;
475
        $entry->{mtt}  = \@mtts;
476
        $entry->{_name} = $self->message_name;
477
        push(@$logEntries, $entry);
478
    }
479
480
    return $logEntries;
481
}
482
483
sub _log_action_buffer {
484
    my $self = shift;
485
    my ($logEntries, $borrowernumber) = @_;
486
    $borrowernumber //= defined $self->borrowernumber
487
        ? $self->borrowernumber : undef;
488
489
    Koha::Patron::Message::Preferences->_log_action_buffer(
490
        $logEntries, $borrowernumber
491
    );
492
}
493
459
=head3 type
494
=head3 type
460
495
461
=cut
496
=cut
(-)a/Koha/Patron/Message/Preferences.pm (+24 lines)
Lines 19-29 package Koha::Patron::Message::Preferences; Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use C4::Context;
23
22
use Koha::Database;
24
use Koha::Database;
23
use Koha::Patron::Message::Attributes;
25
use Koha::Patron::Message::Attributes;
24
use Koha::Patron::Message::Preference;
26
use Koha::Patron::Message::Preference;
25
use Koha::Patron::Message::Transports;
27
use Koha::Patron::Message::Transports;
26
28
29
use Data::Dumper;
30
27
use base qw(Koha::Objects);
31
use base qw(Koha::Objects);
28
32
29
=head1 NAME
33
=head1 NAME
Lines 125-130 sub search_with_message_name { Link Here
125
    return $self->SUPER::search($params, $attributes);
129
    return $self->SUPER::search($params, $attributes);
126
}
130
}
127
131
132
sub _log_action_buffer {
133
    return 0 unless C4::Context->preference("BorrowersLog");
134
    my $self = shift;
135
    my ($logEntries, $borrowernumber) = @_;
136
    return 0 unless $logEntries;
137
138
    if (scalar(@$logEntries)) {
139
        my $d = Data::Dumper->new([$logEntries]);
140
        $d->Indent(0);
141
        $d->Purity(0);
142
        $d->Terse(1);
143
        C4::Log::logaction('MEMBERS', 'MOD MTT', $borrowernumber, $d->Dump($logEntries));
144
    }
145
    else {
146
        C4::Log::logaction('MEMBERS', 'MOD MTT', $borrowernumber, 'All message_transport_types removed')
147
    }
148
149
    return 1;
150
}
151
128
=head3 type
152
=head3 type
129
153
130
=cut
154
=cut
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-3 / +50 lines)
Lines 19-26 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 7;
22
use Test::More tests => 8;
23
24
use t::lib::Mocks;
23
use t::lib::Mocks;
25
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
26
25
Lines 187-192 subtest 'Add preferences from defaults' => sub { Link Here
187
    $schema->storage->txn_rollback;
186
    $schema->storage->txn_rollback;
188
};
187
};
189
188
189
subtest 'Test action logging' => sub {
190
    plan tests => 8;
191
192
    $schema->storage->txn_begin;
193
194
    t::lib::Mocks::mock_preference('BorrowersLog', 1);
195
196
    my $patron = build_a_test_patron();
197
    my ($default, $mtt1, $mtt2) = build_a_test_category_preference({
198
        patron => $patron,
199
    });
200
    Koha::Patron::Message::Preference->new_from_default({
201
        borrowernumber       => $patron->borrowernumber,
202
        categorycode         => $patron->categorycode,
203
        message_attribute_id => $default->message_attribute_id,
204
    })->store;
205
    my $pref = Koha::Patron::Message::Preferences->find({
206
        borrowernumber       => $patron->borrowernumber,
207
        message_attribute_id => $default->message_attribute_id,
208
    });
209
210
    my $actionLog = [];
211
    $pref->_push_to_action_buffer($actionLog);
212
    is(ref($actionLog), 'ARRAY', 'Action log is an array');
213
    is(@{$actionLog}, 1, 'One element in action log');
214
    is($actionLog->[0]->{_name}, $pref->message_name, 'Correct message name');
215
    is(@{$actionLog->[0]->{mtt}}, keys %{$pref->message_transport_types},
216
       'Correct mtts');
217
218
    # Let's push it another time for the lulz
219
    $pref->_push_to_action_buffer($actionLog);
220
    is(@{$actionLog}, 2, 'Two elements in action log');
221
222
    $pref->_log_action_buffer($actionLog, $patron->borrowernumber);
223
    my $logs = C4::Log::GetLogs("","","",["MEMBERS"],["MOD MTT"],
224
        $patron->borrowernumber,"");
225
    is($logs->[0]->{action}, 'MOD MTT', 'Correct log action');
226
    is($logs->[0]->{object}, $patron->borrowernumber, 'Correct borrowernumber');
227
228
    # Test empty action log buffer
229
    my $patron2 = build_a_test_patron();
230
    $pref->_log_action_buffer([], $patron2->borrowernumber);
231
    $logs = C4::Log::GetLogs("","","",["MEMBERS"],["MOD MTT"],
232
        $patron2->borrowernumber,"");
233
    is($logs->[0]->{info}, 'All message_transport_types removed', 'All message_transport_types removed');
234
235
    $schema->storage->txn_rollback;
236
};
237
190
subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub {
238
subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub {
191
    plan tests => 4;
239
    plan tests => 4;
192
240
193
- 

Return to bug 14806