src/Controller/PageController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ConfigWebsite;
  4. use App\Entity\Contact;
  5. use App\Form\ContactType;
  6. use App\Notification\ContactNotification;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Repository\GameTypeRepository;
  9. use App\Repository\GameRepository;
  10. use App\Repository\GameTicketRepository;
  11. use App\Repository\GameTicketTypeRepository;
  12. use App\Repository\ConfigWebsiteRepository;
  13. use App\Twig\AppExtension;
  14. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  17. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. class PageController extends MasterController
  21. {
  22.     /**
  23.      * @Route({"fr": "/", "en": "/"}, name="app_homepage")
  24.      */
  25.     public function homepage(
  26.         GameTypeRepository $game_type_repo,
  27.         GameRepository $game_repo,
  28.         GameTicketRepository $game_ticket_repo,
  29.         GameTicketTypeRepository $game_ticket_type_repo,
  30.         ConfigWebsiteRepository $cw_repo
  31.     ) {
  32.         $array_game_global = array();
  33.         $game_types $game_type_repo->findAll();
  34.         $config_website $cw_repo->findAll()[0];
  35.         $nbr_game_with_ticket $game_repo->countGameWithTicket();
  36.         $gtt_daily $game_ticket_type_repo->findOneBy(['reference' => 'daily']);
  37.         $gtt_loyalty $game_ticket_type_repo->findOneBy(['reference' => 'loyalty']);
  38.         $array_used_daily $game_ticket_repo->getCountUsedGroupByGame($this->getUser(), null$gtt_daily);
  39.         $array_used_loyalty $game_ticket_repo->getCountUsedGroupByGame($this->getUser(), null$gtt_loyalty);
  40.         foreach ($game_types as $key_game_type => $value_game_type) {
  41.             $array_game_global[$value_game_type->getReference()]['type'] = $value_game_type;
  42.             $games $game_repo->findBy(['gameType' => $value_game_type]);
  43.             foreach ($games as $key_game => $value_game) {
  44.                 $array_game_global[$value_game_type->getReference()]['game'][$key_game]['object'] = $value_game;
  45.                 $ticket_used_daily = ($this->getUser() and array_key_exists($value_game->getId(), $array_used_daily)) ? $array_used_daily[$value_game->getId()] : 0;
  46.                 $ticket_used_loyalty = ($this->getUser() and array_key_exists($value_game->getId(), $array_used_loyalty)) ? $array_used_loyalty[$value_game->getId()] : 0;
  47.                 $array_game_global[$value_game_type->getReference()]['game'][$key_game]['ticket_used_daily'] = $ticket_used_daily;
  48.                 $array_game_global[$value_game_type->getReference()]['game'][$key_game]['ticket_used_loyalty'] = $ticket_used_loyalty;
  49.                 $ticket_cost = (!$value_game->getChipCost()) ? ($config_website->getFreeGameEuroPerDay() / $nbr_game_with_ticket $value_game->getNumberTicketPerDay() / $config_website->getChipValue()) : $value_game->getChipCost();
  50.                 $array_game_global[$value_game_type->getReference()]['game'][$key_game]['ticket_cost'] = (int) ceil($ticket_cost);
  51.             }
  52.         }
  53.         //dd($array_game_global);
  54.         return $this->renderCustom('page/home.html.twig', [
  55.             'array_game_global' => $array_game_global,
  56.         ]);
  57.     }
  58.     /**
  59.      * @Route({"fr": "/contact", "en": "/contact"}, name="app_contact")
  60.      */
  61.     public function contact(Request $requestContactNotification $notification)
  62.     {
  63.         $contact = new Contact();
  64.         $form $this->createForm(ContactType::class, $contact)
  65.             ->add('Submit'SubmitType::class);
  66.         $form->handleRequest($request);
  67.         if ($form->isSubmitted() && $form->isValid()) {
  68.             $number_successful_recipients $notification->notify($contact);
  69.             if ($number_successful_recipients 0) {
  70.                 $this->addFlash('notice''Your message has been sent');
  71.             } else {
  72.                 $this->addFlash('error''number_successful_recipients = 0');
  73.             }
  74.             return $this->redirectToRoute('app_homepage');
  75.         }
  76.         return $this->renderCustom('page/contact.html.twig', [
  77.             'form' => $form->createView(),
  78.         ]);
  79.     }
  80.     /**
  81.      * @IsGranted("ROLE_USER")
  82.      * @Route({"fr": "/conversion", "en": "/conversion"}, name="app_conversion")
  83.      */
  84.     public function conversion(Request $requestTranslatorInterface $translatorEntityManagerInterface $em)
  85.     {
  86.         $config_website $this->getDoctrine()->getRepository(ConfigWebsite::class)->findOneBy([]);
  87.         // dd($config_website);
  88.         $array_chip = [10301003001000300010000];
  89.         $array_choices = array();
  90.         foreach ($array_chip as $key => $chip) {
  91.             $point number_format(floor(($chip $config_website->getChipValue() * $config_website->getPointPerEuro()) * ($config_website->getConversionPercent() / 100)), 0'.'' ');
  92.             $array_choices[$point ' ' $translator->trans('points') . ' -> ' $chip ' ' $translator->trans('chips')] = $chip;
  93.         }
  94.         //dd($array_choices);
  95.         $form $this->createFormBuilder()
  96.             ->add('chip'ChoiceType::class, [
  97.                 'label' => false,
  98.                 'choices'  => $array_choices
  99.             ])
  100.             ->add('submit'SubmitType::class, [
  101.                 'attr' => ['class' => 'btn-block btn-dark']
  102.             ])
  103.             ->getForm();
  104.         $form->handleRequest($request);
  105.         if ($form->isSubmitted() && $form->isValid()) {
  106.             $chip_select $form->getData()['chip'];
  107.             if ($chip_select) { // Danger : select could be deleted by user
  108.                 $point = (int) (($chip_select $config_website->getChipValue() * $config_website->getPointPerEuro()) * ($config_website->getConversionPercent() / 100));
  109.                 if ($this->getUser()->getPoint() < $point) {
  110.                     $this->addFlash('error'$translator->trans('Not enough points'));
  111.                 } else {
  112.                     $this->getUser()->setPoint($this->getUser()->getPoint() - $point)
  113.                         ->setChip($this->getUser()->getChip() + $chip_select);
  114.                     $em->flush();
  115.                     $this->addFlash('notice'$translator->trans('Conversion check :') . ' + ' $chip_select ' ' $translator->trans('chips!'));
  116.                     return $this->redirectToRoute('app_homepage');
  117.                 }
  118.             }
  119.         }
  120.         return $this->renderCustom('page/conversion.html.twig', [
  121.             'form' => $form->createView(),
  122.         ]);
  123.     }
  124. }