Commit ac79ff07 authored by Mat's avatar Mat
Browse files

envoie un mail à tous les membres du groupe associé à une infolettre

parent bee11672
......@@ -2,6 +2,8 @@
namespace PotageBundle\Command;
use PotageBundle\Entity\Groupe;
use PotageBundle\Entity\Lettre;
use PotageBundle\Services\Newsletter;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
......@@ -44,10 +46,16 @@ class SendMailCommand extends ContainerAwareCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$ret = $this->newsletter->sendMail(
'mat@collectifs.net',
'[Potage] envoi depuis la commande',
/**
* @var Lettre $lettre
*/
$lettre = $this->getContainer()->get('doctrine')->getManager()
->getRepository('PotageBundle:Lettre')
->findOneForSend(7);
$ret = $this->newsletter->sendLettreToGroup(
$lettre,
'body du mail de test'
);
$retMsg = ( $ret === 1 ) ? "\e[1;32mDone\e[0m\n" : "\e[1;31mError\e[0m\n";
......
......@@ -2,6 +2,7 @@
namespace PotageBundle\Controller;
use PotageBundle\Entity\Lettre;
use PotageBundle\Services\Newsletter;
class DefaultController extends MasterController
......@@ -24,15 +25,17 @@ class DefaultController extends MasterController
*/
public function testAction(Newsletter $newsletter)
{
$ret = $newsletter->sendMail(
'mat@collectifs.net',
'[Potage] envoyé depuis la page de test',
'body du mail de test'
);
/**
* @var Lettre $lettre
*/
$lettre = $this->get('doctrine')->getManager()
->getRepository('PotageBundle:Lettre')
->findOneForSend(7);
$retour = $newsletter->sendLettreToGroup($lettre, 'body du mail de test');
return $this->render('@Potage/Default/test.html.twig', array(
'retour'=> $ret
'retour'=> $retour
));
}
......
......@@ -229,4 +229,19 @@ class LettreRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getResult();
}
/**
* @param $id
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneForSend($id)
{
$qb = $this->createQueryBuilder('l')
->where('l.id = :id')
->setParameters(array(
':id'=> $id
));
return $qb->getQuery()->getOneOrNullResult();
}
}
......@@ -117,4 +117,18 @@ class UtilisateurRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getResult();
}
/**
* @param int $id_group
* @return array
*/
public function findAllByGroup($id_group)
{
$qb = $this->createQueryBuilder('u')
->select('u.id', 'u.nom', 'u.prenom', 'u.email')
->join('u.groupes', 'g')
->where('g.id = :id')
->setParameter(':id', $id_group);
return $qb->getQuery()->getResult();
}
}
......@@ -6,7 +6,11 @@
{% block content %}
<h1>{{ block('title') }}</h1>
{{ retour }}
<ul>
{% for item in retour %}
<li>{{ loop.index }}) {{ item.to }}{{ item.result }}</li>
{% endfor %}
</ul>
{% endblock %}
{% block customJS %}
......
......@@ -2,6 +2,7 @@
namespace PotageBundle\Services;
use PotageBundle\Entity\Lettre;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Newsletter
......@@ -30,7 +31,6 @@ class Newsletter
*/
public function sendMail($to, $subject, $body)
{
$mailer = $this->container->get('mailer');
$twig = $this->container->get('twig');
......@@ -55,4 +55,44 @@ class Newsletter
return $mailer->send($message);
}
}
\ No newline at end of file
/**
* @param Lettre $lettre
* @param $body
* @return array
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
*/
public function sendLettreToGroup(Lettre $lettre, $body)
{
$subject = "[Potage] Envoi de l'infolettre " . $lettre->getReference();
$groupe = $lettre->getGroupe()->getId();
$utilisateurs = $this->container->get('doctrine')
->getRepository('PotageBundle:Utilisateur')
->findAllByGroup($groupe);
$ret = array();
for ($i = 0; $i < count($utilisateurs); $i++)
{
//$email = array(
// $utilisateurs[$i]['email'] => $utilisateurs[$i]['prenom'] . ' ' . $utilisateurs[$i]['nom']
//);
$email = $utilisateurs[$i]['email'];
//dump($email);
$ret[$i] = array(
'to' => $email,
'result' => $this->sendMail($email, $subject, $body) === 1 ? 'done' : 'error'
);
}
return $ret;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment