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

(-)a/C4/Form/MessagingPreferences.pm (-35 / +3 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 145-156 sub handle_form_action { Link Here
145
            categorycode => $categorycode || $updater->{'categorycode'},
144
            categorycode => $categorycode || $updater->{'categorycode'},
146
        });
145
        });
147
        unless ($preference) {
146
        unless ($preference) {
148
            Koha::Patron::Message::Preference->new($updater)->store;
147
            $preference = Koha::Patron::Message::Preference->new($updater)->store;
149
        } else {
148
        } else {
150
            $preference->set($updater)->store;
149
            $preference->set($updater)->store;
151
        }
150
        }
152
151
153
        _pushToActionLogBuffer($logEntries, $updater, $option);
152
        $preference->_push_to_action_buffer($logEntries);
154
153
155
	if ($query->param( $option->{'message_attribute_id'})){
154
	if ($query->param( $option->{'message_attribute_id'})){
156
	    $prefs_set = 1;
155
	    $prefs_set = 1;
Lines 165-171 sub handle_form_action { Link Here
165
    # show the success message
164
    # show the success message
166
    $template->param( settings_updated => 1 );
165
    $template->param( settings_updated => 1 );
167
166
168
    _writeActionLogBuffer($logEntries, $borrowernumber);
167
    Koha::Patron::Message::Preferences->_log_action_buffer($logEntries, $borrowernumber);
169
}
168
}
170
169
171
=head2 set_form_values
170
=head2 set_form_values
Lines 227-263 sub _transport_set { Link Here
227
    return List::MoreUtils::firstidx { $_ eq $name } @transport_methods;
226
    return List::MoreUtils::firstidx { $_ eq $name } @transport_methods;
228
}
227
}
229
228
230
sub _pushToActionLogBuffer {
231
    return unless C4::Context->preference("BorrowersLog");
232
    my ($logEntries, $updater, $option) = @_;
233
234
    if ($updater->{message_transport_types} && scalar(@{$updater->{message_transport_types}})) {
235
        my $entry = {};
236
        $entry->{cc}   = $updater->{categorycode}    if $updater->{categorycode};
237
        $entry->{dig}  = $updater->{wants_digest}    if $updater->{wants_digest};
238
        $entry->{da}   = $updater->{days_in_advance} if $updater->{days_in_advance};
239
        $entry->{mtt}  = $updater->{message_transport_types};
240
        $entry->{_name} = $option->{message_name};
241
        push(@$logEntries, $entry);
242
    }
243
}
244
245
sub _writeActionLogBuffer {
246
    return unless C4::Context->preference("BorrowersLog");
247
    my ($logEntries, $borrowernumber) = @_;
248
249
    if (scalar(@$logEntries)) {
250
        my $d = Data::Dumper->new([$logEntries]);
251
        $d->Indent(0);
252
        $d->Purity(0);
253
        $d->Terse(1);
254
        C4::Log::logaction('MEMBERS', 'MOD MTT', $borrowernumber, $d->Dump($logEntries));
255
    }
256
    else {
257
        C4::Log::logaction('MEMBERS', 'MOD MTT', $borrowernumber, 'All message_transports removed')
258
    }
259
}
260
261
=head1 TODO
229
=head1 TODO
262
230
263
=over 4
231
=over 4
(-)a/Koha/Patron/Message/Preference.pm (+35 lines)
Lines 583-588 sub _validate_message_transport_types { Link Here
583
    }
583
    }
