Skip to content

Instantly share code, notes, and snippets.

@lauris
Created December 14, 2012 08:37
Show Gist options
  • Save lauris/4283672 to your computer and use it in GitHub Desktop.
Save lauris/4283672 to your computer and use it in GitHub Desktop.
<?php
namespace Foobar\NewsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Foobar\NewsBundle\Entity\Item;
use Foobar\NewsBundle\Entity\Subscriber;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\Email;
class DefaultController extends Controller
{
/**
* News list
*
* @return Response
*/
public function indexAction()
{
$items = $this->getDoctrine()
->getRepository('FoobarNewsBundle:Item')
->findAllWithPublishers();
return $this->render('FoobarNewsBundle:Default:index.html.twig', array("items" => $items));
}
/**
* Add news item
*
* @param Request $request
* @return Response
*/
public function addAction(Request $request)
{
$item = new Item();
$form = $this->createFormBuilder($item)
->add('title')
->add('body')
->getForm();
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getEntityManager();
$publisher = $this->get('security.context')->getToken()->getUser();
$item->setPublisher($publisher);
$em->persist($item);
$em->flush();
return $this->redirect(
$this->generateUrl(
'FoobarNewsBundle_add',
array("confirm" => "success")
)
);
}
}
return $this->render('FoobarNewsBundle:Default:add.html.twig', array(
'form' => $form->createView(),
'status' => $request->get("status")
));
}
/**
* Subscribe to news
*
* @param Request $request
* @return Response
*/
public function subscribeAction(Request $request)
{
$subscriber = new Subscriber();
$form = $this->createFormBuilder($subscriber)
->add('email', 'email')
->getForm();
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getEntityManager();
$em->persist($subscriber);
$em->flush();
return $this->redirect(
$this->generateUrl(
'FoobarNewsBundle_subscribe',
array("status" => "success")
)
);
}
}
return $this->render(
'FoobarNewsBundle:Default:subscribe.html.twig',
array(
'form' => $form->createView(),
'status' => $request->get("status"),
'confirm' => $request->get("confirm")
)
);
}
/**
* Confirm email address
*
* @param string $token
* @param string $email
* @throws NotFoundException
* @return Response
*/
public function confirmAction($token, $email)
{
$subscriber = $this->getDoctrine()
->getRepository('FoobarNewsBundle:Subscriber')
->findOneBy(array("token" => $token, "email" => $email));
if(!$subscriber)
{
throw $this->createNotFoundException('Email/Token not found');
}
else
{
$subscriber->setIsActive(true);
$subscriber->setToken(null);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($subscriber);
$em->flush();
return $this->redirect(
$this->generateUrl(
'FoobarNewsBundle_subscribe',
array("confirm" => "success")
)
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment