app/Plugin/ProductOption42/Form/Extension/AddCartExtension.php line 84

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\Form\Extension;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Form\Type\AddCartType;
  15. use Eccube\Service\TaxRuleService;
  16. use Plugin\ProductOption42\Entity\Option;
  17. use Plugin\ProductOption42\Entity\OptionCategory;
  18. use Plugin\ProductOption42\Repository\OptionCategoryRepository;
  19. use Plugin\ProductOption42\Repository\ProductOptionRepository;
  20. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  21. use Symfony\Component\Form\AbstractTypeExtension;
  22. use Symfony\Component\Form\Extension\Core\Type;
  23. use Symfony\Component\Form\FormBuilderInterface;
  24. use Symfony\Component\Form\FormError;
  25. use Symfony\Component\Form\FormEvent;
  26. use Symfony\Component\Form\FormEvents;
  27. use Symfony\Component\HttpFoundation\RequestStack;
  28. use Symfony\Component\Validator\Constraints as Assert;
  29. class AddCartExtension extends AbstractTypeExtension
  30. {
  31.     /**
  32.      * @var \Doctrine\ORM\EntityManagerInterface
  33.      */
  34.     protected $entityManager;
  35.     /**
  36.      * @var array
  37.      */
  38.     protected $eccubeConfig;
  39.     /**
  40.      * @var ProductOptionRepository
  41.      */
  42.     protected $productOptionRepository;
  43.     /**
  44.      * @var OptionCategoryRepository
  45.      */
  46.     protected $OptionCategoryRepository;
  47.     /**
  48.      * @var TaxRuleService
  49.      */
  50.     protected $taxRuleService;
  51.     private $requestStack;
  52.     public function __construct(
  53.         EntityManagerInterface $entityManager,
  54.         EccubeConfig $eccubeConfig,
  55.         OptionCategoryRepository $optionCategoryRepository,
  56.         ProductOptionRepository $productOptionRepository,
  57.         TaxRuleService $taxRuleService,
  58.         RequestStack $requestStack
  59.     ) {
  60.         $this->eccubeConfig $eccubeConfig;
  61.         $this->entityManager $entityManager;
  62.         $this->optionCategoryRepository $optionCategoryRepository;
  63.         $this->productOptionRepository $productOptionRepository;
  64.         $this->taxRuleService $taxRuleService;
  65.         $this->requestStack $requestStack;
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function buildForm(FormBuilderInterface $builder, array $build_options)
  71.     {
  72.         $Product $build_options['product'];
  73.         $request $this->requestStack->getMasterRequest();
  74.         $route $request->get('_route');
  75.         $ProductOptions $this->productOptionRepository->getListByProduct($Product);
  76.         if (is_array($ProductOptions)) {
  77.             foreach ($ProductOptions as $ProductOption) {
  78.                 $Product $ProductOption->getProduct();
  79.                 if($Product->getStockFind() || $route === 'admin_order_search_product'){
  80.                     $Option $ProductOption->getOption();
  81.                     $ProductClasses $Product->getProductClasses();
  82.                     $ProductClass $ProductClasses[0];
  83.                     $optionCategories = [];
  84.                     $optionAttrs = [];
  85.                     foreach ($Option->getOptionCategories() as $OptionCategory) {
  86.                         $select $OptionCategory->getName();
  87.                         if($Option->getPricedispFlg()){
  88.                             $description "";
  89.                             $OptionCategoryVal $OptionCategory->getValue();
  90.                             if(strlen($OptionCategory->getValue()) > && !empty($OptionCategoryVal)){
  91.                                 $description .= '(¥ ' number_format($this->taxRuleService->getPriceIncTax($OptionCategoryVal,$Product$ProductClass));
  92.                             }
  93.                             if($OptionCategory->getDeliveryFreeFlg() == OptionCategory::ON){
  94.                                 if(strlen($description) == 0){
  95.                                     $description .= '(';
  96.                                 }else{
  97.                                     $description .= ' , ';
  98.                                 }
  99.                                 $description .= trans('productoption.common.delivery_free');
  100.                             }
  101.                             if(strlen($description) > 0){
  102.                                 $description .= ')';
  103.                                 $select .= $description;
  104.                             }
  105.                         }
  106.                         $OptionCategory->setLabel($select);
  107.                         $optionCategories[$OptionCategory->getId()] = $OptionCategory;
  108.                         if($OptionCategory->getHiddenFlg()){
  109.                             $optionAttrs[$OptionCategory->getId()] = ['disabled' => 'disabled''style' => 'background: #ddd;'];
  110.                         }
  111.                     }
  112.                     $type $Option->getType();
  113.                     $options = ['mapped' => false];
  114.                     $options['label'] = $Option->getName();
  115.                     if ($Option->getIsRequired()) {
  116.                         $options['required'] = true;
  117.                         $options['constraints'] = [
  118.                             new Assert\NotBlank(),
  119.                         ];
  120.                     } else {
  121.                         $options['required'] = false;
  122.                     }
  123.                     if ($type == Option::SELECT_TYPE) {
  124.                         $options['class'] = 'Plugin\ProductOption42\Entity\OptionCategory';
  125.                         $options['choice_label'] = 'label';
  126.                         $options['expanded'] = false;
  127.                         $options['multiple'] = false;
  128.                         $options['placeholder'] = null;
  129.                         $options['choices'] = $optionCategories;
  130.                         $options['choice_attr'] = $optionAttrs;
  131.                         if ($options['required'] === true) {
  132.                             if($Option->getDisableCategory()){
  133.                                 $options['constraints'][] = new Assert\NotEqualTo([
  134.                                     'value' => $Option->getDisableCategory(),
  135.                                     'message' => 'This value should not be blank.',
  136.                                 ]);
  137.                             }
  138.                         }
  139.                         foreach($optionCategories as $optionCategory){
  140.                             if($optionCategory->getHiddenFlg()){
  141.                                 $options['constraints'][] = new Assert\NotEqualTo([
  142.                                     'value' => $optionCategory,
  143.                                     'message' => 'productoption.option.cart.cannot_select',
  144.                                 ]);
  145.                             }
  146.                         }
  147.                         if($Option->getDefaultCategory()){
  148.                             $options['data'] = $Option->getDefaultCategory();
  149.                         }
  150.                         $form_type EntityType::class;
  151.                     } elseif ($type == Option::RADIO_TYPE) {
  152.                         $options['class'] = 'Plugin\ProductOption42\Entity\OptionCategory';
  153.                         $options['choice_label'] = 'label';
  154.                         $options['expanded'] = true;
  155.                         $options['multiple'] = false;
  156.                         $options['placeholder'] = null;
  157.                         $options['choices'] = $optionCategories;
  158.                         $options['choice_attr'] = $optionAttrs;
  159.                         if ($options['required'] === true) {
  160.                             if($Option->getDisableCategory()){
  161.                                 $options['constraints'][] = new Assert\NotEqualTo([
  162.                                     'value' => $Option->getDisableCategory(),
  163.                                     'message' => 'This value should not be blank.',
  164.                                 ]);
  165.                             }
  166.                         }
  167.                         foreach($optionCategories as $optionCategory){
  168.                             if($optionCategory->getHiddenFlg()){
  169.                                 $options['constraints'][] = new Assert\NotEqualTo([
  170.                                     'value' => $optionCategory,
  171.                                     'message' => 'productoption.option.cart.cannot_select',
  172.                                 ]);
  173.                             }
  174.                         }
  175.                         if($Option->getDefaultCategory()){
  176.                             $options['data'] = $Option->getDefaultCategory();
  177.                         }else{
  178.                             $options['data'] = current($options['choices']);
  179.                         }
  180.                         $form_type EntityType::class;
  181.                     } elseif ($type == Option::CHECKBOX_TYPE) {
  182.                         $options['class'] = 'Plugin\ProductOption42\Entity\OptionCategory';
  183.                         $options['choice_label'] = 'label';
  184.                         $options['expanded'] = true;
  185.                         $options['multiple'] = true;
  186.                         $options['placeholder'] = null;
  187.                         $options['choices'] = $optionCategories;
  188.                         $options['choice_attr'] = $optionAttrs;
  189.                         foreach($optionCategories as $optionCategory){
  190.                             if($optionCategory->getHiddenFlg()){
  191.                                 $options['constraints'][] = new Assert\NotEqualTo([
  192.                                     'value' => $optionCategory,
  193.                                     'message' => 'productoption.option.cart.cannot_select',
  194.                                 ]);
  195.                             }
  196.                         }
  197.                         if($Option->getDefaultCategory()){
  198.                             $data = [];
  199.                             foreach($Option->getDefaultCategory() as $defaultCategory){
  200.                                 $data[] = $defaultCategory;
  201.                             }
  202.                             $options['data'] = $data;
  203.                         }
  204.                         $form_type EntityType::class;
  205.                     } elseif ($type == Option::TEXT_TYPE) {
  206.                         $form_type Type\TextType::class;
  207.                         $OptionCategories $Option->getOptionCategories();
  208.                         if(count($OptionCategories) > 0){
  209.                             $options['attr'] = ['placeholder' => $OptionCategories[0]->getName(), 'data' => $OptionCategories[0]->getId()];
  210.                         }
  211.                     } elseif ($type == Option::TEXTAREA_TYPE) {
  212.                         $form_type Type\TextareaType::class;
  213.                         $OptionCategories $Option->getOptionCategories();
  214.                         if(count($OptionCategories) > 0){
  215.                             $options['attr'] = ['placeholder' => $OptionCategories[0]->getName(), 'data' => $OptionCategories[0]->getId()];
  216.                         }
  217.                     } elseif ($type == Option::DATE_TYPE) {
  218.                         $form_type Type\DateType::class;
  219.                         $options['input'] = 'datetime';
  220.                         $options['widget'] = 'single_text';
  221.                         $options['format'] = 'yyyy-MM-dd';
  222.                         $options['placeholder'] = ['year' => '----''month' => '--''day' => '--'];
  223.                         $options['attr'] = [
  224.                             'class' => 'datetimepicker-input',
  225.                             'data-toggle' => 'datetimepicker',
  226.                         ];
  227.                         $OptionCategories $Option->getOptionCategories();
  228.                     } elseif ($type == Option::NUMBER_TYPE) {
  229.                         $form_type Type\IntegerType::class;
  230.                         $options['attr'] = ['class' => 'number','maxlength' => $this->eccubeConfig['eccube_int_len']];
  231.                         $OptionCategories $Option->getOptionCategories();
  232.                         if(count($OptionCategories) > 0){
  233.                             $options['attr']['placeholder'] = $OptionCategories[0]->getName();
  234.                             $options['attr']['data'] = $OptionCategories[0]->getId();
  235.                         }
  236.                         $options['constraints'][] = new Assert\Regex(['pattern' => '/^-{0,1}\d+$/']);
  237.                         $min $Option->getRequireMin();
  238.                         $max $Option->getRequireMax();
  239.                         if(strlen($min) > 0){
  240.                             $options['attr']['min'] = $min;
  241.                             $options['constraints'][] = new Assert\GreaterThanOrEqual(['value' => $min]);
  242.                         }
  243.                         if(strlen($max) > 0){
  244.                             $options['attr']['max'] = $max;
  245.                             $options['constraints'][] = new Assert\LessThanOrEqual(['value' => $max]);
  246.                         }
  247.                     }
  248.                     $builder->add('productoption' $Option->getId(), $form_type$options);
  249.                 }
  250.             }
  251.         }
  252.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use($build_options){
  253.             $form $event->getForm();
  254.             $Product $build_options['product'];
  255.             $ProductOptions $this->productOptionRepository->getListByProduct($Product);
  256.             if (is_array($ProductOptions)) {
  257.                 foreach ($ProductOptions as $ProductOption) {
  258.                     $Option $ProductOption->getOption();
  259.                     if($Option->getType() == Option::CHECKBOX_TYPE){
  260.                         $min $Option->getRequireMin();
  261.                         $max $Option->getRequireMax();
  262.                         $Options $form['productoption'.$Option->getId()]->getData();
  263.                         if($min == && $max == 1){
  264.                             if(count($Options) != 1){
  265.                                 $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.check')));
  266.                             }
  267.                         }elseif($min == $max && strlen($min) != 0){
  268.                             if(count($Options) != $min){
  269.                                 $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.min',['%num%'=> $min])));
  270.                             }
  271.                         }else{
  272.                             if($min 0){
  273.                                 if(count($Options) < $min){
  274.                                     $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.max',['%num%' => $min])));
  275.                                 }
  276.                             }
  277.                             if($max 0){
  278.                                 if(count($Options) > $max){
  279.                                     $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.limit',['%num%' => $max])));
  280.                                 }
  281.                             }
  282.                         }
  283.                     }
  284.                 }
  285.             }
  286.         });
  287.     }
  288.     public static function getExtendedTypes(): iterable
  289.     {
  290.         return [AddCartType::class];
  291.     }
  292. }