From 1b08be7c85a14efe9dc77424adca2f06e669a606 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Wed, 23 Mar 2022 17:36:50 +0100
Subject: [PATCH] Bug 30063: Be more flexible for filters
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

On later follow-ups (ERM) we need to filter columns that contain AVs,
and so be more flexible. Here we are expecting a _id and _str keys we
are gonna use to build the select's options

Signed-off-by: Séverine Queune <severine.queune@bulac.fr>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 .../prog/en/includes/patron-search.inc        | 20 +++++++---
 koha-tmpl/intranet-tmpl/prog/js/datatables.js | 40 ++++++-------------
 2 files changed, 26 insertions(+), 34 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc
index 88e6d89304..7b68a5940c 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc
@@ -209,14 +209,22 @@
         <script>console.log("Wrong call of patron_searh_js - missing redirect_url");</script>
     [% END %]
     <script>
-        let categories = [% To.json(categories) | $raw %];
-        let categories_map = categories.reduce((map, c) => {
-            map[c.categorycode] = c;
+        let categories = [% To.json(categories) | $raw %].map(e => {
+            e['_id'] = e.categorycode;
+            e['_str'] = e.description;
+            return e;
+        });
+        let categories_map = categories.reduce((map, e) => {
+            map[e._id] = e;
             return map;
         }, {});
-        let libraries  = [% To.json(libraries) | $raw %];
-        let libraries_map = libraries.reduce((map, l) => {
-            map[l.branchcode] = l;
+        let libraries  = [% To.json(libraries) | $raw %].map(e => {
+            e['_id'] = e.branchcode;
+            e['_str'] = e.branchname;
+            return e;
+        });
+        let libraries_map = libraries.reduce((map, e) => {
+            map[e._id] = e;
             return map;
         }, {});
 
diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js
index 5d00d3cd16..d5f100555b 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js
@@ -811,34 +811,18 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) {
                     if ( $(this).data('filter') ) {
                         input_type = 'select'
                         let filter_type = $(this).data('filter');
-                        if ( filter_type == 'libraries' ) {
-                            var existing_search = table_dt.column(i).search();
-                            let select = $('<select><option value=""></option></select');
-
-                            $(libraries).each(function(){
-                                let o = $('<option value="%s">%s</option>'.format(this.branchcode, this.branchname));
-                                if ( existing_search == this.branchcode ) {
-                                    o.prop("selected", "selected");
-                                }
-                                o.appendTo(select);
-                            });
-                            $(this).html( select );
-                        } else if ( filter_type == 'categories' ) {
-                            var existing_search = table_dt.column(i).search();
-                            let select = $('<select><option value=""></option></select');
-
-                            $(categories).each(function(){
-                                let o = $('<option value="%s">%s</option>'.format(this.categorycode, this.description));
-                                if ( existing_search == this.categorycode ) {
-                                    o.prop("selected", "selected");
-                                }
-                                o.appendTo(select);
-                            });
-                            $(this).html( select );
-                        } else {
-                            console.log("Unknown filter " + filter_type);
-                            return;
-                        }
+                        var existing_search = table_dt.column(i).search();
+                        let select = $('<select><option value=""></option></select');
+
+                        // FIXME eval here is bad and dangerous, how do we workaround that?
+                        $(eval(filter_type)).each(function(){
+                            let o = $('<option value="%s">%s</option>'.format(this._id, this._str));
+                            if ( existing_search == this._id ) {
+                                o.prop("selected", "selected");
+                            }
+                            o.appendTo(select);
+                        });
+                        $(this).html( select );
                     } else {
                         var title = $(this).text();
                         var existing_search = table_dt.column(i).search();
-- 
2.20.1