app/Plugin/TabaCustomFields/TabaCustomFieldsEvent.php line 225

Open in your IDE?
  1. <?php
  2. namespace Plugin\TabaCustomFields;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Plugin\TabaCustomFields\Event\Admin\CustomerEvent as AdminCustomerEvent;
  5. use Eccube\Event\EventArgs;
  6. use Plugin\TabaCustomFields\Common\Constants;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Plugin\TabaCustomFields\Event\Admin\ProductEvent as AdminProductEvent;
  9. use Plugin\TabaCustomFields\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\TabaCustomFields\Event\CustomerEvent;
  14. use Plugin\TabaCustomFields\Event\OrderEvent;
  15. use Plugin\TabaCustomFields\Repository\CustomFieldsContentsRepository;
  16. use Plugin\TabaCustomFields\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\FilterControllerEvent;
  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.      * TabaCustomFieldsEvent 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.         ];
  133.     }
  134.     /**
  135.      * Initialize fields into form builder
  136.      *
  137.      * @param EventArgs $event
  138.      */
  139.     public function onAdminCustomerEditInit(EventArgs $event)
  140.     {
  141.         $this->adminCustomerEvent->onAdminCustomerEditInit($event);
  142.     }
  143.     /**
  144.      * Handle data
  145.      *
  146.      * @param EventArgs $event
  147.      */
  148.     public function onAdminCustomerEditComplete(EventArgs $event)
  149.     {
  150.         $this->adminCustomerEvent->onAdminCustomerEditComplete($event);
  151.     }
  152.     /**
  153.      * Initialize fields into form builder
  154.      *
  155.      * @param EventArgs $event
  156.      */
  157.     public function onAdminCustomerSearchInit(EventArgs $event)
  158.     {
  159.         $this->adminCustomerEvent->onAdminCustomerSearchInit($event);
  160.     }
  161.     /**
  162.      * Handle data
  163.      *
  164.      * @param EventArgs $event
  165.      */
  166.     public function onAdminCustomerSearch(EventArgs $event)
  167.     {
  168.         $this->adminCustomerEvent->onAdminCustomerSearch($event);
  169.     }
  170.     /**
  171.      * Initialize fields into form builder
  172.      *
  173.      * @param EventArgs $event
  174.      */
  175.     public function onAdminProductEditInit(EventArgs $event)
  176.     {
  177.         $this->adminProductEvent->onAdminProductEditInit($event);
  178.     }
  179.     /**
  180.      * Handle data
  181.      *
  182.      * @param EventArgs $event
  183.      */
  184.     public function onAdminProductEditComplete(EventArgs $event)
  185.     {
  186.         $this->adminProductEvent->onAdminProductEditComplete($event);
  187.     }
  188.     /**
  189.      * Initialize fields into form builder
  190.      *
  191.      * @param EventArgs $event
  192.      */
  193.     public function onAdminProductSearchInit(EventArgs $event)
  194.     {
  195.         $this->adminProductEvent->onAdminProductSearchInit($event);
  196.     }
  197.     /**
  198.      * Handle data
  199.      *
  200.      * @param EventArgs $event
  201.      */
  202.     public function onAdminProductSearch(EventArgs $event)
  203.     {
  204.         $this->adminProductEvent->onAdminProductSearch($event);
  205.     }
  206.     /**
  207.      * Initialize fields into form builder
  208.      *
  209.      * @param EventArgs $event
  210.      */
  211.     public function onAdminOrderEditInit(EventArgs $event)
  212.     {
  213.         $this->adminOrderEvent->onAdminOrderEditInit($event);
  214.     }
  215.     /**
  216.      * Handle data
  217.      *
  218.      * @param EventArgs $event
  219.      */
  220.     public function onAdminOrderEditComplete(EventArgs $event)
  221.     {
  222.         $this->adminOrderEvent->onAdminOrderEditComplete($event);
  223.     }
  224.     /**
  225.      * Initialize fields into form builder
  226.      *
  227.      * @param EventArgs $event
  228.      */
  229.     public function onAdminOrderSearchInit(EventArgs $event)
  230.     {
  231.         $this->adminOrderEvent->onAdminOrderSearchInit($event);
  232.     }
  233.     /**
  234.      * Handle data
  235.      *
  236.      * @param EventArgs $event
  237.      */
  238.     public function onAdminOrderSearch(EventArgs $event)
  239.     {
  240.         $this->adminOrderEvent->onAdminOrderSearch($event);
  241.     }
  242.     /**
  243.      * Add snippet JS into template
  244.      *
  245.      * @param TemplateEvent $templateEvent
  246.      */
  247.     public function onAdminFileUploadJSRender(TemplateEvent $templateEvent)
  248.     {
  249.         // args
  250.         $templateEvent->setParameter('upload_urlpath_route'Constants::ADMIN_BIND_PREFIX.'file_upload');
  251.         $templateEvent->setParameter('formtype_name'Constants::FILE_UPLOAD_FORMTYPE_NAME);
  252.         //csrf
  253.         $templateEvent->setParameter('csrf_token_name'Constant::TOKEN_NAME);
  254.         $templateEvent->setParameter('csrf_token_key'$this->tokenManager->getToken(Constants::FILE_UPLOAD_FORMTYPE_NAME)->getValue());
  255.         // form
  256.         $templateEvent->setParameter('bilde_form_type_name'$templateEvent->getParameter('form')->vars['id']);
  257.         if ($templateEvent->hasParameter('Customer')) {
  258.             $templateEvent->setParameter('entity''customer');
  259.         } elseif ($templateEvent->hasParameter('Product')) {
  260.             $templateEvent->setParameter('entity''product');
  261.         } elseif ($templateEvent->hasParameter('Order')) {
  262.             $templateEvent->setParameter('entity''order');
  263.         }
  264.         $templateEvent->addSnippet('@TabaCustomFields/common/fileUpload/asset.twig');
  265.         $templateEvent->addSnippet('@TabaCustomFields/common/fileUpload/functionJS.twig');
  266.         // Eyemirrorは独自実装のため、CFで対応後はEyemirrorのEventより削除する
  267.         // $templateEvent->addSnippet('@TabaCustomFields/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 FilterControllerEvent $event
  289.      */
  290.     public function onKernelController(FilterControllerEvent $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('@TabaCustomFields/admin/snippet/nav_taba_app.twig');
  302.                         $taba->set(Constants::PLUGIN_CATEGORY_ID ".menu",true);
  303.                     }
  304.                     // メニューを差し込みます。
  305.                     $templateEvent->addSnippet('@TabaCustomFields/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('@TabaCustomFields/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('@TabaCustomFields/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('@TabaCustomFields/snippet/mypage_history.twig');
  346.     }
  347.     /**
  348.      * カスタムフィールド付きの会員情報CSVダウンロード リンク設置
  349.      *
  350.      * @param TemplateEvent $templateEvent
  351.      */
  352.     public function onAdminCfCustomerCsvDownloadLinkRender(TemplateEvent $templateEvent)
  353.     {
  354.         $templateEvent->addSnippet('@TabaCustomFields/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('@TabaCustomFields/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('@TabaCustomFields/admin/snippet/csv_download_product_link.twig');
  373.     }
  374. }