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

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 EccubeConfig
  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->getMainRequest();
  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.                     $form_type '';
  81.                     $Option $ProductOption->getOption();
  82.                     $ProductClasses $Product->getProductClasses();
  83.                     $ProductClass $ProductClasses[0];
  84.                     $optionCategories = [];
  85.                     $optionAttrs = [];
  86.                     foreach ($Option->getOptionCategories() as $OptionCategory) {
  87.                         $select $OptionCategory->getName();
  88.                         if($Option->getPricedispFlg()){
  89.                             $description "";
  90.                             $OptionCategoryVal $OptionCategory->getValue();
  91.                             if(strlen($OptionCategory->getValue()) > && !empty($OptionCategoryVal)){
  92.                                 $description .= '(¥ ' number_format($this->taxRuleService->getPriceIncTax($OptionCategoryVal,$Product$ProductClass));
  93.                             }
  94.                             if($OptionCategory->getDeliveryFreeFlg() == OptionCategory::ON){
  95.                                 if(strlen($description) == 0){
  96.                                     $description .= '(';
  97.                                 }else{
  98.                                     $description .= ' , ';
  99.                                 }
  100.                                 $description .= trans('productoption.common.delivery_free');
  101.                             }
  102.                             if(strlen($description) > 0){
  103.                                 $description .= ')';
  104.                                 $select .= $description;
  105.                             }
  106.                         }
  107.                         $OptionCategory->setLabel($select);
  108.                         $optionCategories[$OptionCategory->getId()] = $OptionCategory;
  109.                         if($OptionCategory->getHiddenFlg()){
  110.                             $optionAttrs[$OptionCategory->getId()] = ['disabled' => 'disabled''style' => 'background: #ddd;'];
  111.                         }
  112.                     }
  113.                     $type $Option->getType();
  114.                     $options = ['mapped' => false];
  115.                     $options['label'] = $Option->getName();
  116.                     if ($Option->getIsRequired()) {
  117.                         $options['required'] = true;
  118.                         $options['constraints'] = [
  119.                             new Assert\NotBlank(),
  120.                         ];
  121.                     } else {
  122.                         $options['required'] = false;
  123.                     }
  124.                     if ($type == Option::SELECT_TYPE) {
  125.                         dump('SELECT_TYPE');
  126.                         $options['class'] = 'Plugin\ProductOption42\Entity\OptionCategory';
  127.                         $options['choice_label'] = 'label';
  128.                         $options['expanded'] = false;
  129.                         $options['multiple'] = false;
  130.                         $options['placeholder'] = null;
  131.                         $options['choices'] = $optionCategories;
  132.                         $options['choice_attr'] = $optionAttrs;
  133.                         if ($options['required'] === true) {
  134.                             //dump($options['constraints'][0]->message);
  135.                             $options['constraints'][0]->message '選択してください。';
  136.                             if($Option->getDisableCategory()){
  137.                                 $options['constraints'][] = new Assert\NotEqualTo([
  138.                                     'value' => $Option->getDisableCategory(),
  139.                                     'message' => 'This value should not be blank.',
  140.                                 ]);
  141.                             }
  142.                         }
  143.                         foreach($optionCategories as $optionCategory){
  144.                             if($optionCategory->getHiddenFlg()){
  145.                                 $options['constraints'][] = new Assert\NotEqualTo([
  146.                                     'value' => $optionCategory,
  147.                                     'message' => 'productoption.option.cart.cannot_select',
  148.                                 ]);
  149.                             }
  150.                         }
  151.                         if($Option->getDefaultCategory()){
  152.                             $options['data'] = $Option->getDefaultCategory();
  153.                         }
  154.                         //dump($options);
  155.                         $form_type EntityType::class;
  156.                     } elseif ($type == Option::RADIO_TYPE) {
  157.                         $options['class'] = 'Plugin\ProductOption42\Entity\OptionCategory';
  158.                         $options['choice_label'] = 'label';
  159.                         $options['expanded'] = true;
  160.                         $options['multiple'] = false;
  161.                         $options['placeholder'] = null;
  162.                         $options['choices'] = $optionCategories;
  163.                         $options['choice_attr'] = $optionAttrs;
  164.                         if ($options['required'] === true) {
  165.                             if($Option->getDisableCategory()){
  166.                                 $options['constraints'][] = new Assert\NotEqualTo([
  167.                                     'value' => $Option->getDisableCategory(),
  168.                                     'message' => 'This value should not be blank.',
  169.                                 ]);
  170.                             }
  171.                         }
  172.                         foreach($optionCategories as $optionCategory){
  173.                             if($optionCategory->getHiddenFlg()){
  174.                                 $options['constraints'][] = new Assert\NotEqualTo([
  175.                                     'value' => $optionCategory,
  176.                                     'message' => 'productoption.option.cart.cannot_select',
  177.                                 ]);
  178.                             }
  179.                         }
  180.                         if($Option->getDefaultCategory()){
  181.                             $options['data'] = $Option->getDefaultCategory();
  182.                         }else{
  183.                             $options['data'] = current($options['choices']);
  184.                         }
  185.                         $form_type EntityType::class;
  186.                     } elseif ($type == Option::CHECKBOX_TYPE) {
  187.                         $options['class'] = 'Plugin\ProductOption42\Entity\OptionCategory';
  188.                         $options['choice_label'] = 'label';
  189.                         $options['expanded'] = true;
  190.                         $options['multiple'] = true;
  191.                         $options['placeholder'] = null;
  192.                         $options['choices'] = $optionCategories;
  193.                         $options['choice_attr'] = $optionAttrs;
  194.                         foreach($optionCategories as $optionCategory){
  195.                             if($optionCategory->getHiddenFlg()){
  196.                                 $options['constraints'][] = new Assert\NotEqualTo([
  197.                                     'value' => $optionCategory,
  198.                                     'message' => 'productoption.option.cart.cannot_select',
  199.                                 ]);
  200.                             }
  201.                         }
  202.                         if($Option->getDefaultCategory()){
  203.                             $data = [];
  204.                             foreach($Option->getDefaultCategory() as $defaultCategory){
  205.                                 $data[] = $defaultCategory;
  206.                             }
  207.                             $options['data'] = $data;
  208.                         }
  209.                         $form_type EntityType::class;
  210.                     } elseif ($type == Option::TEXT_TYPE || $type == Option::TEXTAREA_TYPE) {
  211.                         if($type == Option::TEXT_TYPE){
  212.                             $form_type Type\TextType::class;
  213.                         }else{
  214.                             $form_type Type\TextareaType::class;
  215.                         }
  216.                         $OptionCategories $Option->getOptionCategories();
  217.                         if(count($OptionCategories) > 0){
  218.                             $options['attr'] = ['placeholder' => $OptionCategories[0]->getName(), 'data' => $OptionCategories[0]->getId()];
  219.                         }
  220.                         $min $Option->getRequireMin();
  221.                         $max $Option->getRequireMax();
  222.                         if(strlen($min) > 0){
  223.                             $options['attr']['minlength'] = $min;
  224.                             $options['constraints'][] = new Assert\Length([
  225.                                 'min' => $min,
  226.                             ]);
  227.                         }
  228.                         if(strlen($max) > 0){
  229.                             $options['attr']['maxlength'] = $max;
  230.                             $options['constraints'][] = new Assert\Length([
  231.                                 'max' => $max,
  232.                             ]);
  233.                         }
  234.                     } elseif ($type == Option::DATE_TYPE) {
  235.                         $form_type Type\DateType::class;
  236.                         $options['input'] = 'datetime';
  237.                         $options['widget'] = 'single_text';
  238.                         $options['format'] = 'yyyy-MM-dd';
  239.                         $options['placeholder'] = ['year' => '----''month' => '--''day' => '--'];
  240.                         $options['attr'] = [
  241.                             'class' => 'datetimepicker-input',
  242.                             'data-toggle' => 'datetimepicker',
  243.                         ];
  244.                         $OptionCategories $Option->getOptionCategories();
  245.                     } elseif ($type == Option::NUMBER_TYPE) {
  246.                         $form_type Type\IntegerType::class;
  247.                         $options['attr'] = ['class' => 'number','maxlength' => $this->eccubeConfig['eccube_int_len']];
  248.                         $OptionCategories $Option->getOptionCategories();
  249.                         if(count($OptionCategories) > 0){
  250.                             $options['attr']['placeholder'] = $OptionCategories[0]->getName();
  251.                             $options['attr']['data'] = $OptionCategories[0]->getId();
  252.                         }
  253.                         $options['constraints'][] = new Assert\Regex(['pattern' => '/^-{0,1}\d+$/']);
  254.                         $min $Option->getRequireMin();
  255.                         $max $Option->getRequireMax();
  256.                         if(strlen($min) > 0){
  257.                             $options['attr']['min'] = $min;
  258.                             $options['constraints'][] = new Assert\GreaterThanOrEqual(['value' => $min]);
  259.                         }
  260.                         if(strlen($max) > 0){
  261.                             $options['attr']['max'] = $max;
  262.                             $options['constraints'][] = new Assert\LessThanOrEqual(['value' => $max]);
  263.                         }
  264.                     }
  265.                     $builder->add('productoption' $Option->getId(), $form_type$options);
  266.                 }
  267.             }
  268.         }
  269.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use($build_options){
  270.             $form $event->getForm();
  271.             $Product $build_options['product'];
  272.             $ProductOptions $this->productOptionRepository->getListByProduct($Product);
  273.             if (is_array($ProductOptions)) {
  274.                 foreach ($ProductOptions as $ProductOption) {
  275.                     $Option $ProductOption->getOption();
  276.                     if($Option->getType() == Option::CHECKBOX_TYPE){
  277.                         $min $Option->getRequireMin();
  278.                         $max $Option->getRequireMax();
  279.                         $Options $form['productoption'.$Option->getId()]->getData();
  280.                         if($min == && $max == 1){
  281.                             if(count($Options) != 1){
  282.                                 $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.check')));
  283.                             }
  284.                         }elseif($min == $max && strlen($min) != 0){
  285.                             if(count($Options) != $min){
  286.                                 $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.min',['%num%'=> $min])));
  287.                             }
  288.                         }else{
  289.                             if($min 0){
  290.                                 if(count($Options) < $min){
  291.                                     $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.max',['%num%' => $min])));
  292.                                 }
  293.                             }
  294.                             if($max 0){
  295.                                 if(count($Options) > $max){
  296.                                     $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.limit',['%num%' => $max])));
  297.                                 }
  298.                             }
  299.                         }
  300.                     }
  301.                 }
  302.             }
  303.         });
  304.     }
  305.     public static function getExtendedTypes(): iterable
  306.     {
  307.         return [AddCartType::class];
  308.     }
  309. }