From a860c5621be10502226dd3c6eb58f0f70e77dcb5 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Wed, 8 May 2024 17:05:26 +0100
Subject: [PATCH] Bug 36815: WIP - Expose to the UI

This patch begins to expose the sample notices to the UI in the notices
editor.

It doesn't look pretty yet, we need to treat the output as code as
aposed to rendering it as html.. and we want to add the 'copy to
content' functoinality to allow resetting from it.
---
 .../prog/en/modules/tools/letter.tt           | 42 +++++++++++++++++++
 tools/letter.pl                               |  4 ++
 2 files changed, 46 insertions(+)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt
index 22379532e50..fad17b6d273 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt
@@ -132,6 +132,26 @@
         </div> <!-- /.modal-dialog -->
     </div> <!-- /#preview_template -->
 
+    <!-- Sample display modal -->
+    <div class="modal" id="noticeSampleModal" tabindex="-1" aria-labelledby="noticeSampleModalLabel" aria-hidden="true">
+        <div class="modal-dialog">
+            <div class="modal-content">
+                <div class="modal-header">
+                    <button type="button" class="closebtn" data-dismiss="modal" aria-label="Close">
+                        <span aria-hidden="true">&times;</span>
+                    </button>
+                    <h5 class="modal-title" id="noticeSampleModalLabel">Default template</h5>
+                </div>
+                <div class="modal-body">
+                    ...
+                </div>
+                <div class="modal-footer">
+                    <button type="button" class="btn btn-default deny cancel" data-dismiss="modal"><i class="fa fa-times"></i> Close</button>
+                </div>
+            </div> <!-- /.modal-content -->
+        </div> <!-- /.modal-dialog -->
+    </div> <!-- /.modal -->
+
     [% IF add_form or copy_form %]
         <div class="main container-fluid">
             <div class="row">
@@ -673,6 +693,12 @@
                                     <a href="/cgi-bin/koha/svc/letters/preview" class="preview_template btn btn-default btn-xs" title="Preview this notice template" data-mtt="[% letter.message_transport_type | html %]" data-lang="[% lang | html %]"><i class="fa-solid fa-eye"></i> Preview</a>
                                 </li>
                             [% END %]
+                            [% IF letter.sample %]
+                            <li>
+                                <button data-noticeid="[% letter.id %]" class="notice-sample btn btn-default btn-sm"><i class="fa-solid fa-eye"></i> View default</button>
+                                <iframe class="hidden" id="notice[% letter.id | html %]" srcdoc="<code><pre>[% letter.sample | html %]</pre></code>"></iframe>
+                            </li>
+                            [% END %]
                         </ol>
                     </fieldset> <!-- /.rows.mtt -->
                 [% END %]
@@ -761,6 +787,22 @@
                     '}';
                 });
             });
+
+            $(".notice-sample").on("click", function(e){
+                e.preventDefault();
+                var title = $(this).text();
+                var noticeid = $(this).data("noticeid");
+                var body = $("#notice" + noticeid ).attr("srcdoc");
+                $("#noticeSampleModalLabel").text( title );
+                $("#noticeSampleModal .modal-body").html( body );
+                $("#noticeSampleModal").modal("show");
+            });
+
+            $("#noticeSampleModal").on("hide.bs.modal", function(){
+                $("#noticeSampleModalLabel").text("");
+                $("#noticeSampleModal .modal-body").html("");
+            });
+
         });
      </script>
     [% Asset.js("js/letter.js") | $raw %]
diff --git a/tools/letter.pl b/tools/letter.pl
index 9e9b9e386fa..dbe84aa26cb 100755
--- a/tools/letter.pl
+++ b/tools/letter.pl
@@ -227,6 +227,10 @@ sub add_form {
                 tt_error   => $letter->{tt_error},
             };
             $letters{ $lang }{params} = $letter;
+
+            my $object = Koha::Notice::Templates->find( { id => $letter->{id} } );
+            $letters{ $lang }{templates}{$mtt}{sample} = $object->get_default;
+            $letters{ $lang }{templates}{$mtt}{id} = $letter->{id};
         }
     }
     else {
-- 
2.45.0