<?php
namespace App\Controller;
use App\Form\ContactType;
use App\Service\Api;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class HomeController extends AbstractController
{
/**
* @Route("/", name="home")
*/
public function index(Request $request, MailerInterface $mailer, TranslatorInterface $translator, Api $apiService)
{
$form = $this->createForm(ContactType::class);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$email = (new TemplatedEmail())
->from(new Address($this->getParameter('app.contactEmail'), 'CONTACT FORM'))
->to(new Address($this->getParameter('app.supportEmail')))
->subject($form->getData()['subject'])
->html(
$this->renderView(
'emails/contact-us.html.twig',
['contact' => $form->getData()]
)
);
$mailer->send($email);
$this->addFlash('success', $translator->trans("Your message has been sent successfully, one of our experts will contact you as soon as possible. Thank you for your trust."));
return $this->redirect($request->getUri());
} else {
$this->addFlash("danger", $translator->trans("An error has occurred while sending your request! please check the form for errors and try again."));
}
}
$data['contactForm'] = $form->createView();
$data['services'] = $apiService->getAllServices();
$data["icons"] = $apiService->getServicesIcons();
return $this->render('home/index.html.twig', $data);
}
/**
* @Route("/home", name="home2")
*/
public function home(Request $request, MailerInterface $mailer, TranslatorInterface $translator, Api $apiService)
{
$form = $this->createForm(ContactType::class);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$email = (new TemplatedEmail())
->from(new Address($this->getParameter('app.contactEmail'), 'CONTACT FORM'))
->to(new Address($this->getParameter('app.supportEmail')))
->subject($form->getData()['subject'])
->html(
$this->renderView(
'emails/contact-us.html.twig',
['contact' => $form->getData()]
)
);
$mailer->send($email);
$this->addFlash('success', $translator->trans("Your message has been sent successfully, one of our experts will contact you as soon as possible. Thank you for your trust."));
return $this->redirect($request->getUri());
} else {
$this->addFlash("danger", $translator->trans("An error has occurred while sending your request! please check the form for errors and try again."));
}
}
$data['contactForm'] = $form->createView();
$data['services'] = $apiService->getAllServices();
$data["icons"] = $apiService->getServicesIcons();
return $this->render('home/home.html.twig', $data);
}
function printr($data) {
echo "<pre>" . print_r($data,true) . "</pre>";
}
/**
* @Route("/sitemap.xml", name="sitemap", defaults={"_format"="xml"})
*/
public function sitemap(Api $apiService): Response
{
$services = $apiService->getAllServices();
$urls=[];
foreach ($services as $service){
$url='https://wizzsocial.com/services/'.$service['SocialNetwork'].'/'.$service['IDSN'];
$urls[]=$url;
foreach ($service['subs'] as $s){
$url='https://wizzsocial.com/services/'.$service['SocialNetwork'].'/'.str_replace(' ','-',$s['Text']).'/'.$s['IDSN'].'/'.$s['IDGService'];
$urls[]=$url;
}
}
$response = new Response(
$this->renderView('home/sitemap.xml.twig', ['urls' => $urls]),
200
);
$response->headers->set('Content-Type', 'text/xml');
return $response;
}
}