app/Plugin/NZCategoryBackground42/EventSubscriber/FrontendSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace Plugin\NZCategoryBackground42\EventSubscriber;
  3. use Eccube\Event\TemplateEvent;
  4. use Plugin\NZCategoryBackground42\Repository\CategoryBackgroundRepository;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. class FrontendSubscriber implements EventSubscriberInterface
  8. {
  9.     private $categoryBackgroundRepository;
  10.     private $requestStack;
  11.     public function __construct(
  12.         CategoryBackgroundRepository $categoryBackgroundRepository,
  13.         RequestStack $requestStack
  14.     ) {
  15.         $this->categoryBackgroundRepository $categoryBackgroundRepository;
  16.         $this->requestStack $requestStack;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             '@admin/Product/index.twig' => 'onAdminProductIndex',
  22.             'Product/list.twig' => 'onProductList',
  23.         ];
  24.     }
  25.     public function onAdminProductIndex(TemplateEvent $event)
  26.     {
  27.         // 管理画面では何もしない(動作確認用)
  28.     }
  29.     public function onProductList(TemplateEvent $event)
  30.     {
  31.         $request $this->requestStack->getCurrentRequest();
  32.         if (!$request) {
  33.             return;
  34.         }
  35.         $categoryId $request->query->get('category_id');
  36.         if (!$categoryId) {
  37.             return;
  38.         }
  39.         $categoryBackground $this->categoryBackgroundRepository->findByCategoryId($categoryId);
  40.         if (!$categoryBackground || !$categoryBackground->getIsActive()) {
  41.             return;
  42.         }
  43.         // CSSを生成
  44.         $css $this->generateCSS($categoryBackground);
  45.         if (empty($css)) {
  46.             return;
  47.         }
  48.         // ソースを取得して直接書き換え
  49.         $source $event->getSource();
  50.         
  51.         $debugInfo sprintf(
  52.             "\n<!-- NZCategoryBackground42: Applied! category_id=%s, type=%s, preset=%s -->\n",
  53.             $categoryId,
  54.             $categoryBackground->getBackgroundType(),
  55.             $categoryBackground->getPresetId() ?: 'custom'
  56.         );
  57.         
  58.         $styleTag sprintf(
  59.             "%s<style type=\"text/css\" id=\"nz-category-bg\">\n/* NZCategoryBackground42 */\n%s\n</style>\n",
  60.             $debugInfo,
  61.             $css
  62.         );
  63.         
  64.         // </head>の前に挿入
  65.         $source str_replace('</head>'$styleTag '</head>'$source);
  66.         
  67.         $event->setSource($source);
  68.     }
  69.     private function generateCSS($categoryBackground)
  70.     {
  71.         $css ".ec-layoutRole__main {\n";
  72.         switch ($categoryBackground->getBackgroundType()) {
  73.             case 'color':
  74.                 if ($categoryBackground->getBackgroundColor()) {
  75.                     $css .= "    background-color: {$categoryBackground->getBackgroundColor()} !important;\n";
  76.                 }
  77.                 break;
  78.             case 'gradient':
  79.                 if ($categoryBackground->getGradientColor1() && $categoryBackground->getGradientColor2()) {
  80.                     $angle $categoryBackground->getGradientAngle() ?: 135;
  81.                     $gradient sprintf(
  82.                         "linear-gradient(%ddeg, %s, %s)",
  83.                         $angle,
  84.                         $categoryBackground->getGradientColor1(),
  85.                         $categoryBackground->getGradientColor2()
  86.                     );
  87.                     $css .= "    background: {$gradient} !important;\n";
  88.                 }
  89.                 break;
  90.             case 'image':
  91.                 if ($categoryBackground->getBackgroundImage()) {
  92.                     $imageUrl "/html/upload/save_image/{$categoryBackground->getBackgroundImage()}";
  93.                     $css .= "    background-image: url('{$imageUrl}') !important;\n";
  94.                     $size $categoryBackground->getBackgroundSize() ?: 'cover';
  95.                     $position $categoryBackground->getBackgroundPosition() ?: 'center';
  96.                     $repeat $categoryBackground->getBackgroundRepeat() ?: 'no-repeat';
  97.                     $css .= "    background-size: {$size} !important;\n";
  98.                     $css .= "    background-position: {$position} !important;\n";
  99.                     $css .= "    background-repeat: {$repeat} !important;\n";
  100.                 }
  101.                 break;
  102.         }
  103.         $css .= "}\n";
  104.         // モバイル対応
  105.         if ($categoryBackground->getMobileBackgroundImage()) {
  106.             $mobileImageUrl "/html/upload/save_image/{$categoryBackground->getMobileBackgroundImage()}";
  107.             $css .= "@media (max-width: 768px) {\n";
  108.             $css .= "    .ec-layoutRole__main {\n";
  109.             $css .= "        background-image: url('{$mobileImageUrl}') !important;\n";
  110.             $css .= "    }\n";
  111.             $css .= "}\n";
  112.         }
  113.         return $css;
  114.     }
  115. }