<?php
namespace App\Controller\Admin;
use App\Entity\Autres;
use App\Entity\DemandeService;
use App\Entity\Feedback;
use App\Entity\FormationsService;
use App\Entity\InterventionEntrepriseService;
use App\Entity\OtherService;
use App\Entity\ProgrammeFormationService;
use App\Entity\Service;
use App\Form\DemandeServiceFormType;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\TextEditorType;
use Symfony\Contracts\Translation\TranslatorInterface;
class ServiceCrudController extends AbstractCrudController
{
private $request;
/**
* @param RequestStack $request
*/
public function __construct(RequestStack $request, EntityManagerInterface $em)
{
$this->request = $request;
$this->em = $em;
}
public static function getEntityFqcn(): string
{
return Service::class;
}
public function configureFields(string $pageName): iterable
{
return [
IdField::new('id')->hideOnForm(),
TextField::new('axe'),
TextareaField::new('description', 'description')->formatValue(function ($value) {
return $value ;
})->setColumns('col-sm-12 col-md-12 col-lg-12 col-xxl-12')
->setTemplatePath('admin/fields/details.html.twig')->setRequired($pageName !== Crud::PAGE_EDIT)->hideOnIndex(),
// TextareaField::new('details', 'détails')->formatValue(function ($value) {
// return $value ;
// })->setColumns('col-sm-12 col-md-12 col-lg-12 col-xxl-12')
// ->setTemplatePath('admin/fields/details.html.twig')->setRequired($pageName !== Crud::PAGE_EDIT)->hideOnIndex(),
];
}
public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
$entityInstance = new OtherService();
$entityInstance->setAxe($this->request->getCurrentRequest()->request->all()['Service']['axe']);
$entityInstance->setDescription($this->request->getCurrentRequest()->request->all()['Service']['description']);
$entityManager->persist($entityInstance);
$entityManager->flush();
}
#[Route('/show/service/detail/{id}', name: 'show_service_detail')]
public function showByServiceDetail(Request $request, EntityManagerInterface $em, TranslatorInterface $translator): Response
{
$averageStars = [] ;
$id = (int) $request->attributes->get('id');
$service = $em->getRepository(Service::class)->find($id);
$isAllowed = false;
$isShowable = false;
$isNotAllowedMessage = $translator->trans('attention.message2');
if ( !$service ){
return $this->redirectToRoute('accueil');
}
switch ($service) {
case $service instanceof InterventionEntrepriseService:
if( $this->isGranted('ROLE_ENTREPRISES') ){
$isAllowed = true;
}else{
$isNotAllowedMessage = $translator->trans('attention.message3');
}
$isShowable = true;
break;
case $service instanceof FormationsService:
if( $this->isGranted('ROLE_ENTREPRISES') || $this->isGranted('ROLE_PARTENAIRES_NATIONAUX') || $this->isGranted('ROLE_PARTENAIRES_REGIONAUX') ){
$isAllowed = true;
}else{
$isNotAllowedMessage = $translator->trans('attention.message4');
}
$isShowable = true;
break;
case $service instanceof ProgrammeFormationService:
if( $this->isGranted('ROLE_PARTENAIRES_NATIONAUX') || $this->isGranted('ROLE_PARTENAIRES_REGIONAUX') ){
$isAllowed = true;
}else{
$isNotAllowedMessage = $translator->trans('attention.message5');
}
$isShowable = true;
break;
case $service instanceof OtherService:
$isAllowed = false;
$isShowable = false;
break;
default:
$isShowable = true;
$isNotAllowedMessage = $translator->trans('attention.message6');
}
$averageStars[$service->getId()] = $em->getRepository(Feedback::class)->GetAverageCountServiceStars($service->getId());
return $this->render('service/detail.html.twig', [
'service' => $service,
'isShowable' => $isShowable,
'isAllowed' => $isAllowed,
'isNotAllowedMessage' => $isNotAllowedMessage,
'averageStars' => $averageStars
]);
}
#[Route('/list/service/index/', name: 'index_service')]
public function listServices(Request $request, EntityManagerInterface $entityManager, PaginatorInterface $paginator): Response
{
$donnees = $entityManager->getRepository(Service::class)->findAll();
$services = $paginator->paginate(
$donnees,
$request->query->getInt('page', 1),
10
);
foreach($services as $service){
$averageStars[$service->getId()] = $entityManager->getRepository(Feedback::class)->getAverageStarsByService($service->getId());
}
return $this->render('service/index.html.twig', [
'services'=>$services,
'averageStars'=>$averageStars
]);
}
public function configureActions(Actions $actions): Actions
{
$actions->add(Crud::PAGE_INDEX, Action::DETAIL)
->update(Crud::PAGE_INDEX, Action::NEW, function (Action $action) {
return $action->setIcon('fa fa-plus')->setLabel('Créer un service');
})->update(Crud::PAGE_NEW, Action::SAVE_AND_RETURN, function (Action $action) {
return $action->setLabel('Enregistrer');
});
return $actions;
}
public function configureCrud(Crud $crud): Crud
{
return $crud ->setPageTitle('index', 'Liste des Services')
->setPageTitle('detail', 'Détails de Service')
->setPageTitle('new', 'Créer un service')
->setPageTitle('edit', 'Modifier un service');
}
}