From 9896106f1cebe78d81cd40aa93da24f54282e4db Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 9 Oct 2013 17:50:07 +0200 Subject: [PATCH] Added tag list; upload: tags autocompletion --- config.ini | 1 + public_html/media/js/upload.js | 17 ++++++++++++++--- src/Controllers/TagController.php | 28 ++++++++++++++++++++++++++-- src/Models/Privilege.php | 1 + src/Views/tag-list.phtml | 13 +++++++++++++ 5 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 src/Views/tag-list.phtml diff --git a/config.ini b/config.ini index bd754ac5..2a72a763 100644 --- a/config.ini +++ b/config.ini @@ -46,3 +46,4 @@ listPosts.unsafe=registered listUsers=registered listComments=registered retrievePost=anonymous +listTags=anonymous diff --git a/public_html/media/js/upload.js b/public_html/media/js/upload.js index 23643cb7..54f1f301 100644 --- a/public_html/media/js/upload.js +++ b/public_html/media/js/upload.js @@ -1,8 +1,12 @@ $(function() { - var handler = $('#file-handler'); - var tags = []; //todo: retrieve tags + var tags = []; + $.getJSON('/tags?json', function(data) + { + tags = data['tags']; + }); + var handler = $('#file-handler'); handler.on('dragenter', function(e) { $(this).addClass('active'); @@ -146,9 +150,16 @@ $(function() postDom.removeAttr('id'); postDom.data('file', file); $('.file-name strong', postDom).text(file.name); - $('.tags input', postDom).tagit({caseSensitive: true, availableTags: tags, placeholderText: $('.tags input').attr('placeholder')}); $('.posts').append(postDom); + postDom.show(); + var tagItOptions = + { caseSensitive: true, + availableTags: tags, + placeholderText: $('.tags input').attr('placeholder') + }; + $('.tags input', postDom).tagit(tagItOptions); + if (!file.type.match('image.*')) { continue; diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index 4072d010..4940c76f 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -4,9 +4,33 @@ class TagController /** * @route /tags */ - public static function listAction() + public function listAction() { $this->context->subTitle = 'tags'; - throw new Exception('Not implemented'); + + PrivilegesHelper::confirmWithException($this->context->user, Privilege::ListTags); + + $dbQuery = R::$f->begin(); + $dbQuery->select('tag.name, COUNT(1) AS count'); + $dbQuery->from('tag'); + $dbQuery->innerJoin('post_tag'); + $dbQuery->on('tag.id = post_tag.tag_id'); + $dbQuery->groupBy('tag.id'); + $rows = $dbQuery->get(); + + $tags = []; + $tagDistribution = []; + foreach ($rows as $row) + { + $tags []= $row['name']; + $tagDistribution[$row['name']] = $row['count']; + } + array_multisort( + array_values($tagDistribution), SORT_DESC, SORT_NUMERIC, + array_keys($tagDistribution), SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE, + $tagDistribution); + + $this->context->transport->tags = $tags; + $this->context->transport->tagDistribution = $tagDistribution; } } diff --git a/src/Models/Privilege.php b/src/Models/Privilege.php index 01f615fc..fed7b6ac 100644 --- a/src/Models/Privilege.php +++ b/src/Models/Privilege.php @@ -7,4 +7,5 @@ class Privilege extends Enum const RetrievePost = 4; const ListUsers = 5; const ListComments = 6; + const ListTags = 7; } diff --git a/src/Views/tag-list.phtml b/src/Views/tag-list.phtml new file mode 100644 index 00000000..0190908f --- /dev/null +++ b/src/Views/tag-list.phtml @@ -0,0 +1,13 @@ +context->transport->errorMessage)): ?> +

context->transport->errorMessage ?>

+ + +