Last active
December 10, 2015 03:08
-
-
Save yesez/4372282 to your computer and use it in GitHub Desktop.
MensajeController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace UCA\SGExaBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use UCA\SGExaBundle\Entity\Mensaje; | |
use UCA\SGExaBundle\Entity\Usuario; | |
use UCA\SGExaBundle\Entity\Plantilla; | |
use UCA\SGExaBundle\Form\MensajeType; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
class MensajeController extends Controller | |
{ | |
public function nuevoAction() | |
{ | |
$mnj = new Mensaje(); | |
$em = $this->get('doctrine')->getEntityManager(); | |
$plantillas = $em->getRepository('UCASGExaBundle:Plantilla')->findAll(); | |
foreach ($plantillas as $pl) { | |
$mnj->getPlantillas()->add($pl); | |
} | |
$formMensaje = $this->get('form.factory')->create( | |
new MensajeType(), | |
$mnj | |
); | |
$request = $this->get('request'); | |
if($request->getMethod() == 'POST' && $request->request->get('enviarMensaje')) | |
{ | |
$formMensaje->bindRequest($request); | |
if ($formMensaje->isValid()) | |
{ | |
$mnj->setAutor($this->getUser()); | |
$nombrePlantilla = $mnj->getPlantilla(); | |
if(!empty($nombrePlantilla)){ | |
$plantilla = new Plantilla(); | |
$plantilla->setNombre($nombrePlantilla); | |
$plantilla->setContenido($mnj->getCuerpo()); | |
$mnj->setPlantilla($plantilla); | |
$em->persist($plantilla); | |
} | |
$em->persist($mnj); | |
$em->flush(); | |
/*----ENVIAR CORREO----*/ | |
$destinatarios = $mnj->getDestinatarios(); | |
foreach ($destinatarios as $destinatario) { | |
$contenido = $this->get('twig')->render( | |
'UCASGExaBundle:Mensaje:mensaje_correo.html.twig', | |
array('mensaje' => $mnj) | |
); | |
$correo = \Swift_Message::newInstance() | |
->setSubject($mnj->getTitulo()) | |
->setFrom('sgexa1@gmail.com') | |
->setTo($destinatario->getEmail()) | |
->setBody($contenido, 'text/html') | |
; | |
$this->get('mailer')->send($correo); | |
return $this->redirect($this->generateUrl('consultarmensaje', array('id' => $mnj->getId()))); | |
} | |
} | |
} | |
elseif($request->getMethod() == 'POST' && $request->request->get('cargarPlantilla')) | |
{ | |
$formMensaje->bindRequest($request); | |
if ($formMensaje->isValid()) | |
{ | |
$plantillaElegida = $mnj->getPlantillas(); | |
$request->getSession()->set('plantillaElegida', $plantillaElegida); | |
$mnj->setCuerpo($plantillaElegida->getContenido()); | |
} | |
} | |
return $this->render('UCASGExaBundle:Mensaje:nuevo_mensaje.html.twig', | |
array('formMensaje'=>$formMensaje->createView()) | |
); | |
} | |
public function consultarAction($id) | |
{ | |
$mensaje = $this->getMensajePorId($id); | |
return $this->render('UCASGExaBundle:Mensaje:consultar_mensaje_correo.html.twig', | |
array('mensaje' => $mensaje)); | |
} | |
private function getMensajePorId($id) | |
{ | |
$em = $this->get('doctrine')->getEntityManager(); | |
$mnj = $em->getRepository('UCASGExaBundle:Mensaje')->findOneById($id); | |
return $mnj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment