<?php
/**
* This file is part of ProductSort4
*
* Copyright(c) Akira Kurozumi <info@a-zumi.net>
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\ProductSort4;
use Eccube\Entity\Product;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Eccube\Event\TemplateEvent;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Doctrine\ORM\EntityManagerInterface;
use Plugin\ProductSort4\Entity\Config;
class Event implements EventSubscriberInterface
{
/**
*
* @var type
*/
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Product/index.twig' => 'onRenderAdminProduct',
EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'onUpdateProductSortNo',
EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'onUpdateProductSortNo'
];
}
public function onRenderAdminProduct(TemplateEvent $event)
{
$Config = $this->entityManager->find(Config::class, Config::ID);
if($Config && $Config->isEnable()) {
$event->addSnippet('@ProductSort4/admin/Product/index.script.twig');
}
}
/**
* 新規商品登録(複製含む)が完了時にplg_sort_noを再設定
*
* @param EventArgs $eventArgs
*/
public function onUpdateProductSortNo(EventArgs $eventArgs)
{
$productRepositry = $this->entityManager->getRepository(Product::class);
$Products = $productRepositry->findBy([], ['plg_sort_no' => 'ASC']);
foreach($Products as $key => $Product) {
$Product->setPlgSortNo($key+1);
$this->entityManager->persist($Product);
}
$this->entityManager->flush();
}
}