app/Plugin/DeliveryDate4/DeliveryDateEvent.php line 53

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.     }
  51.     public function hookAdminSettingShopDeliveryEditComplete(EventArgs $event)
  52.     {
  53.         $form $event->getArgument('form');
  54.         $Delivery $event->getArgument('Delivery');
  55.         $DeliveryDates $form['delivery_dates']->getData();
  56.         foreach($DeliveryDates as $DeliveryDate){
  57.             $DeliveryDate->setDelivery($Delivery);
  58.             $Delivery->addDeliveryDate($DeliveryDate);
  59.             $this->entityManager->persist($DeliveryDate);
  60.         }
  61.         $this->entityManager->flush();
  62.     }
  63.     public function hookAdminProductCopyComplete(EventArgs $event)
  64.     {
  65.         $Product $event->getArgument('Product');
  66.         $CopyProduct $event->getArgument('CopyProduct');
  67.         $orgProductClasses $Product->getProductClasses();
  68.         foreach ($orgProductClasses as $ProductClass) {
  69.             $CopyProductClass $this->productClassRepository->findOneBy(['Product'=> $CopyProduct'ClassCategory1' => $ProductClass->getClassCategory1(), 'ClassCategory2' => $ProductClass->getClassCategory2()]);
  70.             if($CopyProductClass){
  71.                 $CopyProductClass->setDeliveryDateDays($ProductClass->getDeliveryDateDays());
  72.                 $this->entityManager->persist($CopyProductClass);
  73.             }
  74.         }
  75.         $this->entityManager->flush();
  76.     }
  77.     public function onTemplateAdminProductEdit(TemplateEvent $event)
  78.     {
  79.         $twig '@DeliveryDate4/admin/Product/product_days.twig';
  80.         $event->addSnippet($twig);
  81.     }
  82.     public function onTemplateAdminProductClassEdit(TemplateEvent $event)
  83.     {
  84.         $twig '@DeliveryDate4/admin/Product/product_class_days.twig';
  85.         $event->addSnippet($twig);
  86.     }
  87.     public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
  88.     {
  89.         $header $event->getArgument('header');
  90.         $key $event->getArgument('key');
  91.         if($key == trans('deliverydate.common.1')){
  92.             $header['description'] = trans('deliverydate.admin.product.product_csv.delivery_date_description');
  93.             $header['required'] = false;
  94.         }
  95.         $event->setArgument('header',$header);
  96.     }
  97.     public function hookAdminProductCsvImportProductCheck(EventArgs $event)
  98.     {
  99.         $row $event->getArgument('row');
  100.         $data $event->getArgument('data');
  101.         $errors $event->getArgument('errors');
  102.         if(isset($row[trans('deliverydate.common.1')])){
  103.             if($row[trans('deliverydate.common.1')] !== '' && !is_numeric($row[trans('deliverydate.common.1')]) || $row[trans('deliverydate.common.1')] < 0){
  104.                 $message trans('admin.common.csv_invalid_greater_than_zero', [
  105.                     '%line%' => $data->key() + 1,
  106.                     '%name%' => trans('deliverydate.common.1'),
  107.                 ]);
  108.                 $errors[] = $message;
  109.             }
  110.         }
  111.         $event->setArgument('errors',$errors);
  112.     }
  113.     public function hookAdminProductCsvImportProductProcess(EventArgs $event)
  114.     {
  115.         $row $event->getArgument('row');
  116.         $data $event->getArgument('data');
  117.         $ProductClass $event->getArgument('ProductClass');
  118.         $Product $ProductClass->getProduct();
  119.         if(isset($row[trans('deliverydate.common.1')])){
  120.             if($row[trans('deliverydate.common.1')] != ''){
  121.                 $ProductClass->setDeliveryDateDays($row[trans('deliverydate.common.1')]);
  122.             }else{
  123.                 $ProductClass->setDeliveryDateDays(NULL);
  124.             }
  125.         }
  126.     }
  127. }