<?php
namespace App\Controller\Content\Formulaire;
use App\Entity\Formulaire;
use App\Entity\FormulaireChamp;
use App\Entity\FormulaireChampLiee;
use App\Entity\FormulaireEtape;
use App\Entity\FormulaireInput;
use App\Entity\Projet;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Flasher\SweetAlert\Prime\SweetAlertFactory;
class FormulaireCrudController extends AbstractCrudController
{
private ManagerRegistry $manager;
private SweetAlertFactory $flasher;
private AdminContextProvider $adminContextProvider;
private AdminUrlGenerator $adminUrlGenerator;
public function __construct(
ManagerRegistry $manager,
AdminContextProvider $adminContextProvider,
AdminUrlGenerator $adminUrlGenerator,
SweetAlertFactory $flasher
) {
$this->manager = $manager;
$this->adminContextProvider = $adminContextProvider;
$this->adminUrlGenerator = $adminUrlGenerator;
$this->flasher = $flasher;
}
public static function getEntityFqcn(): string
{
return Formulaire::class;
}
public function configureFields(string $pageName): iterable
{
return [
FormField::addPanel(''),
IdField::new('id')->setSortable(false)->onlyOnIndex(),
TextField::new('nom')->stripTags()->setSortable(false)->setColumns('col-sm-12 col-lg-6 col-xxl-6'),
TextField::new('titre')->stripTags()->setSortable(false)->setColumns('col-sm-12 col-lg-6 col-xxl-6'),
TextareaField::new('descriptionDebut')->setSortable(false)->hideOnIndex()->setColumns('col-sm-12 col-lg-6 col-xxl-6'),
TextareaField::new('descriptionFin')->setSortable(false)->hideOnIndex()->setColumns('col-sm-12 col-lg-6 col-xxl-6'),
/*
AssociationField::new('pages')->setSortable(false)
->setQueryBuilder(function (QueryBuilder $queryBuilder) {
$projet = $this->manager->getRepository(Projet::class)->findOneBy(
['id' => $this->getUser()->getProjet()->getId()]
);
$queryBuilder
->andWhere('entity.projet = :val')
->setParameter('val', $projet);
})->hideOnIndex()->setColumns('col-sm-6 col-lg-6 col-xxl-6'),
AssociationField::new('articles')->setSortable(false)
->setQueryBuilder(function (QueryBuilder $queryBuilder) {
$projet = $this->manager->getRepository(Projet::class)->findOneBy(
['id' => $this->getUser()->getProjet()->getId()]
);
$queryBuilder
->join('entity.type', 'type')
->andWhere('type.projet = :val')
->setParameter('val', $projet);
})->hideOnIndex()->setColumns('col-sm-6 col-lg-6 col-xxl-6'),
*/
AssociationField::new('projet')->setCssClass('d-none')->setSortable(false)
->setColumns('col-sm-6 col-lg-4 col-xxl-4')
->setQueryBuilder(function (QueryBuilder $queryBuilder) {
$queryBuilder->andWhere('entity.id = :val')->setParameter('val', $this->getUser()->getProjet()->getId());
})
->hideOnIndex()
->hideOnDetail()
->hideWhenUpdating(),
FormField::addPanel('Étapes')->onlyWhenUpdating(),
];
}
public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
$queryBuilder = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);
$projet = $this->manager->getRepository(Projet::class)->findOneBy(
['id' => $this->getUser()->getProjet()->getId()]
);
if (0 === count($searchDto->getSort())) {
$queryBuilder
->andWhere('entity.projet = :val')->setParameter('val', $projet);
}
return $queryBuilder;
}
public function configureCrud(Crud $crud): Crud
{
return $crud
// the labels used to refer to this entity in titles, buttons, etc.
->overrideTemplate('crud/index', 'content/formulaire/index.html.twig')
->overrideTemplate('crud/edit', 'content/formulaire/crud/edit.html.twig')
->showEntityActionsInlined()
->renderContentMaximized()
->setPaginatorPageSize(30);
}
public function configureActions(Actions $actions): Actions
{
$preview = Action::new('Aperçu')
->linkToRoute('app_formulaire_preview', function (Formulaire $form): array {
return [
'formid' => $form->getId(),
'projectid' => $this->getUser()->getProjet()->getId(),
//'method' => $order->getUser()->getPreferredSendingMethod(),
];
})
->addCssClass('btn crud-action-preview px-2 py-1 ')
->setIcon('fa fa-binoculars')
->setHtmlAttributes(['target' => '_blank']);
$previewedit = Action::new('Aperçu')
->linkToRoute('app_formulaire_preview', function (Formulaire $form): array {
return [
'formid' => $form->getId(),
'projectid' => $this->getUser()->getProjet()->getId(),
//'method' => $order->getUser()->getPreferredSendingMethod(),
];
})
->addCssClass('btn btn-info px-2 py-1 ')
->setIcon('fa fa-binoculars')
->setHtmlAttributes(['target' => '_blank']);
$crudEtape = Action::new('Etapes')
->linkToCrudAction('toFormulaireEtape')
->addCssClass('btn crud-action-content px-2 py-1 ')
->setIcon('fa fa-list-ol');
$crudChamps = Action::new('Champs')
->linkToCrudAction('toFormulaireChamps')
->addCssClass('btn btn-light px-2 py-1 ')
->setIcon('fa fa-clipboard-list')
->createAsGlobalAction();
return $actions
// ->add(Crud::PAGE_INDEX, $crudEtape)
// ->add(Crud::PAGE_INDEX, $crudChamps)
->add(Crud::PAGE_INDEX, $preview)
->add(Crud::PAGE_EDIT, $previewedit)
->add(Crud::PAGE_NEW, Action::SAVE_AND_CONTINUE)
->update(Crud::PAGE_NEW, Action::SAVE_AND_CONTINUE, function (Action $action) {
return $action->setLabel('Créer et ajouter étape')->addCssClass('btn btn-info px-2 py-1 ');
})
/* ->update(Crud::PAGE_INDEX, Action::DETAIL, function (Action $action) {
return $action->setIcon('fa fa-info')->addCssClass('btn crud-action-details px-2 py-1 ');
}) */
->update(Crud::PAGE_INDEX, Action::EDIT, function (Action $action) {
return $action->setIcon('fa fa-pen')->addCssClass('btn crud-action-edit px-2 py-1 ');
})
->update(Crud::PAGE_INDEX, Action::DELETE, function (Action $action) {
return $action->setIcon('fa fa-trash')->addCssClass('btn px-2 py-1');
});
}
public function toFormulaireEtape(AdminContext $context)
{
$url = $this->adminUrlGenerator
->setController(FormulaireEtapeCrudController::class)
->setAction(Action::INDEX)
->set('parentform', $context->getEntity()->getInstance()->getId())
->unset('entityId')
->generateUrl();
return $this->redirect($url);
}
public function toFormulaireChamps(AdminContext $context)
{
$url = $this->adminUrlGenerator
->setController(FormulaireChampCrudController::class)
->setAction(Action::INDEX)
->unset('entityId')
->generateUrl();
return $this->redirect($url);
}
public function ajouterEtape(AdminContext $context)
{
$entityManager = $this->manager->getManager();
$idForm = $context->getEntity()->getInstance()->getId();
$titre = $context->getRequest()->get('titre');
$ordre = $context->getRequest()->get('ordre');
$description = $context->getRequest()->get('description');
$secure = $context->getRequest()->get('secure');
$actif = $context->getRequest()->get('actifetape');
$save = $context->getRequest()->get('save');
$formulaire = $this->manager->getRepository(Formulaire::class)->findOneBy(['id' => $idForm]);
$formEtape = new FormulaireEtape();
//$contenu = new PageContenu();
if ($titre != '') {
$formEtape->setTitre($titre);
}
if ($ordre != '' && $ordre != 0) {
$formEtape->setOrdre($ordre);
}
if ($description != '') {
$formEtape->setDescription($description);
}
if ($secure != '' && $secure != 'Rien') {
$formEtape->setSecure($secure);
if ($formEtape->getSecure() == 'Email') {
// TODO add champliee Email
$formChamp = $this->manager->getRepository(FormulaireChamp::class)->findOneBy(['nom' => 'contact_email_secure']);
if ($formChamp != null) {
$champLiee = new FormulaireChampLiee();
$champLiee->setFormulaireChamp($formChamp);
$champLiee->setFormulaireEtape($formEtape);
$champLiee->setObligatoire(true);
$entityManager->persist($champLiee);
}
} else if ($formEtape->getSecure() == 'SMS') {
// TODO add champliee SMS
$formChamp = $this->manager->getRepository(FormulaireChamp::class)->findOneBy(['nom' => 'contact_tel_secure']);
if ($formChamp != null) {
$champLiee = new FormulaireChampLiee();
$champLiee->setObligatoire(true);
$champLiee->setFormulaireChamp($formChamp);
$champLiee->setFormulaireEtape($formEtape);
$champLiee->setObligatoire(true);
$entityManager->persist($champLiee);
}
}
}
if ($actif != '') {
$formEtape->setActif($actif);
} else {
$formEtape->setActif(false);
}
if ($save != '') {
$formEtape->setSave($save);
}
$formEtape->setFormulaire($formulaire);
$formEtape->setIdImport(date('Y-m-d-H-i-s') . '-formulaire-etape-' . $formEtape->getId() . "-" . $formEtape->getFormulaire()->getId() . "-" . $formEtape->getFormulaire()->getProjet()->getId());
$formEtape->setIdImportFormulaire($formEtape->getFormulaire()->getIdImport());
$entityManager->persist($formEtape);
$entityManager->flush();
$url = $this->adminUrlGenerator
->setController(FormulaireCrudController::class)
->setAction(Action::EDIT)
// ->set('tabactive', 'contenu')
->generateUrl();
return $this->redirect($url);
}
public function editEtape(AdminContext $context)
{
$entityManager = $this->manager->getManager();
$idFormEtape = $context->getRequest()->get('etapeId');
$titre = $context->getRequest()->get('titre');
$ordre = $context->getRequest()->get('ordre');
$description = $context->getRequest()->get('description');
$secure = $context->getRequest()->get('secure' . $idFormEtape);
$actif = $context->getRequest()->get('actifetape' . $idFormEtape);
$save = $context->getRequest()->get('save' . $idFormEtape);
$formEtape = $this->manager->getRepository(FormulaireEtape::class)->findOneBy(['id' => $idFormEtape]);
//$contenu = new PageContenu();
if ($titre != '') {
$formEtape->setTitre($titre);
}
if ($ordre != '' && $ordre != 0) {
$formEtape->setOrdre($ordre);
} else {
$formEtape->setOrdre(null);
}
if ($description != '') {
$formEtape->setDescription($description);
} else {
$formEtape->setDescription(null);
}
if ($secure != $formEtape->getSecure()) {
$formChampEmail = $this->manager->getRepository(FormulaireChamp::class)->findOneBy(['nom' => 'contact_email_secure']);
if ($formChampEmail != null) {
$champLieeEmail = $this->manager->getRepository(FormulaireChampLiee::class)->findOneBy(['formulaireChamp' => $formChampEmail, 'formulaireEtape' => $formEtape]);
if ($champLieeEmail != null) {
$entityManager->remove($champLieeEmail);
}
}
$formChampTel = $this->manager->getRepository(FormulaireChamp::class)->findOneBy(['nom' => 'contact_tel_secure']);
if ($formChampTel != null) {
$champLieeTel = $this->manager->getRepository(FormulaireChampLiee::class)->findOneBy(['formulaireChamp' => $formChampTel, 'formulaireEtape' => $formEtape]);
if ($champLieeTel != null) {
$entityManager->remove($champLieeTel);
}
}
if ($secure != '' && $secure != 'Rien') {
$formEtape->setSecure($secure);
if ($formEtape->getSecure() == 'Email') {
// TODO add champliee Email
$formChamp = $this->manager->getRepository(FormulaireChamp::class)->findOneBy(['nom' => 'contact_email_secure']);
if ($formChamp == null) {
$formChamp = new FormulaireChamp();
$formChamp->setNom('contact_email_secure');
$formChamp->setQuestion('Saisissez votre adresse e-mail pour recevoir le code confidentiel');
$formChamp->setActif(true);
$formInput = $this->manager->getRepository(FormulaireInput::class)->findOneBy(['type' => 'email']);
if ($formInput == null) {
$formInput = new FormulaireInput();
$formInput->setType('email');
$formInput->setActif(true);
$formInput->setProjet($formEtape->getFormulaire()->getProjet());
$entityManager->persist($formInput);
}
$formChamp->setFormulaireInput($formInput);
$entityManager->persist($formChamp);
}
$champLiee = $this->manager->getRepository(FormulaireChampLiee::class)->findOneBy(['formulaireChamp' => $formChamp, 'formulaireEtape' => $formEtape]);
if ($champLiee == null) {
$champLiee = new FormulaireChampLiee();
$champLiee->setFormulaireChamp($formChamp);
$champLiee->setFormulaireEtape($formEtape);
}
$champLiee->setObligatoire(true);
$entityManager->persist($champLiee);
} else if ($formEtape->getSecure() == 'SMS') {
// TODO add champliee SMS
$formChamp = $this->manager->getRepository(FormulaireChamp::class)->findOneBy(['nom' => 'contact_tel_secure']);
if ($formChamp == null) {
$formChamp = new FormulaireChamp();
$formChamp->setNom('contact_tel_secure');
$formChamp->setQuestion('Saisissez votre numéro du téléphone pour recevoir le code confidentiel ');
$formChamp->setActif(true);
$formInput = $this->manager->getRepository(FormulaireInput::class)->findOneBy(['type' => 'tel']);
if ($formInput == null) {
$formInput = new FormulaireInput();
$formInput->setType('tel');
$formInput->setActif(true);
$formInput->setProjet($formEtape->getFormulaire()->getProjet());
$entityManager->persist($formInput);
}
$formChamp->setFormulaireInput($formInput);
$entityManager->persist($formChamp);
}
$champLiee = $this->manager->getRepository(FormulaireChampLiee::class)->findOneBy(['formulaireChamp' => $formChamp, 'formulaireEtape' => $formEtape]);
if ($champLiee == null) {
$champLiee = new FormulaireChampLiee();
$champLiee->setFormulaireChamp($formChamp);
$champLiee->setFormulaireEtape($formEtape);
}
$champLiee->setObligatoire(true);
$entityManager->persist($champLiee);
}
} else {
$formEtape->setSecure(null);
}
/* else {
$formChampEmail = $this->manager->getRepository(FormulaireChamp::class)->findOneBy(['nom' => 'contact_email_secure']);
if ($formChampEmail != null) {
$champLieeEmail = $this->manager->getRepository(FormulaireChampLiee::class)->findOneBy(['formulaireChamp' => $formChampEmail, 'formulaireEtape' => $formEtape]);
if ($champLieeEmail != null) {
$entityManager->remove($champLieeEmail);
}
}
$formChampTel = $this->manager->getRepository(FormulaireChamp::class)->findOneBy(['nom' => 'contact_tel_secure']);
if ($formChampTel != null) {
$champLieeTel = $this->manager->getRepository(FormulaireChampLiee::class)->findOneBy(['formulaireChamp' => $formChampTel, 'formulaireEtape' => $formEtape]);
if ($champLieeTel != null) {
$entityManager->remove($champLieeTel);
}
}
} */
}
if ($actif != '') {
$formEtape->setActif($actif);
} else {
$formEtape->setActif(false);
}
if ($save != '') {
$formEtape->setSave($save);
} else {
$formEtape->setSave(false);
}
if ($formEtape->getIdImport() == null || $formEtape->getIdImport() == '') {
$formEtape->setIdImport(date('Y-m-d-H-i-s') . '-formulaire-etape-' . $formEtape->getId() . "-" . $formEtape->getFormulaire()->getId() . "-" . $formEtape->getFormulaire()->getProjet()->getId());
}
$formEtape->setIdImportFormulaire($formEtape->getFormulaire()->getIdImport());
$entityManager->flush();
$url = $this->adminUrlGenerator
->setController(FormulaireCrudController::class)
->setAction(Action::EDIT)
// ->set('tabactive', 'contenu')
->generateUrl();
return $this->redirect($url);
}
public function ajouterChampLiee(AdminContext $context)
{
$etapeId = $context->getRequest()->get('etapeChampId');
$champId = $context->getRequest()->get('champ');
$ordre = $context->getRequest()->get('ordre');
$obligatoire = $context->getRequest()->get('obligatoire');
$etape = $this->manager->getRepository(FormulaireEtape::class)->findOneBy(['id' => $etapeId]);
$champ = $this->manager->getRepository(FormulaireChamp::class)->findOneBy(['id' => $champId]);
$champLiee = new FormulaireChampLiee();
//$contenu = new PageContenu();
if ($ordre != '') {
$champLiee->setOrdre($ordre);
}
if ($obligatoire != '') {
if ($obligatoire == 'on') {
$obligatoire = true;
}
$champLiee->setObligatoire($obligatoire);
} else {
$obligatoire = false;
$champLiee->setObligatoire($obligatoire);
}
$champLiee->setFormulaireEtape($etape);
$champLiee->setFormulaireChamp($champ);
if ($champ->getNom() == 'contact_email_secure') {
$champLiee->getFormulaireEtape()->setSecure('Email');
$champLiee->setObligatoire(true);
} else if ($champ->getNom() == 'contact_tel_secure') {
$champLiee->getFormulaireEtape()->setSecure('SMS');
$champLiee->setObligatoire(true);
} else {
$champLiee->getFormulaireEtape()->setSecure(null);
}
$champLiee->setIdImport(date('Y-m-d-H-i-s') . '-formulaire-champ-liee-' . $champLiee->getId() . "-" . $champLiee->getFormulaireEtape()->getId() . "-" . $champLiee->getFormulaireEtape()->getFormulaire()->getId() . "-" . $champLiee->getFormulaireEtape()->getFormulaire()->getProjet()->getId());
$champLiee->setIdImportChamp($champLiee->getFormulaireChamp()->getIdImport());
$champLiee->setIdImportEtape($champLiee->getFormulaireEtape()->getIdImport());
$entityManager = $this->manager->getManager();
$entityManager->persist($champLiee);
$entityManager->flush();
$url = $this->adminUrlGenerator
->setController(FormulaireCrudController::class)
->setAction(Action::EDIT)
// ->set('tabactive', 'contenu')
->generateUrl();
return $this->redirect($url);
}
public function editChampLiee(AdminContext $context)
{
$champLieeId = $context->getRequest()->get('champLieeId');
$champId = $context->getRequest()->get('champ');
$ordre = $context->getRequest()->get('ordre');
$obligatoire = $context->getRequest()->get('obligatoire');
$champLiee = $this->manager->getRepository(FormulaireChampLiee::class)->findOneBy(['id' => $champLieeId]);
$champ = $this->manager->getRepository(FormulaireChamp::class)->findOneBy(['id' => $champId]);
//$champLiee = new FormulaireChampLiee();
//$contenu = new PageContenu();
if ($ordre != '') {
$champLiee->setOrdre($ordre);
} else {
$champLiee->setOrdre(null);
}
if ($obligatoire != '') {
if ($obligatoire == 'on') {
$obligatoire = true;
}
$champLiee->setObligatoire($obligatoire);
} else {
$obligatoire = false;
$champLiee->setObligatoire($obligatoire);
}
if ($champLiee->getFormulaireChamp()->getNom() == 'contact_email_secure' || $champLiee->getFormulaireChamp()->getNom() == 'contact_tel_secure') {
if ($champ->getNom() == 'contact_email_secure') {
$champLiee->getFormulaireEtape()->setSecure('Email');
$champLiee->setObligatoire(true);
} else if ($champ->getNom() == 'contact_tel_secure') {
$champLiee->getFormulaireEtape()->setSecure('SMS');
$champLiee->setObligatoire(true);
} else {
$champLiee->getFormulaireEtape()->setSecure(null);
}
}
$champLiee->setFormulaireChamp($champ);
if ($champLiee->getIdImport() == null || $champLiee->getIdImport() == '') {
$champLiee->setIdImport(date('Y-m-d-H-i-s') . '-formulaire-champ-liee-' . $champLiee->getId() . "-" . $champLiee->getFormulaireEtape()->getId() . "-" . $champLiee->getFormulaireEtape()->getFormulaire()->getId() . "-" . $champLiee->getFormulaireEtape()->getFormulaire()->getProjet()->getId());
}
$champLiee->setIdImportChamp($champLiee->getFormulaireChamp()->getIdImport());
$champLiee->setIdImportEtape($champLiee->getFormulaireEtape()->getIdImport());
$entityManager = $this->manager->getManager();
$entityManager->flush();
$url = $this->adminUrlGenerator
->setController(FormulaireCrudController::class)
->setAction(Action::EDIT)
// ->set('tabactive', 'contenu')
->generateUrl();
return $this->redirect($url);
}
public function supprimerEtape(AdminContext $context)
{
$entityManager = $this->manager->getManager();
$idFormEtape = $context->getRequest()->get('etapeId');
$formEtape = $this->manager->getRepository(FormulaireEtape::class)->findOneBy(['id' => $idFormEtape]);
$entityManager->remove($formEtape);
$entityManager->flush();
$url = $this->adminUrlGenerator
->setController(FormulaireCrudController::class)
->setAction(Action::EDIT)
->generateUrl();
return $this->redirect($url);
}
public function supprimerChampLiee(AdminContext $context)
{
$entityManager = $this->manager->getManager();
$champLieeId = $context->getRequest()->get('champLieeId');
$champLiee = $this->manager->getRepository(FormulaireChampLiee::class)->findOneBy(['id' => $champLieeId]);
if ($champLiee->getFormulaireChamp()->getNom() == 'contact_email_secure' || $champLiee->getFormulaireChamp()->getNom() == 'contact_tel_secure') {
$champLiee->getFormulaireEtape()->setSecure(null);
}
$entityManager->remove($champLiee);
$entityManager->flush();
$url = $this->adminUrlGenerator
->setController(FormulaireCrudController::class)
->setAction(Action::EDIT)
->generateUrl();
return $this->redirect($url);
}
}