app/Customize/Controller/Admin/Product/SpreadsheetImportController.php line 149

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller\Admin\Product;
  3. use Customize\Service\ScraperService;
  4. use Eccube\Controller\AbstractController;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. /**
  10.  * Admin controller for launching scraper processes and monitoring progress.
  11.  */
  12. class SpreadsheetImportController extends AbstractController
  13. {
  14.     /** @var ScraperService */
  15.     private $scraperService;
  16.     /** @var array */
  17.     private static $categoryLabels = [
  18.         'cp' => 'カーポート',
  19.         'cy' => 'サイクルポート',
  20.         'cg' => 'カーゲート',
  21.         'sh' => 'ガレージシャッター',
  22.         'wd' => 'ウッドデッキ',
  23.         'rs' => '立水栓・ガーデンシンク',
  24.         'gf' => 'ガーデンファニチャー',
  25.         'tf' => '人工芝・芝生',
  26.         'fe' => 'フェンス・柵・塀',
  27.         'ts' => '手すり',
  28.         'mp' => '門扉',
  29.         'fu' => 'ポスト・門柱・宅配ボックス',
  30.         'tr' => 'テラス屋根',
  31.         'tg' => 'テラス囲い・サンルーム',
  32.         'br' => 'バルコニー屋根',
  33.         'bl' => 'バルコニー・ベランダ',
  34.         'aw' => 'オーニング・日よけ',
  35.         'pg' => 'パーゴラ',
  36.         'mo' => '物置・収納・屋外倉庫',
  37.         'sy' => 'ストックヤード',
  38.         'gs' => 'ゴミステーション',
  39.         'wh' => '倉庫・ガレージ',
  40.         'um' => '二重窓(内窓)',
  41.         'ws' => '窓シャッター',
  42.         'mk' => '面格子・窓格子',
  43.         'fj' => '風除室・玄関フード',
  44.         'rd' => '玄関ドア',
  45.         'st' => '石材',
  46.         'sl' => '照明',
  47.         'dy' => 'DIY',
  48.     ];
  49.     public function __construct(ScraperService $scraperService)
  50.     {
  51.         $this->scraperService $scraperService;
  52.     }
  53.     /**
  54.      * Main page with buttons for product and price scraping.
  55.      *
  56.      * @Route("/%eccube_admin_route%/product/scraper", name="admin_product_scraper", methods={"GET"})
  57.      * @Template("@admin/Product/scraper.twig")
  58.      */
  59.     public function index()
  60.     {
  61.         return [
  62.             'categories'       => self::$categoryLabels,
  63.             'runningJobs'      => $this->scraperService->getRunningJobs(),
  64.             'spreadsheetLinks' => $this->scraperService->getSpreadsheetLinks(),
  65.         ];
  66.     }
  67.     /**
  68.      * Start product scraper (basic data).
  69.      *
  70.      * @Route("/%eccube_admin_route%/product/scraper/start_product", name="admin_product_scraper_start_product", methods={"POST"})
  71.      */
  72.     public function startProduct(Request $request): JsonResponse
  73.     {
  74.         if (!$this->isTokenValid()) {
  75.             return $this->json(['error' => 'Invalid CSRF token'], 403);
  76.         }
  77.         if ($this->scraperService->isProductLocked()) {
  78.             return $this->json(['error' => '基本データ取込は既に実行中です'], 409);
  79.         }
  80.         // Parse comma-separated categories (empty = all)
  81.         $catStr trim((string) $request->request->get('categories'''));
  82.         $categories = [];
  83.         if ($catStr !== '') {
  84.             foreach (explode(','$catStr) as $c) {
  85.                 $c trim($c);
  86.                 if ($c !== '' && preg_match('/^[a-z0-9]+$/'$c)) {
  87.                     $categories[] = $c;
  88.                 }
  89.             }
  90.         }
  91.         $result $this->scraperService->startProductScraper($categories);
  92.         if ($result) {
  93.             $label = empty($categories) ? '全カテゴリ' implode(','$categories);
  94.             return $this->json([
  95.                 'status'  => 'started',
  96.                 'message' => "基本データ取込を開始しました(対象: {$label})",
  97.             ]);
  98.         }
  99.         return $this->json(['error' => '開始できませんでした'], 500);
  100.     }
  101.     /**
  102.      * Start price scraper for a specific category.
  103.      *
  104.      * @Route("/%eccube_admin_route%/product/scraper/start_price", name="admin_product_scraper_start_price", methods={"POST"})
  105.      */
  106.     public function startPrice(Request $request): JsonResponse
  107.     {
  108.         if (!$this->isTokenValid()) {
  109.             return $this->json(['error' => 'Invalid CSRF token'], 403);
  110.         }
  111.         $category $request->request->get('category''');
  112.         if (!$category || !isset(self::$categoryLabels[$category])) {
  113.             return $this->json(['error' => '無効なカテゴリです'], 400);
  114.         }
  115.         if ($this->scraperService->isPriceLocked($category)) {
  116.             $label self::$categoryLabels[$category];
  117.             return $this->json(['error' => "{$label} の価格マトリクス取込は既に実行中です"], 409);
  118.         }
  119.         $maker $request->request->get('maker');
  120.         $result $this->scraperService->startPriceScraper($category$maker ?: null);
  121.         if ($result) {
  122.             $label self::$categoryLabels[$category];
  123.             return $this->json(['status' => 'started''message' => "{$label} の価格マトリクス取込を開始しました"]);
  124.         }
  125.         return $this->json(['error' => '開始できませんでした'], 500);
  126.     }
  127.     /**
  128.      * Get current status of all scrapers (AJAX polling endpoint).
  129.      *
  130.      * @Route("/%eccube_admin_route%/product/scraper/status", name="admin_product_scraper_status", methods={"GET"})
  131.      */
  132.     public function status(): JsonResponse
  133.     {
  134.         $product $this->scraperService->getProductProgress();
  135.         $productLocked $this->scraperService->isProductLocked();
  136.         // Collect price statuses for all categories
  137.         $priceStatuses = [];
  138.         foreach (self::$categoryLabels as $cat => $label) {
  139.             $locked $this->scraperService->isPriceLocked($cat);
  140.             $progress $this->scraperService->getPriceProgress($cat);
  141.             if ($locked || $progress) {
  142.                 $priceStatuses[$cat] = [
  143.                     'locked'   => $locked,
  144.                     'progress' => $progress,
  145.                 ];
  146.             }
  147.         }
  148.         return $this->json([
  149.             'product' => [
  150.                 'locked'   => $productLocked,
  151.                 'progress' => $product,
  152.             ],
  153.             'price' => $priceStatuses,
  154.         ]);
  155.     }
  156.     /**
  157.      * Start DB import from ProductALL sheet (Symfony console command).
  158.      *
  159.      * @Route("/%eccube_admin_route%/product/scraper/db_import_product", name="admin_product_scraper_db_import_product", methods={"POST"})
  160.      */
  161.     public function dbImportProduct(Request $request): JsonResponse
  162.     {
  163.         if (!$this->isTokenValid()) {
  164.             return $this->json(['error' => 'Invalid CSRF token'], 403);
  165.         }
  166.         [$ok$message] = $this->scraperService->startDbImportProduct();
  167.         if ($ok) {
  168.             return $this->json(['status' => 'started''message' => $message]);
  169.         }
  170.         return $this->json(['error' => $message], 409);
  171.     }
  172.     /**
  173.      * Start DB import from PP_{category} sheet for a specific category.
  174.      *
  175.      * @Route("/%eccube_admin_route%/product/scraper/db_import_price", name="admin_product_scraper_db_import_price", methods={"POST"})
  176.      */
  177.     public function dbImportPrice(Request $request): JsonResponse
  178.     {
  179.         if (!$this->isTokenValid()) {
  180.             return $this->json(['error' => 'Invalid CSRF token'], 403);
  181.         }
  182.         $category $request->request->get('category''');
  183.         if (!$category) {
  184.             return $this->json(['error' => 'カテゴリを指定してください'], 400);
  185.         }
  186.         [$ok$message] = $this->scraperService->startDbImportPrice($category);
  187.         if ($ok) {
  188.             return $this->json(['status' => 'started''message' => $message]);
  189.         }
  190.         return $this->json(['error' => $message], 409);
  191.     }
  192.     /**
  193.      * Get execution log for a scraper (for debugging).
  194.      *
  195.      * @Route("/%eccube_admin_route%/product/scraper/log", name="admin_product_scraper_log", methods={"GET"})
  196.      */
  197.     public function log(Request $request): JsonResponse
  198.     {
  199.         $type $request->query->get('type''product');
  200.         $category $request->query->get('category''');
  201.         try {
  202.             $log null;
  203.             if ($type === 'product') {
  204.                 $log $this->scraperService->getProductLog(200);
  205.             } elseif ($type === 'price' && $category) {
  206.                 $log $this->scraperService->getPriceLog($category200);
  207.             } elseif ($type === 'db_import_product') {
  208.                 $log $this->scraperService->getDbImportProductLog(200);
  209.             } elseif ($type === 'db_import_price' && $category) {
  210.                 $log $this->scraperService->getDbImportPriceLog($category200);
  211.             } else {
  212.                 return $this->json(['error' => 'invalid type/category'], 400);
  213.             }
  214.             // Include debug info only for 'product' request (reduces load for repeated polls)
  215.             $debug = ($type === 'product') ? $this->scraperService->getDebugInfo() : null;
  216.             return $this->json([
  217.                 'log'   => $log,
  218.                 'debug' => $debug,
  219.             ]);
  220.         } catch (\Throwable $e) {
  221.             return $this->json([
  222.                 'log'   => '(exception: ' $e->getMessage() . ')',
  223.                 'debug' => null,
  224.             ], 200);
  225.         }
  226.     }
  227.     /**
  228.      * Cancel a running scraper.
  229.      *
  230.      * @Route("/%eccube_admin_route%/product/scraper/cancel", name="admin_product_scraper_cancel", methods={"POST"})
  231.      */
  232.     public function cancel(Request $request): JsonResponse
  233.     {
  234.         if (!$this->isTokenValid()) {
  235.             return $this->json(['error' => 'Invalid CSRF token'], 403);
  236.         }
  237.         $type $request->request->get('type''');
  238.         $category $request->request->get('category''');
  239.         if ($type === 'product') {
  240.             $this->scraperService->cancelProduct();
  241.             return $this->json(['status' => 'cancelled''message' => '基本データ取込をキャンセルしました']);
  242.         }
  243.         if ($type === 'price' && $category) {
  244.             $this->scraperService->cancelPrice($category);
  245.             $label self::$categoryLabels[$category] ?? $category;
  246.             return $this->json(['status' => 'cancelled''message' => "{$label} の価格マトリクス取込をキャンセルしました"]);
  247.         }
  248.         return $this->json(['error' => '無効なリクエスト'], 400);
  249.     }
  250. }