diff --git a/assets/styles/app.css b/assets/styles/app.css index 192bf3b864dc9374d81f36294850aec6abeb54c2..e40715f45bdfd7e4657597a83f8f0600ccbf7626 100644 --- a/assets/styles/app.css +++ b/assets/styles/app.css @@ -316,7 +316,7 @@ button { } .btn-like { - background-color: #ffffff; + background-color: transparent; border: 0px #ccc; padding: 5px 10px; cursor: pointer; diff --git a/src/Controller/HomePageController.php b/src/Controller/HomePageController.php index b6f21d77547dadae1ce43154d41766ce1da3f6ba..d1e957b53f897461e88ab38c4022af39e59e95b5 100644 --- a/src/Controller/HomePageController.php +++ b/src/Controller/HomePageController.php @@ -7,15 +7,27 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; +use Doctrine\Persistence\ManagerRegistry; +use App\Entity\Notification; +use App\Entity\Prestations; +use DateTime; class HomePageController extends AbstractController { + private $doctrine; + + public function __construct(ManagerRegistry $doctrine) { + $this->doctrine = $doctrine; + } + #[Route('', name: 'app_home_page')] public function index(Request $request, PrestationsRepository $prestationsRepository): Response { $prestations = $prestationsRepository->findFilteredPrestations(); + $user = $this->getUser(); return $this->render('home_page/index.html.twig', [ - 'prestations' => $prestations + 'prestations' => $prestations, + 'user' => $user ]); } @@ -30,9 +42,39 @@ class HomePageController extends AbstractController if ($prixMax == '') $prixMax = null; $prestations = $prestationsRepository->findFilteredPrestations($keyword, $prixMin, $prixMax, $typePrestation); + $user = $this->getUser(); return $this->render('home_page/index.html.twig', [ - 'prestations' => $prestations + 'prestations' => $prestations, + 'user' => $user ]); } + + #[Route('/aimer/{id}', name: 'app_like_home_page')] + public function aimerPrestation($id,PrestationsRepository $prestationsRepository): Response + { + $user = $this->getUser(); + $entityManager = $this->doctrine->getManager(); + $prestation = $entityManager->getRepository(Prestations::class)->find($id); + + if ($user != null) { + $notification = new Notification(); + $notification->setDescription($user->getLogin(). " a aimé votre annonce " . $prestation->getTitre()); + $notification->setType("Annonce aimée"); + $notification->setDate(new DateTime()); + $notification->setUserId($prestation->getProprietaire()); + $this->doctrine->getManager()->persist($notification); + $prestation->setIdClient(null); + } else return $this->redirectToRoute('app_login'); + + if ($user->getPrestationsAimees()->contains($prestation)) { + $user->removePrestationsAimee($prestation); + $prestation->removeUtilisateursAimant($user); + } else { + $user->addPrestationsAimee($prestation); + $prestation->addUtilisateursAimant($user); + } + $entityManager->flush(); + return $this->redirectToRoute('app_home_page'); + } } \ No newline at end of file diff --git a/templates/home_page/index.html.twig b/templates/home_page/index.html.twig index b93ba36ecdd3567dda7f2edbd9b04bf90c152b70..ac3c1e970d6c20f1187d8d52af13e71791620933 100644 --- a/templates/home_page/index.html.twig +++ b/templates/home_page/index.html.twig @@ -47,6 +47,17 @@ {% if is_granted('ROLE_ADMIN') %} <p class="tag_litige" id="tag">{{prestation.litiges|length}}</p> {% endif %} + {% if user and not is_granted ("ROLE_ADMIN") %} + {% if user.id != prestation.proprietaire.id %} + <a href="{{ path('app_like_home_page', {'id': prestation.id}) }}"><button class="btn-like"> + {% if user.aimePrestation(prestation) %} + <span class="aime">❤</span> + {% else %} + <span class="non_aime">❤</span> + {% endif %} + </button></a> + {% endif %} + {% endif %} {% if prestation.getType() == 'Pret' %} <p class="tag_pret" id="tag">Prêt</p>