app/Plugin/TabaCustomFields2/TabaCustomFieldsEvent.php line 333

Open in your IDE?
  1. <?php
  2. namespace Plugin\TabaCustomFields2;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Plugin\TabaCustomFields2\Event\Admin\CustomerEvent as AdminCustomerEvent;
  5. use Eccube\Event\EventArgs;
  6. use Plugin\TabaCustomFields2\Common\Constants;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Plugin\TabaCustomFields2\Event\Admin\ProductEvent as AdminProductEvent;
  9. use Plugin\TabaCustomFields2\Event\Admin\OrderEvent as AdminOrderEvent;
  10. use Eccube\Event\TemplateEvent;
  11. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  12. use Eccube\Common\Constant;
  13. use Plugin\TabaCustomFields2\Event\CustomerEvent;
  14. use Plugin\TabaCustomFields2\Event\OrderEvent;
  15. use Plugin\TabaCustomFields2\Repository\CustomFieldsContentsRepository;
  16. use Plugin\TabaCustomFields2\Repository\CustomFieldsRepository;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Eccube\Request\Context;
  20. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  21. use Eccube\Event\EccubeEvents;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. use Symfony\Component\DependencyInjection\ContainerInterface;
  24. class TabaCustomFieldsEvent implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var ContainerInterface
  28.      */
  29.     private $container;
  30.     /**
  31.      * @var EntityManagerInterface
  32.      */
  33.     protected $entityManager;
  34.     /**
  35.      * @var EventDispatcherInterface
  36.      */
  37.     protected $eventDispatcher;
  38.     /**
  39.      * @var Context
  40.      */
  41.     protected $requestContext;
  42.     /**
  43.      * @var CustomFieldsContentsRepository
  44.      */
  45.     protected $customFieldsContentsRepository;
  46.     /**
  47.      * @var CustomFieldsRepository
  48.      */
  49.     protected $customFieldsRepository;
  50.     /**
  51.      * @var CsrfTokenManagerInterface
  52.      */
  53.     protected $tokenManager;
  54.     /**
  55.      * @var AdminCustomerEvent
  56.      */
  57.     protected $adminCustomerEvent;
  58.     /**
  59.      * @var AdminProductEvent
  60.      */
  61.     protected $adminProductEvent;
  62.     /**
  63.      * @var AdminOrderEvent
  64.      */
  65.     protected $adminOrderEvent;
  66.     /**
  67.      * @var CustomerEvent
  68.      */
  69.     protected $customerEvent;
  70.     /**
  71.      * TabaCustomFields2Event constructor.
  72.      */
  73.     public function __construct(
  74.         ContainerInterface $container,
  75.         EntityManagerInterface $entityManager,
  76.         EventDispatcherInterface $eventDispatcher,
  77.         Context $requestContext,
  78.         CustomFieldsContentsRepository $customFieldsContentsRepository,
  79.         CustomFieldsRepository $customFieldsRepository,
  80.         CsrfTokenManagerInterface $tokenManager,
  81.         AdminCustomerEvent $adminCustomerEvent,
  82.         AdminProductEvent $adminProductEvent,
  83.         AdminOrderEvent $adminOrderEvent,
  84.         CustomerEvent $customerEvent
  85.     ) {
  86.         $this->container $container;
  87.         $this->entityManager $entityManager;
  88.         $this->eventDispatcher $eventDispatcher;
  89.         $this->requestContext $requestContext;
  90.         $this->customFieldsContentsRepository $customFieldsContentsRepository;
  91.         $this->customFieldsRepository $customFieldsRepository;
  92.         $this->tokenManager $tokenManager;
  93.         $this->adminCustomerEvent $adminCustomerEvent;
  94.         $this->adminProductEvent $adminProductEvent;
  95.         $this->adminOrderEvent $adminOrderEvent;
  96.         $this->customerEvent $customerEvent;
  97.     }
  98.     /**
  99.      * {@inheritdoc}
  100.      *
  101.      * @return array
  102.      */
  103.     public static function getSubscribedEvents()
  104.     {
  105.         return [
  106.             KernelEvents::CONTROLLER_ARGUMENTS => [['onKernelController'100000000]],
  107.             EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE => [['onAdminCustomerEditInit'10]],
  108.             EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE => [['onAdminCustomerEditComplete'10]],
  109.             EccubeEvents::ADMIN_CUSTOMER_INDEX_INITIALIZE => [['onAdminCustomerSearchInit'10]],
  110.             EccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH => [['onAdminCustomerSearch'10]],
  111.             EccubeEvents::ADMIN_PRODUCT_EDIT_INITIALIZE => [['onAdminProductEditInit'10]],
  112.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => [['onAdminProductEditComplete'10]],
  113.             EccubeEvents::ADMIN_PRODUCT_INDEX_INITIALIZE => [['onAdminProductSearchInit'10]],
  114.             EccubeEvents::ADMIN_PRODUCT_INDEX_SEARCH => [['onAdminProductSearch'10]],
  115.             EccubeEvents::ADMIN_ORDER_EDIT_INDEX_INITIALIZE => [['onAdminOrderEditInit'10]],
  116.             EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE => [['onAdminOrderEditComplete'10]],
  117.             EccubeEvents::ADMIN_ORDER_INDEX_INITIALIZE => [['onAdminOrderSearchInit'10]],
  118.             EccubeEvents::ADMIN_ORDER_INDEX_SEARCH => [['onAdminOrderSearch'10]],
  119.             EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE => [['onFrontCustomerInit'10]],
  120.             EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE => [['onFrontCustomerComplete'10]],
  121.             EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_INITIALIZE => [['onFrontCustomerInit'10]],
  122.             EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_COMPLETE => [['onFrontCustomerComplete'10]],
  123.             '@admin/Customer/index.twig' =>  [['onAdminCfCustomerCsvDownloadLinkRender'10]],
  124.             '@admin/Customer/edit.twig' => [['onAdminFileUploadJSRender'10]],
  125.             '@admin/Product/index.twig' =>  [['onAdminCfProductCsvDownloadLinkRender'10]],
  126.             '@admin/Product/product.twig' => [['onAdminFileUploadJSRender'10]],
  127.             '@admin/Order/index.twig' =>  [['onAdminCfOrderCsvDownloadLinkRender'10]],
  128.             '@admin/Order/edit.twig' => [['onAdminFileUploadJSRender'10]],
  129.             'Shopping/index.twig' => [['onTemplateShoppingIndex'10]],
  130.             'Shopping/confirm.twig' => [['onTemplateShoppingConfirm'10]],
  131.             'Mypage/history.twig' => [['onTemplateMypageHistory'10]],
  132.             'Entry/confirm.twig' => [['onEntryConfirmForCustomFields'10]],
  133.         ];
  134.     }
  135.     /**
  136.      * Initialize fields into form builder
  137.      *
  138.      * @param EventArgs $event
  139.      */
  140.     public function onAdminCustomerEditInit(EventArgs $event)
  141.     {
  142.         $this->adminCustomerEvent->onAdminCustomerEditInit($event);
  143.     }
  144.     /**
  145.      * Handle data
  146.      *
  147.      * @param EventArgs $event
  148.      */
  149.     public function onAdminCustomerEditComplete(EventArgs $event)
  150.     {
  151.         $this->adminCustomerEvent->onAdminCustomerEditComplete($event);
  152.     }
  153.     /**
  154.      * Initialize fields into form builder
  155.      *
  156.      * @param EventArgs $event
  157.      */
  158.     public function onAdminCustomerSearchInit(EventArgs $event)
  159.     {
  160.         $this->adminCustomerEvent->onAdminCustomerSearchInit($event);
  161.     }
  162.     /**
  163.      * Handle data
  164.      *
  165.      * @param EventArgs $event
  166.      */
  167.     public function onAdminCustomerSearch(EventArgs $event)
  168.     {
  169.         $this->adminCustomerEvent->onAdminCustomerSearch($event);
  170.     }
  171.     /**
  172.      * Initialize fields into form builder
  173.      *
  174.      * @param EventArgs $event
  175.      */
  176.     public function onAdminProductEditInit(EventArgs $event)
  177.     {
  178.         $this->adminProductEvent->onAdminProductEditInit($event);
  179.     }
  180.     /**
  181.      * Handle data
  182.      *
  183.      * @param EventArgs $event
  184.      */
  185.     public function onAdminProductEditComplete(EventArgs $event)
  186.     {
  187.         $this->adminProductEvent->onAdminProductEditComplete($event);
  188.     }
  189.     /**
  190.      * Initialize fields into form builder
  191.      *
  192.      * @param EventArgs $event
  193.      */
  194.     public function onAdminProductSearchInit(EventArgs $event)
  195.     {
  196.         $this->adminProductEvent->onAdminProductSearchInit($event);
  197.     }
  198.     /**
  199.      * Handle data
  200.      *
  201.      * @param EventArgs $event
  202.      */
  203.     public function onAdminProductSearch(EventArgs $event)
  204.     {
  205.         $this->adminProductEvent->onAdminProductSearch($event);
  206.     }
  207.     /**
  208.      * Initialize fields into form builder
  209.      *
  210.      * @param EventArgs $event
  211.      */
  212.     public function onAdminOrderEditInit(EventArgs $event)
  213.     {
  214.         $this->adminOrderEvent->onAdminOrderEditInit($event);
  215.     }
  216.     /**
  217.      * Handle data
  218.      *
  219.      * @param EventArgs $event
  220.      */
  221.     public function onAdminOrderEditComplete(EventArgs $event)
  222.     {
  223.         $this->adminOrderEvent->onAdminOrderEditComplete($event);
  224.     }
  225.     /**
  226.      * Initialize fields into form builder
  227.      *
  228.      * @param EventArgs $event
  229.      */
  230.     public function onAdminOrderSearchInit(EventArgs $event)
  231.     {
  232.         $this->adminOrderEvent->onAdminOrderSearchInit($event);
  233.     }
  234.     /**
  235.      * Handle data
  236.      *
  237.      * @param EventArgs $event
  238.      */
  239.     public function onAdminOrderSearch(EventArgs $event)
  240.     {
  241.         $this->adminOrderEvent->onAdminOrderSearch($event);
  242.     }
  243.     /**
  244.      * Add snippet JS into template
  245.      *
  246.      * @param TemplateEvent $templateEvent
  247.      */
  248.     public function onAdminFileUploadJSRender(TemplateEvent $templateEvent)
  249.     {
  250.         // args
  251.         $templateEvent->setParameter('upload_urlpath_route'Constants::ADMIN_BIND_PREFIX.'file_upload');
  252.         $templateEvent->setParameter('formtype_name'Constants::FILE_UPLOAD_FORMTYPE_NAME);
  253.         //csrf
  254.         $templateEvent->setParameter('csrf_token_name'Constant::TOKEN_NAME);
  255.         $templateEvent->setParameter('csrf_token_key'$this->tokenManager->getToken(Constants::FILE_UPLOAD_FORMTYPE_NAME)->getValue());
  256.         // form
  257.         $templateEvent->setParameter('bilde_form_type_name'$templateEvent->getParameter('form')->vars['id']);
  258.         if ($templateEvent->hasParameter('Customer')) {
  259.             $templateEvent->setParameter('entity''customer');
  260.         } elseif ($templateEvent->hasParameter('Product')) {
  261.             $templateEvent->setParameter('entity''product');
  262.         } elseif ($templateEvent->hasParameter('Order')) {
  263.             $templateEvent->setParameter('entity''order');
  264.         }
  265.         $templateEvent->addSnippet('@TabaCustomFields2/common/fileUpload/asset.twig');
  266.         $templateEvent->addSnippet('@TabaCustomFields2/common/fileUpload/functionJS.twig');
  267.         // $templateEvent->addSnippet('@TabaCustomFields2/admin/snippet/datetimepicker.twig');
  268.     }
  269.     /**
  270.      * Initialize fields into form builder
  271.      *
  272.      * @param EventArgs $event
  273.      */
  274.     public function onFrontCustomerInit(EventArgs $event)
  275.     {
  276.         $this->customerEvent->onFrontCustomerInit($event);
  277.     }
  278.     /**
  279.      * Handle data
  280.      *
  281.      * @param EventArgs $event
  282.      */
  283.     public function onFrontCustomerComplete(EventArgs $event)
  284.     {
  285.         $this->customerEvent->onFrontCustomerComplete($event);
  286.     }
  287.     /**
  288.      * @param ControllerArgumentsEvent $event
  289.      */
  290.     public function onKernelController(ControllerArgumentsEvent $event)
  291.     {
  292.         // 管理画面イベント
  293.         if ($this->requestContext->isAdmin()) {
  294.             // テンプレートイベント
  295.             if ($event->getRequest()->attributes->has('_template')) {
  296.                 $template $event->getRequest()->attributes->get('_template');
  297.                 $this->eventDispatcher->addListener($template->getTemplate(), function (TemplateEvent $templateEvent) {
  298.                     // 管理画面のナビゲーションにtaba app のメニューを差し込みます。
  299.                     $taba $this->container->get(Constants::CONTAINER_KEY_NAME);
  300.                     if (!$taba->get(Constants::PLUGIN_CATEGORY_ID ".menu")) {
  301.                         $templateEvent->addSnippet('@TabaCustomFields2/admin/snippet/nav_taba_app.twig');
  302.                         $taba->set(Constants::PLUGIN_CATEGORY_ID ".menu",true);
  303.                     }
  304.                     // メニューを差し込みます。
  305.                     $templateEvent->addSnippet('@TabaCustomFields2/admin/snippet/nav.twig');
  306.                 });
  307.             }
  308.         }
  309.     }
  310.     /**
  311.      * Add js to display field on form
  312.      *
  313.      * @param TemplateEvent $templateEvent
  314.      */
  315.     public function onTemplateShoppingIndex(TemplateEvent $templateEvent)
  316.     {
  317.         $templateEvent->addSnippet('@TabaCustomFields2/snippet/shopping_index.twig');
  318.     }
  319.     /**
  320.      * Add js to display field on form
  321.      *
  322.      * @param TemplateEvent $templateEvent
  323.      */
  324.     public function onTemplateShoppingConfirm(TemplateEvent $templateEvent)
  325.     {
  326.         $templateEvent->addSnippet('@TabaCustomFields2/snippet/shopping_confirm.twig');
  327.     }
  328.     /**
  329.      * Add js to display field on form
  330.      *
  331.      * @param TemplateEvent $templateEvent
  332.      */
  333.     public function onTemplateMypageHistory(TemplateEvent $templateEvent)
  334.     {
  335.         $target_entity 'order';
  336.         $target_id $templateEvent->getParameter('Order')->getId();
  337.         $customFieldsContents $this->customFieldsContentsRepository->getCustomFieldsContents($target_entity$target_id);
  338.         if (!isset($customFieldsContents) || !$customFieldsContents) {
  339.             $customFieldsContents $this->customFieldsContentsRepository->newCustomFieldsContents($target_entity$target_id);
  340.         }
  341.         $templateEvent->setParameter('customFieldsContents'$customFieldsContents);
  342.         // カスタムフィールドの定義を取得
  343.         $customFields $this->customFieldsRepository->getCustomFields($target_entity);
  344.         $templateEvent->setParameter('customFields'$customFields);
  345.         $templateEvent->addSnippet('@TabaCustomFields2/snippet/mypage_history.twig');
  346.     }
  347.     /**
  348.      * カスタムフィールド付きの会員情報CSVダウンロード リンク設置
  349.      *
  350.      * @param TemplateEvent $templateEvent
  351.      */
  352.     public function onAdminCfCustomerCsvDownloadLinkRender(TemplateEvent $templateEvent)
  353.     {
  354.         $templateEvent->addSnippet('@TabaCustomFields2/admin/snippet/csv_download_customer_link.twig');
  355.     }
  356.     /**
  357.      * カスタムフィールド付きの注文情報CSVダウンロード リンク設置
  358.      *
  359.      * @param TemplateEvent $templateEvent
  360.      */
  361.     public function onAdminCfOrderCsvDownloadLinkRender(TemplateEvent $templateEvent)
  362.     {
  363.         $templateEvent->addSnippet('@TabaCustomFields2/admin/snippet/csv_download_order_link.twig');
  364.     }
  365.     /**
  366.      * カスタムフィールド付きの商品情報CSVダウンロード リンク設置
  367.      *
  368.      * @param TemplateEvent $templateEvent
  369.      */
  370.     public function onAdminCfProductCsvDownloadLinkRender(TemplateEvent $templateEvent)
  371.     {
  372.         $templateEvent->addSnippet('@TabaCustomFields2/admin/snippet/csv_download_product_link.twig');
  373.     }
  374.     /**
  375.      * @param TemplateEvent $templateEvent
  376.      */
  377.     public function onEntryConfirmForCustomFields(TemplateEvent $templateEvent)
  378.     {
  379.         $sourceOrigin $templateEvent->getSource();
  380.         $search = [
  381.             "{{ form_widget(f, { type: 'hidden'}) }}",
  382.             '{{ f.vars.data }}'
  383.         ];
  384.         $replace = [
  385.             '',
  386.             "{% if (f.vars.data is iterable) %}
  387.                 {{ f.vars.data|join(', ') }}
  388.                 {{ form_widget(f, {attr: {readonly: 'readonly', class: 'd-none'}}) }}
  389.             {% else %}
  390.                 {{ f.vars.data }}
  391.                 {{ form_widget(f, { type: 'hidden'}) }}
  392.             {% endif %}"
  393.         ];
  394.         $sourceReplaced str_replace($search$replace$sourceOrigin);
  395.         $templateEvent->setSource($sourceReplaced);
  396.     }
  397. }