From 9f5bdc3da0ee49b9a484c6145f80ce23890f1bc9 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 21 Oct 2013 15:07:27 +0200 Subject: [PATCH] A bit more reasonable autocomplete --- public_html/media/js/core.js | 2 +- src/Controllers/TagController.php | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/public_html/media/js/core.js b/public_html/media/js/core.js index 98c0179d..f7a8f4d0 100644 --- a/public_html/media/js/core.js +++ b/public_html/media/js/core.js @@ -154,7 +154,7 @@ $(function() event.preventDefault(); } }).autocomplete({ - minLength: 0, + minLength: 1, source: function(request, response) { var term = extractLast(request.term); diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index 63e999dd..14a339d2 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -10,25 +10,26 @@ class TagController $this->context->subTitle = 'tags'; PrivilegesHelper::confirmWithException(Privilege::ListTags); + $suppliedFilter = InputHelper::get('filter'); $dbQuery = R::$f->begin(); $dbQuery->select('tag.*, COUNT(1) AS count'); $dbQuery->from('tag'); $dbQuery->innerJoin('post_tag'); $dbQuery->on('tag.id = post_tag.tag_id'); - $dbQuery->groupBy('tag.id'); - $dbQuery->orderBy('LOWER(tag.name)')->asc(); - $rows = $dbQuery->get(); - $tags = R::convertToBeans('tag', $rows); - - $suppliedFilter = InputHelper::get('filter'); if ($suppliedFilter) { - $rows = array_filter($rows, function($row) use ($suppliedFilter) - { - return strpos(strtolower($row['name']), strtolower($suppliedFilter)) !== false; - }); + if (strlen($suppliedFilter) >= 3) + $suppliedFilter = '%' . $suppliedFilter; + $suppliedFilter .= '%'; + $dbQuery->where('LOWER(tag.name) LIKE LOWER(?)')->put($suppliedFilter); } + $dbQuery->groupBy('tag.id'); + $dbQuery->orderBy('LOWER(tag.name)')->asc(); + if ($suppliedFilter) + $dbQuery->limit(15); + $rows = $dbQuery->get(); + $tags = R::convertToBeans('tag', $rows); $tags = []; $tagDistribution = [];