app/Plugin/ApexOrderItemOption/Form/Extension/AddCartExtension.php line 71

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