From f4d0230166af810aeb870f6eabd4355c095cdc78 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 10 Mar 2014 00:13:10 +0100 Subject: [PATCH] Refactor to tag autocompletion --- lib/chibi-sql | 2 +- public_html/media/js/core.js | 13 ++++------ src/Controllers/TagController.php | 25 ++++++++++++++++++- .../SearchServices/TagSearchService.php | 14 +++++------ 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/lib/chibi-sql b/lib/chibi-sql index a5d7a039..9b27422f 160000 --- a/lib/chibi-sql +++ b/lib/chibi-sql @@ -1 +1 @@ -Subproject commit a5d7a03965e7089c070defa5907798a1df66c847 +Subproject commit 9b27422f780840f9814d8fc49017716cf6e7fea6 diff --git a/public_html/media/js/core.js b/public_html/media/js/core.js index 0bf79348..b389dc88 100644 --- a/public_html/media/js/core.js +++ b/public_html/media/js/core.js @@ -199,15 +199,10 @@ function split(val) return val.split(/\s+/); } -function extractLast(term) -{ - return split(term).pop(); -} - function retrieveTags(searchTerm, cb) { - var options = { filter: searchTerm + ' order:popularity,desc' }; - $.getJSON('/tags?json', options, function(data) + var options = { search: searchTerm }; + $.getJSON('/tags-autocomplete?json', options, function(data) { var tags = $.map(data.tags.slice(0, 15), function(tag) { @@ -232,7 +227,8 @@ $(function() minLength: 1, source: function(request, response) { - var term = extractLast(request.term); + var terms = split(request.term); + var term = terms.pop(); if (term != '') retrieveTags(term, response); }, @@ -283,6 +279,7 @@ function attachTagIt(element) function(request, response) { var tagit = this; + //var context = tagit.element.tagit('assignedTags'); retrieveTags(request.term.toLowerCase(), function(tags) { if (!tagit.options.allowDuplicates) diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index 5353b5d9..7f54629b 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -14,7 +14,7 @@ class TagController $this->context->viewName = 'tag-list-wrapper'; PrivilegesHelper::confirmWithException(Privilege::ListTags); - $suppliedFilter = $filter ?: InputHelper::get('filter') ?: 'order:alpha,asc'; + $suppliedFilter = $filter ?: 'order:alpha,asc'; $page = max(1, intval($page)); $tagsPerPage = intval($this->config->browsing->tagsPerPage); @@ -43,6 +43,29 @@ class TagController } } + /** + * @route /tags-autocomplete + */ + public function autoCompleteAction() + { + PrivilegesHelper::confirmWithException(Privilege::ListTags); + + $suppliedSearch = InputHelper::get('search'); + + $filter = $suppliedSearch . ' order:popularity,desc'; + $tags = TagSearchService::getEntitiesRows($filter, 15, 1); + + $this->context->transport->tags = + array_values(array_map( + function($tag) + { + return [ + 'name' => $tag['name'], + 'count' => $tag['post_count'] + ]; + }, $tags)); + } + /** * @route /tag/merge */ diff --git a/src/Models/SearchServices/TagSearchService.php b/src/Models/SearchServices/TagSearchService.php index 8f2f6a7d..05b10510 100644 --- a/src/Models/SearchServices/TagSearchService.php +++ b/src/Models/SearchServices/TagSearchService.php @@ -11,13 +11,13 @@ class TagSearchService extends AbstractSearchService public static function getMostUsedTag() { - $stmt = new Sql\SelectStatement(); - $stmt->setTable('post_tag'); - $stmt->addColumn('tag_id'); - $stmt->addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count')); - $stmt->setGroupBy('post_tag.tag_id'); - $stmt->setOrderBy('post_count', Sql\SelectStatement::ORDER_DESC); - $stmt->setLimit(1, 0); + $stmt = (new Sql\SelectStatement) + ->setTable('post_tag') + ->addColumn('tag_id') + ->addColumn(new Sql\AliasFunctor(new Sql\CountFunctor('post_tag.post_id'), 'post_count')) + ->setGroupBy('post_tag.tag_id') + ->setOrderBy('post_count', Sql\SelectStatement::ORDER_DESC) + ->setLimit(1, 0); return Database::fetchOne($stmt); } }