<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Project;
|
|
use App\Entity\Task;
|
|
use App\Form\ProjectType;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
#[Route('/project')]
|
|
class ProjectController extends AbstractController
|
|
{
|
|
|
|
#[Route('/', name: 'app_project')]
|
|
public function index(EntityManagerInterface $entityManager): Response
|
|
{
|
|
$projects = $entityManager->getRepository(Project::class)->findAll();
|
|
|
|
$this->addFlash(
|
|
'info',
|
|
sprintf('%s projet%s trouvé%s', count($projects), (count($projects) > 1 ? 's' : ''), (count($projects) > 1 ? 's' : ''))
|
|
);
|
|
|
|
return $this->render('project/index.html.twig', [
|
|
'projects' => $projects,
|
|
]);
|
|
}
|
|
|
|
#[Route('/create', name: 'app_project_create')]
|
|
public function create(Request $request, EntityManagerInterface $entityManager): Response
|
|
{
|
|
$project = new Project();
|
|
$createForm = $this->createForm(ProjectType::class, $project);
|
|
$createForm->add('submit', SubmitType::class, [
|
|
'label' => 'Créer',
|
|
]);
|
|
|
|
$createForm->handleRequest($request);
|
|
if ($createForm->isSubmitted() and $createForm->isValid()) {
|
|
$project = $createForm->getData();
|
|
|
|
try {
|
|
$entityManager->persist($project);
|
|
$entityManager->flush();
|
|
|
|
$this->addFlash(
|
|
'success',
|
|
'Projet créé !'
|
|
);
|
|
|
|
return $this->redirectToRoute('app_project');
|
|
} catch (\Exception $exception) {
|
|
$this->addFlash(
|
|
'danger',
|
|
'Impossible de créer le projet !'
|
|
);
|
|
}
|
|
}
|
|
|
|
return $this->render('project/create.html.twig', [
|
|
'create_form' => $createForm,
|
|
]);
|
|
}
|
|
|
|
#[Route('/show/{projectSlug}', name: 'app_project_show')]
|
|
public function show(EntityManagerInterface $entityManager, $projectSlug): Response
|
|
{
|
|
$project = $entityManager->getRepository(Project::class)->findOneBySlug($projectSlug);
|
|
$tasks = $entityManager->getRepository(Task::class)->findByProjectPaginated($project);
|
|
|
|
if (!$project) {
|
|
$this->addFlash(
|
|
'warning',
|
|
'Projet non trouvé !'
|
|
);
|
|
|
|
return $this->redirectToRoute('app_project');
|
|
}
|
|
|
|
$count = count($project->getTasks());
|
|
$this->addFlash(
|
|
'info',
|
|
sprintf('%s tâche%s trouvée%s', $count, ($count > 1 ? 's' : ''), ($count > 1 ? 's' : ''))
|
|
);
|
|
|
|
return $this->render('project/show.html.twig', [
|
|
'project' => $project,
|
|
'tasks' => $tasks,
|
|
]);
|
|
}
|
|
|
|
#[Route('/update/{projectSlug}', name: 'app_project_update')]
|
|
public function update(Request $request, EntityManagerInterface $entityManager, $projectSlug): Response
|
|
{
|
|
$repository = $entityManager->getRepository(Project::class);
|
|
$project = $repository->findOneBySlug($projectSlug);
|
|
|
|
if (!$project) {
|
|
$this->addFlash(
|
|
'warning',
|
|
'Projet non trouvé !'
|
|
);
|
|
|
|
return $this->redirectToRoute('app_project');
|
|
}
|
|
|
|
$updateForm = $this->createForm(ProjectType::class, $project);
|
|
$updateForm->add('submit', SubmitType::class, [
|
|
'label' => 'Modifier',
|
|
]);
|
|
|
|
$updateForm->handleRequest($request);
|
|
if ($updateForm->isSubmitted() and $updateForm->isValid()) {
|
|
$project = $updateForm->getData();
|
|
|
|
try {
|
|
$entityManager->persist($project);
|
|
$entityManager->flush();
|
|
|
|
$this->addFlash(
|
|
'success',
|
|
'Projet modifié !'
|
|
);
|
|
|
|
return $this->redirectToRoute('app_project');
|
|
} catch (\Exception $exception) {
|
|
$this->addFlash(
|
|
'danger',
|
|
'Impossible de mopdifier le projet !'
|
|
);
|
|
}
|
|
}
|
|
|
|
return $this->render('project/update.html.twig', [
|
|
'project' => $project,
|
|
'update_form' => $updateForm,
|
|
]);
|
|
}
|
|
|
|
#[Route('/remove/{projectSlug}', name: 'app_project_remove')]
|
|
public function remove(EntityManagerInterface $entityManager, $projectSlug): Response
|
|
{
|
|
$repository = $entityManager->getRepository(Project::class);
|
|
$project = $repository->findOneBySlug($projectSlug);
|
|
|
|
if (!$project) {
|
|
$this->addFlash(
|
|
'warning',
|
|
'Projet non trouvé !'
|
|
);
|
|
|
|
return $this->redirectToRoute('app_project');
|
|
}
|
|
|
|
try {
|
|
$entityManager->remove($project);
|
|
$entityManager->flush();
|
|
|
|
$this->addFlash(
|
|
'success',
|
|
'Projet supprimé !'
|
|
);
|
|
|
|
return $this->redirectToRoute('app_project');
|
|
} catch (\Exception $exception) {
|
|
$this->addFlash(
|
|
'danger',
|
|
'Impossible de supprimer le projet !'
|
|
);
|
|
}
|
|
|
|
return $this->redirectToRoute('app_project');
|
|
}
|
|
|
|
}
|