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

201 lines
7.3 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\Scrumcolumn as Entity;
use App\Form\ScrumcolumnType as Form;
use App\Service\giteaService;
class ScrumcolumnController extends AbstractController
{
private $data = "scrumcolumn";
private $route = "app_scrumcolumn";
private $render = "Scrumcolumn/";
private $entity = "App:Scrumcolumn";
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:Scrumcolumn')->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);
$scrumcolumns = $scrum->getScrumcolumns();
$output=array();
foreach($scrumcolumns as $scrumcolumn) {
array_push($output,array("id"=>$scrumcolumn->getId(),"name"=>"<b>".$scrumcolumn->getName()."</b><br><small>liè au label gitea ".$scrumcolumn->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();
$scrumcolumnids=explode(",",$request->get('lstordered'));
$i=1;
foreach($scrumcolumnids as $id) {
$scrumcolumn=$em->getRepository($this->entity)->find($id);
if($scrumcolumn) {
$scrumcolumn->setRowid($i);
$em->persist($scrumcolumn);
$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")) {
// On s'assure que le label ne contient pas des caractères speciaux
$string = preg_replace('~[^ éèêôöàïî\'@a-zA-Z0-9._-]~', '', $data->getName());
if($string!=$data->getName())
{
$form->addError(new FormError('Caractères interdit dans ce label'));
}
}
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());
}
}
}
}