<?php
namespace App\Controller\API;
use App\Entity\Api\KelSyncDevice;
use App\Entity\Api\KelSyncLinkItem;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Dompdf\Dompdf;
use Dompdf\Options;
/**
* @Route("/kelsync")
*/
class KelSyncController extends AbstractController
{
/**
* @Route("/", name="kelsync_index")
*/
public function index(EntityManagerInterface $entityManager)
{
$devices = $entityManager->getRepository(KelSyncDevice::class)->findAll();
return $this->render('api/kelsync/index.html.twig', [
'pageParams' => [
'title' => 'KelSync',
'domain' => 'kelsync',
'breadcrumbs' => []
],
'devices' => $devices,
]);
}
/**
* @Route("/device/{id}", name="kelsync_device")
*/
public function device(KelSyncDevice $device, EntityManagerInterface $entityManager)
{
// Récupérer les liens triés par ordre
$links = $entityManager->getRepository(KelSyncLinkItem::class)
->findBy(['device' => $device], ['sortOrder' => 'ASC']);
return $this->render('api/kelsync/device.html.twig', [
'pageParams' => [
'title' => $device->getName(),
'domain' => 'kelsync',
'breadcrumbs' => ['KelSync' => 'kelsync_index']
],
'device' => $device,
'links' => $links,
]);
}
/**
* @Route("/device/{id}/link/add", name="kelsync_link_add", methods={"POST"})
*/
public function addDeviceLink(KelSyncDevice $device, Request $request, EntityManagerInterface $entityManager)
{
$data = json_decode($request->getContent(), true);
$link = new KelSyncLinkItem();
$link->setDevice($device);
$link->setTitle($data['title'] ?? '');
$link->setLink($data['link'] ?? '');
// $link->setShortDescription($data['shortDescription'] ?? '');
$link->setSvgIcon($data['svgIcon'] ?? '');
$link->setAddDate(new \DateTime());
// Définir l'ordre (dernier élément)
$maxOrder = $entityManager->getRepository(KelSyncLinkItem::class)
->createQueryBuilder('l')
->select('MAX(l.sortOrder)')
->where('l.device = :device')
->setParameter('device', $device)
->getQuery()
->getSingleScalarResult();
$link->setSortOrder(($maxOrder ?? -1) + 1);
$entityManager->persist($link);
$entityManager->flush();
return new JsonResponse([
'success' => true,
'link' => [
'id' => $link->getId(),
'title' => $link->getTitle(),
'link' => $link->getLink(),
'shortDescription' => $link->getTitle(),
'svgIcon' => $link->getSvgIcon(),
'sortOrder' => $link->getSortOrder(),
]
]);
}
/**
* @Route("/link/{id}/edit", name="kelsync_link_edit", methods={"POST"})
*/
public function editLink(KelSyncLinkItem $link, Request $request, EntityManagerInterface $entityManager)
{
$data = json_decode($request->getContent(), true);
$link->setTitle($data['title'] ?? $link->getTitle());
$link->setLink($data['link'] ?? $link->getLink());
// $link->setShortDescription($data['shortDescription'] ?? $link->getShortDescription());
$link->setSvgIcon($data['svgIcon'] ?? $link->getSvgIcon());
$entityManager->flush();
return new JsonResponse([
'success' => true,
'link' => [
'id' => $link->getId(),
'title' => $link->getTitle(),
'link' => $link->getLink(),
'shortDescription' => $link->getTitle(),
'svgIcon' => $link->getSvgIcon(),
'sortOrder' => $link->getSortOrder(),
]
]);
}
/**
* @Route("/link/{id}/delete", name="kelsync_link_delete", methods={"DELETE"})
*/
public function deleteLink(KelSyncLinkItem $link, EntityManagerInterface $entityManager)
{
$entityManager->remove($link);
$entityManager->flush();
return new JsonResponse(['success' => true]);
}
/**
* @Route("/device/{id}/link/reorder", name="kelsync_link_reorder", methods={"POST"})
*/
public function reorderLinks(KelSyncDevice $device, Request $request, EntityManagerInterface $entityManager)
{
$data = json_decode($request->getContent(), true);
$orders = $data['orders'] ?? [];
foreach ($orders as $linkId => $order) {
$link = $entityManager->getRepository(KelSyncLinkItem::class)->find($linkId);
if ($link && $link->getDevice()->getId() === $device->getId()) {
$link->setSortOrder($order);
}
}
$entityManager->flush();
return new JsonResponse(['success' => true]);
}
/**
* @Route("/device/{id}/edit", name="kelsync_device_edit", methods={"POST"})
*/
public function editDevice(KelSyncDevice $device, Request $request, EntityManagerInterface $entityManager)
{
$data = json_decode($request->getContent(), true);
if (isset($data['name']) && !empty(trim($data['name']))) {
$device->setName(trim($data['name']));
$device->setUpdateDate(new \DateTime());
$entityManager->flush();
return new JsonResponse([
'success' => true,
'device' => [
'id' => $device->getId(),
'name' => $device->getName(),
'deviceId' => $device->getDeviceId(),
'updateDate' => $device->getUpdateDate()->format('Y-m-d H:i:s')
]
]);
}
return new JsonResponse([
'success' => false,
'message' => 'Le nom du device est requis'
], 400);
}
/**
* @Route("/device/{id}/delete", name="kelsync_device_delete", methods={"DELETE"})
*/
public function deleteDevice(KelSyncDevice $device, EntityManagerInterface $entityManager)
{
try {
// Vérifier si le device a des liens
$linkCount = $entityManager->getRepository(KelSyncLinkItem::class)
->count(['device' => $device]);
if ($linkCount > 0) {
return new JsonResponse([
'success' => false,
'message' => 'Impossible de supprimer ce device car il contient encore ' . $linkCount . ' lien(s). Veuillez d\'abord supprimer tous les liens.'
], 400);
}
$entityManager->remove($device);
$entityManager->flush();
return new JsonResponse(['success' => true]);
} catch (\Exception $e) {
return new JsonResponse([
'success' => false,
'message' => 'Erreur lors de la suppression du device'
], 500);
}
}
/**
* @Route("/link/{id}/toggle", name="kelsync_link_toggle", methods={"POST"})
*/
public function toggleLink(KelSyncLinkItem $link, Request $request, EntityManagerInterface $entityManager)
{
$data = json_decode($request->getContent(), true);
$enabled = $data['enabled'] ?? true;
$link->setEnabled($enabled);
$entityManager->flush();
return new JsonResponse([
'success' => true,
'link' => [
'id' => $link->getId(),
'enabled' => $link->isEnabled()
]
]);
}
}