<?php
namespace App\Controller;
use App\Entity\Colonne;
use App\Form\CultureType;
use App\Entity\Parcelle;
use App\Entity\Legume;
use App\Entity\Partie;
use App\Entity\HistoriquePlan;
use App\Entity\HistoriqueDonnee;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;
use App\Repository\PartieRepository;
use App\Repository\TypeRepository;
use App\Repository\ParcelleRepository;
use App\Repository\LegumeRepository;
use App\Repository\HistoriquePlanRepository;
use App\Repository\HistoriqueDonneeRepository;
use App\Repository\ColonneRepository;
use Symfony\Component\HttpFoundation\JsonResponse;
class PlanController extends AbstractController
{
#[Route('/', name: 'index_redirect')]
public function indexRedirect(): Response
{
$annee = date('Y');
return $this->redirectToRoute('index', ['annee' => $annee]);
}
#[Route('/plan/{annee}', name: 'index')]
public function index(int $annee, ParcelleRepository $parcelleRepository, HistoriquePlanRepository $historiqueRepository, TypeRepository $typeRepository): Response
{
$annees = $historiqueRepository->findAllYears();
$view = in_array($annee,$annees) ? 'plan/index.html.twig' : 'plan/nouveau.html.twig';
$parcelles = $parcelleRepository->findAll();
$types = $typeRepository->findAll();
return $this->render($view, [
'parcelles' => $parcelles,
'annee' => $annee,
'types' => $types
]);
}
#[Route('/parcelle/{annee}/{id}', name: 'parcelle')]
public function detailParcelle(Request $req, Parcelle $parcelle, int $annee, TypeRepository $typeRepository, ColonneRepository $colonneRepository, HistoriqueDonneeRepository $historiqueRepository): Response
{
$form = $this->createForm(CultureType::class, null, [
'method' => 'POST',
'action' => $this->generateUrl('update_parcelle')
]);
$form->handleRequest($req);
$enregistrements = $historiqueRepository->findAllSorted($parcelle->getId());
$colonnes = $colonneRepository->findAll();
$types = $typeRepository->findAll();
return $this->render('plan/parcelle.html.twig', [
'parcelle' => $parcelle,
'annee' => $annee,
'form' => $form->createView(),
'types' => $types,
'colonnes' => $colonnes,
'enregistrements' => $enregistrements,
'error' => $req->query->has('error') ? $req->query->get('error') : null
]);
}
#[Route('/updateParcelle', name: 'update_parcelle')]
public function updateParcelle(
Request $req,
PartieRepository $partieRepository,
HistoriquePlanRepository $historiqueRepository,
LegumeRepository $legumeRepository,
ManagerRegistry $doctrine)
{
try {
$parcelle = $req->request->get('parcelle');
if (empty($req->get('parties'))) {
throw new Exception('Aucune partie n\'a été sélectionnée.');
}
$isEngrais = false;
if ($req->get('engrais') !== null) {
$isEngrais = true;
}
$historiques = [];
foreach($req->get('parties') as $idPartie) {
$historiques[] = $partieRepository->find($idPartie)->getHistoriqueForPartieByYear($req->request->get('annee'));
}
$manager = $doctrine->getManager();
foreach ($historiques as $historique) {
$h = $historiqueRepository->findOneBy(['annee' => $req->request->get('annee'), 'partie' => $historique->getPartie()->getId()]);
$h->setLegume($legumeRepository->find($req->get('culture')['legume']));
$h->setEngrais($isEngrais);
$manager->persist($h);
}
$manager->flush();
return $this->redirectToRoute('parcelle', [
'annee' => $req->request->get('annee'),
'id' => $parcelle,
'error' => false
]);
} catch (Exception $e) {
return $this->redirectToRoute('parcelle', [
'annee' => $req->request->get('annee'),
'id' => $parcelle,
'error' => true
]);
}
}
#[Route('/plan/nouveau/{annee}', name: 'nouveau_plan')]
public function newParcelle(int $annee, PartieRepository $partieRepository, LegumeRepository $legumeRepository, ManagerRegistry $doctrine): Response
{
$parties = $partieRepository->findAll();
$manager = $doctrine->getManager();
foreach ($parties as $partie) {
$historique = new HistoriquePlan();
$historique->setPartie($partie);
$historique->setAnnee($annee);
$historique->setEngrais(0);
$historique->setLegume($legumeRepository->find(57));
$manager->persist($historique);
}
$manager->flush();
return $this->redirectToRoute('index', ['annee' => $annee]);
}
#[Route('/parcelle/{annee}/{id}/enregistrement', name: 'nouveau_enregistrement')]
public function newSave(Request $req, Parcelle $parcelle, int $annee, LegumeRepository $legumeRepository, ColonneRepository $colonneRepository, HistoriqueDonneeRepository $historiqueRepository, ManagerRegistry $doctrine): Response
{
$colonnes = $colonneRepository->findAll();
$id = $historiqueRepository->findLastId($parcelle->getId()) ? $historiqueRepository->findLastId($parcelle->getId())->getId() + 1 : 1;
$manager = $doctrine->getManager();
foreach ($colonnes as $colonne) {
$enregistrement = new HistoriqueDonnee();
$enregistrement->setId($id);
$enregistrement->setParcelle($parcelle);
$enregistrement->setAnnee($annee);
$enregistrement->setColonne($colonne);
$enregistrement->setValeur($req->request->get('colonne'.$colonne->getId()));
$manager->persist($enregistrement);
}
$manager->flush();
return $this->redirectToRoute('parcelle', ['annee' => $annee, 'id' => $parcelle->getId()]);
}
#[Route('/colonne/nouveau', name: 'nouveau_colonne', methods: 'POST')]
public function newColumn(Request $req, ManagerRegistry $doctrine, HistoriqueDonneeRepository $historiqueDonneeRepository): Response
{
$column = new Colonne();
$column->setNom($req->request->get('name'));
$manager = $doctrine->getManager();
$manager->persist($column);
$manager->flush();
$lines = $historiqueDonneeRepository->findAllDistinct();
/** @var HistoriqueDonnee $line */
foreach ($lines as $line) {
$newLine = new HistoriqueDonnee();
$newLine->setId($line->getId());
$newLine->setAnnee($line->getAnnee());
$newLine->setParcelle($line->getParcelle());
$newLine->setValeur('');
$newLine->setColonne($column);
$manager->persist($newLine);
$manager->flush();
}
$annee = $req->request->get('annee');
$parcelle = $req->request->get('parcelle');
return $this->redirectToRoute('parcelle', ['annee' => $annee, 'id' => $parcelle]);
}
#[Route('/parcelle/{annee}/{parcelle}/ligne/supprimer/{id}', name: 'supprimer_ligne', methods: 'GET')]
public function removeLine(Request $req, ManagerRegistry $doctrine, int $annee, string $parcelle, int $id, HistoriqueDonneeRepository $historiqueDonneeRepository): Response
{
$lines = $historiqueDonneeRepository->findBy(['id' => $id]);
$manager = $doctrine->getManager();
foreach ($lines as $line) {
$manager->remove($line);
}
$manager->flush();
return $this->redirectToRoute('parcelle', ['annee' => $annee, 'id' => $parcelle]);
}
}