From 1b1a0098ede7dca3b65576d8157519a9dad03c58 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Wed, 23 Feb 2022 15:21:04 +0100
Subject: [PATCH] Bug 30164: Fix datatable wrapper when no filter passed

This problem appears (at least) on the cities list view:
If you pass a filter in the "Search cities" filter in the header, it
won't be taken into account and all the cities will be displayed.

There are two problems. First we are passing an "empty" q=[] in the
body, the filter from the header is passed as URL parameters. We should
not need to pass the q if it's empty.
Then the main problem is coming from a bug in
Koha::REST::Plugin::Query but I didn't manage to track it down. If we
have two 2 passed, only the second one will be used. We are certainly
using a hash somewhere we should not.

This patch is fixing the bug but not on the correct side. A follow-up
bug should take care of the main problem at lower level.

Test plan:
Hit /admin/cities.pl
Create some cities
Use the filter in the header of the page and submit
=> Without this patch all cities are retrieved
=> With this patch applied only the relevant cities are displayed.
---
 koha-tmpl/intranet-tmpl/prog/js/datatables.js | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js
index c95a9723284..2d88fcdd052 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js
@@ -602,7 +602,10 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) {
                                     and_query_parameters.push(default_filters);
                                 }
                                 query_parameters = and_query_parameters;
-                                query_parameters.push(or_query_parameters);
+                                if ( or_query_parameters.length) {
+                                    query_parameters.push(or_query_parameters);
+                                }
+
                                 if(query_parameters.length) {
                                     query_parameters = JSON.stringify(query_parameters.length === 1?query_parameters[0]:{"-and": query_parameters});
                                     if(options.header_filter) {
-- 
2.25.1