<?php
namespace Plugin\IkusMobileApi;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Product;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Plugin\IkusMobileApi\Util\Paths;
use Plugin\IkusMobileApi\Util\ServerConfig;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Event implements EventSubscriberInterface
{
protected EntityManagerInterface $entityManager;
protected Paths $path;
public function __construct(
EntityManagerInterface $entityManager,
Paths $path
)
{
$this->entityManager = $entityManager;
$this->path = $path;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
'@admin/Product/product.twig' => 'onAdminProductRender',
EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'onAdminProductEditComplete',
];
}
/**
* 商品登録・編集画面のレンダリング時の処理
* @param TemplateEvent $event
* @return void
*/
public function onAdminProductRender(TemplateEvent $event): void
{
$prefixPattern = preg_quote('{% for f in form.class|filter(f => f.vars.eccube_form_options.auto_render) %}');
$suffixPattern = preg_quote('{% endfor %}');
$pattern = '|' . $prefixPattern . '(.*?)' . $suffixPattern . '|s';
$source = $event->getSource();
if (preg_match($pattern, $source, $matches, PREG_OFFSET_CAPTURE)) {
$replacement = $matches[0][0] . "{{ include('@IkusMobileApi/admin/Product/ext_ar_file_edit.twig') }}";
$source = preg_replace($pattern, $replacement, $source);
$event->setSource($source);
}
$event->setParameter('upload_max_filesize', ServerConfig::getMaxUploadSize());
$event->setParameter('upload_max_filesize_in_byte', ServerConfig::getMaxUploadSizeInBytes());
$event->addSnippet('@IkusMobileApi/admin/Product/ext_ar_file_edit_js.twig');
}
/**
* 商品登録・編集Submit後の処理
* @param EventArgs $event
* @return void
*/
public function onAdminProductEditComplete(EventArgs $event): void
{
/** @var FormInterface $form */
$form = $event->getArgument('form');
/** @var Product $product */
$product = $event->getArgument('Product');
if (!empty($_FILES['admin_product']['name']['upload_ar_file_ios'])) {
/** @var UploadedFile $ar_file */
$ar_file = $form->get('upload_ar_file_ios')->getData();
$extension = $ar_file->getClientOriginalExtension();
$file_name = rtrim($ar_file->getClientOriginalName(), '.' . $extension) . '_' . date('YmdHi') . '.' . $extension;
$ar_file->move($this->path->getIosArFilePath(), $file_name);
$product->setArFileIos($file_name);
}
if ($product->getArFileIosDeleteFlag()) {
$ar_file_path = $this->path->getIosArFilePath() . $product->getArFileIos();
if (file_exists($ar_file_path)) {
unlink($ar_file_path);
}
$product->setArFileIos(null);
}
if (!empty($_FILES['admin_product']['name']['upload_ar_file_android'])) {
/** @var UploadedFile $ar_file */
$ar_file = $form->get('upload_ar_file_android')->getData();
$extension = $ar_file->getClientOriginalExtension();
$file_name = rtrim($ar_file->getClientOriginalName(), '.' . $extension) . '_' . date('YmdHi') . '.' . $extension;
$ar_file->move($this->path->getAndroidArFilePath(), $file_name);
$product->setArFileAndroid($file_name);
}
if ($product->getArFileAndroidDeleteFlag()) {
$ar_file_path = $this->path->getAndroidArFilePath() . $product->getArFileAndroid();
if (file_exists($ar_file_path)) {
unlink($ar_file_path);
}
$product->setArFileAndroid(null);
}
$this->entityManager->flush($product);
}
}