kekrozsak/src/KekRozsak/FrontBundle/Controller/DefaultController.php

199 lines
5.6 KiB
PHP
Raw Normal View History

<?php
namespace KekRozsak\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
2012-07-15 12:56:31 +00:00
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use KekRozsak\FrontBundle\Entity\UserGroupMembership;
2012-07-15 12:56:31 +00:00
use KekRozsak\FrontBundle\Entity\UserData;
2012-07-15 12:56:31 +00:00
use KekRozsak\SecurityBundle\Form\Type\UserType;
2012-07-09 15:29:37 +00:00
class DefaultController extends Controller
{
/**
* @Route("/", name="KekRozsakFrontBundle_homepage")
*/
public function homepageAction()
{
2012-07-15 12:56:31 +00:00
$mainPageArticle = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:Article')->findOneBy(array('mainPage' => true), true, array('createdAt', 'DESC'), 1);
if (!$mainPageArticle)
throw $this->createNotFoundException('A keresett cikk nem létezik!');
return $this->forward('KekRozsakFrontBundle:Default:article', array('articleSlug' => $mainPageArticle->getSlug()));
}
/**
* @Route("/cikk/{articleSlug}", name="KekRozsakFrontBundle_article")
2012-07-15 12:56:31 +00:00
* @Template()
*
* @param string $articleSlug
*/
public function articleAction($articleSlug)
{
$article = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:Article')->findOneBySlug($articleSlug);
if (!$article)
throw $this->createNotFoundException('A keresett cikk nem létezik!');
2012-07-15 12:56:31 +00:00
return array(
'article' => $article,
);
}
2012-07-09 15:29:37 +00:00
/**
* @Route("/profil", name="KekRozsakFrontBundle_profile_edit")
2012-07-15 12:56:31 +00:00
* @Template("KekRozsakFrontBundle:Default:userprofile.html.twig")
*/
2012-07-09 15:29:37 +00:00
public function profileEditAction()
{
$user = $this->get('security.context')->getToken()->getUser();
2012-07-15 12:56:31 +00:00
$oldPassword = $user->getPassword();
$form = $this->createForm(new UserType(), $user);
2012-07-09 15:29:37 +00:00
$saveSuccess = false;
$request = $this->getRequest();
2012-07-15 12:56:31 +00:00
2012-07-09 15:29:37 +00:00
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
{
2012-07-15 12:56:31 +00:00
if ($this->getPassword() == '')
2012-07-09 15:29:37 +00:00
$user->setPassword($oldPassword);
2012-07-15 12:56:31 +00:00
else
$user->setPassword($this->get('security.encoder_factory')->getEncoder($user)->encodePassword($user->getPassword(), $user->getSalt()));
2012-07-09 15:29:37 +00:00
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->flush();
}
}
2012-07-15 12:56:31 +00:00
return array(
2012-07-09 15:29:37 +00:00
'form' => $form->createView(),
'saveSuccess' => $saveSuccess,
2012-07-15 12:56:31 +00:00
);
2012-07-09 15:29:37 +00:00
}
/**
* @Route("/csoportok", name="KekRozsakFrontBundle_groupList")
* @Template()
*/
public function groupListAction()
{
$groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:Group');
$groups = $groupRepo->findAll(array('name' => 'DESC'));
return array(
'groups' => $groups,
);
}
2012-07-16 15:05:23 +00:00
/**
* @Route("/csoport/{groupSlug}", name="KekRozsakFrontBundle_groupView")
* @Template()
*/
public function groupViewAction($groupSlug)
{
$groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:Group');
if (!($group = $groupRepo->findOneBySlug($groupSlug)))
throw $this->createNotFoundException('A kért csoport nem létezik!');
return array(
'group' => $group,
);
}
/**
* @Route("/csoport/{groupSlug}/tagok", name="KekRozsakFrontBundle_groupMembers")
* @Template()
*/
public function groupMembersAction($groupSlug)
{
$groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:Group');
if (!($group = $groupRepo->findOneBySlug($groupSlug)))
throw $this->createNotFoundException('A kért csoport nem létezik!');
return array(
'group' => $group,
);
}
/**
* @Route("/csoport/{groupSlug}/dokumentumok", name="KekRozsakFrontBundle_groupDocuments")
* @Template()
*/
public function groupDocumentsAction($groupSlug)
{
$groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:Group');
if (!($group = $groupRepo->findOneBySlug($groupSlug)))
throw $this->createNotFoundException('A kért csoport nem létezik!');
return array(
'group' => $group,
);
}
/**
* @Route("/csoport/{groupSlug}/belepes", name="KekRozsakFrontBundle_groupJoin")
* @Template()
*/
public function groupJoinAction($groupSlug)
{
$user = $this->get('security.context')->getToken()->getUser();
$groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:Group');
if (!($group = $groupRepo->findOneBySlug($groupSlug)))
throw $this->createNotFoundException('A kért csoport nem létezik!');
if ($group->isMember($user))
{
return $this->redirect($this->generateUrl('KekRozsakFrontBundle_groupView', array($groupSlug => $group->getSlug())));
}
if ($group->isRequested($user))
{
return array(
'isRequested' => true,
'needApproval' => false,
'group' => $group,
);
}
$membership = new UserGroupMembership();
$membership->setUser($user);
$membership->setGroup($group);
$membership->setMembershipRequestedAt(new \DateTime('now'));
if ($group->isOpen())
{
$membership->setMembershipAcceptedAt(new \DateTime('now'));
}
$em = $this->getDoctrine()->getEntityManager();
$em->persist($membership);
$em->flush();
if ($group->isOpen())
{
return $this->redirect($this->generateUrl('KekRozsakFrontBundle_groupView', array($groupSlug => $group->getSlug())));
}
else
{
$message = \Swift_Message::newInstance()
->setSubject('Új jelentkező a csoportodban (' . $group->getName() . '): ' . $user->getDisplayName())
// TODO: Make this a config parameter!
->setFrom('info@blueroses.hu')
->setTo($group->getLeader()->getEmail())
->setBody($this->renderView('KekRozsakFrontBundle:Email:groupJoinRequest.txt.twig', array('user' => $user, 'group' => $group)));
$this->get('mailer')->send($message);
return array(
'isRequested' => false,
'needApproval' => true,
'group' => $group,
);
}
}
}