From 459144011e778c400a77d5d6e16638fc18e44f37 Mon Sep 17 00:00:00 2001 From: iglocska Date: Mon, 16 Nov 2020 16:17:47 +0100 Subject: [PATCH] fix: [event index] search via attribute key allows for empty input now --- app/Controller/EventsController.php | 80 +++++++++++++++-------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index ec571abc6..676e1640a 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -109,53 +109,55 @@ class EventsController extends AppController */ private function __filterOnAttributeValue($value) { - if (!is_array($value)) { - $pieces = explode('|', strtolower($value)); - } else { - $pieces = $value; - } // dissect the value $include = array(); $exclude = array(); - - foreach ($pieces as $piece) { - if ($piece[0] === '!') { - $exclude[] = '%' . substr($piece, 1) . '%'; + $includeIDs = []; + $excludeIDs = []; + if (!empty($value)) { + if (!is_array($value)) { + $pieces = explode('|', strtolower($value)); } else { - $include[] = "%$piece%"; - } - } - - $includeIDs = array(); - if (!empty($include)) { - $includeConditions = []; - foreach ($include as $i) { - $includeConditions['OR'][] = array('lower(Attribute.value1) LIKE' => $i); - $includeConditions['OR'][] = array('lower(Attribute.value2) LIKE' => $i); + $pieces = $value; } - $includeIDs = array_values($this->Event->Attribute->fetchAttributes($this->Auth->user(), array( - 'conditions' => $includeConditions, - 'flatten' => true, - 'event_ids' => true, - 'list' => true, - ))); - } - - $excludeIDs = array(); - if (!empty($exclude)) { - $excludeConditions = []; - foreach ($exclude as $e) { - $excludeConditions['OR'][] = array('lower(Attribute.value1) LIKE' => $e); - $excludeConditions['OR'][] = array('lower(Attribute.value2) LIKE' => $e); + foreach ($pieces as $piece) { + if ($piece[0] === '!') { + $exclude[] = '%' . substr($piece, 1) . '%'; + } else { + $include[] = "%$piece%"; + } } - $excludeIDs = array_values($this->Event->Attribute->fetchAttributes($this->Auth->user(), array( - 'conditions' => $excludeConditions, - 'flatten' => true, - 'event_ids' => true, - 'list' => true, - ))); + if (!empty($include)) { + $includeConditions = []; + foreach ($include as $i) { + $includeConditions['OR'][] = array('lower(Attribute.value1) LIKE' => $i); + $includeConditions['OR'][] = array('lower(Attribute.value2) LIKE' => $i); + } + + $includeIDs = array_values($this->Event->Attribute->fetchAttributes($this->Auth->user(), array( + 'conditions' => $includeConditions, + 'flatten' => true, + 'event_ids' => true, + 'list' => true, + ))); + } + + if (!empty($exclude)) { + $excludeConditions = []; + foreach ($exclude as $e) { + $excludeConditions['OR'][] = array('lower(Attribute.value1) LIKE' => $e); + $excludeConditions['OR'][] = array('lower(Attribute.value2) LIKE' => $e); + } + + $excludeIDs = array_values($this->Event->Attribute->fetchAttributes($this->Auth->user(), array( + 'conditions' => $excludeConditions, + 'flatten' => true, + 'event_ids' => true, + 'list' => true, + ))); + } } // return -1 as the only value in includedIDs if both arrays are empty. This will mean that no events will be shown if there was no hit if (empty($includeIDs) && empty($excludeIDs)) {