app/Plugin/ProductSort4/Event.php line 48

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of ProductSort4
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  * https://a-zumi.net
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\ProductSort4;
  13. use Eccube\Entity\Product;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Eccube\Event\TemplateEvent;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Plugin\ProductSort4\Entity\Config;
  20. class Event implements EventSubscriberInterface
  21. {
  22.     /**
  23.      *
  24.      * @var type
  25.      */
  26.     private $entityManager;
  27.     public function __construct(EntityManagerInterface $entityManager)
  28.     {
  29.         $this->entityManager $entityManager;
  30.     }
  31.     /**
  32.      * @return array
  33.      */
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             '@admin/Product/index.twig' => 'onRenderAdminProduct',
  38.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'onUpdateProductSortNo',
  39.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'onUpdateProductSortNo'
  40.         ];
  41.     }
  42.     public function onRenderAdminProduct(TemplateEvent $event)
  43.     {
  44.         $Config $this->entityManager->find(Config::class, Config::ID);
  45.         if($Config && $Config->isEnable()) {
  46.             $event->addSnippet('@ProductSort4/admin/Product/index.script.twig');
  47.         }
  48.     }
  49.     /**
  50.      * 新規商品登録(複製含む)が完了時にplg_sort_noを再設定
  51.      *
  52.      * @param EventArgs $eventArgs
  53.      */
  54.     public function onUpdateProductSortNo(EventArgs $eventArgs)
  55.     {
  56.         $productRepositry $this->entityManager->getRepository(Product::class);
  57.         $Products $productRepositry->findBy([], ['plg_sort_no' => 'ASC']);
  58.         foreach($Products as $key => $Product) {
  59.             $Product->setPlgSortNo($key+1);
  60.             $this->entityManager->persist($Product);
  61.         }
  62.         $this->entityManager->flush();
  63.     }
  64. }