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

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.         $result $this->scraperService->startProductScraper();
  81.         if ($result) {
  82.             return $this->json(['status' => 'started''message' => '基本データ取込を開始しました']);
  83.         }
  84.         return $this->json(['error' => '開始できませんでした'], 500);
  85.     }
  86.     /**
  87.      * Start price scraper for a specific category.
  88.      *
  89.      * @Route("/%eccube_admin_route%/product/scraper/start_price", name="admin_product_scraper_start_price", methods={"POST"})
  90.      */
  91.     public function startPrice(Request $request): JsonResponse
  92.     {
  93.         if (!$this->isTokenValid()) {
  94.             return $this->json(['error' => 'Invalid CSRF token'], 403);
  95.         }
  96.         $category $request->request->get('category''');
  97.         if (!$category || !isset(self::$categoryLabels[$category])) {
  98.             return $this->json(['error' => '無効なカテゴリです'], 400);
  99.         }
  100.         if ($this->scraperService->isPriceLocked($category)) {
  101.             $label self::$categoryLabels[$category];
  102.             return $this->json(['error' => "{$label} の価格マトリクス取込は既に実行中です"], 409);
  103.         }
  104.         $maker $request->request->get('maker');
  105.         $result $this->scraperService->startPriceScraper($category$maker ?: null);
  106.         if ($result) {
  107.             $label self::$categoryLabels[$category];
  108.             return $this->json(['status' => 'started''message' => "{$label} の価格マトリクス取込を開始しました"]);
  109.         }
  110.         return $this->json(['error' => '開始できませんでした'], 500);
  111.     }
  112.     /**
  113.      * Get current status of all scrapers (AJAX polling endpoint).
  114.      *
  115.      * @Route("/%eccube_admin_route%/product/scraper/status", name="admin_product_scraper_status", methods={"GET"})
  116.      */
  117.     public function status(): JsonResponse
  118.     {
  119.         $product $this->scraperService->getProductProgress();
  120.         $productLocked $this->scraperService->isProductLocked();
  121.         // Collect price statuses for all categories
  122.         $priceStatuses = [];
  123.         foreach (self::$categoryLabels as $cat => $label) {
  124.             $locked $this->scraperService->isPriceLocked($cat);
  125.             $progress $this->scraperService->getPriceProgress($cat);
  126.             if ($locked || $progress) {
  127.                 $priceStatuses[$cat] = [
  128.                     'locked'   => $locked,
  129.                     'progress' => $progress,
  130.                 ];
  131.             }
  132.         }
  133.         return $this->json([
  134.             'product' => [
  135.                 'locked'   => $productLocked,
  136.                 'progress' => $product,
  137.             ],
  138.             'price' => $priceStatuses,
  139.         ]);
  140.     }
  141.     /**
  142.      * Get execution log for a scraper (for debugging).
  143.      *
  144.      * @Route("/%eccube_admin_route%/product/scraper/log", name="admin_product_scraper_log", methods={"GET"})
  145.      */
  146.     public function log(Request $request): JsonResponse
  147.     {
  148.         $type $request->query->get('type''product');
  149.         $category $request->query->get('category''');
  150.         if ($type === 'product') {
  151.             return $this->json([
  152.                 'log'   => $this->scraperService->getProductLog(200),
  153.                 'debug' => $this->scraperService->getDebugInfo(),
  154.             ]);
  155.         }
  156.         if ($type === 'price' && $category) {
  157.             return $this->json([
  158.                 'log'   => $this->scraperService->getPriceLog($category200),
  159.                 'debug' => $this->scraperService->getDebugInfo(),
  160.             ]);
  161.         }
  162.         return $this->json(['error' => 'invalid type/category'], 400);
  163.     }
  164.     /**
  165.      * Cancel a running scraper.
  166.      *
  167.      * @Route("/%eccube_admin_route%/product/scraper/cancel", name="admin_product_scraper_cancel", methods={"POST"})
  168.      */
  169.     public function cancel(Request $request): JsonResponse
  170.     {
  171.         if (!$this->isTokenValid()) {
  172.             return $this->json(['error' => 'Invalid CSRF token'], 403);
  173.         }
  174.         $type $request->request->get('type''');
  175.         $category $request->request->get('category''');
  176.         if ($type === 'product') {
  177.             $this->scraperService->cancelProduct();
  178.             return $this->json(['status' => 'cancelled''message' => '基本データ取込をキャンセルしました']);
  179.         }
  180.         if ($type === 'price' && $category) {
  181.             $this->scraperService->cancelPrice($category);
  182.             $label self::$categoryLabels[$category] ?? $category;
  183.             return $this->json(['status' => 'cancelled''message' => "{$label} の価格マトリクス取込をキャンセルしました"]);
  184.         }
  185.         return $this->json(['error' => '無効なリクエスト'], 400);
  186.     }
  187. }