<?php
namespace App\Controller;
use App\Entity\Adherent;
use App\Form\AdherentType;
use App\Repository\AdherentRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use \Knp\Component\Pager\PaginatorInterface;
use \App\Services\AppSevices;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
/**
* @Route("/user/adherent")
*/
class AdherentController extends AbstractController {
/**
* @Route("/Admin/", name="app_adherent_index", methods={"GET"})
*/
public function index(Request $request, PaginatorInterface $paginator, AppSevices $services): Response {
$nom = ($request->query->get('nom')) == "" ? "all" : $request->get('nom');
$prenom = ($request->query->get('prenom')) == "" ? "all" : $request->get('prenom');
$numAd = ($request->query->get('numAd')) == "" ? "all" : $request->get('numAd');
$email = ($request->query->get('email')) == "" ? "all" : $request->get('email');
$usersListQuery = $services->getAdherents(array("nom" => $nom, "prenom" => $prenom, "email" => $email, "numAd" => $numAd
))->getQuery();
$adherents = $paginator->paginate($usersListQuery, $request->query->getInt('page', 1), 89);
return $this->render('adherent/index.html.twig', [
'adherents' => $adherents,
]);
}
/**
* @Route("/Admin/adType", name="app_adherent_indexAdherent", methods={"GET"})
*/
public function indexAdherent(Request $request, PaginatorInterface $paginator, AppSevices $services): Response {
$nom = ($request->query->get('nom')) == "" ? "all" : $request->get('nom');
$prenom = ($request->query->get('prenom')) == "" ? "all" : $request->get('prenom');
$numAd = ($request->query->get('numAd')) == "" ? "all" : $request->get('numAd');
$email = ($request->query->get('email')) == "" ? "all" : $request->get('email');
$adherents = $services->getAdherents([])->getQuery()->getResult();
$arrayresult = array();
$typePrestation=$services->getTypePrestations([])->getQuery()->getResult();
$i=0;
foreach ($adherents as $adherent) {
$types = $services->getByTypePrestation(['id' => $adherent->getId()]);
$dette= $services->getSumDette(['id' => $adherent->getId()]);
if($types){
foreach ($types as $value) {
$arrayresult[$i]['adeherent'] = $adherent;
$arrayresult[$i]['prestation'][$value['lib']] = $value;
$arrayresult[$i]['dette'] = $dette;
}}
else{
$arrayresult[$i]['adeherent'] = $adherent;
$arrayresult[$i]['prestation'] = array();
$arrayresult[$i]['dette'] = $dette;
}
$i++;
}
$i=0;
foreach ($adherents as $adherent) {
$types = $services->getSumPrestation(['id' => $adherent->getId()]);
$dette= $services->getSumDette(['id' => $adherent->getId()]);
$arrayresult2[$i]['adeherent'] = $adherent;
$arrayresult2[$i]['p'] = $types;
$arrayresult2[$i]['d'] = $dette;
$i++;
}
// dump($arrayresult2);die();
// dump($arrayresult);die();
return $this->render('adherent/indexStat.html.twig', [
'adherents' => $arrayresult,"type"=>$typePrestation,'adherents2' => $arrayresult2
]);
}
/**
* @Route("/Admin/new", name="app_adherent_new", methods={"GET", "POST"})
* @Route("/Admin/{id}/edit", name="app_adherent_edit", methods={"GET", "POST"})
*/
public function new(Request $request, Adherent $adherent = null, AdherentRepository $adherentRepository, UserPasswordHasherInterface $passwordHasher, AppSevices $serviece): Response {
if (!$adherent) {
$adherent = new Adherent();
$type = 'i';
} else {
$type = 'u';
}
if (in_array('ROLE_ADMIN', $serviece->getUser()->getRoles(), true)) {
$form = $this->createForm(AdherentType::class, $adherent);
} else {
$form = $this->createForm(\App\Form\AdherentType_1::class, $adherent);
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($type == 'i') {
$user = new \App\Entity\User();
$hashedPassword = $passwordHasher->hashPassword(
$user,
"Amicale2023"
);
$user->setPassword($hashedPassword);
} else {
$user = $adherent->getUserCompte();
}
$arrayRole[] = $request->get('role');
if ($request->get('username') != $user->getUsername()) {
$user->setUsername($request->get('username'));
}
if ($arrayRole[0] && $arrayRole[0] != $user->getRoles()[0]) {
$user->setRoles($arrayRole);
}
$adherent->setUserCompte($user);
$adherent->getUserCompte()->setEmailCompte($adherent->getEmail());
$adherentRepository->add($adherent, true);
return $this->redirectToRoute('app_adherent_show', ["id" => $adherent->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('adherent/new.html.twig', [
'adherent' => $adherent,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_adherent_show", methods={"GET"})
*/
public function show(Adherent $adherent, AppSevices $services): Response {
$anne = $services->getAnneExercice(['sort' => 'u.annee', 'order' => 'desc'])->setMaxResults(1)->getQuery()->getOneOrNullResult();
$total = $services->getSumPrestationTotal(['etat' => 'Total', 'id' => $adherent->getId()]);
$types = $services->getByTypePrestation(['id' => $adherent->getId(), 'anne' => $anne->getId()]);
return $this->render('adherent/show.html.twig', [
'adherent' => $adherent, 'type' => $types, 'total' => $total
]);
}
/**
* @Route("/Admin/{id}", name="app_adherent_delete", methods={"POST"})
*/
public function delete(Request $request, Adherent $adherent, AdherentRepository $adherentRepository): Response {
if ($this->isCsrfTokenValid('delete' . $adherent->getId(), $request->request->get('_token'))) {
$adherentRepository->remove($adherent, true);
}
return $this->redirectToRoute('app_adherent_index', [], Response::HTTP_SEE_OTHER);
}
}