From ba3f704493e6d0983e69f9ee8933654ab7532ac7 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Tue, 2 Nov 2021 16:16:29 +0100 Subject: [PATCH] Bug 29393: Ability to send emails from patron details page Test plan - apply Bug 12802 - go to a patron's details page - click on "Add message" in tools bar - choose "Email" in "Add a message for" - Select a notice (module members) or manualy fill the message - Save - Check the message is saved in message_queue table Signed-off-by: Matthias Le Gac Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/Template/Plugin/NoticeTemplates.pm | 55 ++++++++++++++ circ/add_message.pl | 44 ++++++++++-- .../prog/en/includes/members-toolbar.inc | 29 +++++++- .../intranet-tmpl/prog/js/members-menu.js | 33 +++++++++ .../Template/Plugin/NoticeTemplates.t | 71 +++++++++++++++++++ 5 files changed, 224 insertions(+), 8 deletions(-) create mode 100644 Koha/Template/Plugin/NoticeTemplates.pm create mode 100644 t/db_dependent/Template/Plugin/NoticeTemplates.t diff --git a/Koha/Template/Plugin/NoticeTemplates.pm b/Koha/Template/Plugin/NoticeTemplates.pm new file mode 100644 index 0000000000..fe6ed61214 --- /dev/null +++ b/Koha/Template/Plugin/NoticeTemplates.pm @@ -0,0 +1,55 @@ +package Koha::Template::Plugin::NoticeTemplates; + +# Copyright 2021 BibLibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Template::Plugin; +use base qw( Template::Plugin ); + +use Koha::Notice::Templates; + +sub GetByModule { + my ( $self, $module ) = @_; + + return Koha::Notice::Templates->search({ module => $module }); +} + +=head1 NAME + +Koha::Template::Plugin::NoticeTemplates - TT Plugin for notice templates + +=head1 SYNOPSIS + +[% USE NoticeTemplates %] + +[% NoticeTemplates.GetByModule('members') %] + +=head1 ROUTINES + +=head2 GetByModule + +In a template, you can get notice templates by module with +[% letters = NoticeTemplates.GetByModule( 'members' ) %] + +=head1 AUTHOR + +Alex Arnaud + +=cut +1; diff --git a/circ/add_message.pl b/circ/add_message.pl index fb24d5ce78..102beb7bab 100755 --- a/circ/add_message.pl +++ b/circ/add_message.pl @@ -23,6 +23,7 @@ use CGI qw ( -utf8 ); use C4::Auth qw( get_template_and_user ); use C4::Output; +use C4::Letters; use Koha::Patron::Message; my $input = CGI->new; @@ -41,6 +42,8 @@ my $borrowernumber = $input->param('borrowernumber'); my $branchcode = $input->param('branchcode'); my $message_type = $input->param('message_type'); my $borrower_message = $input->param('borrower_message'); +my $borrower_subject = $input->param('borrower_subject'); +my $letter_code = $input->param('select_patron_notice'); my $batch = $input->param('batch'); if ( $op eq 'cud-edit_message' && $message_id) { @@ -48,14 +51,41 @@ if ( $op eq 'cud-edit_message' && $message_id) { $message->update( { message => $borrower_message } ) if $message; } elsif( $op eq 'cud-add_message' ) { - Koha::Patron::Message->new( - { - borrowernumber => $borrowernumber, - branchcode => $branchcode, - message_type => $message_type, - message => $borrower_message, + if ( $message_type eq 'L' or $message_type eq 'B' ) { + Koha::Patron::Message->new( + { + borrowernumber => $borrowernumber, + branchcode => $branchcode, + message_type => $message_type, + message => $borrower_message, + } + )->store; + } + + if ( $message_type eq 'E' ) { + my $letter = { + title => $borrower_subject, + content => $borrower_message + }; + + if ( $letter_code ) { + $letter = C4::Letters::GetPreparedLetter( + module => 'members', + letter_code => $letter_code, + tables => { + 'borrowers' => $borrowernumber + }, + ); } - )->store; + + C4::Letters::EnqueueLetter( + { + letter => $letter, + borrowernumber => $borrowernumber, + message_transport_type => 'email', + } + ) or warn "can't enqueue letter"; + } } my $url = $input->referer; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/members-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/members-toolbar.inc index b4f6c4c7bd..117658ee17 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/members-toolbar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/members-toolbar.inc @@ -3,6 +3,7 @@ [% USE Categories %] [% USE Notices %] [% USE AuthorisedValues %] +[% USE NoticeTemplates %] [% USE scalar %]
[% IF CAN_user_borrowers_edit_borrowers %] @@ -136,6 +137,7 @@
[% bor_notes = AuthorisedValues.Get( 'BOR_NOTES' ) %] @@ -150,8 +152,33 @@ [% END %] + + [% patron_letters = NoticeTemplates.GetByModule( 'members' ) %] + [% IF patron_letters %] +
+ + +
+ [% END %] +
- +
+
    + +
  1. + + +
  2. +
