From 98d07e19428793452342e75b6d65e0c85b9a7076 Mon Sep 17 00:00:00 2001 From: Simon Davie Date: Fri, 22 Jan 2016 17:55:13 +0000 Subject: [PATCH] [Tags Plugin] Enable sorting & beautify with ucfirst() Enable sorting of the tags shown when using the tags plugin. Options for sorting are "Alphabetical", "Count" and "Date". Date is the default as it is the current way of sorting. Also, use PHP function ucfirst to beautify the tags list. Translations added for en_US only. --- bl-plugins/tags/languages/en_US.json | 8 ++++-- bl-plugins/tags/plugin.php | 42 ++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/bl-plugins/tags/languages/en_US.json b/bl-plugins/tags/languages/en_US.json index d6a98ca0..f9bedee0 100644 --- a/bl-plugins/tags/languages/en_US.json +++ b/bl-plugins/tags/languages/en_US.json @@ -3,5 +3,9 @@ { "name": "Tags list", "description": "Shows all tags." - } -} \ No newline at end of file + }, + "tag-sort-order": "Sort the tag list by", + "tag-sort-alphabetical": "Alphabetical order", + "tag-sort-count": "Number of times each tag has been used", + "tag-sort-date": "Date each tag was first used" +} diff --git a/bl-plugins/tags/plugin.php b/bl-plugins/tags/plugin.php index f4742cb6..892707bc 100644 --- a/bl-plugins/tags/plugin.php +++ b/bl-plugins/tags/plugin.php @@ -5,7 +5,8 @@ class pluginTags extends Plugin { public function init() { $this->dbFields = array( - 'label'=>'Tags' + 'label'=>'Tags', + 'sort'=>'date' ); } @@ -18,6 +19,19 @@ class pluginTags extends Plugin { $html .= ''; $html .= ''; + $html .= '
'; + $html .= $Language->get('tag-sort-order').': '; + $html .= '
'; + return $html; } @@ -37,16 +51,32 @@ class pluginTags extends Plugin { foreach($db as $tagKey=>$fields) { - $count = $dbTags->countPostsByTag($tagKey); - - // Print the parent - $html .= '
  • '.$fields['name'].' ('.$count.')
  • '; + $tagArray[] = array('tagKey'=>$tagKey, 'count'=>$dbTags->countPostsByTag($tagKey), 'name'=>ucfirst($fields['name'])); } + // Sort the array based on options + if ($this->getDbField('sort') == "count") + { + usort($tagArray, function($a, $b) { + return $b['count'] - $a['count']; + }); + } + elseif ($this->getDbField('sort') == "alpha") + { + usort($tagArray, function($a, $b) { + return strcmp($a['tagKey'], $b['tagKey']); + }); + } + + foreach($tagArray as $tagKey=>$fields) + { + // Print the parent + $html .= '
  • '.$fields['name'].' ('.$fields['count'].')
  • '; + } $html .= ''; $html .= ''; $html .= ''; return $html; } -} \ No newline at end of file +}