<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\CustomerInformation;
use App\Entity\Evolution;
use App\Form\EvolutionType;
use App\Repository\CustomerInformationRepository;
use App\Repository\EvolutionRepository;
use App\Service\Deck;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class EvolutionController extends AbstractController
{
#[Route('/evolution', name: 'app_form_evolution_new', methods: ['GET', 'POST'])]
public function evolution(
Request $request,
EvolutionRepository $supportRepository,
CustomerInformationRepository $customerInformationRepository,
Deck $deck,
): Response {
$support = new Evolution();
$customerInformation = new CustomerInformation();
$support->setCustomerInformation($customerInformation);
$form = $this->createForm(EvolutionType::class, $support);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$customerInformationRepository->add($customerInformation);
$supportRepository->add($support);
$titre = $support->getCustomerInformation()?->getName() ?? 'Pas de Titre';
$date = $support->getCreated_at()->format('Y-m-d H:i:s');
$plain = 'plain';
$description = $this->renderView('evolution/description.md.twig', [
'support' => $support,
]);
// On crée le nouveau ticket dans Formulaire Evolution du NextCloud
$card = $deck->createCard(
$titre,
$description,
$date,
$plain,
Deck::STACK_EVOLUTION
);
// On récupère la liste des labels
$labels = $deck->getLabels();
// On assigne un label au nouveau ticket
$labelId = null;
foreach ($labels as $label) {
if ($label['title'] === $support->getCustomerInformation()?->getCompany()) {
$labelId = $label['id'];
}
}
if (null !== $labelId) {
$deck->assignLabel($card['id'], $labelId);
} else {
$color = str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', \STR_PAD_LEFT);
$label = $deck->createLabel($support->getCustomerInformation()?->getCompany(), $color);
$deck->assignLabel($card['id'], $label['id']);
}
$this->addFlash('success', 'Votre demande d’évolution a été créée avec succès.');
return $this->redirectToRoute('app_form_index');
}
return $this->render('front/form/evolution.html.twig', [
'form' => $form->createView(),
]);
}
}