From 90c5933cde431f81e2635133cb8ea478c015443d Mon Sep 17 00:00:00 2001
From: George Veranis <gveranis@dataly.gr>
Date: Fri, 2 Sep 2022 22:30:45 +0200
Subject: [PATCH] Bug 29825: Preview of authority record on edit mode as MARC
 formatted view

This bug adds a new functionality on Edit mode of a new or existing authority.
Add a new button 'MARC Preview' in order librarian have the ability to preview
the record on MARC plain structure mode and have all fields and subfields
that entered before save/update the record.

To test:
1. Open or add an authority
2. The preview button isn't on toolbar
3. Apply the patch
4. Do step 1
5. The preview button exists
6. Do some changes or not to the authority record
7. Click to Preview MARC button and a modal window will appear with all fields
   indicators and subfields that filled in the form of cataloguing.

Mentored by: Andreas Roussos

Signed-off-by: Phil Ringnalda <phil@chetcolibrary.org>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 .../modals/edit_authority_preview.inc         | 11 ++++
 .../en/modules/authorities/authorities.tt     | 55 ++++++++++++++++++-
 2 files changed, 65 insertions(+), 1 deletion(-)
 create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/modals/edit_authority_preview.inc

diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/modals/edit_authority_preview.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/modals/edit_authority_preview.inc
new file mode 100644
index 00000000000..c4f9aae3fc4
--- /dev/null
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/modals/edit_authority_preview.inc
@@ -0,0 +1,11 @@
+<div id="auEditPreview" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="auEditPreview" aria-hidden="true">
+    <div class="modal-dialog">
+        <div class="modal-content">
+            <div class="modal-header">
+                <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">×</button>
+                <h3 id="avCreateLabel">Preview based on MARC structure</h3>
+            </div>
+            <div class="modal-body" id="auloadpreview"></div>
+        </div> <!-- /.modal-content -->
+    </div> <!-- /.modal-dialog -->
+</div> <!-- /#auEditPreview -->
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/authorities/authorities.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/authorities/authorities.tt
index 101187a51a8..c6d6f14605c 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/authorities/authorities.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/authorities/authorities.tt
@@ -126,6 +126,56 @@
             Sticky.hcSticky('refresh');
         });
 
+        $('#preview').click(function (){
+            var content="<h3>Authority #: [% authid %]</h3><hr>";
+            content+="<div style='color:#004d99;'>";
+            var line=1;
+            var has_input=0;
+            var sf_has_input=0;
+            $("#authoritytabs").find("[id^=tag_]").each(function(){
+                //check if tag has content
+                has_input=0;
+                $(this).find(".input_marceditor").each(function(){
+                    if($(this).val()) has_input=1;
+                });
+
+                if(has_input){
+                    line=1;
+                    //tag
+                    var $tag = $(this).find(".tagnum").text();
+                    content += $tag.replace("*",") +"&nbsp;&nbsp;";
+                    //indicators
+                    $(this).find(".indicator").each(function(){
+                        if( $(this).val() ) content += $(this).val();
+                        else content += "_";
+                    });
+                    content += "&nbsp;&nbsp;";
+                    //subfields
+                    $(this).find(".subfield_line").each(function(){
+                        //check if subfield has content
+                        sf_has_input=0;
+                        $(this).find(".input_marceditor").each(function(){
+                            if($(this).val()) sf_has_input=1;
+                    });
+                    if(sf_has_input){
+                        if(line>1) content += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
+                        //code
+                        $(this).find(".subfieldcode").each(function(){
+                            $(this).find(".flat").each(function(){
+                                if($(this).val()!="00")
+                                    content += "\$"+$(this).val()+"&nbsp;&nbsp;";
+                            });
+                        });
+                        //value
+                        $(this).find(".input_marceditor").each(function(){
+                            content += "<span style='color:#000;'>"+$(this).val()+"</span>";
+                        });
+                        content+="</br>";
+                        line+=1;
+                    }
+                }
+            });
+         });
     });
 
     function selectTab( tablink ){
@@ -476,6 +526,9 @@
                                 [% END %]
                             </ul> <!-- /#settings-menu -->
                         </div> <!-- /.btn-group -->
+                        <div class="btn-group">
+                            <a class="btn btn-default" id="preview" href="#"><i class="fa fa-eye"></i> MARC Preview</a>
+                        </div>
                         <div class="btn-group">
                             [% IF ( authid ) %]
                                 <a class="btn btn-default" id="cancel" href="/cgi-bin/koha/authorities/detail.pl?authid=[% authid | url %]">Cancel</a>
@@ -763,7 +816,7 @@
                 </form>
 
                 [% INCLUDE 'modals/cataloguing_create_av.inc' %]
-
+                [% INCLUDE 'modals/edit_authority_preview.inc' %]
             </div>
         </div>
     </div>
-- 
2.30.2