Bug 32030: ERM - Refactoring
[srvgit] / koha-tmpl / intranet-tmpl / prog / en / includes / columns_settings.inc
index 7f3c214..8df6eb5 100644 (file)
@@ -1,28 +1,35 @@
+[% USE raw %]
+[% USE Koha %]
 [% USE TablesSettings %]
 <!-- columns_settings.inc -->
 
 <script>
-function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
+function KohaTable(id_selector, dt_parameters, table_settings, add_filters) {
     var counter = 0;
     var hidden_ids = [];
     var included_ids = [];
     var selector = '#' + id_selector;
 
-    $(columns_settings).each( function() {
-        var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', selector ).index( selector + ' th' );
-        var used_id = dt_parameters.bKohaColumnsUseNames ? named_id : counter;
-        if ( used_id == -1 ) return;
+    if ( table_settings ) {
+        var columns_settings = table_settings['columns'];
 
-        if ( this['is_hidden'] == "1" ) {
-            hidden_ids.push( used_id );
-        }
-        if ( this['cannot_be_toggled'] == "0" ) {
-            included_ids.push( used_id );
-        }
-        counter++;
-    });
+        $(columns_settings).each( function() {
+            var named_id = $( 'thead th[data-colname="' + this.columnname + '"]', selector ).index( selector + ' th' );
+            var used_id = dt_parameters.bKohaColumnsUseNames ? named_id : counter;
+            if ( used_id == -1 ) return;
+
+            if ( this['is_hidden'] == "1" ) {
+                hidden_ids.push( used_id );
+            }
+            if ( this['cannot_be_toggled'] == "0" ) {
+                included_ids.push( used_id );
+            }
+            counter++;
+        });
+    }
 
     var exportColumns = ":visible:not(.noExport)";
+    var exportRows = ":visible:not(.noExport)";
     if( dt_parameters.hasOwnProperty("exportColumns") ){
         // A custom buttons configuration has been passed from the page
         exportColumns = dt_parameters["exportColumns"];
@@ -41,20 +48,27 @@ function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
         }
     }
 
+    var export_numeric = {
+        body: function ( data, row, column, node ) {
+            var newnode = $(node);
+
+            if ( newnode.find(".noExport").length > 0 ) {
+                newnode = newnode.clone();
+                newnode.find(".noExport").remove();
+            }
+            let tp = newnode.text().replace( /\n/g, ' ' ).trim();
+            tp = $('<p>' + tp + '</p>').text();
+            return $.isNumeric(tp.replace(',', '.')) ? tp.replace(',', '.') : tp;
+        }
+    }
+
     var export_buttons = [
         {
-            extend: 'excelHtml5',
-            text: _("Excel"),
-            exportOptions: {
-                columns: exportColumns,
-                format:  export_format
-            },
-        },
-        {
             extend: 'csvHtml5',
             text: _("CSV"),
             exportOptions: {
                 columns: exportColumns,
+                rows: exportRows,
                 format:  export_format
             },
         },
@@ -63,6 +77,7 @@ function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
             text: _("Copy"),
             exportOptions: {
                 columns: exportColumns,
+                rows: exportRows,
                 format:  export_format
             },
         },
@@ -71,11 +86,38 @@ function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
             text: _("Print"),
             exportOptions: {
                 columns: exportColumns,
+                rows: exportRows,
                 format:  export_format
             },
         }
     ];
 
+    [% IF Koha.Preference("CurrencyFormat") != 'FR' %]
+        export_buttons.unshift (
+            {
+                extend: 'excelHtml5',
+                text: _("Excel"),
+                exportOptions: {
+                    columns: exportColumns,
+                    rows: exportRows,
+                    format:  export_format
+                },
+            }
+        );
+    [% ELSE %]
+        export_buttons.unshift (
+            {
+                extend: 'excelHtml5',
+                text: _("Excel"),
+                exportOptions: {
+                    columns: exportColumns,
+                    rows: exportRows,
+                    format:  export_numeric
+                },
+            }
+        );
+    [% END %]
+
     dt_parameters[ "buttons" ] = [
         {
             fade: 100,
@@ -100,7 +142,8 @@ function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
                 titleAttr: _("Columns settings"),
                 text: '<i class="fa fa-lg fa-gear"></i> <span class="dt-button-text">' + _("Columns") + '</span>',
                 exportOptions: {
-                    columns: exportColumns
+                    columns: exportColumns,
+                    rows: exportRows,
                 }
             }
         );
