<?php
namespace App\Controller;
use App\Entity\Creation;
use App\Entity\Page;
use App\Form\CreationType;
use App\Repository\CreationRepository;
use App\Repository\PageRepository;
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\HttpFoundation\File\UploadedFile;
/**
* @Route("/")
*/
class HomeController extends AbstractController{
/**
* @Route("/", name="home", methods={"GET"})
*/
public function index(): Response{
return $this->redirectToRoute('homeByLang', ['lang' => 'en']);
}
/**
* @Route("home/legal_notice", name="legal_notice", methods={"GET"})
*/
public function legal_notice(): Response{
return $this->render('home/legal_notice.html.twig', [
'controller_name' => 'HomeController',]);
}
/**
* @Route("home/{lang}/", name="homeByLang", methods={"GET"})
*/
public function homeByLang(Request $request, CreationRepository $creationRepository, PageRepository $pageRepository): Response{
$lang = $request->get('lang');
$page = $pageRepository->findByLang($lang);
$allPages = $pageRepository->findAll();
$creations = $creationRepository->findByLang($lang);
$creationsTamim = $creationRepository->findByTamimByLang($lang);
$creationsSebastien = $creationRepository->findBySebastienByLang($lang);
$creationsRes = [];
$nbCreations = count($creations);
$nbCreationsSebastien = count($creationsSebastien);
$nbCreationsTamim = count($creationsTamim);
$usedRandsSebastien = [];
$usedRandsTamim = [];
$nbCreationsSebUsed = 0;
$nbCreationsTamimUsed = 0;
for($i = 0; $i < $nbCreations; $i++){
if(empty($creationsRes)){
$rand = rand(0, $nbCreationsTamim-1);
$creationsRes[] = $creationsTamim[$rand];
$usedRandsTamim[] = $rand;
$nbCreationsTamimUsed++;
}
if(end($creationsRes)->getOwner() == "tamim" && $nbCreationsSebUsed < $nbCreationsSebastien){
$rand = rand(0, $nbCreationsSebastien-1);
while(in_array($rand, $usedRandsSebastien)){
$rand = rand(0, $nbCreationsSebastien-1);
}
$creationsRes[] = $creationsSebastien[$rand];
$usedRandsSebastien[] = $rand;
$nbCreationsSebUsed++;
}else if(end($creationsRes)->getOwner() == "sebastien" && $nbCreationsTamimUsed < $nbCreationsTamim){
$rand = rand(0, $nbCreationsTamim-1);
while(in_array($rand, $usedRandsTamim)){
$rand = rand(0, $nbCreationsTamim-1);
}
$creationsRes[] = $creationsTamim[$rand];
$usedRandsTamim[] = $rand;
$nbCreationsTamimUsed++;
}else{
if($nbCreationsTamimUsed < $nbCreationsTamim){
$rand = rand(0, $nbCreationsTamim-1);
while(in_array($rand, $usedRandsTamim)){
$rand = rand(0, $nbCreationsTamim-1);
}
$creationsRes[] = $creationsTamim[$rand];
$usedRandsTamim[] = $rand;
$nbCreationsTamimUsed++;
}
if($nbCreationsSebUsed < $nbCreationsSebastien){
$rand = rand(0, $nbCreationsSebastien-1);
while(in_array($rand, $usedRandsSebastien)){
$rand = rand(0, $nbCreationsSebastien-1);
}
$creationsRes[] = $creationsSebastien[$rand];
$usedRandsSebastien[] = $rand;
$nbCreationsSebUsed++;
}
}
}
return $this->render('home/index.html.twig', [
'creations' => $creationsRes,
'allPages' => $allPages,
'mainPage' => $page,
]);
}
}