src/Controller/DefaultController.php line 109
<?php
namespace App\Controller;
use App\Entity\Game;
use App\Entity\Page;
use App\Entity\User;
use App\Entity\Word;
use App\Entity\Category;
use Symfony\Component\Mime\Email;
use App\Controller\CachedController;
use App\Entity\UserSubscribeActivity;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
#[Route('/{_locale}')]
class DefaultController extends AbstractController
{
private $doctrine;
private $entityManager;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
$this->entityManager = $this->doctrine->getManager();
}
#[Route('/default', name: 'app_default')]
public function index(): Response
{
return $this->render('default/index.html.twig', [
'controller_name' => 'DefaultController',
]);
}
#[Route('/restricted-access.html', name: 'front_restricted_access')]
public function restrictedAccessAction(){
$response = new Response();
$response->setCache(['max_age' => CachedController::ONE_WEEK_CACHE, 's_maxage' => CachedController::ONE_WEEK_CACHE]);
return $this->render('default/restricted_access.html.twig', array());
}
#[Route('/page/{url}', name: 'static_page')]
public function staticPage(Request $request, $url): Response
{
$locale = $request->get('_locale','fr');
$pageRepository = $this->doctrine->getRepository(Page::class);
$content = $pageRepository->findOneBy(['url' => $url, 'locale' => $locale]);
if( $content )
{
return $this->render('default/static_page.html.twig', [
'content' => $content ,
]);
}else{
throw $this->createNotFoundException();
}
}
#[Route('/mentions-legales.html', name: 'front_mention_legale')]
public function mentionLegales(Request $request): Response
{
return $this->render('default/mention-legales.html.twig', []);
}
#[Route('/plan-du-site.html', name: 'front_sitemap')]
public function siteMap(Request $request): Response
{
$locale = $request->get('_locale','fr');
$repository = $this->doctrine->getRepository(Page::class);
$link = $repository->getI18nPageByRoute('cgv', $locale);
$categories = $this->doctrine->getRepository(Category::class)->getFrontCategories();
$themes = $this->doctrine->getRepository(Category::class)->getFrontThemes();
$games = $this->doctrine->getRepository(Game::class)->getPublishedGamesByTitle($request->getLocale());
$response = new Response();
$response->setCache(['max_age' => CachedController::ONE_WEEK_CACHE, 's_maxage' => CachedController::ONE_WEEK_CACHE]);
return $this->render('default/sitemap.html.twig', [
'cgv' => $link,
'categories' => $categories,
'themes' => $themes,
'games' => $games
],$response);
}
#[Route('/foire-aux-questions.html', name: 'front_faqs')]
public function faqs(Request $request): Response
{
die();
}
#[Route('/qui-sommes-nous.html', name: 'front_whoarewe')]
public function whoarewe(Request $request): Response
{
$tDebut = new \DateTime();
$tDebut->setTimestamp(1323212400);
$intervalle = $tDebut->diff(new \DateTime());
$nbMois = $intervalle->m + $intervalle->y * 12;
$nbFans = 120547;
$nbInscrit = $this->doctrine->getRepository(User::class)->countUsers();
$nbAbonne = $this->doctrine->getRepository(User::class)->countSubscriber();
$nbJeux = $this->doctrine->getRepository(Game::class)->countGames();
$nbMots = $this->doctrine->getRepository(Word::class)->getRoundedCount(1);
$response = new Response();
$response->setCache(['max_age' => CachedController::ONE_WEEK_CACHE, 's_maxage' => CachedController::ONE_WEEK_CACHE]);
$locale = $request->get('_locale','fr');
$locales = array('fr');
if(array_search($locale, $locales) === false)
$locale = 'fr';
return $this->render('whoarewe/whoarewe-'.$locale.'.html.twig', [
'nbFan' => $nbFans,
'nbMois' => $nbMois,
'nbInscrit' => $nbInscrit,
'nbAbonne' => $nbAbonne,
'nbJeux' => $nbJeux,
'nbMots' => $nbMots
], $response);
}
#[Route('/charte.html', name: 'front_charte')]
public function charte(Request $request): Response
{
$response = new Response();
$response->setCache(['max_age' => CachedController::ONE_WEEK_CACHE, 's_maxage' => CachedController::ONE_WEEK_CACHE]);
$locale = $request->get('_locale','fr');
$locales = array('fr','de');
if(array_search($locale, $locales) === false)
$locale = 'fr';
return $this->render('charte/charteV3-'.$locale.'.html.twig', [
], $response);
}
#[Route('/contact-questions.html', name: 'front_contact_new')]
public function contactNew(Request $request): Response
{
$locale = $request->get('_locale','fr');
$locales = array('fr','de');
if(array_search($locale, $locales) === false) {
return $this->redirect($this->generateUrl('front_contact'));
//$locale = 'fr';
}
$response = new Response();
$response->setCache(['max_age' => CachedController::ONE_WEEK_CACHE, 's_maxage' => CachedController::ONE_WEEK_CACHE]);
return $this->render('faq/faqs-'.$locale.'.html.twig', ['locale' => $locale], $response);
}
const MAIL_CONTACT = "contact@speakyplanet.com";
const MAIL_CONTACT_DE = "kontakt-de@speakyplanet.com";
#[Route('/contact.html', name: 'front_contact')]
public function contact(Request $request, MailerInterface $mailer): Response
{
$defaultData = [];
$isUser = false;
$subscribedUser = false;
/** @var User $user */
$user = $this->getUser();
if($user)
{
$defaultData = [
'firstName' => $user->getFirstName(),
'lastName' => $user->getLastName(),
'email' => $user->getEmail(),
];
$isUser = true;
$subscribedUser = $user->getIsSubscriber();
}
$form = $this->createFormBuilder($defaultData, ['translation_domain' => 'contact'])
->add('firstName', TextType::class, [
'constraints' => new Length(['min' => 3]),
'label' => "Votre nom :"
])
->add('lastName', TextType::class, [
'constraints' => new Length(['min' => 3]),
'label' => "Votre prénom :"
])
->add('email', EmailType::class, [
'label' => "Votre adresse e-mail :"
])
->add('reason', ChoiceType::class, [
'choices' => [
'une demande de renseignement' => 'renseignement',
'un problème technique' => 'problème technique',
'votre avis sur le site' => 'votre avis',
'une proposition de partenariat' => 'partenariat',
'une demande de désabonnement' => 'désabonnement',
'Satisfait ou remboursé sous 48h' => 'remboursement',
],
'label' => "Votre question concerne :",
])
->add('message', TextareaType::class, [
'label' => "Votre message :",
])
->add('envoyer', SubmitType::class, [
'label' => "Envoyer",
])
->add('captcha', Recaptcha3Type::class, [
'constraints' => new Recaptcha3(),
'action_name' => 'contact',
'locale' => $request->getLocale(),
])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$isAutomaticUnsubscribe = $user != null && $data['reason'] == "désabonnement";
if(!$isAutomaticUnsubscribe)
{
switch ($request->getLocale())
{
case 'fr':
$contactEmail = self::MAIL_CONTACT;
break;
case 'de':
$contactEmail = self::MAIL_CONTACT_DE;
break;
default:
$contactEmail = self::MAIL_CONTACT;
break;
}
$browser = $_SERVER['HTTP_USER_AGENT'];
$html = $this->renderView('mail/mail_contact.html.twig', [
'nom' => $data['lastName'],
'prenom' => $data['firstName'],
'reason' => $data['reason'],
'message' => $data['message'],
'browser' => $browser
]);
$text = preg_replace( "/\n\s+/", "\n", rtrim(html_entity_decode(strip_tags($html))) );
$email = (new Email())
->from($contactEmail)
->to($contactEmail)
->replyTo($data['email'])
->subject('[Contact] ' . $data['lastName'] . ' ' . $data['firstName'])
->text($text)
->html($html);
$mailer->send($email);
return $this->redirectToRoute('front_contact_sent');
} else {
$repository = $this->doctrine->getRepository(UserSubscribeActivity::class);
$activity = $repository->findOneBy(array('user' => $user->getId(), 'statut' => UserSubscribeActivity::STATUT_PENDING));
if($activity == null)
{
$activity = new UserSubscribeActivity();
$activity->setUser($user);
$activity->setStatut(UserSubscribeActivity::STATUT_PENDING);
$activity->setType(UserSubscribeActivity::TYPE_UNSUBSCRIBE);
$activity->setIsAuto(true);
$em = $this->entityManager;
$em->persist($activity);
$em->flush();
}
return $this->redirectToRoute('front_unsubscribe_sent');
}
}
return $this->renderForm('default/contact.html.twig', [
'form' => $form,
'subscribedUser' => $subscribedUser,
]);
}
#[Route('/email')]
public function sendEmail(MailerInterface $mailer): Response
{
$email = (new Email())
->from(self::MAIL_CONTACT)
->to('anthony@speakyplanet.com')
//->cc('cc@example.com')
//->bcc('bcc@example.com')
//->replyTo('fabien@example.com')
//->priority(Email::PRIORITY_HIGH)
->subject('Time for Symfony Mailer!')
->text('Sending emails is fun again!')
->html('<p>See Twig integration for better HTML integration!</p>');
try {
$mailer->send($email);
} catch (TransportExceptionInterface $e) {
dump($e);
// some error prevented the email sending; display an
// error message or try to resend the message
}
return $this->render('default/contact_sent.html.twig', []);
}
#[Route('/contact-envoye.html', name: 'front_contact_sent')]
public function contactSent(Request $request): Response
{
return $this->render('default/contact_sent.html.twig', []);
}
#[Route('/desabonnement-envoye.html', name: 'front_unsubscribe_sent')]
public function unsubscribeSent(Request $request): Response
{
return $this->render('default/unsubscribe_sent.html.twig', []);
}
}