<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\User;
use App\Entity\Agences;
use App\Entity\Hotels;
use App\Entity\Contact;
use App\Form\ProfilType;
use App\Form\ImageProfilType;
use Symfony\Component\HttpFoundation\Request;
use App\Controller\Fonctions\Fonctions;
use App\Repository\UserRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\String\Slugger\SluggerInterface;
#[Route('/profil', name: 'app_profil')]
class ProfilController extends AbstractController
{
#[Route('/new', name: 'app_profil_new', methods: ['GET', 'POST'])]
public function new(Request $request, UserRepository $userRepository, ManagerRegistry $doctrine, SluggerInterface $slugger): Response
{
$fonction = new Fonctions();
$logo = $fonction->FonctionLogo();
$user = $this->getUser();
$domaine = $fonction->FonctionUrl();
$entityManager = $doctrine->getManager();
$userRepo = $entityManager->getRepository(User::class);
$iduser = $userRepo->find($user);
$message = new User();
$form = $this->createForm(ProfilType::class, $message);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$nom = $message->getNom();
$prenom = $message->getPrenom();
$adresse = $message->getAdresse();
$telephone = $message->getTelephone();
$facebook = $message->getFacebook();
$instagram = $message->getInstagram();
$linkedin = $message->getLinkedin();
$twitter = $message->getTwitter();
$siteperso = $message->getSiteperso();
$psedo = $message->getPsedo();
$slug = $slugger->slug($psedo);
$user = $userRepo->find($iduser);
$user->setNom($nom);
$user->setPrenom($prenom);
$user->setAdresse($adresse);
$user->setTelephone($telephone);
$user->setFacebook($facebook);
$user->setInstagram($instagram);
$user->setLinkedin($linkedin);
$user->setTwitter($twitter);
$user->setSiteperso($siteperso);
$user->setPsedo($psedo);
$user->setSlug($slug);
$user->setCredits(10);
$user->setValidation(1);
$user->setDomaine($domaine);
$entityManager->flush();
return $this->redirectToRoute('app_messages_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('profil/new.html.twig', [
'message' => $message,
'form' => $form,
'logo' => $logo,
]);
}
#[Route('/image', name: 'app_profil_image', methods: ['GET', 'POST'])]
public function image(Request $request, UserRepository $userRepository, ManagerRegistry $doctrine, SluggerInterface $slugger): Response
{
$fonction = new Fonctions();
$logo = $fonction->FonctionLogo();
$user = $this->getUser();
$domaine = $fonction->FonctionUrl();
$entityManager = $doctrine->getManager();
$userRepo = $entityManager->getRepository(User::class);
$iduser = $userRepo->find($user);
$message = new User();
$form = $this->createForm(ImageProfilType::class, $message);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$image = $form->get('image')->getData();
$fichier = md5(uniqid()) . '.' . $image->guessExtension();
$user->setImage($fichier);
$entityManager->flush();
$image->move(
$this->getParameter('images_directory'),
$fichier
);
return $this->redirectToRoute('app_messages_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('profil/new.html.twig', [
'message' => $message,
'form' => $form,
'logo' => $logo,
]);
}
#[Route('/account', name: 'profil_account')]
public function account(): Response
{
return $this->render('profil/index.html.twig', []);
}
#[Route('/profil/{type}/{id}', name: 'profil_profil')]
public function profil(ManagerRegistry $doctrine, $type, $id): Response
{
// calcule du nombre de crédit
$entityManager = $doctrine->getManager();
if ($type == 'profil') {
$profil = new User();
$userRepo = $entityManager->getRepository(User::class);
$usersByRole = $userRepo->findBy(["id" => $id]);
foreach ($usersByRole as $user) {
$nom = $user->getNom();
$id = $user->getId();
}
}
if ($type == 'agence') {
$profil = new Agences();
$userRepo = $entityManager->getRepository(User::class);
$usersByRole = $userRepo->findBy(["id" => $id]);
foreach ($usersByRole as $user) {
$nom = $user->getNom();
$id = $user->getId();
}
}
if ($type == 'hotel') {
$profil = new Hotels();
$userRepo = $entityManager->getRepository(Hotels::class);
$usersByRole = $userRepo->findBy(["id" => $id]);
foreach ($usersByRole as $user) {
$nom = $user->getNom();
$id = $user->getId();
}
}
return $this->render('profil/index.html.twig', ['type' => $type, 'profil' => $id, 'nom' => $nom,]);
}
#[Route('/contact', name: 'profil_contact')]
public function contact(ManagerRegistry $doctrine): Response
{
$user = $this->getUser();
$entityManager = $doctrine->getManager();
// création de la gallerie image
$userRepo = $entityManager->getRepository(Contact::class);
$contacts = $userRepo->findBy(["agence" => $user]);
return $this->render('profil/contact.html.twig', ['contacts' => $contacts,]);
}
}