app/Plugin/IkusMobileApi/Event.php line 47

Open in your IDE?
  1. <?php
  2. namespace Plugin\IkusMobileApi;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Eccube\Entity\Product;
  5. use Eccube\Event\EccubeEvents;
  6. use Eccube\Event\EventArgs;
  7. use Eccube\Event\TemplateEvent;
  8. use Plugin\IkusMobileApi\Util\Paths;
  9. use Plugin\IkusMobileApi\Util\ServerConfig;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\HttpFoundation\File\UploadedFile;
  13. class Event implements EventSubscriberInterface
  14. {
  15.     protected EntityManagerInterface $entityManager;
  16.     protected Paths $path;
  17.     public function __construct(
  18.         EntityManagerInterface $entityManager,
  19.         Paths $path
  20.     )
  21.     {
  22.         $this->entityManager $entityManager;
  23.         $this->path $path;
  24.     }
  25.     /**
  26.      * @return array
  27.      */
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             '@admin/Product/product.twig' => 'onAdminProductRender',
  32.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'onAdminProductEditComplete',
  33.         ];
  34.     }
  35.     /**
  36.      * 商品登録・編集画面のレンダリング時の処理
  37.      * @param TemplateEvent $event
  38.      * @return void
  39.      */
  40.     public function onAdminProductRender(TemplateEvent $event): void
  41.     {
  42.         $prefixPattern preg_quote('{% for f in form.class|filter(f => f.vars.eccube_form_options.auto_render) %}');
  43.         $suffixPattern preg_quote('{% endfor %}');
  44.         $pattern '|' $prefixPattern '(.*?)' $suffixPattern '|s';
  45.         $source $event->getSource();
  46.         if (preg_match($pattern$source$matchesPREG_OFFSET_CAPTURE)) {
  47.             $replacement $matches[0][0] . "{{ include('@IkusMobileApi/admin/Product/ext_ar_file_edit.twig') }}";
  48.             $source preg_replace($pattern$replacement$source);
  49.             $event->setSource($source);
  50.         }
  51.         $event->setParameter('upload_max_filesize'ServerConfig::getMaxUploadSize());
  52.         $event->setParameter('upload_max_filesize_in_byte'ServerConfig::getMaxUploadSizeInBytes());
  53.         $event->addSnippet('@IkusMobileApi/admin/Product/ext_ar_file_edit_js.twig');
  54.     }
  55.     /**
  56.      * 商品登録・編集Submit後の処理
  57.      * @param EventArgs $event
  58.      * @return void
  59.      */
  60.     public function onAdminProductEditComplete(EventArgs $event): void
  61.     {
  62.         /** @var FormInterface $form */
  63.         $form $event->getArgument('form');
  64.         /** @var Product $product */
  65.         $product $event->getArgument('Product');
  66.         if (!empty($_FILES['admin_product']['name']['upload_ar_file_ios'])) {
  67.             /** @var UploadedFile $ar_file */
  68.             $ar_file $form->get('upload_ar_file_ios')->getData();
  69.             $extension $ar_file->getClientOriginalExtension();
  70.             $file_name rtrim($ar_file->getClientOriginalName(), '.' $extension) . '_' date('YmdHi') . '.' $extension;
  71.             $ar_file->move($this->path->getIosArFilePath(), $file_name);
  72.             $product->setArFileIos($file_name);
  73.         }
  74.         if ($product->getArFileIosDeleteFlag()) {
  75.             $ar_file_path $this->path->getIosArFilePath() . $product->getArFileIos();
  76.             if (file_exists($ar_file_path)) {
  77.                 unlink($ar_file_path);
  78.             }
  79.             $product->setArFileIos(null);
  80.         }
  81.         if (!empty($_FILES['admin_product']['name']['upload_ar_file_android'])) {
  82.             /** @var UploadedFile $ar_file */
  83.             $ar_file $form->get('upload_ar_file_android')->getData();
  84.             $extension $ar_file->getClientOriginalExtension();
  85.             $file_name rtrim($ar_file->getClientOriginalName(), '.' $extension) . '_' date('YmdHi') . '.' $extension;
  86.             $ar_file->move($this->path->getAndroidArFilePath(), $file_name);
  87.             $product->setArFileAndroid($file_name);
  88.         }
  89.         if ($product->getArFileAndroidDeleteFlag()) {
  90.             $ar_file_path $this->path->getAndroidArFilePath() . $product->getArFileAndroid();
  91.             if (file_exists($ar_file_path)) {
  92.                 unlink($ar_file_path);
  93.             }
  94.             $product->setArFileAndroid(null);
  95.         }
  96.         $this->entityManager->flush($product);
  97.     }
  98. }