From f1a731c859a3b37036a7a9f6530484d0df5b74c7 Mon Sep 17 00:00:00 2001
From: Viktor Pedersen <viktor@dsv.su.se>
Date: Thu, 9 Sep 2021 11:26:15 +0200
Subject: [PATCH] bug searching in search function

---
 include/Product.php   | 8 +++-----
 include/functions.php | 9 ++++++---
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/include/Product.php b/include/Product.php
index 6451afa..a3a98bc 100644
--- a/include/Product.php
+++ b/include/Product.php
@@ -143,11 +143,9 @@ class Product extends Entity {
                 }
             } else if($field == 'tag') {
                 foreach($this->get_tags() as $tag) {
-                    if(match($values, $tag)) {
-                        if(!array_key_exists('tags', $matches)) {
-                            $matches['tags'] = array();
-                        }
-                        $matches['tags'][] = $tag;
+                    $match = match($values, $tag, $matchAll);
+                    if($match) {
+                        $matches['tags'] = $tag;
                         break;
                     } else {
                         if($matchAll) {
diff --git a/include/functions.php b/include/functions.php
index 099b4bb..1f16f6f 100644
--- a/include/functions.php
+++ b/include/functions.php
@@ -253,16 +253,19 @@ function suggest_content($fieldname) {
     return $out;
 }
 
-function match($searchterms, $subject) {
+function match($searchterms, $subject, $matchAll=false) {
+    $out = array();
     if(!is_array($searchterms)) {
         $searchterms = array($searchterms);
     }
     foreach($searchterms as $term) {
         if(fnmatch('*'.$term.'*', $subject, FNM_CASEFOLD)) {
-            return true;
+            $out[] = $term;
+        } else if($matchAll) {
+            return array();
         }
     }
-    return false;
+    return $out;
 }
 
 function format_date($date) {