123 lines
4.6 KiB
PHP
123 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace Trinity\Bundle\NewsletterBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
|
use FOS\UserBundle\Propel\UserQuery;
|
|
use FOS\UserBundle\Propel\GroupQuery;
|
|
use FOS\UserBundle\Propel\User;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
|
use Symfony\Component\Validator\Constraints\NotEqualTo;
|
|
|
|
class NewsletterController extends Controller
|
|
{
|
|
/**
|
|
* @Route("/newsletter/show/{id}", name="newsletter_preview")
|
|
* @Template()
|
|
* @ParamConverter("object", class="Trinity\Bundle\NewsletterBundle\Model\Newsletter")
|
|
*/
|
|
public function indexAction($object)
|
|
{
|
|
if (!$object->getModel()) {
|
|
throw $this->createNotFoundException('This newsletter does not have a defined template.');
|
|
}
|
|
|
|
$blocks = array();
|
|
|
|
foreach ($object->getModel()->getBlocks() as $block) {
|
|
$blocks[$block->getName()] = $block;
|
|
}
|
|
|
|
$user = new User();
|
|
$user->setUnsubscribeToken('show');
|
|
|
|
return $this->render(
|
|
$object->getModel()->getTemplate(),
|
|
array(
|
|
'model' => $object->getModel() ,
|
|
'blocks' => $blocks ,
|
|
'user' => $user,
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @Route("/newsletter/subscribe", name="newsletter_subscribe")
|
|
* @Template()
|
|
*
|
|
*/
|
|
public function subscribeAction(Request $request)
|
|
{
|
|
$defaultData = array(
|
|
'email' => ($this->getUser())?$this->getUser()->getEmail():null,
|
|
);
|
|
|
|
// TODO : use DataTransformer and specific Type to manage this
|
|
$form = $this->container->get('form.factory')->createNamedBuilder('newsletter_subscribe','form', $defaultData)
|
|
->setMethod('POST')
|
|
->setAction($this->generateUrl('newsletter_subscribe'))
|
|
->add('email', 'email', array(
|
|
'attr' => array(
|
|
'placeholder' => ($this->getUser())?$this->getUser()->getEmail():$this->get('translator')->trans('mail@example.com')
|
|
),
|
|
'constraints' => array(
|
|
new NotBlank(),
|
|
new NotEqualTo(array('value'=> $this->get('translator')->trans('mail@example.com')))
|
|
)
|
|
))
|
|
->add('referer','hidden')
|
|
->getForm();
|
|
$form->handleRequest($request);
|
|
|
|
if($request->isMethod('POST')) {
|
|
if ($form->isValid()) {
|
|
$datas = $form->getData();
|
|
$this->save(array_merge($datas,array('newsletter' => true)));
|
|
$request->getSession()->getFlashBag()->add('success',$this->get('translator')->trans('Thank you for your subscription to our newsletter.'));
|
|
$this->redirect($this->generateUrl("newsletter_subscribe"));
|
|
}
|
|
}
|
|
|
|
return $this->render(
|
|
'TrinityNewsletterBundle:Newsletter:subscribe.html.twig',
|
|
array(
|
|
'form' => $form->createView()
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @Route("/newsletter/unsubscribe/{token}", name="newsletter_unsubscribe")
|
|
* @Template()
|
|
*/
|
|
public function unsubscribeAction($token)
|
|
{
|
|
$user = UserQuery::create()->filterByUnsubscribeToken($token)->findOne();
|
|
|
|
if (!$user) {
|
|
throw $this->createNotFoundException('No user found for this token');
|
|
}
|
|
|
|
$blck_group = GroupQuery::create()->filterByCode('BLACKLIST')->findOne();
|
|
|
|
if (!$blck_group) {
|
|
throw $this->createNotFoundException('You must define a group with "BLACKLIST" code in order to allow user\'s unsubscribe');
|
|
}
|
|
|
|
$user->addGroup($blck_group)->save();
|
|
}
|
|
|
|
protected function save(array $datas)
|
|
{
|
|
$this->get('trinity.user_register')->setCurrentUser($this->getUser());
|
|
|
|
$user = $this->get('trinity.user_register')->registerUserIfUnknown($datas);
|
|
$user = $this->get('trinity.user_register')->updateOptins($datas, $user);
|
|
}
|
|
}
|