AdhesionController.php 2.09 KB
Newer Older
Julien Jorry 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
<?php

namespace App\Controller;

use App\Entity\Adherent;
use App\Entity\Payment;
use App\Form\Type\AdhererFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

class AdhesionController extends AbstractController
{
    /**
     * @Route("/adherer", name="adherer")
     */
    public function adhererAction(Request $request)
    {
        $adherent = new Adherent();

        $form = $this->createForm(AdhererFormType::class, $adherent);
        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            if ($form->isValid()) {
                $adherentNew = $form->getData();

                // Serialize form data in json to store with payment object and persist when payment is valid
                $serializer = $this->container->get('serializer');
                $data = $serializer->normalize(
                    $adherentNew,
                    null,
                    [AbstractNormalizer::ATTRIBUTES => ['user' => ['username', 'email', 'firstname', 'lastname', 'plainPassword'],
                                                      'groupe' => ['id'],
                    'geoloc' => ['adresse',
                        'cpostal',
                        'ville', ], ]]
                );
                $jsondata = $serializer->serialize($data, 'json');

                // Redirect to payment page
                return $this->forward('App\Controller\FluxController::preparePaymentAction', [
                    'form' => $form,
                    'type' => Payment::TYPE_ADHESION,
                    'extra_data' => $jsondata,
                ]);
            } else {
                $this->addFlash(
                    'error',
                    'Problème avec l\'adhésion, veuillez vérifier les informations du formulaire !'
                );
            }
        }

        return $this->render('@kohinos/adherent/adherer.html.twig', [
            'form' => $form->createView(),
        ]);
    }
}