mirror of
https://framagit.org/tykayn/date-poll-api
synced 2023-08-25 08:23:11 +02:00
110 lines
3.4 KiB
PHP
110 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Owner;
|
|
use App\Entity\Poll;
|
|
use JMS\Serializer\Type\Exception\Exception;
|
|
use Swift_Message;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
class FramadateController extends AbstractController {
|
|
|
|
public function __construct( \Swift_Mailer $mailer ) {
|
|
$this->mail_service = $mailer;
|
|
}
|
|
|
|
/**
|
|
* generic way to send email with html template
|
|
*
|
|
* @param $config
|
|
*
|
|
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
|
*/
|
|
public function sendMailWithVars( $config ) {
|
|
|
|
if ( ! isset( $config[ 'poll' ] ) ) {
|
|
$config[ 'poll' ] = new Poll();
|
|
}
|
|
$emailChoicesTemplates = [
|
|
'creation_poll' => 'creation-mail.html.twig',
|
|
'edit_poll' => 'modification-notification-mail.html.twig',
|
|
'creation_poll_admin' => 'author-mail.html.twig',
|
|
'owner_list' => 'owner-list.html.twig',
|
|
'expiration' => 'expiration-mail.html.twig',
|
|
'creation_comment' => 'comment-notification.html.twig',
|
|
'creation_vote' => 'vote-notification.html.twig',
|
|
];
|
|
$emailChoicesTitles = [
|
|
'creation_poll' => 'Framadate | Création de sondage - lien public - ' . $config[ 'poll' ]->getTitle(),
|
|
'edit_poll' => 'Framadate | Modification de sondage - ' . $config[ 'poll' ]->getTitle(),
|
|
'creation_poll_admin' => 'Framadate | Création de sondage - lien admin - ',
|
|
'owner_list' => 'Framadate | Vos sondages créés',
|
|
'expiration' => 'Framadate | Notice d\'expiration du sondage ' . $config[ 'poll' ]->getTitle(),
|
|
'creation_comment' => 'Framadate | Commentaire de "' . $config[ 'owner' ]->getPseudo() . '" - sondage ' . $config[ 'poll' ]->getTitle(),
|
|
'creation_vote' => 'Framadate | Vote de "' . $config[ 'owner' ]->getPseudo() . '" - sondage ' . $config[ 'poll' ]->getTitle(),
|
|
];
|
|
// Create a message
|
|
$htmlbody = $this->renderView(
|
|
$config[ 'email_template' ],
|
|
$config
|
|
);
|
|
$message = ( new Swift_Message( 'Wonderful Subject from FAT computer' ) )
|
|
->setContentType( "text/html" )
|
|
->setCharset( 'UTF-8' )
|
|
->setFrom( [ 'contact@framadate-api.cipherbliss.com' ] )
|
|
->setTo( [ 'contact@cipherbliss.com' ] )
|
|
->setBody( $htmlbody, 'text/html' );
|
|
|
|
|
|
// Send the message
|
|
$numSent = $this->mail_service->send( $message );
|
|
$this->numSent = $numSent;
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* send created polls to an owner
|
|
*
|
|
* @param Owner $owner
|
|
*
|
|
* @return int|void
|
|
* @throws Exception
|
|
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
|
*/
|
|
public function sendOwnerPollsAction( Owner $owner ) {
|
|
|
|
$config = [
|
|
'owner' => $owner,
|
|
'title' => 'Framadate | Mes sondages',
|
|
'email_template' => 'emails/owner-list.html.twig',
|
|
];
|
|
$this->sendMailWithVars( $config );
|
|
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* @param Owner $foundOwner
|
|
* @param Poll|null $poll
|
|
*
|
|
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
|
*/
|
|
public function sendCreationMailAction( Owner $foundOwner, Poll $poll = null ) {
|
|
|
|
// anti spam , limit to every minute TODO
|
|
|
|
$config = [
|
|
'owner' => $foundOwner,
|
|
'from' => 'ne-pas-repondre@framadate-api.cipherbliss.com',
|
|
'poll' => $poll,
|
|
'title' => 'Création de sondage - ' . ( $poll ? $poll->getTitle() : $poll ),
|
|
'email_template' => 'emails/creation-mail.html.twig',
|
|
];
|
|
|
|
return $this->sendMailWithVars( $config );
|
|
}
|
|
}
|