src/Controller/EspaceAffilie/SubscribeController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller\EspaceAffilie;
  3. use App\Form\SubscribeFormType;
  4. use App\Service\Generale;
  5. use App\Service\WSCMS;
  6. use App\Service\WSEspaceAffilie;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface as Session;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route("/subscribe")
  15.  */
  16. class SubscribeController extends AbstractController
  17. {
  18.     private $service;
  19.     public function __construct(Generale $service)
  20.     {
  21.         $this->service = $service;
  22.     }
  23.     /**
  24.      * @Route("/", name="subscribe")
  25.      */
  26.     public function subscribe(WSCMS $WSCMS, WSEspaceAffilie $affilie, Session $session, Request $request): Response
  27.     {
  28.         $pays = array_column($WSCMS->Pays()['Pays'], 'id', 'name');
  29.         $_pays = array_column($WSCMS->Pays()['Pays'], 'id', 'code');
  30.         $agence = $WSCMS->Agence()['Agence'];
  31.         $form = $this->get('form.factory')->createNamed('', SubscribeFormType::class, null, [
  32.             'pays' => $pays,
  33.             'nationalite' => $_pays[$agence['nationalite']],
  34.             'csrf_protection' => true
  35.         ])->add('js_ok');
  36.         if (isset($_SERVER['LIST_OFFICES']))
  37.             $form->add('bureau', ChoiceType::class, [
  38.                 'choices' => array_flip(json_decode($_SERVER['LIST_OFFICES'], true)),
  39.                 'required' => false
  40.             ]);
  41.         $form->handleRequest($request);
  42.         if ($form->isSubmitted()) {
  43.             if ($form->isValid()) {
  44.                 $data = $form->getData();
  45.                 if ($data['js_ok'] !== '1') {
  46.                     exit; // bot probable
  47.                 }
  48.                 // à la soumission
  49.                 if (time() - $_SESSION['form_time'] < 3) {
  50.                     exit; // robot
  51.                 }
  52.                 if ($data['file'])
  53.                     $data['file'] = json_encode([
  54.                         'base64' => base64_encode(file_get_contents($data['file']->getPathName())),
  55.                         'originalName' => $data['file']->getClientOriginalName(),
  56.                         'size' => $data['file']->getSize(),
  57.                         'mimeType' => $data['file']->getMimeType()
  58.                     ]);
  59.                 if (isset($data['bureau'])) {
  60.                     $session->set('DOMAINE_BACK', $data['bureau']);
  61.                     $data['bureau'] = json_decode($_SERVER['LIST_OFFICES'], true)[$data['bureau']];
  62.                 }
  63.                 unset($data['js_ok']);
  64.                 $subscribe = $affilie->Subscribe($data);
  65.                 if (isset($subscribe['Erreur']))
  66.                     $this->addFlash('danger', $subscribe['Erreur']);
  67.                 elseif (isset($subscribe['Error']))
  68.                     $this->addFlash('danger', $subscribe['Error']);
  69.                 elseif (isset($subscribe['succes']) && $subscribe['succes'] == 'ok')
  70.                     return $this->redirectToRoute('sucess_subscribe');
  71.             } else {
  72.                 $this->addFlash('danger', (string)$form->getErrors(true, false));
  73.                 return $this->redirect($_SERVER['HTTP_REFERER']);
  74.             }
  75.         }
  76.         $_SESSION['form_time'] = time();
  77.         return $this->render($this->service->checkCustomTemplate('EspaceAffilie/Subscribe/subscribe.html.twig'), [
  78.             'form' => $form->createView()
  79.         ]);
  80.     }
  81.     /**
  82.      * @Route("/sucess", name="sucess_subscribe")
  83.      */
  84.     public function showSuccesAction()
  85.     {
  86.         return $this->render($this->service->checkCustomTemplate('EspaceAffilie/Subscribe/sucess_subscribe.html.twig'));
  87.     }
  88. }