src/Controller/Content/DownloadController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Content;
  3. use App\Controller\BaseController;
  4. use App\Helpers\Mysql\MysqlEntitySearch;
  5. use App\Helpers\Odbc\OdbcProdukte;
  6. use App\Service\SessionService;
  7. use App\Service\IceCatService;
  8. use App\Service\SearchResultsFormatter;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class DownloadController extends BaseController
  16. {
  17.     /**
  18.      * @Route("/download", name="download")
  19.      */
  20.     public function download(): Response
  21.     {
  22.          return $this->baseRender('content/downloads.html.twig');
  23.     }
  24.     /**
  25.      * @Route("/entity-search", name="entity_search")
  26.      */
  27.     public function entitySearch(): Response
  28.     {
  29.          return $this->baseRender('content/entity_search.html.twig');
  30.     }
  31.     /**
  32.      * @Route("/search-entity-fast", name="search_entity_fast")
  33.      * v3.0.0 - Now returns formatted HTML from backend
  34.      */
  35.     public function searchEntityFast(
  36.         Request $request,
  37.         MysqlEntitySearch $entitySearch,
  38.         SearchResultsFormatter $formatter
  39.     ): JsonResponse
  40.     {
  41.         $search $request->query->get('search''');
  42.         $category $request->query->get('category'null);
  43.         $manufacturer $request->query->get('manufacturer'null);
  44.         // Execute search with filters (validation is handled by MysqlEntitySearch)
  45.         $searchResults $entitySearch->search($search$category$manufacturer);
  46.         // Format results into HTML using service (proper MVC separation)
  47.         // Optional: Set checkIceCat=true to actually call IceCat API (WARNING: Slow!)
  48.         $checkIceCat $request->query->get('check_icecat''false') === 'true';
  49.         $formattedResults $formatter->formatSearchResults($searchResults$checkIceCat);
  50.         // Return both formatted HTML and raw data for backward compatibility
  51.         $response = [
  52.             'html' => $formattedResults['html'],
  53.             'count' => $formattedResults['count'],
  54.             'scenario' => $formattedResults['scenario'],
  55.             'metadata' => $searchResults['metadata'] ?? [],
  56.             'icecat_status' => $formattedResults['icecat_status'] ?? [],
  57.             // Keep raw data for any custom frontend processing if needed
  58.             'results' => $searchResults['results'] ?? [],
  59.             'alternatives' => $searchResults['alternatives'] ?? [],
  60.         ];
  61.         return new JsonResponse($response);
  62.     }
  63.     /**
  64.      * @Route("/entity-search/categories", name="entity_search_categories")
  65.      */
  66.     public function getCategories(MysqlEntitySearch $entitySearch): JsonResponse
  67.     {
  68.         $categories $entitySearch->getCategories();
  69.         return new JsonResponse($categories);
  70.     }
  71.     /**
  72.      * @Route("/entity-search/manufacturers", name="entity_search_manufacturers")
  73.      */
  74.     public function getManufacturers(MysqlEntitySearch $entitySearch): JsonResponse
  75.     {
  76.         $manufacturers $entitySearch->getManufacturers();
  77.         return new JsonResponse($manufacturers);
  78.     }
  79.     /**
  80.      * @Route("/opcache-reset-temp", name="opcache_reset_temp")
  81.      * TEMPORARY: Reset PHP opcache - DELETE THIS ROUTE AFTER USE
  82.      */
  83.     public function opcacheReset(): JsonResponse
  84.     {
  85.         if (function_exists('opcache_reset')) {
  86.             opcache_reset();
  87.             return new JsonResponse(['status' => 'OPcache reset successfully']);
  88.         }
  89.         return new JsonResponse(['status' => 'OPcache not available'], 500);
  90.     }
  91.     /**
  92.      * @Route("/product-details", name="product_details")
  93.      * Get detailed product information for modal display (includes IceCat data)
  94.      */
  95.     public function getProductDetails(Request $requestManagerRegistry $doctrineIceCatService $iceCatService): JsonResponse
  96.     {
  97.         $articleNr $request->query->get('article_nr''');
  98.         $manufacturer $request->query->get('manufacturer''');
  99.         if (empty($articleNr)) {
  100.             return new JsonResponse(['error' => 'Article number is required'], 400);
  101.         }
  102.         try {
  103.             // First try to get from ODBC (produkte table)
  104.             $productData null;
  105.             $source 'mysql';
  106.             try {
  107.                 $odbc = new OdbcProdukte();
  108.                 $products $odbc->searchByArtnr($articleNr1);
  109.                 if (!empty($products)) {
  110.                     $product $products[0];
  111.                     $productData = [
  112.                         'article_nr' => $product->getArtnr(),
  113.                         'manufacturer' => $product->getHersteller(),
  114.                         'description' => $product->getDescription(),
  115.                         'ean' => $product->getEan(),
  116.                         'category' => $product->getArtGr(),
  117.                         'image_file' => $product->getBildDatei(),
  118.                         'price' => $product->getVk(),
  119.                         'availability' => $product->getAvailability(),
  120.                         'delivery' => $product->getDeliveryTime(),
  121.                         'color' => '',
  122.                         'pages' => '',
  123.                         'compatible_with' => '',
  124.                     ];
  125.                     $source 'odbc';
  126.                 }
  127.             } catch (\Exception $e) {
  128.                 error_log('ODBC not available for product details: ' $e->getMessage());
  129.             }
  130.             // Fallback to MySQL entity table if ODBC not available
  131.             if (!$productData) {
  132.                 $connection $doctrine->getConnection();
  133.                 $sql "
  134.                     SELECT
  135.                         entity_id,
  136.                         name1 as article_nr,
  137.                         hersteller as manufacturer,
  138.                         name2 as description,
  139.                         ean,
  140.                         artgr as category,
  141.                         picfile as image_file,
  142.                         keywords,
  143.                         description as long_description,
  144.                         color,
  145.                         size
  146.                     FROM entity
  147.                     WHERE name1 = :article_nr
  148.                 ";
  149.                 if ($manufacturer) {
  150.                     $sql .= " AND hersteller = :manufacturer";
  151.                 }
  152.                 $sql .= " LIMIT 1";
  153.                 $params = ['article_nr' => $articleNr];
  154.                 if ($manufacturer) {
  155.                     $params['manufacturer'] = $manufacturer;
  156.                 }
  157.                 $stmt $connection->prepare($sql);
  158.                 $result $stmt->executeQuery($params);
  159.                 $productData $result->fetchAssociative();
  160.                 if ($productData) {
  161.                     // Add empty fields that might not exist in entity table
  162.                     $productData['price'] = null;
  163.                     $productData['availability'] = null;
  164.                     $productData['delivery'] = null;
  165.                     $productData['pages'] = '';
  166.                     $productData['compatible_with'] = '';
  167.                     $productData['long_description'] = $productData['long_description'] ?? '';
  168.                 }
  169.             }
  170.             if (!$productData) {
  171.                 return new JsonResponse(['error' => 'Product not found'], 404);
  172.             }
  173.             // Add source information
  174.             $productData['source'] = $source;
  175.             // Build image URL
  176.             if (!empty($productData['image_file']) && $productData['image_file'] !== 'nopic.gif') {
  177.                 $manufacturerLower strtolower($productData['manufacturer'] ?? $manufacturer);
  178.                 $productData['image_url'] = '/image/' $manufacturerLower '/' $productData['image_file'];
  179.             } else {
  180.                 $productData['image_url'] = '/img/nopic.gif';
  181.             }
  182.             // Fetch IceCat product specifications with detailed response information
  183.             $iceCatSpecs null;
  184.             try {
  185.                 $ean $productData['ean'] ?? '';
  186.                 $productManufacturer $productData['manufacturer'] ?? $manufacturer;
  187.                 // Handle special HP manufacturer case (from old code)
  188.                 if (substr($productManufacturer02) === 'HP') {
  189.                     $productManufacturer 'HP';
  190.                 }
  191.                 // Request detailed response including HTTP status, raw response, etc.
  192.                 $iceCatDetailedResponse $iceCatService->getProductSpecs($ean$articleNr$productManufacturertrue);
  193.                 if ($iceCatDetailedResponse && $iceCatDetailedResponse['success']) {
  194.                     $productData['icecat_available'] = true;
  195.                     $productData['icecat_specs'] = $iceCatDetailedResponse['specs'];
  196.                     // Include detailed API response information
  197.                     $productData['icecat_details'] = [
  198.                         'http_status_code' => $iceCatDetailedResponse['http_status'],
  199.                         'request_url' => $iceCatDetailedResponse['request_url'],
  200.                         'response_time_ms' => $iceCatDetailedResponse['response_time_ms'],
  201.                         'raw_response' => $iceCatDetailedResponse['raw_response'],
  202.                         'error' => $iceCatDetailedResponse['error']
  203.                     ];
  204.                 } else {
  205.                     $productData['icecat_available'] = false;
  206.                     // Include details about why IceCat failed
  207.                     $productData['icecat_details'] = [
  208.                         'http_status_code' => $iceCatDetailedResponse['http_status'] ?? null,
  209.                         'request_url' => $iceCatDetailedResponse['request_url'] ?? null,
  210.                         'response_time_ms' => $iceCatDetailedResponse['response_time_ms'] ?? null,
  211.                         'raw_response' => $iceCatDetailedResponse['raw_response'] ?? null,
  212.                         'error' => $iceCatDetailedResponse['error'] ?? 'Product not found in IceCat database'
  213.                     ];
  214.                 }
  215.             } catch (\Exception $e) {
  216.                 error_log('IceCat fetch failed for ' $articleNr ': ' $e->getMessage());
  217.                 $productData['icecat_available'] = false;
  218.                 $productData['icecat_details'] = [
  219.                     'http_status_code' => null,
  220.                     'request_url' => null,
  221.                     'response_time_ms' => null,
  222.                     'raw_response' => null,
  223.                     'error' => $e->getMessage()
  224.                 ];
  225.             }
  226.             return new JsonResponse($productData);
  227.         } catch (\Exception $e) {
  228.             error_log('Error fetching product details: ' $e->getMessage());
  229.             return new JsonResponse(['error' => 'Failed to load product details: ' $e->getMessage()], 500);
  230.         }
  231.     }
  232. }