From bccb5f2e10615054c7767607d93096a199defbee Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Mon, 3 Nov 2014 17:23:22 +0100
Subject: [PATCH] [SIGNED-OFF] Bug 13190: Reintroduce the checkouts export feature

Bug 11703 breaks the checkouts export feature.

To reproduce: Fill the ExportWithCsvProfile pref and go on the
circ/circulation.pl page. The export column appears, but not the export
button.

Test plan:
Go on the checkout list (circ/circulation.pl and members/moremember.pl)
and verify the export column and the export button appears.
If you click on the button, a file should be generated.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 C4/Record.pm                                       |    3 +-
 circ/circulation.pl                                |    2 -
 .../intranet-tmpl/prog/en/js/pages/circulation.js  |   11 +--
 .../prog/en/modules/circ/circulation.tt            |  119 ++++++++++++--------
 .../prog/en/modules/members/moremember.tt          |   15 ++-
 tools/export.pl                                    |    2 +-
 6 files changed, 91 insertions(+), 61 deletions(-)

diff --git a/C4/Record.pm b/C4/Record.pm
index ed386a3..16d3f54 100644
--- a/C4/Record.pm
+++ b/C4/Record.pm
@@ -366,6 +366,7 @@ C<$itemnumbers> - a list of itemnumbers to export
 
 sub marc2csv {
     my ($biblios, $id, $itemnumbers) = @_;
+    $itemnumbers ||= [];
     my $output;
     my $csv = Text::CSV::Encoded->new();
 
@@ -380,7 +381,7 @@ sub marc2csv {
     eval $preprocess if ($preprocess);
 
     my $firstpass = 1;
-    if ( $itemnumbers ) {
+    if ( @$itemnumbers ) {
         for my $itemnumber ( @$itemnumbers) {
             my $biblionumber = GetBiblionumberFromItemnumber $itemnumber;
             $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber] );
diff --git a/circ/circulation.pl b/circ/circulation.pl
index 2a1f211..0955cfa 100755
--- a/circ/circulation.pl
+++ b/circ/circulation.pl
@@ -596,8 +596,6 @@ $template->param(
     SpecifyDueDate            => $duedatespec_allow,
     CircAutocompl             => C4::Context->preference("CircAutocompl"),
     AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
-    export_remove_fields      => C4::Context->preference("ExportRemoveFields"),
-    export_with_csv_profile   => C4::Context->preference("ExportWithCsvProfile"),
     canned_bor_notes_loop     => $canned_notes,
     debarments                => GetDebarments({ borrowernumber => $borrowernumber }),
     todaysdate                => dt_from_string()->set(hour => 23)->set(minute => 59),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/pages/circulation.js b/koha-tmpl/intranet-tmpl/prog/en/js/pages/circulation.js
index 8ab4d17..cea3e88 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/js/pages/circulation.js
+++ b/koha-tmpl/intranet-tmpl/prog/en/js/pages/circulation.js
@@ -41,15 +41,10 @@ $(document).ready(function() {
         minute: 59
     });
     $("#export_submit").on("click",function(){
-        var export_format = $("#export_formats").val();
-        export_checkouts(export_format);
+        var output_format = $("#output_format").val();
+        export_checkouts(output_format);
         return false;
     });
-
-    toggle_onsite_checkout();
-    $("#onsite_checkout").click(function(){
-        toggle_onsite_checkout();
-    });
 });
 
 function export_checkouts(format) {
@@ -74,7 +69,7 @@ function export_checkouts(format) {
         $("#dont_export_item").val(1);
     }
 
-    document.getElementById("export_format").value = format;
+    document.getElementById("output_format").value = format;
     document.issues.submit();
 }
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
index 7421979..94ef257 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
@@ -1,7 +1,7 @@
 [% USE Koha %]
 [% USE Branches %]
 [% USE KohaDates %]
-[% IF ( export_remove_fields OR export_with_csv_profile ) %]
+[% IF Koha.Preference('ExportRemoveFields') OR Koha.Preference('ExportWithCsvProfile') %]
    [% SET exports_enabled = 1 %]
 [% END %]
 [% USE AuthorisedValues %]
@@ -45,17 +45,6 @@ var MSG_EXPORT_SELECT_CHECKOUTS = _("You must select checkout(s) to export");
 [% IF ( borrowernumber ) %]if($.cookie("holdfor") != [% borrowernumber %]){ $.cookie("holdfor",null, { path: "/", expires: 0 }); }[% ELSE %]$.cookie("holdfor",null, { path: "/", expires: 0 });[% END %]
 [% UNLESS ( borrowernumber ) %][% UNLESS ( selectborrower ) %]window.onload=function(){ $('#findborrower').focus(); };[% END %][% END %]
 
-$(document).ready(function() {
-    [% IF !( CircAutoPrintQuickSlip == 'clear' ) %]
-        // listen submit to trigger qslip on empty checkout
-        $('#mainform').bind('submit',function() {
-            if ($('#barcode').val() == '') {
-                return printx_window( '[% CircAutoPrintQuickSlip %]' );
-            }
-        });
-    [% END %]
-});
-
 // On-site checkout
 function toggle_onsite_checkout(){
     if ( $("#onsite_checkout").attr('checked') ) {
@@ -70,6 +59,21 @@ function toggle_onsite_checkout(){
     }
 }
 
+$(document).ready(function() {
+    [% IF !( CircAutoPrintQuickSlip == 'clear' ) %]
+        // listen submit to trigger qslip on empty checkout
+        $('#mainform').bind('submit',function() {
+            if ($('#barcode').val() == '') {
+                return printx_window( '[% CircAutoPrintQuickSlip %]' );
+            }
+        });
+    [% END %]
+    toggle_onsite_checkout();
+    $("#onsite_checkout").click(function(){
+        toggle_onsite_checkout();
+    });
+});
+
 //]]>
 </script>
 </head>
@@ -770,41 +774,66 @@ No patron matched <span class="ex">[% message %]</span>
             </p>
         </div>
 
-        <table id="issues-table">
-            <thead>
-                <tr>
-                    <th scope="col">&nbsp;</th>
-                    <th scope="col">&nbsp;</th>
-                    <th scope="col">Due date</th>
-                    <th scope="col">Due date</th>
-                    <th scope="col">Title</th>
-                    <th scope="col">Item type</th>
-                    <th scope="col">Checked out on</th>
-                    <th scope="col">Checked out from</th>
-                    <th scope="col">Call no</th>
-                    <th scope="col">Charge</th>
-                    <th scope="col">Price</th>
-                    <th scope="col">Renew <p class="column-tool"><a href="#" id="CheckAllRenewals">select all</a> | <a href="#" id="UncheckAllRenewals">none</a></p></th>
-                    <th scope="col">Check in <p class="column-tool"><a href="#" id="CheckAllCheckins">select all</a> | <a href="#" id="UncheckAllCheckins">none</a></p></th>
-                    <th scope="col">Export <p class="column-tool"><a href="#" id="CheckAllExports">select all</a> | <a href="#" id="UncheckAllExports">none</a></p></th>
-                </tr>
-            </thead>
-            [% INCLUDE 'checkouts-table-footer.inc' %]
-        </table>
-
-        <label for="issues-table-load-immediately">Always show checkouts immediately</label>
-        <input id="issues-table-load-immediately" type="checkbox" />
-
-        <fieldset id="issues-table-actions" class="action">
-            [% IF ( CAN_user_circulate_override_renewals ) %]
-                [% IF ( AllowRenewalLimitOverride ) %]
-                    <label for="override_limit">Override renewal limit:</label>
-                    <input type="checkbox" name="override_limit" id="override_limit" value="1" />
+        <form name="issues" action="/cgi-bin/koha/tools/export.pl" method="post" class="checkboxed">
+            <table id="issues-table">
+                <thead>
+                    <tr>
+                        <th scope="col">&nbsp;</th>
+                        <th scope="col">&nbsp;</th>
+                        <th scope="col">Due date</th>
+                        <th scope="col">Due date</th>
+                        <th scope="col">Title</th>
+                        <th scope="col">Item type</th>
+                        <th scope="col">Checked out on</th>
+                        <th scope="col">Checked out from</th>
+                        <th scope="col">Call no</th>
+                        <th scope="col">Charge</th>
+                        <th scope="col">Price</th>
+                        <th scope="col">Renew <p class="column-tool"><a href="#" id="CheckAllRenewals">select all</a> | <a href="#" id="UncheckAllRenewals">none</a></p></th>
+                        <th scope="col">Check in <p class="column-tool"><a href="#" id="CheckAllCheckins">select all</a> | <a href="#" id="UncheckAllCheckins">none</a></p></th>
+                        <th scope="col">Export <p class="column-tool"><a href="#" id="CheckAllExports">select all</a> | <a href="#" id="UncheckAllExports">none</a></p></th>
+                    </tr>
+                </thead>
+                [% INCLUDE 'checkouts-table-footer.inc' %]
+            </table>
+
+            <label for="issues-table-load-immediately">Always show checkouts immediately</label>
+            <input id="issues-table-load-immediately" type="checkbox" />
+
+            <fieldset id="issues-table-actions" class="action">
+                [% IF ( CAN_user_circulate_override_renewals ) %]
+                    [% IF ( AllowRenewalLimitOverride ) %]
+                        <label for="override_limit">Override renewal limit:</label>
+                        <input type="checkbox" name="override_limit" id="override_limit" value="1" />
+                    [% END %]
+                [% END %]
+                <button class="btn" id="RenewCheckinChecked"><i class="icon-check"></i> Renew or return checked items</button>
+                <button class="btn" id="RenewAll"><i class="icon-book"></i> Renew all</button>
+            </fieldset>
+
+            [% IF issuecount %]
+                [% IF ( exports_enabled ) %]
+                    <fieldset>
+                        <label for="output_format"><b>Export checkouts using format:</b></label>
+                        <select name="output_format" id="output_format">
+                            <option value="iso2709_995">ISO2709 with items</option>
+                            <option value="iso2709">ISO2709 without items</option>
+                            [% IF Koha.Preference('ExportWithCsvProfile') %]
+                                <option value="csv">CSV</option>
+                            [% END %]
+                        </select>
+
+                       <label for="export_remove_fields">Don't export fields:</label> <input type="text" id="export_remove_fields" name="export_remove_fields" value="[% export_remove_fields %]" title="Use for iso2709 exports" />
+                        <input type="hidden" name="op" value="export" />
+                        <input type="hidden" id="output_format" name="output_format" value="iso2709" />
+                        <input type="hidden" id="dont_export_item" name="dont_export_item" value="0" />
+                        <input type="hidden" id="record_type" name="record_type" value="bibs" />
+                        <button class="btn btn-small" id="export_submit"><i class="icon-download-alt"></i> Export</button>
+                    </fieldset>
                 [% END %]
             [% END %]
-            <button class="btn" id="RenewCheckinChecked"><i class="icon-check"></i> Renew or return checked items</button>
-            <button class="btn" id="RenewAll"><i class="icon-book"></i> Renew all</button>
-        </fieldset>
+        </form>
+
     [% ELSE %]
         <p>Patron has nothing checked out.</p>
     [% END %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
index 24408ca..2a3b818 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
@@ -1,5 +1,8 @@
 [% USE Koha %]
 [% USE KohaDates %]
+[% IF Koha.Preference('ExportRemoveFields') OR Koha.Preference('ExportWithCsvProfile') %]
+   [% SET exports_enabled = 1 %]
+[% END %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Patrons &rsaquo;
 [% IF ( unknowuser ) %]
@@ -17,6 +20,7 @@
 <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery-ui-timepicker-addon.min.js"></script>
 [% INCLUDE 'timepicker.inc' %]
 <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.dataTables.rowGrouping.js"></script>
+<script type="text/javascript" src="[% themelang %]/js/pages/circulation.js"></script>
 <script type="text/javascript" src="[% themelang %]/js/checkouts.js"></script>
 <script type="text/javascript" src="[% themelang %]/js/holds.js"></script>
 <script type="text/JavaScript">
@@ -34,6 +38,9 @@ var relatives_borrowernumbers = new Array();
     relatives_borrowernumbers.push("[% b %]");
 [% END %]
 
+var MSG_ADD_MESSAGE = _("Add a new message");
+var MSG_EXPORT_SELECT_CHECKOUTS = _("You must select checkout(s) to export");
+
 $(document).ready(function() {
     $('#finesholdsissues').tabs({
         // Correct table sizing for tables hidden in tabs
@@ -459,18 +466,18 @@ function validate1(date) {
 
                         [% IF ( exports_enabled ) %]
                             <fieldset>
-                                <label for="export_formats"><b>Export checkouts using format:</b></label>
-                                <select name="export_formats" id="export_formats">
+                                <label for="output_format"><b>Export checkouts using format:</b></label>
+                                <select name="output_format" id="output_format">
                                     <option value="iso2709_995">ISO2709 with items</option>
                                     <option value="iso2709">ISO2709 without items</option>
-                                    [% IF ( export_with_csv_profile ) %]
+                                    [% IF Koha.Preference('ExportWithCsvProfile') %]
                                         <option value="csv">CSV</option>
                                     [% END %]
                                 </select>
 
                                <label for="export_remove_fields">Don't export fields:</label> <input type="text" id="export_remove_fields" name="export_remove_fields" value="[% export_remove_fields %]" title="Use for iso2709 exports" />
                                 <input type="hidden" name="op" value="export" />
-                                <input type="hidden" id="export_format" name="format" value="iso2709" />
+                                <input type="hidden" id="output_format" name="output_format" value="iso2709" />
                                 <input type="hidden" id="dont_export_item" name="dont_export_item" value="0" />
                                 <input type="hidden" id="record_type" name="record_type" value="bibs" />
                                 <button class="btn btn-small" id="export_submit"><i class="icon-download-alt"></i> Export</button>
diff --git a/tools/export.pl b/tools/export.pl
index 97415f8..658aef7 100755
--- a/tools/export.pl
+++ b/tools/export.pl
@@ -44,7 +44,7 @@ my $op       = $query->param("op")       || '';
 my $filename = $query->param("filename") || 'koha.mrc';
 my $dbh      = C4::Context->dbh;
 my $marcflavour = C4::Context->preference("marcflavour");
-my $format = $query->param("format") || 'iso2709';
+my $format = $query->param("format") || $query->param("output_format") || 'iso2709';
 
 # Checks if the script is called from commandline
 my $commandline = not defined $ENV{GATEWAY_INTERFACE};
-- 
1.7.2.5