252 lines
8.3 KiB
PHP
252 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace Deblan\Bundle\BlogBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Trinity\Bundle\ContentManagerBundle\Controller\PageController;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
|
use Deblan\Bundle\BlogBundle\Form\Type\UserCommentType;
|
|
use Deblan\Bundle\BlogBundle\Model\Comment;
|
|
use Deblan\Bundle\BlogBundle\Model\Post;
|
|
use Deblan\Bundle\BlogBundle\Model\PostFollower;
|
|
use Deblan\Bundle\BlogBundle\Model\PostFollowerPeer;
|
|
use Deblan\Component\Flash;
|
|
use Elastica\Query\QueryString;
|
|
use Elastica\Filter\Bool;
|
|
use Elastica\Query\Match;
|
|
|
|
class DefaultController extends PageController
|
|
{
|
|
/**
|
|
* @param $category
|
|
* @return \Symfony\Bundle\FrameworkBundle\Controller\Response
|
|
* @ParamConverter("category", class="Deblan\Bundle\BlogBundle\Model\Category", options={"mapping"={"id":"id"}})
|
|
*/
|
|
public function categoryAction($category)
|
|
{
|
|
$this->getPage()->setObject($category);
|
|
$this->initPager();
|
|
|
|
if ($this->getRequest()->query->get('format') === 'rss') {
|
|
$this->getPage()->setTemplate('DeblanBlogBundle:Default:postRss.html.twig');
|
|
}
|
|
|
|
return $this->defaultRender();
|
|
}
|
|
|
|
/**
|
|
* @param $tag
|
|
*/
|
|
public function tagAction($tag)
|
|
{
|
|
$this->getPage()->setObject($tag);
|
|
$this->initPager();
|
|
|
|
if ($this->getRequest()->query->get('format') === 'rss') {
|
|
$this->getPage()->setTemplate('DeblanBlogBundle:Default:postRss.html.twig');
|
|
}
|
|
|
|
return $this->defaultRender();
|
|
}
|
|
|
|
/**
|
|
* @param $query
|
|
*/
|
|
public function searchEngineAction($page)
|
|
{
|
|
$query = $this->getRequest()->query->get('query');
|
|
// $finder = $this->get('fos_elastica.finder.deblan.post');
|
|
|
|
// $boolQuery = new Bool();
|
|
|
|
// $fieldQuery = new QueryString();
|
|
// $fieldQuery->setParam('query', $query);
|
|
|
|
// $fieldActive = new Match();
|
|
// $fieldActive->setField('active', '1');
|
|
|
|
// $boolQuery->addShould($fieldQuery);
|
|
// $boolQuery->addMust($fieldActive);
|
|
|
|
// $pager = $finder->findPaginated($fieldQuery);
|
|
// $pager->setMaxPerPage(5);
|
|
// $pager->setCurrentPage($page);
|
|
|
|
if (empty($query)) {
|
|
$query = $this->getRequest()->query->get('query');
|
|
}
|
|
|
|
$this->getPage()
|
|
->setObject(explode(' ', $query));
|
|
|
|
$this->initPager();
|
|
|
|
if ($this->getRequest()->query->get('format') === 'rss') {
|
|
$this->getPage()->setTemplate('DeblanBlogBundle:Default:postRss.html.twig');
|
|
}
|
|
|
|
return $this->defaultRender();
|
|
}
|
|
|
|
/**
|
|
* @param $post
|
|
* @return \Symfony\Bundle\FrameworkBundle\Controller\Response
|
|
* @ParamConverter("post", class="Deblan\Bundle\BlogBundle\Model\Post", options={"mapping"={"id":"id"}})
|
|
*/
|
|
public function postAction($post)
|
|
{
|
|
$this->getPage()->setObject($post);
|
|
|
|
$trueUrl = $this->generateUrl(
|
|
'post',
|
|
array(
|
|
'post' => $post->getSlug(),
|
|
'id' => $post->getId()
|
|
),
|
|
true
|
|
);
|
|
|
|
if ($this->getRequest()->getUri() !== $trueUrl) {
|
|
if ($this->getNode()->getRouteName() !== 'hosting') {
|
|
return $this->redirect($trueUrl);
|
|
}
|
|
}
|
|
|
|
$form = $this->createForm(new UserCommentType(), new Comment());
|
|
|
|
if ($this->getRequest()->isMethod('post')) {
|
|
$form->handleRequest($this->getRequest());
|
|
|
|
if ($form->isValid()) {
|
|
$form->getData()
|
|
->setPost($post)
|
|
->setActive(true)
|
|
->save();
|
|
|
|
$messages = array('Ton commentaire a été posté, merci :)');
|
|
|
|
$this->sendNewCommentEmail($post, $form->getData());
|
|
|
|
$form_post = $this->getRequest()->request->get($form->getName());
|
|
|
|
if (!empty($form_post['Follow']) && $form->getData()->getEmail()) {
|
|
$follower = PostFollowerPeer::getSubscriptionIfExists($post, $form->getData());
|
|
|
|
if ($follower === null) {
|
|
$follower = new PostFollower();
|
|
|
|
$follower
|
|
->setPost($post)
|
|
->setComment($form->getData())
|
|
->setActive(false)
|
|
->setKey(md5($post->getId().$form->getData()->getEmail().mt_rand()))
|
|
->save();
|
|
}
|
|
|
|
if (!$follower->getActive()) {
|
|
$this->sendNewFollowerRequest($post, $follower);
|
|
$messages[] = 'Tu vas recevoir un email pour confirmer ton abonnement.';
|
|
} else {
|
|
$messages[] = 'BTW tu étais déjà abonné !';
|
|
}
|
|
} elseif (!empty($form_post['Follow']) && !$form->getData()->getEmail()) {
|
|
$messages[] = 'Cependant je n\'ai pas pu t\'abonner sans ton email.';
|
|
}
|
|
|
|
$this->getSessionUser()->setFlash('message', new Flash\FlashSuccess(implode(' ', $messages)), false);
|
|
|
|
return $this->redirect($this->getRequest()->getUri());
|
|
} else {
|
|
$this->getSessionUser()->setFlash('message', new Flash\FlashError('Arf, il y a des soucis dans le formulaire :('), false);
|
|
}
|
|
}
|
|
|
|
return $this->defaultRender(array(
|
|
'full' => true,
|
|
'form' => $form->createView(),
|
|
));
|
|
}
|
|
|
|
/**
|
|
* @param $postFollower
|
|
* @return \Symfony\Bundle\FrameworkBundle\Controller\Response
|
|
* @ParamConverter("postFollower", class="Deblan\Bundle\BlogBundle\Model\PostFollower", options={"mapping"={"postFollower":"key"}})
|
|
*/
|
|
public function subscriptionAction($postFollower, $active)
|
|
{
|
|
$postFollower->setActive((bool) $active)->save();
|
|
|
|
$this->getSessionUser()->setFlash(
|
|
'message',
|
|
new Flash\FlashSuccess($active ? 'Ton abonnement a bien été pris en compte, merci :)' : 'C\'est bon, plus aucune notification pour cet article !'),
|
|
false
|
|
);
|
|
|
|
return $this->redirect($this->generateUrl('homepage'));
|
|
}
|
|
|
|
protected function sendNewFollowerRequest(Post $post, PostFollower $follower)
|
|
{
|
|
$message = \Swift_Message::newInstance()
|
|
->setSubject('[Deblan] Souscription')
|
|
->setFrom('subscription@deblan.fr')
|
|
->setTo($follower->getComment()->getEmail())
|
|
->setBody(
|
|
$this->renderView('DeblanBlogBundle:Mail:followerNewSubscription.html.twig',
|
|
array(
|
|
'post' => $post,
|
|
'follower' => $follower,
|
|
)
|
|
),
|
|
'text/html'
|
|
);
|
|
|
|
$this->get('mailer')->send($message);
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function sendNewCommentEmail(Post $post, Comment $comment)
|
|
{
|
|
$message = \Swift_Message::newInstance()
|
|
->setSubject('[Deblan] Nouveau commentaire')
|
|
->setFrom('subscription@deblan.fr')
|
|
->setTo('simon@deblan.fr')
|
|
->setBody(
|
|
$this->renderView('DeblanBlogBundle:Mail:new_comment.html.twig',
|
|
array(
|
|
'post' => $post,
|
|
'comment' => $comment,
|
|
)
|
|
),
|
|
'text/html'
|
|
);
|
|
|
|
$this->get('mailer')->send($message);
|
|
|
|
foreach ($post->getActiveFollowers() as $follower) {
|
|
if ($follower->getComment()->getEmail() === $comment->getEmail()) {
|
|
continue;
|
|
}
|
|
|
|
$message = \Swift_Message::newInstance()
|
|
->setSubject('[Deblan] Nouveau commentaire')
|
|
->setFrom('subscription@deblan.fr')
|
|
->setTo($follower->getComment()->getEmail())
|
|
->setBody(
|
|
$this->renderView('DeblanBlogBundle:Mail:followerNewComment.html.twig',
|
|
array(
|
|
'post' => $post,
|
|
'follower' => $follower,
|
|
)
|
|
),
|
|
'text/html'
|
|
);
|
|
|
|
$this->get('mailer')->send($message);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
}
|