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 || $type == Option::TEXTAREA_TYPE) {
  206.                         if($type == Option::TEXT_TYPE){
  207.                             $form_type Type\TextType::class;
  208.                         }else{
  209.                             $form_type Type\TextareaType::class;
  210.                         }
  211.                         $OptionCategories $Option->getOptionCategories();
  212.                         if(count($OptionCategories) > 0){
  213.                             $options['attr'] = ['placeholder' => $OptionCategories[0]->getName(), 'data' => $OptionCategories[0]->getId()];
  214.                         }
  215.                         $min $Option->getRequireMin();
  216.                         $max $Option->getRequireMax();
  217.                         if(strlen($min) > 0){
  218.                             $options['attr']['minlength'] = $min;
  219.                             $options['constraints'][] = new Assert\Length([
  220.                                 'min' => $min,
  221.                             ]);
  222.                         }
  223.                         if(strlen($max) > 0){
  224.                             $options['attr']['maxlength'] = $max;
  225.                             $options['constraints'][] = new Assert\Length([
  226.                                 'max' => $max,
  227.                             ]);
  228.                         }
  229.                     } elseif ($type == Option::DATE_TYPE) {
  230.                         $form_type Type\DateType::class;
  231.                         $options['input'] = 'datetime';
  232.                         $options['widget'] = 'single_text';
  233.                         $options['format'] = 'yyyy-MM-dd';
  234.                         $options['placeholder'] = ['year' => '----''month' => '--''day' => '--'];
  235.                         $options['attr'] = [
  236.                             'class' => 'datetimepicker-input',
  237.                             'data-toggle' => 'datetimepicker',
  238.                         ];
  239.                         $OptionCategories $Option->getOptionCategories();
  240.                     } elseif ($type == Option::NUMBER_TYPE) {
  241.                         $form_type Type\IntegerType::class;
  242.                         $options['attr'] = ['class' => 'number','maxlength' => $this->eccubeConfig['eccube_int_len']];
  243.                         $OptionCategories $Option->getOptionCategories();
  244.                         if(count($OptionCategories) > 0){
  245.                             $options['attr']['placeholder'] = $OptionCategories[0]->getName();
  246.                             $options['attr']['data'] = $OptionCategories[0]->getId();
  247.                         }
  248.                         $options['constraints'][] = new Assert\Regex(['pattern' => '/^-{0,1}\d+$/']);
  249.                         $min $Option->getRequireMin();
  250.                         $max $Option->getRequireMax();
  251.                         if(strlen($min) > 0){
  252.                             $options['attr']['min'] = $min;
  253.                             $options['constraints'][] = new Assert\GreaterThanOrEqual(['value' => $min]);
  254.                         }
  255.                         if(strlen($max) > 0){
  256.                             $options['attr']['max'] = $max;
  257.                             $options['constraints'][] = new Assert\LessThanOrEqual(['value' => $max]);
  258.                         }
  259.                     }
  260.                     $builder->add('productoption' $Option->getId(), $form_type$options);
  261.                 }
  262.             }
  263.         }
  264.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use($build_options){
  265.             $form $event->getForm();
  266.             $Product $build_options['product'];
  267.             $ProductOptions $this->productOptionRepository->getListByProduct($Product);
  268.             if (is_array($ProductOptions)) {
  269.                 foreach ($ProductOptions as $ProductOption) {
  270.                     $Option $ProductOption->getOption();
  271.                     if($Option->getType() == Option::CHECKBOX_TYPE){
  272.                         $min $Option->getRequireMin();
  273.                         $max $Option->getRequireMax();
  274.                         $Options $form['productoption'.$Option->getId()]->getData();
  275.                         if($min == && $max == 1){
  276.                             if(count($Options) != 1){
  277.                                 $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.check')));
  278.                             }
  279.                         }elseif($min == $max && strlen($min) != 0){
  280.                             if(count($Options) != $min){
  281.                                 $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.min',['%num%'=> $min])));
  282.                             }
  283.                         }else{
  284.                             if($min 0){
  285.                                 if(count($Options) < $min){
  286.                                     $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.max',['%num%' => $min])));
  287.                                 }
  288.                             }
  289.                             if($max 0){
  290.                                 if(count($Options) > $max){
  291.                                     $form['productoption'.$Option->getId()]->addError(new FormError(trans('productoption.option.cart.error.limit',['%num%' => $max])));
  292.                                 }
  293.                             }
  294.                         }
  295.                     }
  296.                 }
  297.             }
  298.         });
  299.     }
  300.     public static function getExtendedTypes(): iterable
  301.     {
  302.         return [AddCartType::class];
  303.     }
  304. }