UserController.php 4.21 KB
Newer Older
Damien Moulard committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
<?php

namespace App\Controller;

use App\Entity\User;
use App\Entity\CotisationAdherent;
use App\Entity\CotisationPrestataire;
use App\Entity\Payment;
use App\Enum\MoyenEnum;
use App\Form\Type\CotiserFormType;
use App\Form\Type\UserInfosFormType;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Security\Core\Security;

class UserController extends AbstractController
{
    private $em;
    private $translator;
    private $security;

    public function __construct(EntityManagerInterface $em, TranslatorInterface $translator, Security $security)
    {
        $this->em = $em;
        $this->translator = $translator;
        $this->security = $security;
    }

    /**
     * @Route("/cotiser", name="cotiser")
     * @IsGranted("ROLE_USER")
     */
    public function cotiserAction(Request $request)
    {
        $options = [];
        $payment_type = '';
        if ($this->security->getUser()->isGranted('ROLE_ADHERENT')) {
            $options['data_class'] = CotisationAdherent::class;
            $payment_type = Payment::TYPE_COTISATION_ADHERENT;
        } elseif ($this->security->getUser()->isGranted('ROLE_PRESTATAIRE')) {
            $options['data_class'] = CotisationPrestataire::class;
            $payment_type = Payment::TYPE_COTISATION_PRESTA;
        }

        $form = $this->createForm(CotiserFormType::class, null, $options);
        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            $cotisation = $form->getData();
            if ($form->get('payMLC')->isClicked()) {
                $cotisation->setMoyen(MoyenEnum::MOYEN_MLC);
            } else {
                $cotisation->setMoyen(MoyenEnum::MOYEN_CB);
            }
            if ($form->isValid()) {
                if ($form->get('payMLC')->isClicked()) {
                    $cotisation->setRecu(true);
                    $this->em->persist($cotisation);
                    $this->em->flush();
                    $this->addFlash(
                        'success',
                        // "Cotisation pour l'année {$cotisation->getCotisationInfos()->getAnnee()} bien reçue. Merci !"
                        $this->translator->trans("Cotisation bien reçue. Merci !")
                    );
                    return $this->redirectToRoute('index');
                } else {
                    // Redirect to payment page
                    return $this->forward('App\Controller\FluxController::preparePaymentAction', [
                        'form'  => $form,
                        'type'  => $payment_type
                    ]);
                }
            } else {
                $this->addFlash(
                    'error',
                    $this->translator->trans('Problème avec la cotisation !')
                );
            }
        }

        return $this->render('cotiser.html.twig', array(
            'form' => $form->createView()
        ));
    }

    /**
     * @Route("/userinfos", name="user_infos")
     * @IsGranted("ROLE_USER")
     */
    public function userInfosAction(Request $request)
    {
        $form = $this->createForm(UserInfosFormType::class, $this->getUser());
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->persist($form->getData());
            $this->em->flush();
            $this->addFlash(
                'success',
                $this->translator->trans("Infos de l'utilisateur modifiée !")
            );
            $referer = $request->headers->get('referer');
            if ($referer && !$request->isXmlHttpRequest()) {
                return $this->redirect($referer);
            } elseif (!$request->isXmlHttpRequest()) {
                return new Response('', Response::HTTP_BAD_REQUEST);
            }
        }
        return $this->redirectToRoute('index');
        // return $this->render('presta/infos.html.twig', array(
        //     'form' => $form->createView()
        // ));
    }
}