app/Plugin/ProductField/EventSubscriber/ProductSubscriber.php line 77

Open in your IDE?
  1. <?php
  2. namespace Plugin\ProductField\EventSubscriber;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Event\TemplateEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Plugin\ProductField\Repository\ConfigRepository;
  7. use Plugin\ProductField\Repository\ProductFieldContentRepository;
  8. use Plugin\ProductField\Common\CommonField;
  9. use Plugin\ProductField\Common\CommonDefine;
  10. use Eccube\Event\EccubeEvents;
  11. use Eccube\Event\EventArgs;
  12. use Eccube\Common\EccubeConfig;
  13. use Plugin\ProductField\Service\ProductFileUploader;
  14. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  15. use Symfony\Component\Asset\Packages;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. use Doctrine\Persistence\ManagerRegistry;
  18. class ProductSubscriber extends AbstractController implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var ConfigRepository
  22.      */
  23.     protected $configRepository;
  24.     protected $ProductFieldContentRepository;
  25.     protected $eccubeConfig;
  26.     protected $productFileUploader;
  27.     protected $packages;
  28.     protected $doctrine;
  29.     /**
  30.      * ConfigController constructor.
  31.      *
  32.      * @param ConfigRepository $configRepository
  33.      */
  34.     public function __construct(
  35.         ConfigRepository $configRepository,
  36.         ProductFieldContentRepository $ProductFieldContentRepository,
  37.         EccubeConfig $eccubeConfig,
  38.         ProductFileUploader $productFileUploader,
  39.         Packages $packages,
  40.         ManagerRegistry $doctrine
  41.     ) {
  42.         $this->configRepository $configRepository;
  43.         $this->ProductFieldContentRepository $ProductFieldContentRepository;
  44.         $this->eccubeConfig $eccubeConfig;
  45.         $this->productFileUploader $productFileUploader;
  46.         $this->packages $packages;
  47.         $this->doctrine $doctrine;
  48.     }
  49.     protected function getProductFieldContent($product_id$meta_key)
  50.     {
  51.         $ProductFieldContent $this->ProductFieldContentRepository->getProductFieldContent($product_id$meta_key);
  52.         if (empty($ProductFieldContent)) {
  53.             //無いとき
  54.             $ProductFieldContent $this->ProductFieldContentRepository->newProductFieldContent($product_id$meta_key);
  55.         }
  56.         return $ProductFieldContent;
  57.     }
  58.     /**
  59.      * @inheritDoc
  60.      */
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             '@admin/Product/product.twig' => 'onTemplateAdminProduct',
  65.             EccubeEvents::ADMIN_PRODUCT_EDIT_INITIALIZE => 'onAdminProductEditInit',
  66.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'onAdminProductEditComplete'
  67.         ];
  68.     }
  69.     public function onAdminProductEditInit(EventArgs $event)
  70.     {
  71.         $Configs $this->configRepository->joinMetaKey($event->getArguments()["Product"]["id"]);
  72.         $builder $event->getArgument('builder');
  73.         //auto_render trueにすると製品に自動的に表示されるがhtmlを編集したいのでコメントアウト onTemplateAdminProductでtwig追加
  74.         if (!empty($Configs)) {
  75.             foreach ($Configs as $index => $Config) {
  76.                 $CommonField = new CommonField();
  77.                 //関連商品で必要
  78.                 $CommonField->setEntityManager($this->doctrine->getManager());
  79.                 $create $CommonField->create([
  80.                     "product_id" => $event->getArguments()["Product"]["id"],
  81.                     "data" => $Config,
  82.                     "eccube_save_image_dir" => $this->eccubeConfig["eccube_save_image_dir"],
  83.                 ]);
  84.                 $validation $CommonField->validation($Config);
  85.                 if ($Config["a_type"] === "select" || $Config["a_type"] === "radio" || $Config["a_type"] === "checkbox" || $Config["a_type"] === "relation_product" || $Config["a_type"] === "relation_product_multiple") {
  86.                     //choicesがある場合、三項演算子などで空を含めるとエラーになるので分岐
  87.                     $builder->add(CommonDefine::ADD_FIELD_NAME "_" $Config["a_meta_key"], $create["type"], [
  88.                         'placeholder' => '選択して下さい',
  89.                         'constraints' => $validation,
  90.                         'mapped' => false//フォームのプロパティでない場合
  91.                         'attr' => $create["attr"],
  92.                         // 'required' => !empty($Config["a_not_blank"]) ? true : false,
  93.                         'required' => true,
  94.                         'label' => $create["name"],
  95.                         'data' => $create["data"],
  96.                         'choices' =>  array_keys($create["choices"]),
  97.                         'choice_label' => function ($choice$key$value) use ($create) {
  98.                             //キーと値が逆なので変更
  99.                             return $create["choices"][$choice];
  100.                         },
  101.                         'expanded' => $Config["a_type"] === "radio" || $Config["a_type"] === "checkbox" true false,
  102.                         'multiple' => $Config["a_type"] === "checkbox" ||  $Config["a_type"] === "relation_product_multiple" true false,
  103.                         'eccube_form_options' => [
  104.                             'plg_product_field_required' => !empty($Config["a_not_blank"]) ? true false,
  105.                             'plg_product_field_render' => true,
  106.                             'plg_product_field_image' => false,
  107.                             'plg_product_field_file' => false,
  108.                             'plg_product_field_hidden' => false,
  109.                             'auto_render' => false// 自動表示フラグ
  110.                         ],
  111.                     ]);
  112.                 } else {
  113.                     $builder->add(CommonDefine::ADD_FIELD_NAME "_" $Config["a_meta_key"], $create["type"], [
  114.                         'constraints' => $validation,
  115.                         'mapped' => false//フォームのプロパティでない場合
  116.                         'attr' => $create["attr"],
  117.                         'required' => !empty($Config["a_not_blank"]) ? true false,
  118.                         'label' => $create["name"],
  119.                         'data' => $create["data"],
  120.                         'eccube_form_options' => [
  121.                             'plg_product_field_required' => !empty($Config["a_not_blank"]) ? true false,
  122.                             'plg_product_field_render' => true,
  123.                             'plg_product_field_image' => $Config["a_type"] === "image" true false,
  124.                             'plg_product_field_file' => $Config["a_type"] === "file" true false,
  125.                             'plg_product_field_hidden' => false,
  126.                             'auto_render' => false// 自動表示フラグ
  127.                         ],
  128.                     ]);
  129.                     if ($Config["a_type"] === "image" || $Config["a_type"] === "file") {
  130.                         $builder->add($Config["a_type"] . "_" CommonDefine::ADD_FIELD_NAME "_" $Config["a_meta_key"], HiddenType::class, [
  131.                             'constraints' => !empty($Config["a_not_blank"]) && !empty($create["data"]) ? [new NotBlank()] : [],
  132.                             'mapped' => false,
  133.                             'data' => !empty($create["data"]) ? $this->packages->getUrl($Config["b_meta_content"], 'save_image') : "",
  134.                             'error_bubbling' => false,
  135.                             'eccube_form_options' => [
  136.                                 'plg_product_field_required' => false,
  137.                                 'plg_product_field_render' => true,
  138.                                 'plg_product_field_image' => false,
  139.                                 'plg_product_field_file' => false,
  140.                                 'plg_product_field_hidden' => true,
  141.                                 'auto_render' => false// 自動表示フラグ
  142.                             ],
  143.                         ]);
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     public function onTemplateAdminProduct(TemplateEvent $event)
  150.     {
  151.         $form_adds = [];
  152.         foreach ($event->getParameters()["form"] as $f) {
  153.             if (!empty($f->vars["eccube_form_options"]["plg_product_field_render"])) {
  154.                 $form_adds[] = $f;
  155.             }
  156.         }
  157.         $event->setParameter('form_adds'$form_adds);
  158.         $event->addSnippet('@ProductField/admin/Product/product_fields.twig');
  159.     }
  160.     public function onAdminProductEditComplete(EventArgs $event)
  161.     {
  162.         $form $event->getArgument('form');
  163.         $Configs $this->configRepository->joinMetaKey($event->getArgument('Product')->getId());
  164.         if (!empty($Configs)) {
  165.             foreach ($form as $key => $f) {
  166.                 //plg_product_field_renderがなければスキップ plg_product_field_hiddenがtrueならスキップ
  167.                 if (empty($f->getConfig()->getOptions()["eccube_form_options"]["plg_product_field_render"]) || !empty($f->getConfig()->getOptions()["eccube_form_options"]["plg_product_field_hidden"])) {
  168.                     continue;
  169.                 }
  170.                 foreach ($Configs as $Config) {
  171.                     if ($key === CommonDefine::ADD_FIELD_NAME "_" $Config["a_meta_key"]) {
  172.                         $ProductFieldContent $this->getProductFieldContent($event->getArgument('Product')->getId(), $Config["a_meta_key"]);
  173.                         //画像ファイルで空でなければ
  174.                         if ($Config["a_type"] === "image" || $Config["a_type"] === "file") {
  175.                             if (empty($form->get($Config["a_type"] . "_" $f->getName())->getData())) {
  176.                                 $ProductFieldContent->setMetaContent("");
  177.                             }
  178.                             if (!empty($f->getData())) {
  179.                                 $filename $this->productFileUploader->upload($f->getData());
  180.                                 $ProductFieldContent->setMetaContent($filename);
  181.                             }
  182.                         } elseif ($Config["a_type"] === "checkbox") {
  183.                             $ProductFieldContent->setMetaContent(serialize($f->getData()));
  184.                         } elseif ($Config["a_type"] === "relation_product_multiple") {
  185.                             //getDataだと順番が初期値に置き換えられてしまう Postで取得
  186.                             $relation_items = !empty($_POST[$f->getParent()->getName()][$f->getName()]) ? $_POST[$f->getParent()->getName()][$f->getName()] : [];
  187.                             $ProductFieldContent->setMetaContent(serialize($relation_items));
  188.                         } else {
  189.                             $ProductFieldContent->setMetaContent($f->getData());
  190.                         }
  191.                         $this->entityManager->persist($ProductFieldContent);
  192.                         $this->entityManager->flush($ProductFieldContent);
  193.                         break;
  194.                     }
  195.                 }
  196.             }
  197.         }
  198.     }
  199. }