<?php
/*
* This file is part of the OnehandredSiteMaps.
*
* Copyright (C) 2018 ONE HAND RED
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\OnehandredSiteMaps\Event;
use Eccube\Common\EccubeConfig;
use Eccube\Entity\Master\ProductStatus;
use Eccube\Entity\Product;
use Eccube\Event\EventArgs;
use Eccube\Repository\Master\ProductStatusRepository;
use Eccube\Repository\PageRepository;
use Eccube\Repository\ProductRepository;
use Plugin\OnehandredSiteMaps\Entity\OnehandredSiteMaps;
use Plugin\OnehandredSiteMaps\Repository\OnehandredSiteMapsRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OnehandredSiteMapsEvent implements EventSubscriberInterface
{
const PAGE_ID_CATEGORY = 2;
const PAGE_ID_PRODUCT = 3;
const PARAM_ID_PAGE = 0;
/**
* @var EccubeConfig
*/
protected $eccubeConfig;
/**
* @var OnehandredSiteMapsRepository
*/
protected $onehandredSiteMapsRepository;
/**
* @var PageRepository
*/
protected $pageRepository;
/**
* @var ProductStatusRepository
*/
protected $productStatusRepository;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
* OnehandredSiteMapsEvent constructor.
*
* @param EccubeConfig $eccubeConfig
* @param OnehandredSiteMapsRepository $onehandredSiteMapsRepository
* @param PageRepository $pageRepository
* @param ProductStatusRepository $productStatusRepository
* @param ProductRepository $productRepository
*/
public function __construct(
EccubeConfig $eccubeConfig,
OnehandredSiteMapsRepository $onehandredSiteMapsRepository,
PageRepository $pageRepository,
ProductStatusRepository $productStatusRepository,
ProductRepository $productRepository
) {
$this->eccubeConfig = $eccubeConfig;
$this->onehandredSiteMapsRepository = $onehandredSiteMapsRepository;
$this->pageRepository = $pageRepository;
$this->productStatusRepository = $productStatusRepository;
$this->productRepository = $productRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'admin.content.page.edit.complete' => 'onAdminContentPageEditComplete',
'admin.content.page.delete.complete' => 'onAdminContentPageDeleteComplete',
'admin.product.category.index.complete' => 'onAdminProductCategoryIndexComplete',
'admin.product.category.delete.complete' => 'onAdminProductCategoryDeleteComplete',
'admin.product.edit.complete' => 'onAdminProductEditComplete',
'admin.product.delete.complete' => 'onAdminProductDeleteComplete',
'admin.product.copy.complete' => 'onAdminProductCopyComplete',
];
}
/**
* Admin/Content/PageController edit.
*/
public function onAdminContentPageEditComplete(EventArgs $event)
{
$Show = $this->productStatusRepository
->find(ProductStatus::DISPLAY_SHOW);
$Hide = $this->productStatusRepository
->find(ProductStatus::DISPLAY_HIDE);
$Abolished = $this->productStatusRepository
->find(ProductStatus::DISPLAY_ABOLISHED);
$Page = $event->getArgument('Page');
$SiteMaps = $this->onehandredSiteMapsRepository
->getByPageId($Page->getId());
if (empty($SiteMaps)) {
$Status = $Show;
if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
$Status = $Hide;
}
$this->register($Page->getId(), self::PARAM_ID_PAGE, $Status, $Page->getUpdateDate());
} else {
foreach ($SiteMaps as $SiteMap) {
// status
$Status = $Show;
if (self::PAGE_ID_PRODUCT == $SiteMap->getOnehandredSiteMapsPageId()) {
$Product = $this->productRepository->find($SiteMap->getOnehandredSiteMapsParamId());
if ($Product) {
$Status = $Product->getStatus();
}
if ($Show == $Status) {
if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
$Status = $Hide;
}
}
} else {
if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
$Status = $Hide;
}
}
$SiteMap->setOnehandredSiteMapsStatus($Status);
// lastmod
$SiteMap->setOnehandredSiteMapsLastmod(
$Page->getUpdateDate()
->format(
$this->eccubeConfig['onehandredsitemaps.lastmod_date_format']
)
);
$this->onehandredSiteMapsRepository->register($SiteMap);
}
}
}
/**
* Admin/Content/PageController delete.
*/
public function onAdminContentPageDeleteComplete(EventArgs $event)
{
// $Page 取得不可!
// $Page = $event->getArgument('Page');
// $SiteMaps = $this->onehandredSiteMapsRepository
// ->getByPageId($Page->getId());
$SiteMaps = $this->onehandredSiteMapsRepository
->getByNotExistsPage();
foreach ($SiteMaps as $SiteMap) {
$this->onehandredSiteMapsRepository->unregister($SiteMap);
}
}
/**
* Admin/Product/CategoryController index.
*/
public function onAdminProductCategoryIndexComplete(EventArgs $event)
{
$Show = $this->productStatusRepository
->find(ProductStatus::DISPLAY_SHOW);
$Hide = $this->productStatusRepository
->find(ProductStatus::DISPLAY_HIDE);
$Page = $this->pageRepository->find(self::PAGE_ID_CATEGORY);
$Category = $event->getArgument('TargetCategory');
$SiteMaps = $this->onehandredSiteMapsRepository
->getByKey(self::PAGE_ID_CATEGORY, $Category->getId());
if (empty($SiteMaps)) {
$Status = $Show;
if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
$Status = $Hide;
}
$this->register(self::PAGE_ID_CATEGORY, $Category->getId(), $Status, $Category->getUpdateDate());
} else {
foreach ($SiteMaps as $SiteMap) {
// lastmod
$SiteMap->setOnehandredSiteMapsLastmod(
$Category->getUpdateDate()
->format(
$this->eccubeConfig['onehandredsitemaps.lastmod_date_format']
)
);
$this->onehandredSiteMapsRepository->register($SiteMap);
}
}
}
/**
* Admin/Product/CategoryController delete.
*/
public function onAdminProductCategoryDeleteComplete(EventArgs $event)
{
// $Category 取得不可!
// $Category = $event->getArgument('TargetCategory');
// $SiteMaps = $this->onehandredSiteMapsRepository
// ->getByKey(self::PAGE_ID_CATEGORY, $Category->getId());
$SiteMaps = $this->onehandredSiteMapsRepository
->getByNotExistsCategory();
foreach ($SiteMaps as $SiteMap) {
$this->onehandredSiteMapsRepository->unregister($SiteMap);
}
}
/**
* Admin/Product/ProductController edit.
*/
public function onAdminProductEditComplete(EventArgs $event)
{
$Show = $this->productStatusRepository
->find(ProductStatus::DISPLAY_SHOW);
$Hide = $this->productStatusRepository
->find(ProductStatus::DISPLAY_HIDE);
$Page = $this->pageRepository->find(self::PAGE_ID_PRODUCT);
$Product = $event->getArgument('Product');
$SiteMaps = $this->onehandredSiteMapsRepository
->getByKey(self::PAGE_ID_PRODUCT, $Product->getId());
if (empty($SiteMaps)) {
$Status = $Product->getStatus();
if ($Show == $Product->getStatus()) {
if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
$Status = $Hide;
}
}
$this->register(self::PAGE_ID_PRODUCT, $Product->getId(), $Status, $Product->getUpdateDate());
} else {
foreach ($SiteMaps as $SiteMap) {
// status
$Status = $Product->getStatus();
if ($Show == $Status) {
if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
$Status = $Hide;
}
}
$SiteMap->setOnehandredSiteMapsStatus($Status);
// lastmod
$SiteMap->setOnehandredSiteMapsLastmod(
$Product->getUpdateDate()
->format(
$this->eccubeConfig['onehandredsitemaps.lastmod_date_format']
)
);
$this->onehandredSiteMapsRepository->register($SiteMap);
}
}
}
/**
* Admin/Product/ProductController delete.
*/
public function onAdminProductDeleteComplete(EventArgs $event)
{
// $Product 取得不可!
// $Product = $event->getArgument('Product');
// $SiteMaps = $this->onehandredSiteMapsRepository
// ->getByKey(self::PAGE_ID_PRODUCT, $Product->getId());
$SiteMaps = $this->onehandredSiteMapsRepository
->getByNotExistsProduct();
foreach ($SiteMaps as $SiteMap) {
$this->onehandredSiteMapsRepository->unregister($SiteMap);
}
}
/**
* Admin/Product/ProductController copy.
*/
public function onAdminProductCopyComplete(EventArgs $event)
{
$Product = $event->getArgument('Product');
$SiteMaps = $this->onehandredSiteMapsRepository
->getByKey(self::PAGE_ID_PRODUCT, $Product->getId());
$CopyProduct = $event->getArgument('CopyProduct');
if (empty($SiteMaps)) {
$this->register(self::PAGE_ID_PRODUCT, $CopyProduct->getId(), $CopyProduct->getStatus(), $CopyProduct->getUpdateDate());
} else {
foreach ($SiteMaps as $SiteMap) {
$CopySiteMaps = clone $SiteMap;
$CopySiteMaps->setOnehandredSiteMapsParamId($CopyProduct->getId());
$CopySiteMaps->setOnehandredSiteMapsLastmod(
$CopyProduct->getUpdateDate()
->format(
$this->eccubeConfig['onehandredsitemaps.lastmod_date_format']
)
);
$this->onehandredSiteMapsRepository->register($CopySiteMaps);
}
}
}
private function register($page_id, $param_id, $status, $update_date)
{
$SiteMap = new OnehandredSiteMaps();
$SiteMap->setOnehandredSiteMapsPageId($page_id);
$SiteMap->setOnehandredSiteMapsParamId($param_id);
// status
$SiteMap->setOnehandredSiteMapsStatus($status);
// changefreq
$SiteMap->setOnehandredSiteMapsChangefreq(
$this->eccubeConfig['onehandredsitemaps.changefreq']
);
// priority
$SiteMap->setOnehandredSiteMapsPriority(
$this->eccubeConfig['onehandredsitemaps.priority_nomal']
);
// lastmod
$SiteMap->setOnehandredSiteMapsLastmod(
$update_date
->format(
$this->eccubeConfig['onehandredsitemaps.lastmod_date_format']
)
);
$this->onehandredSiteMapsRepository->register($SiteMap);
}
}