584
}
584
}
585
585
586
sub _push_to_action_buffer {
587
    return unless C4::Context->preference("BorrowersLog");
588
    my $self = shift;
589
    my ($logEntries) = @_;
590
    return unless $logEntries;
591
    Koha::Exceptions::MissingParameter->throw(
592
        error => 'You must pass an array reference to '.
593
                 'Koha::Patron::Message::Preference::_push_to_action_buffer'
594
    ) if ref($logEntries) ne 'ARRAY';
595
596
    my @mtts = keys %{$self->message_transport_types};
597
    if (@mtts) {
598
        my $entry = {};
599
        $entry->{cc}   = $self->categorycode    if $self->categorycode;
600
        $entry->{dig}  = $self->wants_digest    if $self->wants_digest;
601
        $entry->{da}   = $self->days_in_advance if $self->days_in_advance;
602
        $entry->{mtt}  = \@mtts;
603
        $entry->{_name} = $self->message_name;
604
        push(@$logEntries, $entry);
605
    }
606
607
    return $logEntries;
608
}
609
610
sub _log_action_buffer {
611
    my $self = shift;
612
    my ($logEntries, $borrowernumber) = @_;
613
    $borrowernumber //= defined $self->borrowernumber
614
        ? $self->borrowernumber : undef;
615
616
    Koha::Patron::Message::Preferences->_log_action_buffer(
617
        $logEntries, $borrowernumber
618
    );
619
}
620
586
=head3 type
621
=head3 type
587
622
588
=cut
623
=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 154-159 sub TO_JSON { Link Here
154
    return $preferences;
158
    return $preferences;
155
}
159
}
156
160
161
sub _log_action_buffer {
162
    return 0 unless C4::Context->preference("BorrowersLog");
163
    my $self = shift;
164
    my ($logEntries, $borrowernumber) = @_;
165
    return 0 unless $logEntries;
166
167
    if (scalar(@$logEntries)) {
168
        my $d = Data::Dumper->new([$logEntries]);
169
        $d->Indent(0);
170
        $d->Purity(0);
171
        $d->Terse(1);
172
        C4::Log::logaction('MEMBERS', 'MOD MTT', $borrowernumber, $d->Dump($logEntries));
173
    }
174
    else {
175
        C4::Log::logaction('MEMBERS', 'MOD MTT', $borrowernumber, 'All message_transport_types removed')
176
    }
177
178
    return 1;
179
}
180
157
=head3 type
181
=head3 type
158
182
159
=cut
183
=cut
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-2 / +50 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 8;
22
use Test::More tests => 9;
23
23
24
use t::lib::Mocks;
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
Lines 365-370 subtest 'Add preferences from defaults' => sub { Link Here
365
    $schema->storage->txn_rollback;
365
    $schema->storage->txn_rollback;
366
};
366
};
367
367
368
subtest 'Test action logging' => sub {
369
    plan tests => 8;
370
371
    $schema->storage->txn_begin;
372
373
    t::lib::Mocks::mock_preference('BorrowersLog', 1);
374
375
    my $patron = build_a_test_patron();
376
    my ($default, $mtt1, $mtt2) = build_a_test_category_preference({
377
        patron => $patron,
378
    });
379
    Koha::Patron::Message::Preference->new_from_default({
380
        borrowernumber       => $patron->borrowernumber,
381
        categorycode         => $patron->categorycode,
382
        message_attribute_id => $default->message_attribute_id,
383
    })->store;
384
    my $pref = Koha::Patron::Message::Preferences->find({
385
        borrowernumber       => $patron->borrowernumber,
386
        message_attribute_id => $default->message_attribute_id,
387
    });
388
389
    my $actionLog = [];
390
    $pref->_push_to_action_buffer($actionLog);
391
    is(ref($actionLog), 'ARRAY', 'Action log is an array');
392
    is(@{$actionLog}, 1, 'One element in action log');
393
    is($actionLog->[0]->{_name}, $pref->message_name, 'Correct message name');
394
    is(@{$actionLog->[0]->{mtt}}, keys %{$pref->message_transport_types},
395
       'Correct mtts');
396
397
    # Let's push it another time for the lulz
398
    $pref->_push_to_action_buffer($actionLog);
399
    is(@{$actionLog}, 2, 'Two elements in action log');
400
401
    $pref->_log_action_buffer($actionLog, $patron->borrowernumber);
402
    my $logs = C4::Log::GetLogs("","","",["MEMBERS"],["MOD MTT"],
403
        $patron->borrowernumber,"");
404
    is($logs->[0]->{action}, 'MOD MTT', 'Correct log action');
405
    is($logs->[0]->{object}, $patron->borrowernumber, 'Correct borrowernumber');
406
407
    # Test empty action log buffer
408
    my $patron2 = build_a_test_patron();
409
    $pref->_log_action_buffer([], $patron2->borrowernumber);
410
    $logs = C4::Log::GetLogs("","","",["MEMBERS"],["MOD MTT"],
411
        $patron2->borrowernumber,"");
412
    is($logs->[0]->{info}, 'All message_transport_types removed', 'All message_transport_types removed');
413
414
    $schema->storage->txn_rollback;
415
};
416
368
subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub {
417
subtest 'Test Koha::Patron::Message::Preference->message_transport_types' => sub {
369
    plan tests => 4;
418
    plan tests => 4;
370
419
371
- 

Return to bug 14806