app/Plugin/ApexOrderItemOption/Event/CartEvent.php line 42

Open in your IDE?
  1. <?php
  2. namespace Plugin\ApexOrderItemOption\Event;
  3. use Eccube\Event\TemplateEvent;
  4. use Eccube\Form\Type\AddCartType;
  5. use Plugin\ApexOrderItemOption\Entity\OptionCategory;
  6. use Plugin\ApexOrderItemOption\Service\CartItemOptionFormService;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Form\FormFactoryInterface;
  9. class CartEvent implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var FormFactoryInterface
  13.      */
  14.     private $formFactory;
  15.     /**
  16.      * @var CartItemOptionFormService
  17.      */
  18.     private $cartItemOptionFormService;
  19.     public function __construct(
  20.         FormFactoryInterface $formFactory,
  21.         CartItemOptionFormService $cartItemOptionFormService
  22.     ) {
  23.         $this->formFactory $formFactory;
  24.         $this->cartItemOptionFormService $cartItemOptionFormService;
  25.     }
  26.     /**
  27.      * @return array
  28.      */
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             'Cart/index.twig' => 'onTemplateCart',
  33.         ];
  34.     }
  35.     public function onTemplateCart(TemplateEvent $event)
  36.     {
  37.         $this->addCartItemOptionForms($event);
  38.         $source $event->getSource();
  39.         if(preg_match("/url\('cart\_handle\_item'\s*,\s*\{'operation'\s*:\s*'down'\s*,\s*'productClassId'\s*:\s*ProductClass\.id/",$source$result)){
  40.             $search $result[0];
  41.             $snipet "url('productoption_cart_handle_item', {'operation': 'down', 'cartItemId': CartItem.id";
  42.             $replace $snipet;
  43.             $source str_replace($search$replace$source);
  44.         }
  45.         if(preg_match("/url\('cart\_handle\_item'\s*,\s*\{'operation'\s*:\s*'up'\s*,\s*'productClassId'\s*:\s*ProductClass\.id/",$source$result)){
  46.             $search $result[0];
  47.             $snipet "url('productoption_cart_handle_item', {'operation': 'up', 'cartItemId': CartItem.id";
  48.             $replace $snipet;
  49.             $source str_replace($search$replace$source);
  50.         }
  51.         if(preg_match("/url\('cart\_handle\_item',\s\{'operation'\:\s'remove',\s'productClassId'\:\sProductClass\.id/",$source$result)){
  52.             $search $result[0];
  53.             $snipet "url('productoption_cart_handle_item', {'operation': 'remove', 'cartItemId': CartItem.id";
  54.             $replace $snipet;
  55.             $source str_replace($search$replace$source);
  56.         }
  57.         if(preg_match("/\<\/div\>[\n|\r\n\\r]\s*<div\sclass\=\"ec\-cartRow\_\_unitPrice\"\>/",$source$result)){
  58.             $search $result[0];
  59.             $replace "{{ include('@ApexOrderItemOption/default/Cart/cart_option.twig') }}" $search;
  60.             $source str_replace($search$replace$source);
  61.         }
  62.         $event->setSource($source);
  63.     }
  64.     /**
  65.      * カート明細ごとに、オプション編集用の AddCartType を組み立ててテンプレートへ渡す.
  66.      *
  67.      * 現在の option_serial / 数量 / 規格を初期値として流し込み、
  68.      * widget の id は明細IDを付けて一意にする(同じ商品が複数明細に並ぶため).
  69.      *
  70.      * @param TemplateEvent $event
  71.      */
  72.     private function addCartItemOptionForms(TemplateEvent $event)
  73.     {
  74.         $parameters $event->getParameters();
  75.         if (!isset($parameters['Carts'])) {
  76.             return;
  77.         }
  78.         $forms = [];
  79.         foreach ($parameters['Carts'] as $Cart) {
  80.             foreach ($Cart->getCartItems() as $CartItem) {
  81.                 $ProductClass $CartItem->getProductClass();
  82.                 if (is_null($ProductClass)) {
  83.                     continue;
  84.                 }
  85.                 $Product $ProductClass->getProduct();
  86.                 if (is_null($Product) || count($Product->getProductOptions()) === 0) {
  87.                     continue;
  88.                 }
  89.                 // 在庫切れの場合 AddCartExtension がオプション項目を組み立てないため、
  90.                 // フォームを渡さず編集ボタン自体を出さない
  91.                 if (!$Product->getStockFind()) {
  92.                     continue;
  93.                 }
  94.                 $builder $this->formFactory->createNamedBuilder(
  95.                     '',
  96.                     AddCartType::class,
  97.                     null,
  98.                     [
  99.                         'product' => $Product,
  100.                         'id_add_product_id' => false,
  101.                     ]
  102.                 );
  103.                 // 初期値は必ずビルダー段階で入れること.
  104.                 // AddCartType / AddCartExtension が 'data'(既定値) を渡している項目は
  105.                 // data_locked となり、組み立て後の Form::setData() が無視されるため.
  106.                 // 規格は変更させないため、現在の明細の規格で固定する
  107.                 $builder->get('ProductClass')->setData($ProductClass);
  108.                 if ($builder->has('quantity')) {
  109.                     $builder->get('quantity')->setData($CartItem->getQuantity());
  110.                 }
  111.                 $this->cartItemOptionFormService->applyOptionSerial($Product$builder$CartItem->getOptionSerial());
  112.                 $view $builder->getForm()->createView();
  113.                 $this->cartItemOptionFormService->uniquifyWidgetIds($view'ci'.$CartItem->getId());
  114.                 $forms[$CartItem->getId()] = $view;
  115.             }
  116.         }
  117.         $parameters['CartItemOptionForms'] = $forms;
  118.         $event->setParameters($parameters);
  119.         // オプション項目のスタイル(.plg_aoio / .option_Label 等)はテーマのCSSには無く、
  120.         // このテンプレートにしか定義がないため、カート画面でも読み込む
  121.         if (count($forms) > 0) {
  122.             $event->addAsset('Product/option_css.twig');
  123.         }
  124.     }
  125. }