Made tag cloud look better

This commit is contained in:
Gergely POLONKAI 2012-10-06 15:44:11 +02:00
parent 77b251fadf
commit 0d49fd358c

View File

@ -22,22 +22,34 @@ class TagCloud extends \Twig_Extension
/** /**
* @DI\InjectParams() * @DI\InjectParams()
*/ */
public function __construct(RegistryInterface $doctrine) { public function __construct(RegistryInterface $doctrine)
{
$this->doctrine = $doctrine; $this->doctrine = $doctrine;
} }
protected function orderTags($a, $b)
{
return strcasecmp($a['name'], $b['name']);
}
public function getGlobals() 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(); $tagCloudList = $tagCloudQuery->getResult();
$tagCloud = array(); $tagCloud = array();
if (count($tagCloudList) > 0) {
$tMax = $tagCloudList[0]['ct'];
$tMin = 1;
}
foreach ($tagCloudList as $cloudElement) { foreach ($tagCloudList as $cloudElement) {
$tag = $cloudElement[0]; $tag = $cloudElement[0];
$tagCount = $cloudElement['ct']; $tagCount = $cloudElement['ct'];
$size = ($tagCount == 0) ? 0 : floor(log($tagCount, 10)); if ($tagCount >= $tMin) {
if ($size > 5) $size = 5; $size = floor((5.0 * ($tagCount - $tMin)) / ($tMax - $tMin));
$tagCloud[] = array('name' => $tag->getName(), 'size' => $size); $tagCloud[] = array('name' => $tag->getName(), 'size' => $size);
} }
}
usort($tagCloud, array($this, 'orderTags'));
return array( return array(
'tagCloud' => $tagCloud, 'tagCloud' => $tagCloud,