From 2c9c549ec14f20269625784484a6c07c44c9b637 Mon Sep 17 00:00:00 2001
From: Lari Taskula <larit@student.uef.fi>
Date: Wed, 16 Sep 2015 18:31:57 +0300
Subject: [PATCH] Bug 12426: Allow resend for sent messages
Content-Type: text/plain; charset=utf-8

This patch allows to resend both sent and failed messages.

With messages in 'sent' status, we have to be careful not to accidentally send
sent messages again. With the previous patch using GET request, this was likely
to happen because of browser storing the GET parameters.

This patch changes request method from GET to POST. Instead of a simple link,
we now have a form element.

In notices.pl we redirect back to notices.pl, because with POST there is a risk
of resending the message accidentally by form resubmission at refresh.

To test, find/create a Patron that has sent or failed notices in message_queue:
1. Enable EnchancedMessagingPreferences system preference
2. Go to Patrons -> Notices
3. In the Notice column, click the title of the sent or failed message
4. Observe that there is nothing for resending the sent or failed message
5. Apply the patches.
6. Reload Notices page and repeat step 3
7. Observe that there is now a link "Resend" in the Status-column
8. Click Resend
9. Observe that the message gets into 'pending' status

Tested all patches together. Works as expected.
Signed-off-by: Marc Veron <veron@veron.ch>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 C4/Letters.pm                                              |    2 +-
 koha-tmpl/intranet-tmpl/prog/en/modules/members/notices.tt |   12 +++++++++++-
 members/notices.pl                                         |    2 ++
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/C4/Letters.pm b/C4/Letters.pm
index 77deba9..79db37a 100644
--- a/C4/Letters.pm
+++ b/C4/Letters.pm
@@ -1121,7 +1121,7 @@ sub ResendMessage {
 
     my $message = GetMessage( $message_id );
     return unless $message;
-    if ( $message->{status} eq 'failed' ) {
+    if ( $message->{status} ne 'pending' ) {
         return ((C4::Letters::_set_message_status( {
                     message_id => $message_id,
                     status => 'pending',
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/notices.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/notices.tt
index d4aa4b7..6572e6c 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/notices.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/notices.tt
@@ -71,9 +71,19 @@
 		<td>
             [% IF ( QUEUED_MESSAGE.status == 'sent' ) %]sent
             [% ELSIF ( QUEUED_MESSAGE.status == 'pending' ) %]pending
-            [% ELSIF ( QUEUED_MESSAGE.status == 'failed' ) %]failed <div class="notice"><a href="/cgi-bin/koha/members/notices.pl?borrowernumber=[% borrowernumber %]&amp;op=resend_notice&amp;message_id=[% QUEUED_MESSAGE.message_id %]" title="Attempt to resend the notice">Resend</a></div>
+            [% ELSIF ( QUEUED_MESSAGE.status == 'failed' ) %]failed
             [% ELSIF ( QUEUED_MESSAGE.status == 'deleted' ) %]deleted
             [% ELSE %][% QUEUED_MESSAGE.status %][% END %]
+            [% IF ( QUEUED_MESSAGE.status != 'pending' ) %]
+            <div class="notice">
+                <form action="/cgi-bin/koha/members/notices.pl?borrowernumber=[% borrowernumber %]" method="POST">
+                    <input type="hidden" name="op" value="resend_notice" />
+                    <input type="hidden" name="borrowernumber" value="[% borrowernumber %]" />
+                    <input type="hidden" name="message_id" value="[% QUEUED_MESSAGE.message_id %]" />
+                    <a href="#" onclick="$(this).closest('form').submit();return false;" title="Attempt to resend the notice">Resend</a>
+                </form>
+            </div>
+            [% END %]
         </td>
         <td><span title="[% QUEUED_MESSAGE.time_queued %]">[% QUEUED_MESSAGE.time_queued | $KohaDates with_hours => 1 %]</span></td>
 	    </tr>
diff --git a/members/notices.pl b/members/notices.pl
index 1f7cce9..33b4be9 100755
--- a/members/notices.pl
+++ b/members/notices.pl
@@ -56,6 +56,8 @@ if ( $op eq 'resend_notice' ) {
     my $message = C4::Letters::GetMessage( $message_id );
     if ( $message->{borrowernumber} = $borrowernumber ) {
         C4::Letters::ResendMessage( $message_id );
+        # redirect to self to avoid form submission on refresh
+        print $input->redirect("/cgi-bin/koha/members/notices.pl?borrowernumber=$borrowernumber");
     }
 }
 
-- 
1.7.10.4