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

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