src/Controller/API/KelSyncController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller\API;
  3. use App\Entity\Api\KelSyncDevice;
  4. use App\Entity\Api\KelSyncLinkItem;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Dompdf\Dompdf;
  12. use Dompdf\Options;
  13. /**
  14.  * @Route("/kelsync")
  15.  */
  16. class KelSyncController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/", name="kelsync_index")
  20.      */
  21.     public function index(EntityManagerInterface $entityManager)
  22.     {
  23.         $devices $entityManager->getRepository(KelSyncDevice::class)->findAll();
  24.         return $this->render('api/kelsync/index.html.twig', [
  25.             'pageParams' => [
  26.                 'title' => 'KelSync',
  27.                 'domain' => 'kelsync',
  28.                 'breadcrumbs' => []
  29.             ],
  30.             'devices' => $devices,
  31.         ]);
  32.     }
  33.     /**
  34.      * @Route("/device/{id}", name="kelsync_device")
  35.      */
  36.     public function device(KelSyncDevice $deviceEntityManagerInterface $entityManager)
  37.     {
  38.         // Récupérer les liens triés par ordre
  39.         $links $entityManager->getRepository(KelSyncLinkItem::class)
  40.             ->findBy(['device' => $device], ['sortOrder' => 'ASC']);
  41.         return $this->render('api/kelsync/device.html.twig', [
  42.             'pageParams' => [
  43.                 'title' => $device->getName(),
  44.                 'domain' => 'kelsync',
  45.                 'breadcrumbs' => ['KelSync' => 'kelsync_index']
  46.             ],
  47.             'device' => $device,
  48.             'links' => $links,
  49.         ]);
  50.     }
  51.     /**
  52.      * @Route("/device/{id}/link/add", name="kelsync_link_add", methods={"POST"})
  53.      */
  54.     public function addDeviceLink(KelSyncDevice $deviceRequest $requestEntityManagerInterface $entityManager)
  55.     {
  56.         $data json_decode($request->getContent(), true);
  57.         $link = new KelSyncLinkItem();
  58.         $link->setDevice($device);
  59.         $link->setTitle($data['title'] ?? '');
  60.         $link->setLink($data['link'] ?? '');
  61. //        $link->setShortDescription($data['shortDescription'] ?? '');
  62.         $link->setSvgIcon($data['svgIcon'] ?? '');
  63.         $link->setAddDate(new \DateTime());
  64.         // Définir l'ordre (dernier élément)
  65.         $maxOrder $entityManager->getRepository(KelSyncLinkItem::class)
  66.             ->createQueryBuilder('l')
  67.             ->select('MAX(l.sortOrder)')
  68.             ->where('l.device = :device')
  69.             ->setParameter('device'$device)
  70.             ->getQuery()
  71.             ->getSingleScalarResult();
  72.         $link->setSortOrder(($maxOrder ?? -1) + 1);
  73.         $entityManager->persist($link);
  74.         $entityManager->flush();
  75.         return new JsonResponse([
  76.             'success' => true,
  77.             'link' => [
  78.                 'id' => $link->getId(),
  79.                 'title' => $link->getTitle(),
  80.                 'link' => $link->getLink(),
  81.                 'shortDescription' => $link->getTitle(),
  82.                 'svgIcon' => $link->getSvgIcon(),
  83.                 'sortOrder' => $link->getSortOrder(),
  84.             ]
  85.         ]);
  86.     }
  87.     /**
  88.      * @Route("/link/{id}/edit", name="kelsync_link_edit", methods={"POST"})
  89.      */
  90.     public function editLink(KelSyncLinkItem $linkRequest $requestEntityManagerInterface $entityManager)
  91.     {
  92.         $data json_decode($request->getContent(), true);
  93.         $link->setTitle($data['title'] ?? $link->getTitle());
  94.         $link->setLink($data['link'] ?? $link->getLink());
  95. //        $link->setShortDescription($data['shortDescription'] ?? $link->getShortDescription());
  96.         $link->setSvgIcon($data['svgIcon'] ?? $link->getSvgIcon());
  97.         $entityManager->flush();
  98.         return new JsonResponse([
  99.             'success' => true,
  100.             'link' => [
  101.                 'id' => $link->getId(),
  102.                 'title' => $link->getTitle(),
  103.                 'link' => $link->getLink(),
  104.                 'shortDescription' => $link->getTitle(),
  105.                 'svgIcon' => $link->getSvgIcon(),
  106.                 'sortOrder' => $link->getSortOrder(),
  107.             ]
  108.         ]);
  109.     }
  110.     /**
  111.      * @Route("/link/{id}/delete", name="kelsync_link_delete", methods={"DELETE"})
  112.      */
  113.     public function deleteLink(KelSyncLinkItem $linkEntityManagerInterface $entityManager)
  114.     {
  115.         $entityManager->remove($link);
  116.         $entityManager->flush();
  117.         return new JsonResponse(['success' => true]);
  118.     }
  119.     /**
  120.      * @Route("/device/{id}/link/reorder", name="kelsync_link_reorder", methods={"POST"})
  121.      */
  122.     public function reorderLinks(KelSyncDevice $deviceRequest $requestEntityManagerInterface $entityManager)
  123.     {
  124.         $data json_decode($request->getContent(), true);
  125.         $orders $data['orders'] ?? [];
  126.         foreach ($orders as $linkId => $order) {
  127.             $link $entityManager->getRepository(KelSyncLinkItem::class)->find($linkId);
  128.             if ($link && $link->getDevice()->getId() === $device->getId()) {
  129.                 $link->setSortOrder($order);
  130.             }
  131.         }
  132.         $entityManager->flush();
  133.         return new JsonResponse(['success' => true]);
  134.     }
  135.     /**
  136.      * @Route("/device/{id}/edit", name="kelsync_device_edit", methods={"POST"})
  137.      */
  138.     public function editDevice(KelSyncDevice $deviceRequest $requestEntityManagerInterface $entityManager)
  139.     {
  140.         $data json_decode($request->getContent(), true);
  141.         if (isset($data['name']) && !empty(trim($data['name']))) {
  142.             $device->setName(trim($data['name']));
  143.             $device->setUpdateDate(new \DateTime());
  144.             $entityManager->flush();
  145.             return new JsonResponse([
  146.                 'success' => true,
  147.                 'device' => [
  148.                     'id' => $device->getId(),
  149.                     'name' => $device->getName(),
  150.                     'deviceId' => $device->getDeviceId(),
  151.                     'updateDate' => $device->getUpdateDate()->format('Y-m-d H:i:s')
  152.                 ]
  153.             ]);
  154.         }
  155.         return new JsonResponse([
  156.             'success' => false,
  157.             'message' => 'Le nom du device est requis'
  158.         ], 400);
  159.     }
  160.     /**
  161.      * @Route("/device/{id}/delete", name="kelsync_device_delete", methods={"DELETE"})
  162.      */
  163.     public function deleteDevice(KelSyncDevice $deviceEntityManagerInterface $entityManager)
  164.     {
  165.         try {
  166.             // Vérifier si le device a des liens
  167.             $linkCount $entityManager->getRepository(KelSyncLinkItem::class)
  168.                 ->count(['device' => $device]);
  169.             if ($linkCount 0) {
  170.                 return new JsonResponse([
  171.                     'success' => false,
  172.                     'message' => 'Impossible de supprimer ce device car il contient encore ' $linkCount ' lien(s). Veuillez d\'abord supprimer tous les liens.'
  173.                 ], 400);
  174.             }
  175.             $entityManager->remove($device);
  176.             $entityManager->flush();
  177.             return new JsonResponse(['success' => true]);
  178.         } catch (\Exception $e) {
  179.             return new JsonResponse([
  180.                 'success' => false,
  181.                 'message' => 'Erreur lors de la suppression du device'
  182.             ], 500);
  183.         }
  184.     }
  185.     /**
  186.      * @Route("/link/{id}/toggle", name="kelsync_link_toggle", methods={"POST"})
  187.      */
  188.     public function toggleLink(KelSyncLinkItem $linkRequest $requestEntityManagerInterface $entityManager)
  189.     {
  190.         $data json_decode($request->getContent(), true);
  191.         $enabled $data['enabled'] ?? true;
  192.         $link->setEnabled($enabled);
  193.         $entityManager->flush();
  194.         return new JsonResponse([
  195.             'success' => true,
  196.             'link' => [
  197.                 'id' => $link->getId(),
  198.                 'enabled' => $link->isEnabled()
  199.             ]
  200.         ]);
  201.     }
  202. }