src/Controller/ApiThemeController.php line 96

  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use App\Repository\ThemeRepository;
  7. use App\Repository\ThemeCategorieRepository;
  8. use App\Repository\ThemeSousCategorieRepository;
  9. use Knp\Component\Pager\PaginatorInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpFoundation\File\File;
  13. #[Route('/api'name'api_')]
  14. class ApiThemeController extends AbstractController
  15. {
  16.     #[Route('/theme/{name}'name'_theme')]
  17.     public function index(Request $requestRequestStack $requestStackThemeRepository $themeRepositoryPaginatorInterface $paginatorstring $name): Response
  18.     {
  19.         $baseUrl $requestStack->getCurrentRequest()->getSchemeAndHttpHost();
  20.         $themeRepo $themeRepository->findDataByName($name);
  21.         $page $request->query->getInt('page'1);
  22.         $pageSize 1000;
  23.         $themes $paginator->paginate($themeRepo$page$pageSize);
  24.         $data = [];
  25.         foreach ($themes as $theme) {
  26.             if ($theme[$name] === null) {
  27.                 $rst "";
  28.             } else {
  29.                 $rst $baseUrl '/' $theme[$name];
  30.             }
  31.             $data[] = [
  32.                 'text_data' => $theme['text_data'],
  33.                 'image_data' => $baseUrl '/' .  $theme['image_data'],
  34.                 $name => $rst,
  35.                 'ref' => $theme['ref'],
  36.             ];
  37.         }
  38.         $totalItems $themes->getTotalItemCount();
  39.         $totalPages ceil($totalItems $pageSize);
  40.         return $this->json([
  41.             'message' => 'get theme',
  42.             'data' => $data,
  43.             'total_pages' => $totalPages,
  44.         ], 200);
  45.     }
  46.     #[Route('/theme/{name}/{ref}'name'_theme_ref')]
  47.     public function getThemeRefImage(Request $requestThemeRepository $themeRepositorystring $namestring $ref): Response
  48.     {
  49.         $getImageData $themeRepository->getImageData($ref);
  50.         $imagePath $getImageData['0']['image_data'];
  51.         // Emplacement de l'image dans le dossier public/theme/$ref/image.png
  52.         $publicPath $this->getParameter('kernel.project_dir') . '/public';
  53.         $imageFile = new File("{$publicPath}/theme/{$ref}/image.png");
  54.         // Vérifier si le fichier existe
  55.         if ($imageFile->isFile()) {
  56.             // Lire le contenu du fichier
  57.             $fileContent file_get_contents($imageFile->getPathname());
  58.             // Convertir en base64
  59.             $base64Image base64_encode($fileContent);
  60.             return $this->json([
  61.                 'message' => 'get theme image data',
  62.                 'data' => 'data:image/png;base64,' $base64Image,
  63.             ], 200);
  64.         }
  65.         return $this->json([
  66.             'message' => 'Image not found',
  67.         ], 404);
  68.     }
  69.     #[Route('/thematique/{name}/{categorie}/{sub}'name'_get_theme_categorie')]
  70.     public function getThemeCategorie(Request $requestRequestStack $requestStackThemeRepository $themeRepositoryPaginatorInterface $paginatorstring $namestring $categorieThemeCategorieRepository $ThemeCategorieRepositoryThemeSousCategorieRepository $SousCategorieRepositorystring $sub null): Response
  71.     {
  72.         $baseUrl $requestStack->getCurrentRequest()->getSchemeAndHttpHost();
  73.         $themeCategorie $ThemeCategorieRepository->findOneBy(['nom' => $categorie]);
  74.         if (!$themeCategorie) {
  75.             return $this->json([
  76.                 'message' => 'categorie non trouvé',
  77.             ], 404);
  78.         }
  79.         // Si sous-catégorie n'est pas nulle, alors on l'utilise
  80.         // Si sous-catégorie n'est pas nulle, alors on l'utilise
  81.         if ($sub !== null) {
  82.             $themeSousCategorie $SousCategorieRepository->findOneBy(['nom' => $sub]);
  83.             if (!$themeSousCategorie) {
  84.                 return $this->json([
  85.                     'message' => 'sous-categorie non trouvé',
  86.                 ], 404);
  87.             }
  88.             $themeRepo $themeRepository->findDataByNameAndCategorieOrSubcategorie($name$themeCategorie->getId(), $themeSousCategorie->getId());
  89.         } else {
  90.             $themeRepo $themeRepository->findDataByNameAndCategorieOrSubcategorie($name$themeCategorie->getId());
  91.         }
  92.         $page $request->query->getInt('page'1);
  93.         $pageSize 1000;
  94.         $themes $paginator->paginate($themeRepo$page$pageSize);
  95.         $data = [];
  96.         foreach ($themes as $theme) {
  97.             if ($theme[$name] === null) {
  98.                 $rst "";
  99.             } else {
  100.                 $rst $baseUrl '/' $theme[$name];
  101.             }
  102.             $data[] = [
  103.                 //'text_data' => $theme['text_data'],
  104.                 //'image_data' => $baseUrl . '/' .  $theme['image_data'],
  105.                 $name => $rst,
  106.                 'ref' => $theme['ref'],
  107.             ];
  108.         }
  109.         $totalItems $themes->getTotalItemCount();
  110.         $totalPages ceil($totalItems $pageSize);
  111.         return $this->json([
  112.             'message' => 'get theme cat & sub',
  113.             'data' => $data,
  114.             'total_pages' => $totalPages,
  115.         ], 200);
  116.     }
  117.     #[Route('/recup/{ref}'name'_get_theme_recup')]
  118.     public function getThemeRecup(Request $requestRequestStack $requestStackThemeRepository $themeRepositoryPaginatorInterface $paginatorstring $ref): Response
  119.     {
  120.         $baseUrl $requestStack->getCurrentRequest()->getSchemeAndHttpHost();
  121.        
  122.         $themeRepo $themeRepository->findOneByRef($ref);
  123.         return $this->json([
  124.             'message' => 'recup image text',
  125.             'data' => $themeRepo,
  126.         ], 200);
  127.         /*$page = $request->query->getInt('page', 1);
  128.         $pageSize = 1000;
  129.         $themes = $paginator->paginate($themeRepo, $page, $pageSize);
  130.         $data[] = [
  131.             'text_data' => $themeRepo['text_data'],
  132.             'image_data' => $baseUrl . '/' .  $themeRepo['image_data'],
  133.             //$name => $rst,
  134.             //'ref' => $theme['ref'],
  135.         ];
  136.         return $this->json([
  137.             'message' => 'recup image text',
  138.             'data' => $data,
  139.         ], 200);*/
  140.     }
  141. }