diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php index 9f0f68b..1cbceb5 100644 --- a/src/Controller/CommentController.php +++ b/src/Controller/CommentController.php @@ -5,12 +5,12 @@ namespace App\Controller; use App\Entity\Comment; use App\Entity\Owner; use App\Entity\Poll; +use DateTime; use FOS\RestBundle\Controller\Annotations\Delete; use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Route; use JMS\Serializer\SerializerBuilder; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -19,8 +19,7 @@ use Symfony\Component\HttpFoundation\Request; * @package App\Controller * @Route("/api/v1",name="api_") */ -class CommentController extends AbstractController { - +class CommentController extends FramadateController { /** * @Get( @@ -71,7 +70,7 @@ class CommentController extends AbstractController { $foundOwner = new Owner(); $foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] ) ->setEmail( $data[ 'owner' ][ 'email' ] ) - ->setModifierToken( uniqid() ); + ->setModifierToken( uniqid( '', true ) ); } // anti flood $seconds_limit_lastpost = 5; @@ -84,7 +83,7 @@ class CommentController extends AbstractController { // check time of last comment - $now = new \DateTime(); + $now = new DateTime(); $now = $now->format( 'Y-m-d H:i:s' ); $date_first = strtotime( $lastCommentOfOwner[ 0 ]->getCreatedAt()->format( 'Y-m-d H:i:s' ) ); $date_second = strtotime( $now ); @@ -107,7 +106,7 @@ class CommentController extends AbstractController { } } $comment->setOwner( $foundOwner ) - ->setCreatedAt( new \DateTime() ) + ->setCreatedAt( new DateTime() ) ->setPoll( $poll ); $foundOwner->addComment( $comment ); @@ -116,6 +115,8 @@ class CommentController extends AbstractController { $em->persist( $comment ); $em->flush(); + $this->mail_service->sendCommentNotification($comment); + return $this->json( [ 'message' => 'you created a comment', 'data' => [ diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 602e6be..6a1b85d 100755 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -6,7 +6,7 @@ use App\Entity\Owner; use App\Service\MailService; use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Route; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Swift_Mailer; use Symfony\Component\HttpFoundation\JsonResponse; /** @@ -14,15 +14,12 @@ use Symfony\Component\HttpFoundation\JsonResponse; * @package App\Controller * @Route("/api/v1",name="api_") */ -class DefaultController extends AbstractController { +class DefaultController extends FramadateController { /** * @var MailService */ - private $mail_service; + protected $mail_service; - public function __construct( MailService $mail_service ) { - $this->mail_service = $mail_service; - } /** * Send a mail with all the data to one user @@ -32,11 +29,11 @@ class DefaultController extends AbstractController { * ) * * @param $email - * @param \Swift_Mailer $mailer + * @param Swift_Mailer $mailer * * @return JsonResponse */ - public function sendPollsToUser( $email, \Swift_Mailer $mailer ) { + public function sendPollsToUser( $email, Swift_Mailer $mailer ) { $repository = $this->getDoctrine()->getRepository( Owner::class ); // find user by email diff --git a/src/Controller/FramadateController.php b/src/Controller/FramadateController.php new file mode 100644 index 0000000..7baeb59 --- /dev/null +++ b/src/Controller/FramadateController.php @@ -0,0 +1,12 @@ +mail_service = $mail_service; + } +} diff --git a/src/Controller/IndexController.php b/src/Controller/IndexController.php index e084dca..3d92b6c 100755 --- a/src/Controller/IndexController.php +++ b/src/Controller/IndexController.php @@ -4,14 +4,13 @@ namespace App\Controller; use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Route; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; /** * Class DefaultController * @package App\Controller * @Route("/",name="homepage") */ -class IndexController extends AbstractController { +class IndexController extends FramadateController { /** * @Get(path ="/", * name = "get_default") diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 3567909..9c47dab 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -12,7 +12,7 @@ use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Put; use FOS\RestBundle\Controller\Annotations\Route; use JMS\Serializer\SerializerBuilder; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Swift_Mailer; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -21,7 +21,8 @@ use Symfony\Component\HttpFoundation\Request; * @package App\Controller * @Route("/api/v1/poll",name="api_") */ -class PollController extends AbstractController { +class PollController extends FramadateController { + /** * @Get( @@ -138,7 +139,7 @@ class PollController extends AbstractController { * * @return JsonResponse */ - public function newPollAction( Request $request, \Swift_Mailer $mailer, MailService $mail_service ) { + public function newPollAction( Request $request, Swift_Mailer $mailer, MailService $mail_service ) { $data = $request->getContent(); @@ -250,24 +251,29 @@ class PollController extends AbstractController { * * @param Owner $admin_user * @param Poll $poll - * @param \Swift_Mailer $mailer + * @param Swift_Mailer $mailer * * @return int + * not that the email tktest_commentateur@tktest.com does not really exist */ // public function sendCreationMailAction( Owner $admin_user, Poll $poll, \Swift_Mailer $mailer) { - public function testSendCreationMailAction( MailService $mail_service, $emailChoice = 'creation_vote' ) { + public function testSendCreationMailAction( + MailService $mail_service, + $emailChoice = 'tktest_commentateur@tktest.com' + ) { $em = $this->getDoctrine()->getRepository( Owner::class ); $foundOwner = $em->findOneByEmail( $emailChoice ); - if($foundOwner){ - $poll = $foundOwner->getPolls()[ 0 ]; - $comment = $foundOwner->getComments()[ 0 ]; + if ( $foundOwner ) { + $poll = $foundOwner->getPolls()[ 0 ]; + $comment = $foundOwner->getComments()[ 0 ]; $sent = $mail_service->sendCreationMailAction( $foundOwner, $poll ); - if($sent){ - return $this->json(["message"=>"test email sent!"],200); + if ( $sent ) { + return $this->json( [ "message" => "test email sent!" ], 200 ); } } - return $this->json(["message"=>"user with this email was not found"],400); + + return $this->json( [ "message" => "user with this email was not found" ], 400 ); } @@ -332,6 +338,7 @@ class PollController extends AbstractController { $em->flush(); + return $this->json( [ 'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ), 'data' => [ diff --git a/src/Controller/VoteController.php b/src/Controller/VoteController.php index 4edf10f..a3b3241 100644 --- a/src/Controller/VoteController.php +++ b/src/Controller/VoteController.php @@ -11,7 +11,6 @@ use FOS\RestBundle\Controller\Annotations\Delete; use FOS\RestBundle\Controller\Annotations\Patch; use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Route; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -20,7 +19,7 @@ use Symfony\Component\HttpFoundation\Request; * @package App\Controller * @Route("/api/v1",name="api_") */ -class VoteController extends AbstractController { +class VoteController extends FramadateController { /** * add a vote stack on a poll diff --git a/src/Service/MailService.php b/src/Service/MailService.php index bd2ba1a..c6f6c79 100644 --- a/src/Service/MailService.php +++ b/src/Service/MailService.php @@ -4,6 +4,7 @@ namespace App\Service; +use App\Entity\Comment; use App\Entity\Owner; use App\Entity\Poll; use Doctrine\ORM\EntityManagerInterface; @@ -13,6 +14,7 @@ use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; use Symfony\Component\Mime\Address; +use Symfony\Component\Mime\Email; class MailService { @@ -89,6 +91,19 @@ class MailService { 'title' => 'Framadate | Mes sondages', 'email_template' => 'emails/owner-list.html.twig', ]; + $this->sendMailWithVars( $config ); + return 1; + } + + + public function sendCommentNotification( Comment $comment ) { + + $config = [ + 'comment' => $comment, + 'owner' => $comment->getOwner(), + 'title' => 'Framadate | commentaire de '.$comment->getOwner()->getPseudo(). ' _ sondage '. $comment->getPoll()->getTitle(), + 'email_template' => 'emails/comment-notification.html.twig', + ]; return $this->sendMailWithVars( $config ); } @@ -124,13 +139,27 @@ class MailService { 'creation_vote' => 'Framadate | Vote de "' . $config['owner']->getPseudo() . '" - sondage ' . $config['poll']->getTitle(), ]; - $email = ( new TemplatedEmail() ) - ->from( new Address( $config[ 'from' ] ) ) -// ->setHeaders( [new Header('charset', 'UTF-8' )]) - ->subject( $config[ 'title' ] ) - ->to( $config[ 'owner' ]->getEmail() ) - ->htmlTemplate( $config[ 'email_template' ] ) - ->context( $config ); +// $email = ( new Email() ) +// ->from( new Address( $config[ 'from' ] ) ) +//// ->setHeaders( [new Header('charset', 'UTF-8' )]) +// ->subject( $config[ 'title' ] ) +// ->to( $config[ 'owner' ]->getEmail() ) +// ->htmlTemplate( $config[ 'email_template' ] ) +// ->context( $config ); + $email = (new TemplatedEmail()) + ->from('fabien@example.com') + ->to(new Address('ryan@example.com')) + ->subject('Thanks for signing up!') + + // path of the Twig template to render + ->htmlTemplate('emails/footer.html.twig') + + // pass variables (name => value) to the template + ->context([ + 'expiration_date' => new \DateTime('+7 days'), + 'username' => 'foo', + ]) + ; // send email return $this-> diff --git a/templates/emails/author-mail.html.twig b/templates/emails/author-mail.html.twig index 9a154ed..443372c 100755 --- a/templates/emails/author-mail.html.twig +++ b/templates/emails/author-mail.html.twig @@ -1,5 +1,5 @@ {#[Framadate][Réservé à l'auteur] Sondage: TESSSSSSSSSST#} -{% extends 'email-base.html.twig' %} +{% extends 'email-base-plaintext.html.twig' %} {% block content %}

diff --git a/templates/emails/comment-notification-mail.html.twig b/templates/emails/comment-notification-mail.html.twig deleted file mode 100755 index f59fb22..0000000 --- a/templates/emails/comment-notification-mail.html.twig +++ /dev/null @@ -1,6 +0,0 @@ -{#[Framadate] Notification d'un sondage : TESSSSSSSSSST#} -smoi vient de rédiger un commentaire. -Vous pouvez retrouver votre sondage avec le lien suivant : https://framadate.org/NGutN7jB9vtoGOEjCfUJWBwr/admin - -Merci de votre confiance. -Framadate diff --git a/templates/emails/comment-notification.html.twig b/templates/emails/comment-notification.html.twig index 579ffd3..1d97396 100644 --- a/templates/emails/comment-notification.html.twig +++ b/templates/emails/comment-notification.html.twig @@ -1,5 +1,5 @@ {#[Framadate] Notification d'un sondage : TESSSSSSSSSST#} -{% extends 'email-base.html.twig' %} +{% extends 'email-base-plaintext.html.twig' %} {% block content %} {{ owner.pseudo }} diff --git a/templates/emails/creation-mail.html.twig b/templates/emails/creation-mail.html.twig index 081beef..e79d7f4 100755 --- a/templates/emails/creation-mail.html.twig +++ b/templates/emails/creation-mail.html.twig @@ -1,5 +1,5 @@ {#[Framadate][Pour diffusion aux sondés] Sondage: TESSSSSSSSSST#} -{% extends 'email-base.html.twig' %} +{% extends 'email-base-plaintext.html.twig' %} {% block content %} diff --git a/templates/emails/expiration-mail.html.twig b/templates/emails/expiration-mail.html.twig index 49adf56..31c7777 100755 --- a/templates/emails/expiration-mail.html.twig +++ b/templates/emails/expiration-mail.html.twig @@ -1,5 +1,5 @@ {#[Framadate][expiration] Sondage: TESSSSSSSSSST#} -{% extends 'email-base.html.twig' %} +{% extends 'email-base-plaintext.html.twig' %} {% block content %} diff --git a/templates/emails/modification-notification-mail.html.twig b/templates/emails/modification-notification-mail.html.twig index e6c2faa..33b7f58 100755 --- a/templates/emails/modification-notification-mail.html.twig +++ b/templates/emails/modification-notification-mail.html.twig @@ -1,5 +1,5 @@ {#[Framadate] Participation au sondage : TESSSSSSSSSST#} -{% extends 'email-base.html.twig' %} +{% extends 'email-base-plaintext.html.twig' %} {% block content %} {{ poll.owner.pseudo }} , diff --git a/templates/emails/owner-list.html.twig b/templates/emails/owner-list.html.twig index 2a9a868..07be2d8 100755 --- a/templates/emails/owner-list.html.twig +++ b/templates/emails/owner-list.html.twig @@ -1,5 +1,5 @@ {#[Framadate][Réservé à l'auteur] Sondage: TESSSSSSSSSST#} -{% extends 'email-base.html.twig' %} +{% extends 'email-base-plaintext.html.twig' %} {% block content %}

diff --git a/templates/emails/vote-notification.html.twig b/templates/emails/vote-notification.html.twig index b1b174d..2dc4b0a 100644 --- a/templates/emails/vote-notification.html.twig +++ b/templates/emails/vote-notification.html.twig @@ -1,5 +1,5 @@ {#[Framadate] Notification d'un sondage : TESSSSSSSSSST#} -{% extends 'email-base.html.twig' %} +{% extends 'email-base-plaintext.html.twig' %} {% block content %} {{ owner.pseudo }}