src/Controller/ThemeController.php line 275

  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\ThemeSousCategorieRepository;
  4. use App\Repository\ThemeCategorieRepository;
  5. use App\Repository\ThemeRepository;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\File\File;
  11. use Symfony\Component\HttpFoundation\File\UploadedFile;
  12. use App\Entity\Theme;
  13. use App\Entity\ThemeSousCategorie;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. class ThemeController extends AbstractController
  16. {
  17.     #[Route('/theme/index'name'app_theme')]
  18.     public function index(ThemeSousCategorieRepository $themeSousCategorieRepositoryThemeCategorieRepository $themeCategorieRepository): Response
  19.     {
  20.         $sousCategories $themeSousCategorieRepository->findAll();
  21.         $themeCategories $themeCategorieRepository->findAll();
  22.         return $this->render('theme/index.html.twig', [
  23.             'sous_categories' => $sousCategories,
  24.             'theme_categories' => $themeCategories,
  25.         ]);
  26.     }
  27.     #[Route('/theme/list'name'app_theme_list')]
  28.     public function list(ThemeRepository $themeRepository): Response
  29.     {
  30.      $themes $themeRepository->findBy([], ['id' => 'DESC'], 200);
  31.         //$themes = $themeRepository->findAll();
  32.         return $this->render('theme/list.html.twig', [
  33.             'themes' => $themes,
  34.         ]);
  35.     }
  36.     #[Route('/theme/manuelle'name'app_theme_manuelle')]
  37.     public function manuelle(ThemeSousCategorieRepository $themeSousCategorieRepositoryThemeCategorieRepository $themeCategorieRepository): Response
  38.     {
  39.         $sousCategories $themeSousCategorieRepository->findAll();
  40.         $themeCategories $themeCategorieRepository->findAll();
  41.         return $this->render('theme/manuelle.html.twig', [
  42.             'sous_categories' => $sousCategories,
  43.             'theme_categories' => $themeCategories,
  44.         ]);
  45.     }
  46.     #[Route('/theme/delete/{id}'name'app_theme_delete')]
  47.     public function delete(Request $requestThemeRepository $themeRepository$id,  EntityManagerInterface $entityManager): Response
  48.     {
  49.         $theme $themeRepository->find($id);
  50.         if (!$theme) {
  51.             return $this->json([
  52.                 'error' => 'Le thème spécifié n\'existe pas.'
  53.             ], Response::HTTP_NOT_FOUND);
  54.         }
  55.         $entityManager->remove($theme);
  56.         $entityManager->flush();
  57.         //redirect to list
  58.         return $this->redirectToRoute('app_theme_list');
  59.     }
  60.     #[Route('/theme/save'name'app_theme_save')]
  61.     public function saveData(Request $requestThemeRepository $themeRepositoryThemeSousCategorieRepository $themeSousCategorieRepository): Response
  62.     {
  63.         $data json_decode($request->getContent(), true);
  64.         $uniqueId $data['ref'];
  65.         $existingTheme $themeRepository->findOneBy(['ref' => $uniqueId]);
  66.         if ($existingTheme) {
  67.             return $this->json([
  68.                 'code' => 'error',
  69.                 'message' => 'non unique'
  70.             ]);
  71.         }
  72.         // Créer le répertoire s'il n'existe pas
  73.         $outputDirectory $this->getParameter('kernel.project_dir') . '/public/theme/' $uniqueId;
  74.         if (!file_exists($outputDirectory)) {
  75.             mkdir($outputDirectory0755true);
  76.         }
  77.         $base64Image $data['image'];
  78.         $outputFile $outputDirectory '/image.png';
  79.         $this->saveBase64AsImage($base64Image$outputFile);
  80.         // Enregistrer toutes les miniatures non vides
  81.         $miniatures $data['miniatures'];
  82.         foreach ($miniatures as $miniatureName => $base64Miniature) {
  83.             if (!empty($base64Miniature)) {
  84.                 $outputMiniatureFile $outputDirectory '/' $miniatureName '.png';
  85.                 $this->saveBase64AsImage($base64Miniature$outputMiniatureFile);
  86.             }
  87.         }
  88.         // Convertir le tableau des positions en chaîne JSON
  89.         $textData json_encode($data['positions']);
  90.         // Traiter les données reçues (sauvegarde en base de données, etc.)
  91.         $theme = new Theme();
  92.         $sousCategorieId $data['sousCategorie'];
  93.         $sousCategorie $themeSousCategorieRepository->find($sousCategorieId);
  94.         $theme->setRef($uniqueId);
  95.         $theme->setSoussCategorie($sousCategorie);
  96.         $theme->setImageData('theme/' $uniqueId '/image.png');
  97.         $theme->setTextData($textData);
  98.         // Vérifier si la miniature est présente avant de l'enregistrer
  99.         if (!empty($data['miniatures']['eco40'])) {
  100.             $theme->setEco40('theme/' $uniqueId '/eco40.png');
  101.         }
  102.         if (!empty($data['miniatures']['eco180'])) {
  103.             $theme->setEco180('theme/' $uniqueId '/eco180.png');
  104.         }
  105.         if (!empty($data['miniatures']['eco200'])) {
  106.             $theme->setEco200('theme/' $uniqueId '/eco200.png');
  107.         }
  108.         if (!empty($data['miniatures']['eco300'])) {
  109.             $theme->setEco300('theme/' $uniqueId '/eco300.png');
  110.         }
  111.         if (!empty($data['miniatures']['eco400'])) {
  112.             $theme->setEco400('theme/' $uniqueId '/eco400.png');
  113.         }
  114.         if (!empty($data['miniatures']['eco500'])) {
  115.             $theme->setEco500('theme/' $uniqueId '/eco500.png');
  116.         }
  117.         if (!empty($data['miniatures']['ecovin'])) {
  118.             $theme->setEcovin('theme/' $uniqueId '/ecovin.png');
  119.         }
  120.         $themeRepository->save($themetrue);
  121.         // Retourner une réponse JSON avec un message de succès
  122.         return $this->json([
  123.             'code' => 'ok',
  124.             'message' => 'Données sauvegardées avec succès
  125.         '
  126.         ]);
  127.     }
  128.     #[Route('/theme/save/manuelle'name'app_theme_save_manuelle')]
  129.     public function saveDataManuelle(Request $requestThemeRepository $themeRepositoryThemeSousCategorieRepository $themeSousCategorieRepository): Response
  130.     {
  131.         $data json_decode($request->getContent(), true);
  132.         $uniqueId $data['ref'];
  133.         $theme $themeRepository->findOneBy(['ref' => $uniqueId]);
  134.         // Créer le répertoire s'il n'existe pas
  135.         $outputDirectory $this->getParameter('kernel.project_dir') . '/public/theme/' $uniqueId;
  136.         if (!file_exists($outputDirectory)) {
  137.             mkdir($outputDirectory0755true);
  138.         }
  139.         // Enregistrer toutes les miniatures non vides
  140.         $miniatures $data['miniatures'];
  141.         foreach ($miniatures as $miniatureName => $base64Miniature) {
  142.             if (!empty($base64Miniature)) {
  143.                 $outputMiniatureFile $outputDirectory '/' $miniatureName '.png';
  144.                 $this->saveBase64AsImage($base64Miniature$outputMiniatureFile);
  145.             }
  146.         }
  147.         if ($theme) {
  148.             // Si le thème existe déjà, mettez à jour ses miniatures
  149.             if (!empty($data['miniatures']['eco180'])) {
  150.                 $theme->setEco180('theme/' $uniqueId '/eco180.png');
  151.             }
  152.             if (!empty($data['miniatures']['eco200'])) {
  153.                 $theme->setEco200('theme/' $uniqueId '/eco200.png');
  154.             }
  155.             if (!empty($data['miniatures']['eco300'])) {
  156.                 $theme->setEco300('theme/' $uniqueId '/eco300.png');
  157.             }
  158.             if (!empty($data['miniatures']['eco400'])) {
  159.                 $theme->setEco400('theme/' $uniqueId '/eco400.png');
  160.             }
  161.             if (!empty($data['miniatures']['eco500'])) {
  162.                 $theme->setEco500('theme/' $uniqueId '/eco500.png');
  163.             }
  164.             if (!empty($data['miniatures']['ecovin'])) {
  165.                 $theme->setEcovin('theme/' $uniqueId '/ecovin.png');
  166.             }
  167.         } else {
  168.             // Si le thème n'existe pas, créez un nouveau thème
  169.             $theme = new Theme();
  170.             $sousCategorieId $data['sousCategorie'];
  171.             $sousCategorie $themeSousCategorieRepository->find($sousCategorieId);
  172.             $theme->setRef($uniqueId);
  173.             $theme->setSoussCategorie($sousCategorie);
  174.             // Convertir le tableau des positions en chaîne JSON
  175.             $textData json_encode($data['positions']);
  176.             $theme->setTextData($textData);
  177.             $imageData json_encode(($data['image']));
  178.             $theme->setImageData($imageData);
  179.             foreach ($miniatures as $miniatureName => $base64Miniature) {
  180.                 if (!empty($base64Miniature)) {
  181.                     $setterMethod 'set' ucfirst($miniatureName);
  182.                     $theme->$setterMethod('theme/' $uniqueId '/' $miniatureName '.png');
  183.                 }
  184.             }
  185.         }
  186.         //$base64Image = $data['image'];
  187.         //$outputFile = $outputDirectory . '/image.png';
  188.         //$this->saveBase64AsImage($base64Image, $outputFile);
  189.         //$theme->setImageData('theme/' . $uniqueId . '/image.png');
  190.         $themeRepository->save($themetrue);
  191.         // Retourner une réponse JSON avec un message de succès
  192.         return $this->json([
  193.             'code' => 'ok',
  194.             'message' => 'Données sauvegardées avec succès'
  195.         ]);
  196.     }
  197.     private function saveBase64AsImage($base64$outputFile)
  198.     {
  199.         $data base64_decode(preg_replace('#^data:image/\w+;base64,#i'''$base64));
  200.         file_put_contents($outputFile$data);
  201.     }
  202.     #[Route('/search/theme'name'app_theme_search'methods: ['GET'])]
  203.     public function search(Request $requestThemeRepository $themeRepository): Response
  204.     {
  205.         $searchTerm $request->query->get('search');
  206.         $themes $themeRepository->findBySearchTerm($searchTerm);
  207.         return new Response($this->renderView('theme/_search_results.html.twig', [
  208.             'themes' => $themes,
  209.         ]));
  210.     }
  211.     #[Route('/{id}'name'app_theme_show'methods: ['GET'])]
  212.     public function show(Theme $theme): Response
  213.     {
  214.         return $this->render('theme/show.html.twig', [
  215.             'theme' => $theme,
  216.         ]);
  217.     }
  218. /*
  219.     #[Route('theme/moulinette', name: 'app_theme_moulinette', methods: ['GET'])]
  220.     public function moulinette(EntityManagerInterface $entityManager): Response
  221.     {
  222.         $themes = $entityManager->getRepository(Theme::class)
  223.             ->findThemesBySousCategorie('ANIMAUX CUTE');
  224.         $validation = []; // Initialisation de la variable de validation
  225.         foreach ($themes as $theme) {
  226.             $modifier = true; // Réinitialiser pour chaque thème
  227.             $data = json_decode($theme->getTextData(), true);
  228.             if ($data) {
  229.                 $count = count($data);
  230.                 $orientations = ["gauche", "droite", "centre"];
  231.                 for ($i = 0; $i < $count; $i++) {
  232.                     // Vérifier si 'orientationText' existe déjà avant de modifier
  233.                     if (!isset($data[$i]['orientationText'])) {
  234.                         $data[$i]['orientationText'] = $orientations[$i % 3];
  235.                     } else {
  236.                         $modifier = false;
  237.                     }
  238.                 }
  239.                 if ($modifier) {
  240.                     $theme->setTextData(json_encode($data));
  241.                     $entityManager->persist($theme);
  242.                 }
  243.                 // Stocker les informations de validation
  244.                 $validation[] = [
  245.                     'id' => $theme->getId(),
  246.                     'text_data' => json_encode($data),
  247.                     'modifie' => $modifier
  248.                 ];
  249.             }
  250.         }
  251.         //test validation
  252.         //dd($validation);
  253.         $entityManager->flush();
  254.         return new Response('terminé');
  255.     }
  256.     */
  257. }