app/Plugin/OnehandredSiteMaps/Event/OnehandredSiteMapsEvent.php line 154

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the OnehandredSiteMaps.
  4.  *
  5.  * Copyright (C) 2018 ONE HAND RED
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\OnehandredSiteMaps\Event;
  11. use Eccube\Common\EccubeConfig;
  12. use Eccube\Entity\Master\ProductStatus;
  13. use Eccube\Entity\Product;
  14. use Eccube\Event\EventArgs;
  15. use Eccube\Repository\Master\ProductStatusRepository;
  16. use Eccube\Repository\PageRepository;
  17. use Eccube\Repository\ProductRepository;
  18. use Plugin\OnehandredSiteMaps\Entity\OnehandredSiteMaps;
  19. use Plugin\OnehandredSiteMaps\Repository\OnehandredSiteMapsRepository;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class OnehandredSiteMapsEvent implements EventSubscriberInterface
  22. {
  23.     const PAGE_ID_CATEGORY 2;
  24.     const PAGE_ID_PRODUCT 3;
  25.     const PARAM_ID_PAGE 0;
  26.     /**
  27.      * @var EccubeConfig
  28.      */
  29.     protected $eccubeConfig;
  30.     /**
  31.      * @var OnehandredSiteMapsRepository
  32.      */
  33.     protected $onehandredSiteMapsRepository;
  34.     /**
  35.      * @var PageRepository
  36.      */
  37.     protected $pageRepository;
  38.     /**
  39.      * @var ProductStatusRepository
  40.      */
  41.     protected $productStatusRepository;
  42.     /**
  43.      * @var ProductRepository
  44.      */
  45.     protected $productRepository;
  46.     /**
  47.      * OnehandredSiteMapsEvent constructor.
  48.      *
  49.      * @param EccubeConfig                 $eccubeConfig
  50.      * @param OnehandredSiteMapsRepository $onehandredSiteMapsRepository
  51.      * @param PageRepository               $pageRepository
  52.      * @param ProductStatusRepository      $productStatusRepository
  53.      * @param ProductRepository            $productRepository
  54.      */
  55.     public function __construct(
  56.         EccubeConfig $eccubeConfig,
  57.         OnehandredSiteMapsRepository $onehandredSiteMapsRepository,
  58.         PageRepository $pageRepository,
  59.         ProductStatusRepository $productStatusRepository,
  60.         ProductRepository $productRepository
  61.     ) {
  62.         $this->eccubeConfig $eccubeConfig;
  63.         $this->onehandredSiteMapsRepository $onehandredSiteMapsRepository;
  64.         $this->pageRepository $pageRepository;
  65.         $this->productStatusRepository $productStatusRepository;
  66.         $this->productRepository $productRepository;
  67.     }
  68.     /**
  69.      * @return array
  70.      */
  71.     public static function getSubscribedEvents()
  72.     {
  73.         return [
  74.             'admin.content.page.edit.complete' => 'onAdminContentPageEditComplete',
  75.             'admin.content.page.delete.complete' => 'onAdminContentPageDeleteComplete',
  76.             'admin.product.category.index.complete' => 'onAdminProductCategoryIndexComplete',
  77.             'admin.product.category.delete.complete' => 'onAdminProductCategoryDeleteComplete',
  78.             'admin.product.edit.complete' => 'onAdminProductEditComplete',
  79.             'admin.product.delete.complete' => 'onAdminProductDeleteComplete',
  80.             'admin.product.copy.complete' => 'onAdminProductCopyComplete',
  81.         ];
  82.     }
  83.     /**
  84.      * Admin/Content/PageController edit.
  85.      */
  86.     public function onAdminContentPageEditComplete(EventArgs $event)
  87.     {
  88.         $Show $this->productStatusRepository
  89.             ->find(ProductStatus::DISPLAY_SHOW);
  90.         $Hide $this->productStatusRepository
  91.             ->find(ProductStatus::DISPLAY_HIDE);
  92.         $Abolished $this->productStatusRepository
  93.             ->find(ProductStatus::DISPLAY_ABOLISHED);
  94.         
  95.         $Page $event->getArgument('Page');
  96.         $SiteMaps $this->onehandredSiteMapsRepository
  97.             ->getByPageId($Page->getId());
  98.         if (empty($SiteMaps)) {
  99.             $Status $Show;
  100.             if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
  101.                 $Status $Hide;
  102.             }
  103.             $this->register($Page->getId(), self::PARAM_ID_PAGE$Status$Page->getUpdateDate());
  104.         } else {
  105.             foreach ($SiteMaps as $SiteMap) {
  106.                 // status
  107.                 $Status $Show;
  108.                 if (self::PAGE_ID_PRODUCT == $SiteMap->getOnehandredSiteMapsPageId()) {
  109.                     $Product $this->productRepository->find($SiteMap->getOnehandredSiteMapsParamId());
  110.                     if ($Product) {
  111.                         $Status $Product->getStatus();
  112.                     }
  113.                     if ($Show == $Status) {
  114.                         if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
  115.                             $Status $Hide;
  116.                         }
  117.                     }
  118.                 } else {
  119.                     if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
  120.                         $Status $Hide;
  121.                     }
  122.                 }
  123.                 $SiteMap->setOnehandredSiteMapsStatus($Status);
  124.                 // lastmod
  125.                 $SiteMap->setOnehandredSiteMapsLastmod(
  126.                     $Page->getUpdateDate()
  127.                         ->format(
  128.                             $this->eccubeConfig['onehandredsitemaps.lastmod_date_format']
  129.                         )
  130.                 );
  131.                 $this->onehandredSiteMapsRepository->register($SiteMap);
  132.             }
  133.         }
  134.     }
  135.     /**
  136.      * Admin/Content/PageController delete.
  137.      */
  138.     public function onAdminContentPageDeleteComplete(EventArgs $event)
  139.     {
  140.         // $Page 取得不可!
  141.         // $Page = $event->getArgument('Page');
  142.         // $SiteMaps = $this->onehandredSiteMapsRepository
  143.         //     ->getByPageId($Page->getId());
  144.         $SiteMaps $this->onehandredSiteMapsRepository
  145.             ->getByNotExistsPage();
  146.         foreach ($SiteMaps as $SiteMap) {
  147.             $this->onehandredSiteMapsRepository->unregister($SiteMap);
  148.         }
  149.     }
  150.     /**
  151.      * Admin/Product/CategoryController index.
  152.      */
  153.     public function onAdminProductCategoryIndexComplete(EventArgs $event)
  154.     {
  155.         $Show $this->productStatusRepository
  156.             ->find(ProductStatus::DISPLAY_SHOW);
  157.         $Hide $this->productStatusRepository
  158.             ->find(ProductStatus::DISPLAY_HIDE);
  159.         $Page $this->pageRepository->find(self::PAGE_ID_CATEGORY);
  160.         $Category $event->getArgument('TargetCategory');
  161.         $SiteMaps $this->onehandredSiteMapsRepository
  162.             ->getByKey(self::PAGE_ID_CATEGORY$Category->getId());
  163.         if (empty($SiteMaps)) {
  164.             $Status $Show;
  165.             if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
  166.                 $Status $Hide;
  167.             }
  168.             $this->register(self::PAGE_ID_CATEGORY$Category->getId(), $Status$Category->getUpdateDate());
  169.         } else {
  170.             foreach ($SiteMaps as $SiteMap) {
  171.                 // lastmod
  172.                 $SiteMap->setOnehandredSiteMapsLastmod(
  173.                     $Category->getUpdateDate()
  174.                         ->format(
  175.                             $this->eccubeConfig['onehandredsitemaps.lastmod_date_format']
  176.                         )
  177.                 );
  178.                 $this->onehandredSiteMapsRepository->register($SiteMap);
  179.             }
  180.         }
  181.     }
  182.     /**
  183.      * Admin/Product/CategoryController delete.
  184.      */
  185.     public function onAdminProductCategoryDeleteComplete(EventArgs $event)
  186.     {
  187.         // $Category 取得不可!
  188.         // $Category = $event->getArgument('TargetCategory');
  189.         // $SiteMaps = $this->onehandredSiteMapsRepository
  190.         //     ->getByKey(self::PAGE_ID_CATEGORY, $Category->getId());
  191.         $SiteMaps $this->onehandredSiteMapsRepository
  192.             ->getByNotExistsCategory();
  193.         foreach ($SiteMaps as $SiteMap) {
  194.             $this->onehandredSiteMapsRepository->unregister($SiteMap);
  195.         }
  196.     }
  197.     /**
  198.      * Admin/Product/ProductController edit.
  199.      */
  200.     public function onAdminProductEditComplete(EventArgs $event)
  201.     {
  202.         $Show $this->productStatusRepository
  203.             ->find(ProductStatus::DISPLAY_SHOW);
  204.         $Hide $this->productStatusRepository
  205.             ->find(ProductStatus::DISPLAY_HIDE);
  206.         $Page $this->pageRepository->find(self::PAGE_ID_PRODUCT);
  207.         $Product $event->getArgument('Product');
  208.         $SiteMaps $this->onehandredSiteMapsRepository
  209.             ->getByKey(self::PAGE_ID_PRODUCT$Product->getId());
  210.         if (empty($SiteMaps)) {
  211.             $Status $Product->getStatus();
  212.             if ($Show == $Product->getStatus()) {
  213.                 if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
  214.                     $Status $Hide;
  215.                 }
  216.             }
  217.             $this->register(self::PAGE_ID_PRODUCT$Product->getId(), $Status$Product->getUpdateDate());
  218.         } else {
  219.             foreach ($SiteMaps as $SiteMap) {
  220.                 // status
  221.                 $Status $Product->getStatus();
  222.                 if ($Show == $Status) {
  223.                     if (($Page->getMetaRobots()) && (false !== strpos($Page->getMetaRobots(), 'noindex'))) {
  224.                         $Status $Hide;
  225.                     }
  226.                 }
  227.                 $SiteMap->setOnehandredSiteMapsStatus($Status);
  228.                 // lastmod
  229.                 $SiteMap->setOnehandredSiteMapsLastmod(
  230.                     $Product->getUpdateDate()
  231.                         ->format(
  232.                             $this->eccubeConfig['onehandredsitemaps.lastmod_date_format']
  233.                         )
  234.                 );
  235.                 $this->onehandredSiteMapsRepository->register($SiteMap);
  236.             }
  237.         }
  238.     }
  239.     /**
  240.      * Admin/Product/ProductController delete.
  241.      */
  242.     public function onAdminProductDeleteComplete(EventArgs $event)
  243.     {
  244.         // $Product 取得不可!
  245.         // $Product = $event->getArgument('Product');
  246.         // $SiteMaps = $this->onehandredSiteMapsRepository
  247.         //     ->getByKey(self::PAGE_ID_PRODUCT, $Product->getId());
  248.         $SiteMaps $this->onehandredSiteMapsRepository
  249.             ->getByNotExistsProduct();
  250.         foreach ($SiteMaps as $SiteMap) {
  251.             $this->onehandredSiteMapsRepository->unregister($SiteMap);
  252.         }
  253.     }
  254.     /**
  255.      * Admin/Product/ProductController copy.
  256.      */
  257.     public function onAdminProductCopyComplete(EventArgs $event)
  258.     {
  259.         $Product $event->getArgument('Product');
  260.         $SiteMaps $this->onehandredSiteMapsRepository
  261.             ->getByKey(self::PAGE_ID_PRODUCT$Product->getId());
  262.         $CopyProduct $event->getArgument('CopyProduct');
  263.         if (empty($SiteMaps)) {
  264.             $this->register(self::PAGE_ID_PRODUCT$CopyProduct->getId(), $CopyProduct->getStatus(), $CopyProduct->getUpdateDate());
  265.         } else {
  266.             foreach ($SiteMaps as $SiteMap) {
  267.                 $CopySiteMaps = clone $SiteMap;
  268.                 $CopySiteMaps->setOnehandredSiteMapsParamId($CopyProduct->getId());
  269.                 $CopySiteMaps->setOnehandredSiteMapsLastmod(
  270.                     $CopyProduct->getUpdateDate()
  271.                         ->format(
  272.                             $this->eccubeConfig['onehandredsitemaps.lastmod_date_format']
  273.                         )
  274.                 );
  275.                 $this->onehandredSiteMapsRepository->register($CopySiteMaps);
  276.             }
  277.         }
  278.     }
  279.     private function register($page_id$param_id$status$update_date)
  280.     {
  281.         $SiteMap = new OnehandredSiteMaps();
  282.         $SiteMap->setOnehandredSiteMapsPageId($page_id);
  283.         $SiteMap->setOnehandredSiteMapsParamId($param_id);
  284.         // status
  285.         $SiteMap->setOnehandredSiteMapsStatus($status);
  286.         // changefreq
  287.         $SiteMap->setOnehandredSiteMapsChangefreq(
  288.             $this->eccubeConfig['onehandredsitemaps.changefreq']
  289.         );
  290.         // priority
  291.         $SiteMap->setOnehandredSiteMapsPriority(
  292.             $this->eccubeConfig['onehandredsitemaps.priority_nomal']
  293.         );
  294.         // lastmod
  295.         $SiteMap->setOnehandredSiteMapsLastmod(
  296.             $update_date
  297.                 ->format(
  298.                     $this->eccubeConfig['onehandredsitemaps.lastmod_date_format']
  299.                 )
  300.         );
  301.         $this->onehandredSiteMapsRepository->register($SiteMap);
  302.     }
  303. }