@@ -118,6 +161,19 @@ function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
         }
     );
 
+    if ( table_settings && CAN_user_parameters_manage_column_config ) {
+        dt_parameters[ "buttons" ].push(
+            {
+                className: "dt_button_configure_table",
+                titleAttr: _("Table settings"),
+                text: '<i class="fa fa-lg fa-wrench"></i> <span class="dt-button-text">' + _("Configure") + '</span>',
+                action: function() {
+                    window.location = '/cgi-bin/koha/admin/columns_settings.pl?module=' + table_settings['module'] + '&page=' + table_settings['page'] + '&table=' + table_settings['table'];
+                },
+            }
+        );
+    }
+
     var table = $(selector);
     if ( add_filters ) {
         // Duplicate the table header row for columnFilter
@@ -134,11 +190,23 @@ function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
         { "targets": [ "string-sort" ],  "type": "string" },
         { "targets": [ "anti-the" ],     "type": "anti-the" },
         { "targets": [ "NoSort" ],       "orderable": false, "searchable": false },
+        { "targets": [ "NoVisible" ],    "visible": false }
     ];
-    if ( new_parameters["columnDefs"] === undefined ) {
-        new_parameters["columnDefs"] = default_column_defs;
+    if ( new_parameters["aoColumnDefs"] === undefined ) {
+        new_parameters["aoColumnDefs"] = default_column_defs;
     } else {
-        $.extend(true, new_parameters, default_column_defs);
+        $(default_column_defs).each(function(){
+            new_parameters["aoColumnDefs"].push(this);
+        });
+    }
+
+    if ( table_settings ) {
+        if ( table_settings.hasOwnProperty('default_display_length') && table_settings['default_display_length'] != null ) {
+            new_parameters["pageLength"] = table_settings['default_display_length'];
+        }
+        if ( table_settings.hasOwnProperty('default_sort_order') && table_settings['default_sort_order'] != null ) {
+            new_parameters["order"] = [[ table_settings['default_sort_order'], 'asc' ]];
+        }
     }
 
     table.dataTable(new_parameters);
@@ -165,5 +233,44 @@ function KohaTable(id_selector, dt_parameters, columns_settings, add_filters) {
     return table;
 }
 
+
+/* get_columns_saved_state checks for a DataTables configuration saved
+* in the browser's local storage. If it is present, the columns
+* configuration supplied by Koha is overwritten
+*
+* It takes two parameters:
+*  - localstorage_config, the DataTables saved state object from local storage
+*  - columns_settings, the columns settings object supplied by the template
+*
+* An example:
+*
+*    var columns_settings = [% ColumnsSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
+*    var saved_table = localStorage.getItem("DataTables_TABLE_ID_/cgi-bin/koha/PATH/TO/SCRIPT.pl");
+*    var updated_settings = get_columns_saved_state( saved_table, columns_settings );
+*
+*    KohaTable("TABLE_ID", {
+*        "stateSave": true
+*    }, updated_settings);
+*/
+
+function get_columns_saved_state( localstorage_config, columns_settings ){
+    var tables = JSON.parse( localstorage_config );
+    // if a table configuration was found in local storage, parse it
+    if( tables ){
+        var stateSave_column_visibility = [];
+        $(tables.columns).each(function(){
+            stateSave_column_visibility.push( this.visible === true ? 0 : 1 );
+        });
+        $.each( columns_settings, function( index, key ){
+            if( stateSave_column_visibility[ index ] !== columns_settings[key] ){
+                columns_settings[ index ].is_hidden = stateSave_column_visibility[ index ];
+            }
+        });
+        return columns_settings;
+    } else {
+        return columns_settings;
+    }
+}
+
 </script>
 <!-- / columns_settings.inc -->