From 0d49fd358c53360c1e1575d6bbd2863f60073146 Mon Sep 17 00:00:00 2001 From: Gergely POLONKAI Date: Sat, 6 Oct 2012 15:44:11 +0200 Subject: [PATCH] Made tag cloud look better --- .../FrontBundle/Twig/TagCloud.php | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/GergelyPolonkai/FrontBundle/Twig/TagCloud.php b/src/GergelyPolonkai/FrontBundle/Twig/TagCloud.php index b002ae3..91b95db 100644 --- a/src/GergelyPolonkai/FrontBundle/Twig/TagCloud.php +++ b/src/GergelyPolonkai/FrontBundle/Twig/TagCloud.php @@ -22,22 +22,34 @@ class TagCloud extends \Twig_Extension /** * @DI\InjectParams() */ - public function __construct(RegistryInterface $doctrine) { + public function __construct(RegistryInterface $doctrine) + { $this->doctrine = $doctrine; } + protected function orderTags($a, $b) + { + return strcasecmp($a['name'], $b['name']); + } + public function getGlobals() { - $tagCloudQuery = $this->doctrine->getEntityManager()->createQuery('SELECT t, count(tg) ct FROM GergelyPolonkaiFrontBundle:Tag t LEFT JOIN t.tagging tg GROUP BY t.id ORDER BY t.name'); + $tagCloudQuery = $this->doctrine->getEntityManager()->createQuery('SELECT t, count(tg) ct FROM GergelyPolonkaiFrontBundle:Tag t LEFT JOIN t.tagging tg GROUP BY t.id ORDER BY ct DESC'); $tagCloudList = $tagCloudQuery->getResult(); $tagCloud = array(); + if (count($tagCloudList) > 0) { + $tMax = $tagCloudList[0]['ct']; + $tMin = 1; + } foreach ($tagCloudList as $cloudElement) { $tag = $cloudElement[0]; $tagCount = $cloudElement['ct']; - $size = ($tagCount == 0) ? 0 : floor(log($tagCount, 10)); - if ($size > 5) $size = 5; - $tagCloud[] = array('name' => $tag->getName(), 'size' => $size); + if ($tagCount >= $tMin) { + $size = floor((5.0 * ($tagCount - $tMin)) / ($tMax - $tMin)); + $tagCloud[] = array('name' => $tag->getName(), 'size' => $size); + } } + usort($tagCloud, array($this, 'orderTags')); return array( 'tagCloud' => $tagCloud,