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,