+
diff --git a/koha-tmpl/intranet-tmpl/prog/js/members-menu.js b/koha-tmpl/intranet-tmpl/prog/js/members-menu.js index cbd91ce13e..2a9b0cec69 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/members-menu.js +++ b/koha-tmpl/intranet-tmpl/prog/js/members-menu.js @@ -77,6 +77,39 @@ $(document).ready(function(){ startup(); }); + $("#message_type").on("change",function(){ + if ($(this).val() == 'E') { + $("label[for='borrower_message']").show(); + $('#subject_form').show(); + $("label[for='select_patron_notice']").show(); + $('#select_patron_notice').show(); + $("label[for='select_patron_messages']").hide(); + $('#select_patron_messages').hide(); + $('#borrower_message').val(''); + $('#select_patron_notice').val(''); + } else { + $('#subject_form').hide(); + $("label[for='borrower_message']").hide(); + $("label[for='select_patron_notice']").hide(); + $('#select_patron_notice').hide(); + $("label[for='select_patron_messages']").show(); + $('#select_patron_messages').show(); + $('#borrower_subject').prop( "disabled", false ); + $('#borrower_message').prop( "disabled", false ); + $('#select_patron_messages').val(''); + } + }); + + $("#select_patron_notice").on("change",function(){ + if ($(this).val()) { + $('#borrower_subject').prop( "disabled", true ); + $('#borrower_message').prop( "disabled", true ); + } else { + $('#borrower_subject').prop( "disabled", false ); + $('#borrower_message').prop( "disabled", false ); + } + }); + $(".edit-patronimage").on("click", function(e){ e.preventDefault(); var borrowernumber = $(this).data("borrowernumber"); diff --git a/t/db_dependent/Template/Plugin/NoticeTemplates.t b/t/db_dependent/Template/Plugin/NoticeTemplates.t new file mode 100644 index 0000000000..84e38578d6 --- /dev/null +++ b/t/db_dependent/Template/Plugin/NoticeTemplates.t @@ -0,0 +1,71 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 4; + +use Koha::Database; +use Koha::Notice::Templates; + +use t::lib::TestBuilder; + +BEGIN { + use_ok('Koha::Template::Plugin::NoticeTemplates'); +} + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; +$schema->storage->txn_begin; + +Koha::Notice::Templates->delete(); + +my $notice_templates = Koha::Notice::Templates->search({ module => 'members'}); + +$builder->build({ + source => 'Letter', + value => { + name => 'Hold cancellation', + module => 'reserves' + } +}); + +$builder->build({ + source => 'Letter', + value => { + name => 'Account expiration', + module => 'members' + } +}); + +$builder->build({ + source => 'Letter', + value => { + name => 'Discharge', + module => 'members' + } +}); + +my $plugin = Koha::Template::Plugin::NoticeTemplates->new(); +ok($plugin, "initialized notice templates plugin"); + +my $notices = $plugin->GetByModule('members'); +is( $notices->count, 2, 'returns 2 defined members letters' ); + +$notices = $plugin->GetByModule('reserves'); +is( $notices->count, 1, 'returns 2 defined reserves letters' ); + +$schema->storage->txn_rollback; -- 2.39.5