app/Plugin/NZCategoryBackground42/EventSubscriber/ResponseSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace Plugin\NZCategoryBackground42\EventSubscriber;
  3. use Plugin\NZCategoryBackground42\Repository\CategoryBackgroundRepository;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. class ResponseSubscriber implements EventSubscriberInterface
  8. {
  9.     private $categoryBackgroundRepository;
  10.     public function __construct(CategoryBackgroundRepository $categoryBackgroundRepository)
  11.     {
  12.         $this->categoryBackgroundRepository $categoryBackgroundRepository;
  13.     }
  14.     public static function getSubscribedEvents()
  15.     {
  16.         return [
  17.             KernelEvents::RESPONSE => ['onKernelResponse'0],
  18.         ];
  19.     }
  20.     public function onKernelResponse(ResponseEvent $event)
  21.     {
  22.         $request $event->getRequest();
  23.         $response $event->getResponse();
  24.         // カテゴリページかどうかチェック
  25.         $route $request->attributes->get('_route');
  26.         if ($route !== 'product_list') {
  27.             return;
  28.         }
  29.         // カテゴリIDを取得
  30.         $categoryId $request->query->get('category_id');
  31.         if (!$categoryId) {
  32.             return;
  33.         }
  34.         // 背景設定を取得
  35.         $categoryBackground $this->categoryBackgroundRepository->findByCategoryId($categoryId);
  36.         if (!$categoryBackground || !$categoryBackground->getIsActive()) {
  37.             // デバッグコメントを追加
  38.             $content $response->getContent();
  39.             $debugComment sprintf(
  40.                 "\n<!-- NZCategoryBackground42: category_id=%s, found=%s -->\n",
  41.                 $categoryId,
  42.                 $categoryBackground 'YES (but inactive)' 'NO'
  43.             );
  44.             $content str_replace('</head>'$debugComment '</head>'$content);
  45.             $response->setContent($content);
  46.             return;
  47.         }
  48.         // CSSを生成
  49.         $css $this->generateCSS($categoryBackground);
  50.         if (empty($css)) {
  51.             return;
  52.         }
  53.         // HTMLに挿入
  54.         $content $response->getContent();
  55.         
  56.         $debugInfo sprintf(
  57.             "\n<!-- NZCategoryBackground42: Applied! category_id=%s, type=%s, preset=%s -->\n",
  58.             $categoryId,
  59.             $categoryBackground->getBackgroundType(),
  60.             $categoryBackground->getPresetId() ?: 'custom'
  61.         );
  62.         
  63.         $styleTag sprintf(
  64.             "%s<style type=\"text/css\" id=\"nz-category-bg\">\n/* NZCategoryBackground42 v1.3.0 */\n%s\n</style>\n",
  65.             $debugInfo,
  66.             $css
  67.         );
  68.         
  69.         $content str_replace('</head>'$styleTag '</head>'$content);
  70.         $response->setContent($content);
  71.     }
  72.     private function generateCSS($categoryBackground)
  73.     {
  74.         $css "/* ページ全体に背景を適用 */\n";
  75.         $css .= "body.product_list,\n";
  76.         $css .= ".ec-layoutRole,\n";
  77.         $css .= ".ec-layoutRole__contents {\n";
  78.         switch ($categoryBackground->getBackgroundType()) {
  79.             case 'color':
  80.                 if ($categoryBackground->getBackgroundColor()) {
  81.                     $css .= "    background-color: {$categoryBackground->getBackgroundColor()} !important;\n";
  82.                 }
  83.                 break;
  84.             case 'gradient':
  85.                 if ($categoryBackground->getGradientColor1() && $categoryBackground->getGradientColor2()) {
  86.                     $angle $categoryBackground->getGradientAngle() ?: 135;
  87.                     $gradient sprintf(
  88.                         "linear-gradient(%ddeg, %s, %s)",
  89.                         $angle,
  90.                         $categoryBackground->getGradientColor1(),
  91.                         $categoryBackground->getGradientColor2()
  92.                     );
  93.                     $css .= "    background: {$gradient} !important;\n";
  94.                     $css .= "    background-attachment: fixed !important;\n";
  95.                 }
  96.                 break;
  97.             case 'image':
  98.                 if ($categoryBackground->getBackgroundImage()) {
  99.                     $imageUrl "/html/upload/save_image/{$categoryBackground->getBackgroundImage()}";
  100.                     $css .= "    background-image: url('{$imageUrl}') !important;\n";
  101.                     $size $categoryBackground->getBackgroundSize() ?: 'cover';
  102.                     $position $categoryBackground->getBackgroundPosition() ?: 'center';
  103.                     $repeat $categoryBackground->getBackgroundRepeat() ?: 'no-repeat';
  104.                     $css .= "    background-size: {$size} !important;\n";
  105.                     $css .= "    background-position: {$position} !important;\n";
  106.                     $css .= "    background-repeat: {$repeat} !important;\n";
  107.                     $css .= "    background-attachment: fixed !important;\n";
  108.                 }
  109.                 break;
  110.         }
  111.         $css .= "}\n\n";
  112.         // 個別の要素も透過して背景を表示
  113.         $css .= "/* 各エリアを透過 */\n";
  114.         $css .= ".ec-layoutRole__header,\n";
  115.         $css .= ".ec-layoutRole__left,\n";
  116.         $css .= ".ec-layoutRole__mainWithColumn,\n";
  117.         $css .= ".ec-layoutRole__main {\n";
  118.         $css .= "    background: transparent !important;\n";
  119.         $css .= "}\n";
  120.         // モバイル対応
  121.         if ($categoryBackground->getMobileBackgroundImage()) {
  122.             $mobileImageUrl "/html/upload/save_image/{$categoryBackground->getMobileBackgroundImage()}";
  123.             $css .= "@media (max-width: 768px) {\n";
  124.             $css .= "    body.product_list,\n";
  125.             $css .= "    .ec-layoutRole,\n";
  126.             $css .= "    .ec-layoutRole__contents {\n";
  127.             $css .= "        background-image: url('{$mobileImageUrl}') !important;\n";
  128.             $css .= "    }\n";
  129.             $css .= "}\n";
  130.         }
  131.         return $css;
  132.     }
  133. }