app/Plugin/ProductOption42/Event/AdminProductEvent.php line 93

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : ProductOption
  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\ProductOption42\Event;
  12. use Eccube\Event\EccubeEvents;
  13. use Eccube\Event\EventArgs;
  14. use Eccube\Event\TemplateEvent;
  15. use Plugin\ProductOption42\Entity\Option;
  16. use Plugin\ProductOption42\Entity\ProductOption;
  17. use Plugin\ProductOption42\Repository\ProductOptionRepository;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class AdminProductEvent implements EventSubscriberInterface
  20. {
  21.     private $productOptionRepository;
  22.     public function __construct(
  23.             ProductOptionRepository $productOptionRepository
  24.             )
  25.     {
  26.         $this->productOptionRepository $productOptionRepository;
  27.     }
  28.     /**
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             '@admin/Product/index.twig' => 'onTemplateAdminProduct',
  35.             '@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
  36.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
  37.             EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
  38.         ];
  39.     }
  40.     public function onTemplateAdminProduct(TemplateEvent $event)
  41.     {
  42.         $twig '@ProductOption42/admin/Product/product_list.twig';
  43.         $event->addSnippet($twig);
  44.         $js '@ProductOption42/admin/Product/product_list.js';
  45.         $event->addAsset($js);
  46.     }
  47.     public function onTemplateAdminProductEdit(TemplateEvent $event)
  48.     {
  49.         $source $event->getSource();
  50.         if(preg_match("/\<\/div\>[\n|\r\n|\r]\s*\{\%\sif\sid\sis\snot\snull\s\%\}[\n|\r\n|\r]/",$source$result)){
  51.             $search $result[0];
  52.             $replace_parts trim($search'</div>');
  53.             $replace "</div>{{ include('@ProductOption42/admin/Product/product_edit.twig') }}" $replace_parts;
  54.             $source str_replace($search$replace$source);
  55.         }
  56.         $event->setSource($source);
  57.         $parameters $event->getParameters();
  58.         $arrType[Option::SELECT_TYPE] = trans("productoption.option.type.select");
  59.         $arrType[Option::RADIO_TYPE] = trans("productoption.option.type.radio");
  60.         $arrType[Option::CHECKBOX_TYPE] = trans("productoption.option.type.checkbox");
  61.         $arrType[Option::TEXT_TYPE] = trans("productoption.option.type.text");
  62.         $arrType[Option::TEXTAREA_TYPE] = trans("productoption.option.type.textarea");
  63.         $arrType[Option::DATE_TYPE] = trans("productoption.option.type.date");
  64.         $arrType[Option::NUMBER_TYPE] = trans("productoption.option.type.number");
  65.         $parameters['arrType'] = $arrType;
  66.         $event->setParameters($parameters);
  67.     }
  68.     public function hookAdminProductCopyComplete(EventArgs $event)
  69.     {
  70.         $Product $event->getArgument('Product');
  71.         $CopyProduct $event->getArgument('CopyProduct');
  72.         foreach ($Product->getProductOptions() as $oldProductOption) {
  73.             $newProductOption = new ProductOption();
  74.             $newProductOption->setProduct($CopyProduct);
  75.             $newProductOption->setOption($oldProductOption->getOption());
  76.             $newProductOption->setSortNo($oldProductOption->getSortNo());
  77.             $this->productOptionRepository->save($newProductOption);
  78.         }
  79.     }
  80.     public function hookAdminProductCsvExport(EventArgs $event)
  81.     {
  82.         $ExportCsvRow $event->getArgument('ExportCsvRow');
  83.         if ($ExportCsvRow->isDataNull()) {
  84.             $ProductClass $event->getArgument('ProductClass');
  85.             $Product $ProductClass->getProduct();
  86.             $Csv $event->getArgument('Csv');
  87.             $csvEntityName str_replace('\\\\''\\'$Csv->getEntityName());
  88.             if($csvEntityName == 'Plugin\ProductOption42\Entity\ProductOption'){
  89.                 $array = [];
  90.                 foreach($Product->getProductOptions() as $ProductOption){
  91.                     if($Csv->getFieldName() == 'product_option_id'){
  92.                         $array[] = $ProductOption->getOption()->getId();
  93.                     }elseif($Csv->getFieldName() == 'product_option_name'){
  94.                         $array[] = $ProductOption->getOption()->getBackendName();
  95.                     }
  96.                 }
  97.                 $ExportCsvRow->setData(implode(','$array));
  98.             }
  99.         }
  100.     }
  101. }