<?php
namespace App\Controller;
use App\Entity\ConfigWebsite;
use App\Entity\Contact;
use App\Form\ContactType;
use App\Notification\ContactNotification;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\GameTypeRepository;
use App\Repository\GameRepository;
use App\Repository\GameTicketRepository;
use App\Repository\GameTicketTypeRepository;
use App\Repository\ConfigWebsiteRepository;
use App\Twig\AppExtension;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Contracts\Translation\TranslatorInterface;
use Doctrine\ORM\EntityManagerInterface;
class PageController extends MasterController
{
/**
* @Route({"fr": "/", "en": "/"}, name="app_homepage")
*/
public function homepage(
GameTypeRepository $game_type_repo,
GameRepository $game_repo,
GameTicketRepository $game_ticket_repo,
GameTicketTypeRepository $game_ticket_type_repo,
ConfigWebsiteRepository $cw_repo
) {
$array_game_global = array();
$game_types = $game_type_repo->findAll();
$config_website = $cw_repo->findAll()[0];
$nbr_game_with_ticket = $game_repo->countGameWithTicket();
$gtt_daily = $game_ticket_type_repo->findOneBy(['reference' => 'daily']);
$gtt_loyalty = $game_ticket_type_repo->findOneBy(['reference' => 'loyalty']);
$array_used_daily = $game_ticket_repo->getCountUsedGroupByGame($this->getUser(), null, $gtt_daily);
$array_used_loyalty = $game_ticket_repo->getCountUsedGroupByGame($this->getUser(), null, $gtt_loyalty);
foreach ($game_types as $key_game_type => $value_game_type) {
$array_game_global[$value_game_type->getReference()]['type'] = $value_game_type;
$games = $game_repo->findBy(['gameType' => $value_game_type]);
foreach ($games as $key_game => $value_game) {
$array_game_global[$value_game_type->getReference()]['game'][$key_game]['object'] = $value_game;
$ticket_used_daily = ($this->getUser() and array_key_exists($value_game->getId(), $array_used_daily)) ? $array_used_daily[$value_game->getId()] : 0;
$ticket_used_loyalty = ($this->getUser() and array_key_exists($value_game->getId(), $array_used_loyalty)) ? $array_used_loyalty[$value_game->getId()] : 0;
$array_game_global[$value_game_type->getReference()]['game'][$key_game]['ticket_used_daily'] = $ticket_used_daily;
$array_game_global[$value_game_type->getReference()]['game'][$key_game]['ticket_used_loyalty'] = $ticket_used_loyalty;
$ticket_cost = (!$value_game->getChipCost()) ? ($config_website->getFreeGameEuroPerDay() / $nbr_game_with_ticket / $value_game->getNumberTicketPerDay() / $config_website->getChipValue()) : $value_game->getChipCost();
$array_game_global[$value_game_type->getReference()]['game'][$key_game]['ticket_cost'] = (int) ceil($ticket_cost);
}
}
//dd($array_game_global);
return $this->renderCustom('page/home.html.twig', [
'array_game_global' => $array_game_global,
]);
}
/**
* @Route({"fr": "/contact", "en": "/contact"}, name="app_contact")
*/
public function contact(Request $request, ContactNotification $notification)
{
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact)
->add('Submit', SubmitType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$number_successful_recipients = $notification->notify($contact);
if ($number_successful_recipients > 0) {
$this->addFlash('notice', 'Your message has been sent');
} else {
$this->addFlash('error', 'number_successful_recipients = 0');
}
return $this->redirectToRoute('app_homepage');
}
return $this->renderCustom('page/contact.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @IsGranted("ROLE_USER")
* @Route({"fr": "/conversion", "en": "/conversion"}, name="app_conversion")
*/
public function conversion(Request $request, TranslatorInterface $translator, EntityManagerInterface $em)
{
$config_website = $this->getDoctrine()->getRepository(ConfigWebsite::class)->findOneBy([]);
// dd($config_website);
$array_chip = [10, 30, 100, 300, 1000, 3000, 10000];
$array_choices = array();
foreach ($array_chip as $key => $chip) {
$point = number_format(floor(($chip * $config_website->getChipValue() * $config_website->getPointPerEuro()) * ($config_website->getConversionPercent() / 100)), 0, '.', ' ');
$array_choices[$point . ' ' . $translator->trans('points') . ' -> ' . $chip . ' ' . $translator->trans('chips')] = $chip;
}
//dd($array_choices);
$form = $this->createFormBuilder()
->add('chip', ChoiceType::class, [
'label' => false,
'choices' => $array_choices
])
->add('submit', SubmitType::class, [
'attr' => ['class' => 'btn-block btn-dark']
])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$chip_select = $form->getData()['chip'];
if ($chip_select) { // Danger : select could be deleted by user
$point = (int) (($chip_select * $config_website->getChipValue() * $config_website->getPointPerEuro()) * ($config_website->getConversionPercent() / 100));
if ($this->getUser()->getPoint() < $point) {
$this->addFlash('error', $translator->trans('Not enough points'));
} else {
$this->getUser()->setPoint($this->getUser()->getPoint() - $point)
->setChip($this->getUser()->getChip() + $chip_select);
$em->flush();
$this->addFlash('notice', $translator->trans('Conversion check :') . ' + ' . $chip_select . ' ' . $translator->trans('chips!'));
return $this->redirectToRoute('app_homepage');
}
}
}
return $this->renderCustom('page/conversion.html.twig', [
'form' => $form->createView(),
]);
}
}