Added forum topic favourite function

Solves issue #12

Signed-off-by: Gergely Polonkai (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Gergely Polonkai (W00d5t0ck) 2012-08-23 19:06:26 +02:00
parent 4d8ae5c0f0
commit 6bdb9530a3
5 changed files with 223 additions and 1 deletions

View File

@ -0,0 +1,30 @@
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration,
Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your need!
*/
class Version20120823160202 extends AbstractMigration
{
public function up(Schema $schema)
{
// this up() migration is autogenerated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
$this->addSql("CREATE TABLE user_favourite_forum_topics (user_id INT NOT NULL, forum_topic_id INT NOT NULL, INDEX IDX_917D04B0A76ED395 (user_id), INDEX IDX_917D04B038A6ADDA (forum_topic_id), PRIMARY KEY(user_id, forum_topic_id)) ENGINE = InnoDB");
$this->addSql("ALTER TABLE user_favourite_forum_topics ADD CONSTRAINT FK_917D04B0A76ED395 FOREIGN KEY (user_id) REFERENCES user_data (user_id)");
$this->addSql("ALTER TABLE user_favourite_forum_topics ADD CONSTRAINT FK_917D04B038A6ADDA FOREIGN KEY (forum_topic_id) REFERENCES forum_topics (id)");
}
public function down(Schema $schema)
{
// this down() migration is autogenerated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
$this->addSql("DROP TABLE user_favourite_forum_topics");
}
}

View File

@ -4,12 +4,15 @@ namespace KekRozsak\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response;
use KekRozsak\FrontBundle\Entity\ForumTopicGroup; use KekRozsak\FrontBundle\Entity\ForumTopicGroup;
use KekRozsak\FrontBundle\Entity\ForumTopic; use KekRozsak\FrontBundle\Entity\ForumTopic;
use KekRozsak\FrontBundle\Entity\ForumPost; use KekRozsak\FrontBundle\Entity\ForumPost;
use KekRozsak\FrontBundle\Entity\UserData;
use KekRozsak\FrontBundle\Form\Type\ForumTopicGroupType; use KekRozsak\FrontBundle\Form\Type\ForumTopicGroupType;
use KekRozsak\FrontBundle\Form\Type\ForumTopicType; use KekRozsak\FrontBundle\Form\Type\ForumTopicType;
use KekRozsak\FrontBundle\Form\Type\ForumPostType; use KekRozsak\FrontBundle\Form\Type\ForumPostType;
@ -168,4 +171,56 @@ class ForumController extends Controller
'form' => $form->createView(), 'form' => $form->createView(),
); );
} }
/**
* @Route("/{topicGroupSlug}/{topicSlug}/kedvenc-be", name="KekRozsakFrontBundle_forumFavouriteTopic", options={"expose": true})
* @Method("GET")
* @ParamConverter("topic", options={"mapping"={"topicGroup"="topicGroup", "topicSlug"="slug"}})
* @ParamConverter("topicGroup", options={"mapping"={"topicGroupSlug"="slug"}})
*
* @param KekRozsak\FrontBundle\Entity\ForumTopicGroup $topicGroup
* @param KekRozsak\FrontBundle\Entity\ForumTopic $topic
* @return array
*/
public function favouriteTopic(ForumTopicGroup $topicGroup, ForumTopic $topic)
{
$user = $this->get('security.context')->getToken()->getUser();
if (($userData = $user->getUserData()) === null) {
$userData = new UserData();
$userData->setUser($user);
}
$userData->addFavouriteTopic($topic);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($userData);
$em->flush();
return new Response();
}
/**
* @Route("/{topicGroupSlug}/{topicSlug}/kedvenc-ki", name="KekRozsakFrontBundle_forumUnfavouriteTopic", options={"expose": true})
* @Method("GET")
* @ParamConverter("topic", options={"mapping"={"topicGroup"="topicGroup", "topicSlug"="slug"}})
* @ParamConverter("topicGroup", options={"mapping"={"topicGroupSlug"="slug"}})
*
* @param KekRozsak\FrontBundle\Entity\ForumTopicGroup $topicGroup
* @param KekRozsak\FrontBundle\Entity\ForumTopic $topic
* @return array
*/
public function unfavouriteTopic(ForumTopicGroup $topicGroup, ForumTopic $topic)
{
$user = $this->get('security.context')->getToken()->getUser();
if (($userData = $user->getUserData()) === null) {
$userData = new UserData();
$userData->setUser($user);
}
$userData->removeFavouriteTopic($topic);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($userData);
$em->flush();
return new Response();
}
} }

View File

@ -3,6 +3,7 @@
namespace KekRozsak\FrontBundle\Entity; namespace KekRozsak\FrontBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use KekRozsak\SecurityBundle\Entity\User; use KekRozsak\SecurityBundle\Entity\User;
@ -21,6 +22,7 @@ class UserData
$this->googleTalkPublic = false; $this->googleTalkPublic = false;
$this->skypePublic = false; $this->skypePublic = false;
$this->phoneNumberPublic = false; $this->phoneNumberPublic = false;
$this->favouriteTopics = new ArrayCollection();
} }
/** /**
@ -452,4 +454,66 @@ class UserData
{ {
return $this->phoneNumberPublic; return $this->phoneNumberPublic;
} }
/**
* ArrayCollection of the User's favourite ForumTopics
*
* @var Doctrine\Common\Collections\ArrayCollection $favouriteTopics
*
* @ORM\ManyToMany(targetEntity="ForumTopic", fetch="LAZY")
* @ORM\JoinTable(name="user_favourite_forum_topics", joinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* }, inverseJoinColumns={
* @ORM\JoinColumn(name="forum_topic_id")
* }
* )
*/
protected $favouriteTopics;
/**
* Add a favourite ForumTopic
*
* @param KekRozsak\FrontBundle\Entity\ForumTopic $topic
* @return UserData
*/
public function addFavouriteTopic(ForumTopic $topic)
{
// TODO: Check if not null
$this->favouriteTopics->add($topic);
return $this;
}
/**
* Remove a favourite ForumTopic
*
* @param KekRozsak\FrontBundle\Entity\ForumTopic $topic
* @return UserData
*/
public function removeFavouriteTopic(ForumTopic $topic)
{
// TODO: Check if not null
$this->favouriteTopics->removeElement($topic);
return $this;
}
/**
* Get favourite ForumTopics
*
* @return Doctrine\Common\Collections\ArrayCollection
*/
public function getFavouriteTopics()
{
return $this->favouriteTopics;
}
/**
* Check if given ForumTopic is favourited by User
*
* @param KekRozsak\FrontBundle\Entity\ForumTopic $topic
* @return boolean
*/
public function isFavouriteForumTopic(ForumTopic $topic)
{
return $this->favouriteTopics->contains($topic);
}
} }

