<?php
namespace Plugin\NZCategoryBackground42\EventSubscriber;
use Eccube\Event\TemplateEvent;
use Plugin\NZCategoryBackground42\Repository\CategoryBackgroundRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class FrontendSubscriber implements EventSubscriberInterface
{
private $categoryBackgroundRepository;
private $requestStack;
public function __construct(
CategoryBackgroundRepository $categoryBackgroundRepository,
RequestStack $requestStack
) {
$this->categoryBackgroundRepository = $categoryBackgroundRepository;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents()
{
return [
'@admin/Product/index.twig' => 'onAdminProductIndex',
'Product/list.twig' => 'onProductList',
];
}
public function onAdminProductIndex(TemplateEvent $event)
{
// 管理画面では何もしない(動作確認用)
}
public function onProductList(TemplateEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
if (!$request) {
return;
}
$categoryId = $request->query->get('category_id');
if (!$categoryId) {
return;
}
$categoryBackground = $this->categoryBackgroundRepository->findByCategoryId($categoryId);
if (!$categoryBackground || !$categoryBackground->getIsActive()) {
return;
}
// CSSを生成
$css = $this->generateCSS($categoryBackground);
if (empty($css)) {
return;
}
// ソースを取得して直接書き換え
$source = $event->getSource();
$debugInfo = sprintf(
"\n<!-- NZCategoryBackground42: Applied! category_id=%s, type=%s, preset=%s -->\n",
$categoryId,
$categoryBackground->getBackgroundType(),
$categoryBackground->getPresetId() ?: 'custom'
);
$styleTag = sprintf(
"%s<style type=\"text/css\" id=\"nz-category-bg\">\n/* NZCategoryBackground42 */\n%s\n</style>\n",
$debugInfo,
$css
);
// </head>の前に挿入
$source = str_replace('</head>', $styleTag . '</head>', $source);
$event->setSource($source);
}
private function generateCSS($categoryBackground)
{
$css = ".ec-layoutRole__main {\n";
switch ($categoryBackground->getBackgroundType()) {
case 'color':
if ($categoryBackground->getBackgroundColor()) {
$css .= " background-color: {$categoryBackground->getBackgroundColor()} !important;\n";
}
break;
case 'gradient':
if ($categoryBackground->getGradientColor1() && $categoryBackground->getGradientColor2()) {
$angle = $categoryBackground->getGradientAngle() ?: 135;
$gradient = sprintf(
"linear-gradient(%ddeg, %s, %s)",
$angle,
$categoryBackground->getGradientColor1(),
$categoryBackground->getGradientColor2()
);
$css .= " background: {$gradient} !important;\n";
}
break;
case 'image':
if ($categoryBackground->getBackgroundImage()) {
$imageUrl = "/html/upload/save_image/{$categoryBackground->getBackgroundImage()}";
$css .= " background-image: url('{$imageUrl}') !important;\n";
$size = $categoryBackground->getBackgroundSize() ?: 'cover';
$position = $categoryBackground->getBackgroundPosition() ?: 'center';
$repeat = $categoryBackground->getBackgroundRepeat() ?: 'no-repeat';
$css .= " background-size: {$size} !important;\n";
$css .= " background-position: {$position} !important;\n";
$css .= " background-repeat: {$repeat} !important;\n";
}
break;
}
$css .= "}\n";
// モバイル対応
if ($categoryBackground->getMobileBackgroundImage()) {
$mobileImageUrl = "/html/upload/save_image/{$categoryBackground->getMobileBackgroundImage()}";
$css .= "@media (max-width: 768px) {\n";
$css .= " .ec-layoutRole__main {\n";
$css .= " background-image: url('{$mobileImageUrl}') !important;\n";
$css .= " }\n";
$css .= "}\n";
}
return $css;
}
}