Bug 22038: Fix excel export if CurrencyFormat is FR
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / columns_settings.inc
1 [% USE Koha %]
2 [% USE TablesSettings %]
3 <!-- columns_settings.inc -->
4
5 <script>
6 function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
7     var counter = 0;
8     var hidden_ids = [];
9     var included_ids = [];
10     var selector = '#' + id_selector;
11
12     $(columns_settings).each( function() {
13         var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', selector ).index( selector + ' th' );
14         var used_id = dt_parameters.bKohaColumnsUseNames ? named_id : counter;
15         if ( used_id == -1 ) return;
16
17         if ( this['is_hidden'] == "1" ) {
18             hidden_ids.push( used_id );
19         }
20         if ( this['cannot_be_toggled'] == "0" ) {
21             included_ids.push( used_id );
22         }
23         counter++;
24     });
25
26     var exportColumns = ":visible:not(.noExport)";
27     if( dt_parameters.hasOwnProperty("exportColumns") ){
28         // A custom buttons configuration has been passed from the page
29         exportColumns = dt_parameters["exportColumns"];
30     }
31
32     var export_format = {
33         body: function ( data, row, column, node ) {
34             var newnode = $(node);
35
36             if ( newnode.find(".noExport").length > 0 ) {
37                 newnode = newnode.clone();
38                 newnode.find(".noExport").remove();
39             }
40
41             return newnode.text().replace( /\n/g, ' ' ).trim();
42         }
43     }
44
45     var export_numeric = {
46         body: function ( data, row, column, node ) {
47             var newnode = $(node);
48
49             if ( newnode.find(".noExport").length > 0 ) {
50                 newnode = newnode.clone();
51                 newnode.find(".noExport").remove();
52             }
53             let tp = newnode.text().replace( /\n/g, ' ' ).trim();
54             tp = $('<p>' + tp + '</p>').text();
55             return $.isNumeric(tp.replace(',', '.')) ? tp.replace(',', '.') : tp;
56         }
57     }
58
59     var export_buttons = [
60         {
61             extend: 'csvHtml5',
62             text: _("CSV"),
63             exportOptions: {
64                 columns: exportColumns,
65                 format:  export_format
66             },
67         },
68         {
69             extend: 'copyHtml5',
70             text: _("Copy"),
71             exportOptions: {
72                 columns: exportColumns,
73                 format:  export_format
74             },
75         },
76         {
77             extend: 'print',
78             text: _("Print"),
79             exportOptions: {
80                 columns: exportColumns,
81                 format:  export_format
82             },
83         }
84     ];
85
86     [% IF Koha.Preference("CurrencyFormat") != 'FR' %]
87         export_buttons.unshift (
88             {
89                 extend: 'excelHtml5',
90                 text: _("Excel"),
91                 exportOptions: {
92                     columns: exportColumns,
93                     format:  export_format
94                 },
95             }
96         );
97     [% ELSE %]
98         export_buttons.unshift (
99             {
100                 extend: 'excelHtml5',
101                 text: _("Excel"),
102                 exportOptions: {
103                     columns: exportColumns,
104                     format:  export_numeric
105                 },
106             }
107         );
108     [% END %]
109
110     dt_parameters[ "buttons" ] = [
111         {
112             fade: 100,
113             className: "dt_button_clear_filter",
114             titleAttr: _("Clear filter"),
115             enabled: false,
116             text: '<i class="fa fa-lg fa-remove"></i> <span class="dt-button-text">' + _("Clear filter") + '</span>',
117             action: function ( e, dt, node, config ) {
118                 dt.search( "" ).draw("page");
119                 node.addClass("disabled");
120             }
121         }
122     ];
123
124     if( included_ids.length > 0 ){
125         dt_parameters[ "buttons" ].push(
126             {
127                 extend: 'colvis',
128                 fade: 100,
129                 columns: included_ids,
130                 className: "columns_controls",
131                 titleAttr: _("Columns settings"),
132                 text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + _("Columns") + '</span>',
133                 exportOptions: {
134                     columns: exportColumns
135                 }
136             }
137         );
138     }
139
140     dt_parameters[ "buttons" ].push(
141         {
142             extend: 'collection',
143             autoClose: true,
144             fade: 100,
145             className: "export_controls",
146             titleAttr: _("Export or print"),
147             text: '<i class="fa fa-lg fa-download"></i> <span class="dt-button-text">' + _("Export") + '</span>',
148             buttons: export_buttons
149         }
150     );
151
152     var table = $(selector);
153     if ( add_filters ) {
154         // Duplicate the table header row for columnFilter
155         thead_row = table.find('thead tr');
156         clone = thead_row.clone().addClass('filters_row');
157         clone.find("th.NoSort").html('');
158         thead_row.before(clone);
159     }
160
161     var new_parameters = {}
162     $.extend(true, new_parameters, dataTablesDefaults, dt_parameters);
163     var default_column_defs = [
164         { "targets": [ "title-string" ], "type": "title-string" },
165         { "targets": [ "string-sort" ],  "type": "string" },
166         { "targets": [ "anti-the" ],     "type": "anti-the" },
167         { "targets": [ "NoSort" ],       "orderable": false, "searchable": false },
168         { "targets": [ "NoVisible" ],    "visible": false }
169     ];
170     if ( new_parameters["aoColumnDefs"] === undefined ) {
171         new_parameters["aoColumnDefs"] = default_column_defs;
172     } else {
173         $(default_column_defs).each(function(){
174             new_parameters["aoColumnDefs"].push(this);
175         });
176     }
177
178     table.dataTable(new_parameters);
179     table.DataTable().on("column-visibility.dt", function(){
180         if( typeof columnsInit == 'function' ){
181             // This function can be created separately and used to trigger
182             // an event after the DataTable has loaded AND column visibility
183             // has been updated according to the table's configuration
184             columnsInit();
185         }
186     }).columns( hidden_ids ).visible( false );
187
188     if ( add_filters ) {
189         // show a link to activate filtering
190         link = $('<a>')
191             .attr('href', '#')
192             .attr('id', id_selector + '_activate_filters');
193         $("." + id_selector  + "_table_controls").prepend(link);
194         deactivate_filters(id_selector);
195     }
196
197     $(".dt_button_clear_filter, .columns_controls, .export_controls").tooltip();
198
199     return table;
200 }
201
202 </script>
203 <!-- / columns_settings.inc -->