View File

@ -17,6 +17,11 @@
<dd><a href="{{ path('KekRozsakFrontBundle_groupList') }}">További csoportok</a></dd> <dd><a href="{{ path('KekRozsakFrontBundle_groupList') }}">További csoportok</a></dd>
<dt>Kedvenc Fórum-témáim</dt> <dt>Kedvenc Fórum-témáim</dt>
{% if app.user.userData %}
{% for topic in app.user.userData.favouriteTopics %}
<dd><a href="{{ path('KekRozsakFrontBundle_forumPostList', {topicGroupSlug: topic.topicGroup.slug, topicSlug: topic.slug}) }}">{{ topic.title }}</a></dd>
{% endfor %}
{% endif %}
<dd><a href="{{ path('KekRozsakFrontBundle_forumTopicGroupList') }}">Fórum</a></dd> <dd><a href="{{ path('KekRozsakFrontBundle_forumTopicGroupList') }}">Fórum</a></dd>
<dt>Üzenetek</dt> <dt>Üzenetek</dt>

View File

@ -18,7 +18,7 @@
<table class="forum-lista"> <table class="forum-lista">
<thead> <thead>
<tr> <tr>
<td></td> <td colspan="2"></td>
<td>Hozzászólások száma</td> <td>Hozzászólások száma</td>
<td>Utolsó hozzászólás</td> <td>Utolsó hozzászólás</td>
</tr> </tr>
@ -26,6 +26,13 @@
<tbody> <tbody>
{% for topic in topicGroup.topics %} {% for topic in topicGroup.topics %}
<tr class="{{ cycle(['odd', 'even'], loop.index0) }}"> <tr class="{{ cycle(['odd', 'even'], loop.index0) }}">
<td>
{% if app.user.userData and app.user.userData.isFavouriteForumTopic(topic) %}
<span class="gomb unfavourite-topic-button" id="unfavourite-topic-button-{{ topic.slug }}">[Kedvenc ikon]</span>
{% else %}
<span class="gomb favourite-topic-button" id="favourite-topic-button-{{ topic.slug }}">[Nem kedvenc ikon]</span>
{% endif %}
</td>
<td><a href="{{ path('KekRozsakFrontBundle_forumPostList', { topicGroupSlug: topicGroup.slug, topicSlug: topic.slug }) }}">{{ topic.title }}</a></td> <td><a href="{{ path('KekRozsakFrontBundle_forumPostList', { topicGroupSlug: topicGroup.slug, topicSlug: topic.slug }) }}">{{ topic.title }}</a></td>
<td></td> <td></td>
<td> <td>
@ -44,3 +51,64 @@
Ebben a témakörben nincsenek témák. Ebben a témakörben nincsenek témák.
{% endif %} {% endif %}
{% endblock content %} {% endblock content %}
{% block bottomscripts %}
<script type="text/javascript">
{# TODO: The following two functions should also update the top-left profile
# box
#}
function favouriteOn()
{
var self = $(this)
var topicSlug = self.attr('id').replace(/^favourite-topic-button-/, '');
url = Routing.generate('KekRozsakFrontBundle_forumFavouriteTopic', {
topicGroupSlug: '{{ topicGroup.slug }}',
topicSlug: topicSlug
});
$.ajax({
type: 'GET',
url: url
}).done(function() {
self.html('[Kedvenc ikon]');
self.removeClass('favourite-topic-button');
self.addClass('unfavourite-topic-button');
self.attr('id', 'unfavourite-topic-button-' + topicSlug);
self.off('click.updateFav');
self.on('click.updateFav', favouriteOff);
}).error(function() {
alert('Nem siker!');
});
}
function favouriteOff()
{
var self = $(this)
var topicSlug = self.attr('id').replace(/^unfavourite-topic-button-/, '');
url = Routing.generate('KekRozsakFrontBundle_forumUnfavouriteTopic', {
topicGroupSlug: '{{ topicGroup.slug }}',
topicSlug: topicSlug
});
$.ajax({
type: 'GET',
url: url
}).done(function() {
self.html('[Nem kedvenc ikon]');
self.removeClass('unfavourite-topic-button');
self.addClass('favourite-topic-button');
self.attr('id', 'favourite-topic-button-' + topicSlug);
self.off('click.updateFav');
self.on('click.updateFav', favouriteOn);
}).error(function() {
alert('Nem siker!');
});
}
function setupFavButtons()
{
$('.favourite-topic-button').on('click.updateFav', favouriteOn);
$('.unfavourite-topic-button').on('click.updateFav', favouriteOff);
}
$(document).ready(setupFavButtons);
</script>
{% endblock bottomscripts %}