ninegitea/src/Controller/ScrumteamController.php
2024-02-16 16:40:41 +01:00

195 lines
6.9 KiB
PHP
Executable File

<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\FormError;
use App\Entity\Scrumteam as Entity;
use App\Form\ScrumteamType as Form;
use App\Service\giteaService;
class ScrumteamController extends AbstractController
{
private $data = "scrumteam";
private $route = "app_scrumteam";
private $render = "Scrumteam/";
private $entity = "App:Scrumteam";
public function __construct(giteaService $giteaservice) { $this->giteaservice = $giteaservice; }
public function submit($scrumid, Request $request)
{
// Initialisation de l'enregistrement
$em = $this->getDoctrine()->getManager();
$scrum=$em->getRepository("App:Scrum")->find($scrumid);
$data = new Entity();
$data->setScrum($scrum);
$last = $em->getRepository('App:Scrumteam')->findOneBy(["scrum"=>$scrum], ['rowid' => 'DESC']);
if(!$last) $data->setRowid(0);
else $data->setRowid($last->getRowid()+1);
// Récupérer les repos de gitea
$gitealabels=$this->giteaservice->getLabels($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"]);
if(!is_array($gitealabels)) die("Probleme de connexion avec gitea veuillez vous <a href='/logout'>reconnecter</a>");
// Création du formulaire
$form = $this->createForm(Form::class,$data,array("mode"=>"submit","gitealabels"=>$gitealabels));
// Récupération des data du formulaire
$form->handleRequest($request);
// Sur erreur
$this->getErrorForm(null,$form,$request,$data,"submit");
// Sur validation
if ($form->get('submit')->isClicked() && $form->isValid()) {
$data = $form->getData();
$gitealabel=$this->giteaservice->getLabel($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"],$data->getGiteaid());
$data->setGiteajson(json_decode(json_encode($gitealabel), true));
$em->persist($data);
$em->flush();
// Retour à la liste
return $this->render($this->render.'close.html.twig');
}
// Affichage du formulaire
return $this->render($this->render.'edit.html.twig', [
'useheader' => false,
'usesidebar' => false,
$this->data => $data,
'mode' => 'submit',
'form' => $form->createView()
]);
}
public function update($id,Request $request)
{
// Initialisation de l'enregistrement
$em = $this->getDoctrine()->getManager();
$data=$em->getRepository($this->entity)->find($id);
$scrum=$data->getScrum();
// Récupérer les repos de gitea
$gitealabels=$this->giteaservice->getLabels($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"]);
// Création du formulaire
$form = $this->createForm(Form::class,$data,array("mode"=>"submit","gitealabels"=>$gitealabels));
// Récupération des data du formulaire
$form->handleRequest($request);
// Sur erreur
$this->getErrorForm(null,$form,$request,$data,"update");
// Sur validation
if ($form->get('submit')->isClicked() && $form->isValid()) {
$data = $form->getData();
$gitealabel=$this->giteaservice->getLabel($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"],$data->getGiteaid());
$data->setGiteajson(json_decode(json_encode($gitealabel), true));
$em->persist($data);
$em->flush();
// Retour à la liste
return $this->render($this->render.'close.html.twig');
}
// Affichage du formulaire
return $this->render($this->render.'edit.html.twig', [
'useheader' => false,
'usesidebar' => false,
$this->data => $data,
'mode' => 'update',
'form' => $form->createView()
]);
}
public function delete($id,Request $request)
{
// Initialisation de l'enregistrement
$em = $this->getDoctrine()->getManager();
$data=$em->getRepository($this->entity)->find($id);
// Controle avant suppression
$error=false;
if($id<0) $error=true;
if($error)
return $this->redirectToRoute($this->route."_update",["id"=>$id]);
else {
$em->remove($data);
$em->flush();
// Retour à la liste
return $this->render($this->render.'close.html.twig');
}
}
public function select($scrumid, Request $request)
{
// S'assurer que c'est un appel ajax
if (!$request->isXmlHttpRequest()) {
return new JsonResponse(array('message' => 'Interdit'), 400);
}
$em = $this->getDoctrine()->getManager();
$scrum=$em->getRepository("App:Scrum")->find($scrumid);
$scrumteams = $scrum->getScrumteams();
$output=array();
foreach($scrumteams as $scrumteam) {
array_push($output,array("id"=>$scrumteam->getId(),"name"=>"<b>".$scrumteam->getName()."</b><br><small>liè au label gitea ".$scrumteam->getGiteajson()["name"]."</small>"));
}
$response = new Response(json_encode($output));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
public function order($scrumid, Request $request)
{
$em = $this->getDoctrine()->getManager();
$scrumteamids=explode(",",$request->get('lstordered'));
$i=1;
foreach($scrumteamids as $id) {
$scrumteam=$em->getRepository($this->entity)->find($id);
if($scrumteam) {
$scrumteam->setRowid($i);
$em->persist($scrumteam);
$em->flush();
}
$i++;
}
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
return $response;
}
protected function getErrorForm($id,$form,$request,$data,$mode) {
if ($form->get('submit')->isClicked()&&$mode=="delete") {
}
if ($form->get('submit')->isClicked() && $mode=="submit") {
}
if ($form->get('submit')->isClicked() && ($mode=="submit" || $mode=="update")) {
}
if ($form->get('submit')->isClicked() && !$form->isValid()) {
$this->get('session')->getFlashBag()->clear();
$errors = $form->getErrors();
foreach( $errors as $error ) {
$request->getSession()->getFlashBag()->add("error", $error->getMessage());
}
}
}
}