Added a very basic Tag Cloud

This commit is contained in:
Gergely POLONKAI 2012-10-06 13:23:24 +02:00
parent 9281d8dab1
commit dea174d090
2 changed files with 57 additions and 0 deletions

View File

@ -34,6 +34,13 @@
</div> </div>
<div id="content"> <div id="content">
<div id="content-padding"></div> <div id="content-padding"></div>
{% if tagCloud|length > 0 %}
<div id="tag-cloud">
{% for cloudItem in tagCloud %}
<a href="{{ path('GergelyPolonkaiFrontBundle_blogTagList', { name: cloudItem.name }) }}" class="size{{ cloudItem.size }}">{{ cloudItem.name }}</a>
{% endfor %}
</div>
{% endif %}
<div id="menu"> <div id="menu">
<ul> <ul>
<li><a href="{{ path('GergelyPolonkaiFrontBundle_about') }}">About me</a></li> <li><a href="{{ path('GergelyPolonkaiFrontBundle_about') }}">About me</a></li>

View File

@ -0,0 +1,50 @@
<?php
namespace GergelyPolonkai\FrontBundle\Twig;
use Symfony\Bridge\Doctrine\RegistryInterface;
use JMS\DiExtraBundle\Annotation as DI;
/**
* Description of tagCloud
*
* @author polesz
*
* @DI\Service
* @DI\Tag("twig.extension")
*/
class TagCloud extends \Twig_Extension
{
/**
* @var Symfony\Bridge\Doctrine\RegistryInterface $doctrine
*/
private $doctrine;
/**
* @DI\InjectParams()
*/
public function __construct(RegistryInterface $doctrine) {
$this->doctrine = $doctrine;
}
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');
$tagCloudList = $tagCloudQuery->getResult();
$tagCloud = array();
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);
}
return array(
'tagCloud' => $tagCloud,
);
}
public function getName() {
return 'gergelypolonkaifront_tagcloud';
}
}