From 657d6977b43a3a2b63ee988d40eb30a8e6ef6d96 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Wed, 18 Nov 2020 21:29:51 +0100
Subject: [PATCH] FIX #15404 TIME 0:45 filter on subentites does not work on
 undefined entities + customer repo

---
 .../controllers/ResourceListController.php    | 37 ++++++++++++++-----
 1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/src/app/resource/controllers/ResourceListController.php b/src/app/resource/controllers/ResourceListController.php
index d196e55ca77..a646b0c50e7 100644
--- a/src/app/resource/controllers/ResourceListController.php
+++ b/src/app/resource/controllers/ResourceListController.php
@@ -284,16 +284,33 @@ class ResourceListController
                 $queryData[] = explode(',', $replace);
             }
         }
-        if (!empty($args['data']['entitiesChildren'])) {
-            $entities = explode(',', $args['data']['entitiesChildren']);
-            $entitiesChildren = [];
-            foreach ($entities as $entity) {
-                $children = EntityModel::getEntityChildren(['entityId' => $entity]);
-                $entitiesChildren = array_merge($entitiesChildren, $children);
-            }
-            if (!empty($entitiesChildren)) {
-                $where[] = 'destination in (?)';
-                $queryData[] = $entitiesChildren;
+        if (isset($args['data']['entitiesChildren'])) {
+            if (empty($args['data']['entitiesChildren'])) {
+                $where[] = 'destination is null';
+            } else {
+                $entities = explode(',', $args['data']['entitiesChildren']);
+                $entitiesChildren = [];
+                foreach ($entities as $entity) {
+                    if (!empty($entity)) {
+                        $children = EntityModel::getEntityChildren(['entityId' => $entity]);
+                        $entitiesChildren = array_merge($entitiesChildren, $children);
+                    }
+                }
+    
+                $tmpWhere = [];
+    
+                $replace = preg_replace('/(^,)|(,$)/', '', $args['data']['entitiesChildren']);
+                $replace = preg_replace('/(,,)/', ',', $replace);
+                if ($replace != $args['data']['entitiesChildren']) {
+                    $tmpWhere[] = 'destination is null';
+                }
+                if (!empty($entitiesChildren)) {
+                    $tmpWhere[] = 'destination in (?)';
+                    $queryData[] = $entitiesChildren;
+                }
+                if (!empty($tmpWhere)) {
+                    $where[] = '(' . implode(' or ', $tmpWhere) . ')';
+                }
             }
         }
         if (!empty($args['data']['doctypes'])) {
-- 
GitLab