<?php
namespace Plugin\AddOnForCategory4\EventSubscriber\Controller\Admin\Product;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Eccube\Common\EccubeConfig;
use Eccube\Event\TemplateEvent;
use Eccube\Event\EventArgs;
use Eccube\Event\EccubeEvents;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\File;
class CategoryControllerSubscriber implements EventSubscriberInterface
{
/**
* @var EccubeConfig
*/
private $eccubeConfig;
/**
* Event constructor.
* @param EccubeConfig $eccubeConfig
*/
public function __construct(
EccubeConfig $eccubeConfig
) {
$this->eccubeConfig = $eccubeConfig;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Product/category.twig' => ['AdminProductCategory', 1],
EccubeEvents::ADMIN_PRODUCT_CATEGORY_INDEX_COMPLETE => 'AdminProductCategoryIndexComplete',
];
}
/**
* @param TemplateEvent $event
*/
public function AdminProductCategory(TemplateEvent $event)
{
$event->addSnippet('@AddOnForCategory4/admin/Product/admin_product_category.twig');
}
public function AdminProductCategoryIndexComplete(EventArgs $event)
{
$add_category_image = null;
$form = $event->getArgument('form');
$TargetCategory = $event->getArgument('TargetCategory');
if($form->get('AddOnForCategory')->getData() != null) {
$add_category_image = $form->get('AddOnForCategory')->getData()->getCategoryImage();
}
if ($TargetCategory) {
if($TargetCategory->getAddOnForCategory() !== null && $TargetCategory->getAddOnForCategory()->getCategoryImage() !== null){
$add_category_image = $TargetCategory->getAddOnForCategory()->getCategoryImage();
}
}
// 移動
if ($add_category_image) {
$file_path = $this->eccubeConfig['eccube_temp_image_dir'] . '/' . $add_category_image;
if (file_exists($file_path)) {
$file = new File($file_path);
$file->move($this->eccubeConfig['eccube_save_image_dir']);
}
}
}
}