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