app/Plugin/DeliveryDate4/DeliveryDateEvent.php line 108

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : DeliveryDate4
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Plugin\DeliveryDate4;
  12. use Eccube\Repository\ProductClassRepository;
  13. use Eccube\Event\EccubeEvents;
  14. use Eccube\Event\TemplateEvent;
  15. use Eccube\Event\EventArgs;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. class DeliveryDateEvent implements EventSubscriberInterface
  19. {
  20.     private $entityManager;
  21.     private $productClassRepository;
  22.     public function __construct(
  23.             EntityManagerInterface $entityManager,
  24.             ProductClassRepository $productClassRepository
  25.             )
  26.     {
  27.         $this->entityManager $entityManager;
  28.         $this->productClassRepository $productClassRepository;
  29.     }
  30.     /**
  31.      * @return array
  32.      */
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             '@admin/Setting/Shop/delivery_edit.twig' => 'onTemplateAdminSettingShopDeliveryEdit',
  37.             EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_EDIT_COMPLETE => 'hookAdminSettingShopDeliveryEditComplete',
  38.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
  39.             '@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
  40.             '@admin/Product/product_class.twig' => 'onTemplateAdminProductClassEdit',
  41.             'csvimportproductext.admin.product.csv.import.product.descriptions' => 'hookAdminProductCsvImportProductDescriptions',
  42.             'csvimportproductext.admin.product.csv.import.product.check'=> 'hookAdminProductCsvImportProductCheck',
  43.             'csvimportproductext.admin.product.csv.import.product.process' => 'hookAdminProductCsvImportProductProcess',
  44.         ];
  45.     }
  46.     public function onTemplateAdminSettingShopDeliveryEdit(TemplateEvent $event)
  47.     {
  48.         $twig '@DeliveryDate4/admin/Setting/Shop/delivery_date.twig';
  49.         $event->addSnippet($twig);
  50.         $js '@DeliveryDate4/admin/Setting/Shop/delivery_date.js';
  51.         $event->addAsset($js);
  52.     }
  53.     public function hookAdminSettingShopDeliveryEditComplete(EventArgs $event)
  54.     {
  55.         $form $event->getArgument('form');
  56.         $Delivery $event->getArgument('Delivery');
  57.         $DeliveryDates $form['delivery_dates']->getData();
  58.         foreach($DeliveryDates as $DeliveryDate){
  59.             $DeliveryDate->setDelivery($Delivery);
  60.             $Delivery->addDeliveryDate($DeliveryDate);
  61.             $this->entityManager->persist($DeliveryDate);
  62.         }
  63.         $this->entityManager->flush();
  64.     }
  65.     public function hookAdminProductCopyComplete(EventArgs $event)
  66.     {
  67.         $Product $event->getArgument('Product');
  68.         $CopyProduct $event->getArgument('CopyProduct');
  69.         $orgProductClasses $Product->getProductClasses();
  70.         foreach ($orgProductClasses as $ProductClass) {
  71.             $CopyProductClass $this->productClassRepository->findOneBy(['Product'=> $CopyProduct'ClassCategory1' => $ProductClass->getClassCategory1(), 'ClassCategory2' => $ProductClass->getClassCategory2()]);
  72.             if($CopyProductClass){
  73.                 $CopyProductClass->setDeliveryDateDays($ProductClass->getDeliveryDateDays());
  74.                 $this->entityManager->persist($CopyProductClass);
  75.             }
  76.         }
  77.         $this->entityManager->flush();
  78.     }
  79.     public function onTemplateAdminProductEdit(TemplateEvent $event)
  80.     {
  81.         $twig '@DeliveryDate4/admin/Product/product_days.twig';
  82.         $event->addSnippet($twig);
  83.         $js '@DeliveryDate4/admin/Product/product_days.js';
  84.         $event->addAsset($js);
  85.     }
  86.     public function onTemplateAdminProductClassEdit(TemplateEvent $event)
  87.     {
  88.         $twig '@DeliveryDate4/admin/Product/product_class_days.twig';
  89.         $event->addSnippet($twig);
  90.         $js '@DeliveryDate4/admin/Product/product_class_days.js';
  91.         $event->addAsset($js);
  92.     }
  93.     public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
  94.     {
  95.         $header $event->getArgument('header');
  96.         $key $event->getArgument('key');
  97.         if($key == trans('deliverydate.common.1')){
  98.             $header['description'] = trans('deliverydate.admin.product.product_csv.delivery_date_description');
  99.             $header['required'] = false;
  100.         }
  101.         $event->setArgument('header',$header);
  102.     }
  103.     public function hookAdminProductCsvImportProductCheck(EventArgs $event)
  104.     {
  105.         $row $event->getArgument('row');
  106.         $data $event->getArgument('data');
  107.         $errors $event->getArgument('errors');
  108.         if(isset($row[trans('deliverydate.common.1')])){
  109.             if($row[trans('deliverydate.common.1')] !== '' && !is_numeric($row[trans('deliverydate.common.1')]) || $row[trans('deliverydate.common.1')] < 0){
  110.                 $message trans('admin.common.csv_invalid_greater_than_zero', [
  111.                     '%line%' => $data->key() + 1,
  112.                     '%name%' => trans('deliverydate.common.1'),
  113.                 ]);
  114.                 $errors[] = $message;
  115.             }
  116.         }
  117.         $event->setArgument('errors',$errors);
  118.     }
  119.     public function hookAdminProductCsvImportProductProcess(EventArgs $event)
  120.     {
  121.         $row $event->getArgument('row');
  122.         $data $event->getArgument('data');
  123.         $ProductClass $event->getArgument('ProductClass');
  124.         $Product $ProductClass->getProduct();
  125.         if(isset($row[trans('deliverydate.common.1')])){
  126.             if($row[trans('deliverydate.common.1')] != ''){
  127.                 $ProductClass->setDeliveryDateDays($row[trans('deliverydate.common.1')]);
  128.             }else{
  129.                 $ProductClass->setDeliveryDateDays(NULL);
  130.             }
  131.         }
  132.     }
  133. }