<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\CustomerInformation;
use App\Entity\Support;
use App\Form\SupportType;
use App\Repository\CustomerInformationRepository;
use App\Repository\SupportRepository;
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 SupportController extends AbstractController
{
#[Route('/support', name: 'app_form_support_new', methods: ['GET', 'POST'])]
public function support(
Request $request,
SupportRepository $supportRepository,
CustomerInformationRepository $customerInformationRepository,
Deck $deck,
): Response {
$support = new Support();
$customerInformation = new CustomerInformation();
$support->setCustomerInformation($customerInformation);
$form = $this->createForm(SupportType::class, $support);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$customerInformationRepository->add($customerInformation);
$supportRepository->add($support);
// On définit l'urgence de la demande
$stackId = Deck::STACK_CLASSIQUE; // Classique
if ($support->getIsBlocking()) {
$stackId = Deck::STACK_URGENT; // Urgent
if ($support->getBlockingDeadline()) {
$interval = $support->getCreated_at()->diff($support->getBlockingDeadline());
if (0 === $interval->invert && 3 < $interval->d) {
$stackId = Deck::STACK_PRIORITAIRE; // Prioritaire
}
}
}
// On récupère la liste des labels
$labels = $deck->getLabels();
$titre = $support->getCustomerInformation()?->getName() ?? 'Pas de Titre';
$date = $support->getCreated_at()->format('Y-m-d H:i:s');
$plain = 'plain';
$description = $this->renderView('support/description.md.twig', [
'support' => $support,
]);
// On crée le nouveau ticket dans Formulaire Support du NextCloud
$card = $deck->createCard(
$titre,
$description,
$date,
$plain,
$stackId
);
// On assigne un label au nouveau ticket
$labelId = null;
$labelUrgent = null;
$labelPrioritaire = null;
foreach ($labels as $label) {
if ($label['title'] === $support->getCustomerInformation()?->getCompany()) {
$labelId = $label['id'];
}
if ('Urgent' === $label['title']) {
$labelUrgent = $label;
}
if ('Prioritaire' === $label['title']) {
$labelPrioritaire = $label;
}
}
if (null !== $labelId) {
$deck->assignLabel($card['id'], $labelId, $stackId);
} 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']);
}
// Si la demande est urgente / prioritaire
if ($support->getIsBlocking()) {
if (118 === $stackId) {
$deck->assignLabel($card['id'], $labelUrgent['id']);
} elseif (121 === $stackId) {
$deck->assignLabel($card['id'], $labelPrioritaire['id']);
}
}
$this->addFlash('success', 'Votre demande de support a été créée avec succès.');
return $this->redirectToRoute('app_form_index');
}
return $this->render('front/form/support.html.twig', [
'form' => $form->createView(),
]);
}
}