app/Plugin/EccubePaymentLite42/Form/Type/Admin/ConfigType.php line 40

Open in your IDE?
  1. <?php
  2. namespace Plugin\EccubePaymentLite42\Form\Type\Admin;
  3. use Doctrine\ORM\PersistentCollection;
  4. use Eccube\Common\EccubeConfig;
  5. use Eccube\Form\Validator\Email;
  6. use Plugin\EccubePaymentLite42\Entity\Config;
  7. use Plugin\EccubePaymentLite42\Entity\ConvenienceStore;
  8. use Plugin\EccubePaymentLite42\Entity\GmoEpsilonPayment;
  9. use Plugin\EccubePaymentLite42\Entity\IpBlackList;
  10. use Plugin\EccubePaymentLite42\Entity\MyPageRegularSetting;
  11. use Plugin\EccubePaymentLite42\Entity\RegularCycle;
  12. use Plugin\EccubePaymentLite42\Entity\RegularCycleType;
  13. use Plugin\EccubePaymentLite42\Entity\RegularOrder;
  14. use Plugin\EccubePaymentLite42\Entity\RegularStatus;
  15. use Plugin\EccubePaymentLite42\Repository\ConfigRepository;
  16. use Plugin\EccubePaymentLite42\Repository\GmoEpsilonPaymentRepository;
  17. use Plugin\EccubePaymentLite42\Repository\RegularOrderRepository;
  18. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  19. use Symfony\Component\Form\AbstractType;
  20. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  21. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  22. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  23. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  24. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  25. use Symfony\Component\Form\Extension\Core\Type\TextType;
  26. use Symfony\Component\Form\Form;
  27. use Symfony\Component\Form\FormBuilderInterface;
  28. use Symfony\Component\Form\FormError;
  29. use Symfony\Component\Form\FormEvent;
  30. use Symfony\Component\Form\FormEvents;
  31. use Symfony\Component\Form\FormInterface;
  32. use Symfony\Component\Form\FormView;
  33. use Symfony\Component\OptionsResolver\OptionsResolver;
  34. use Symfony\Component\Validator\Constraints as Assert;
  35. use Symfony\Component\Validator\Constraints\NotBlank;
  36. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  37. class ConfigType extends AbstractType
  38. {
  39.     /**
  40.      * @var EccubeConfig
  41.      */
  42.     protected $eccubeConfig;
  43.     /**
  44.      * @var GmoEpsilonPaymentRepository
  45.      */
  46.     private $gmoEpsilonPaymentRepository;
  47.     /**
  48.      * @var RegularOrderRepository
  49.      */
  50.     private $regularOrderRepository;
  51.     /**
  52.      * @var ConfigRepository
  53.      */
  54.     private $configRepository;
  55.     public function __construct(
  56.         EccubeConfig $eccubeConfig,
  57.         GmoEpsilonPaymentRepository $gmoEpsilonPaymentRepository,
  58.         RegularOrderRepository $regularOrderRepository,
  59.         ConfigRepository $configRepository
  60.     ) {
  61.         $this->eccubeConfig $eccubeConfig;
  62.         $this->gmoEpsilonPaymentRepository $gmoEpsilonPaymentRepository;
  63.         $this->regularOrderRepository $regularOrderRepository;
  64.         $this->configRepository $configRepository;
  65.     }
  66.     public function buildForm(FormBuilderInterface $builder, array $options)
  67.     {
  68.         $builder
  69.             ->add('contract_code'TextType::class, [
  70.                 'constraints' => [
  71.                     new Assert\NotBlank(['message' => '※ 契約コードが入力されていません。']),
  72.                     new Assert\Length(['max' => $this->eccubeConfig['eccube_smtext_len']]),
  73.                 ],
  74.             ])
  75.             ->add('environmental_setting'ChoiceType::class, [
  76.                 'choices' => [
  77.                     '開発' => Config::ENVIRONMENTAL_SETTING_DEVELOPMENT,
  78.                     '本番' => Config::ENVIRONMENTAL_SETTING_PRODUCTION,
  79.                 ],
  80.                 'expanded' => true,
  81.                 'multiple' => false,
  82.                 'constraints' => [
  83.                     new Assert\NotBlank([
  84.                         'message' => '環境設定は必須項目です。',
  85.                     ]),
  86.                 ],
  87.             ])
  88.             ->add('credit_payment_setting'ChoiceType::class, [
  89.                 'choices' => [
  90.                     'リンク決済' => Config::LINK_PAYMENT,
  91.                     'トークン決済' => Config::TOKEN_PAYMENT,
  92.                 ],
  93.                 'expanded' => true,
  94.                 'multiple' => false,
  95.                 'constraints' => [
  96.                     new Assert\NotBlank([
  97.                         'message' => 'クレジットカード決済設定は必須項目です。',
  98.                     ]),
  99.                 ],
  100.             ])
  101.             ->add('gmo_epsilon_payments'EntityType::class, [
  102.                 'class' => GmoEpsilonPayment::class,
  103.                 'multiple' => true,
  104.                 'expanded' => true,
  105.                 'constraints' => [
  106.                     new Assert\NotBlank(['message' => '※ 利用決済方法が選択されていません。']),
  107.                 ],
  108.             ])
  109.             ->add('convenience_stores'EntityType::class, [
  110.                 'class' => ConvenienceStore::class,
  111.                 'multiple' => true,
  112.                 'expanded' => true,
  113.             ])
  114.             ->add('card_expiration_notification_days'ChoiceType::class, [
  115.                 'choices' => [
  116.                     '-' => null,
  117.                     '1' => 1,
  118.                     '2' => 2,
  119.                     '3' => 3,
  120.                     '4' => 4,
  121.                     '5' => 5,
  122.                     '6' => 6,
  123.                     '7' => 7,
  124.                     '8' => 8,
  125.                     '9' => 9,
  126.                     '10' => 10,
  127.                     '11' => 11,
  128.                     '12' => 12,
  129.                     '13' => 13,
  130.                     '14' => 14,
  131.                     '15' => 15,
  132.                     '16' => 16,
  133.                     '17' => 17,
  134.                     '18' => 18,
  135.                     '19' => 19,
  136.                     '20' => 20,
  137.                     '21' => 21,
  138.                     '22' => 22,
  139.                     '23' => 23,
  140.                     '24' => 24,
  141.                     '25' => 25,
  142.                 ],
  143.                 'multiple' => false,
  144.                 'expanded' => false,
  145.                 'constraints' => [
  146.                     new Assert\NotBlank([
  147.                         'message' => '必須項目です。',
  148.                     ]),
  149.                 ],
  150.             ])
  151.             ->add('regular'ChoiceType::class, [
  152.                 'choices' => [
  153.                     '利用しない' => 0,
  154.                     '利用する' => 1,
  155.                 ],
  156.                 'multiple' => false,
  157.                 'expanded' => true,
  158.             ])
  159.             ->add('regular_order_notification_email'EmailType::class, [
  160.                 'required' => false,
  161.                 'constraints' => [
  162.                    // new Email(['strict' => $this->eccubeConfig['eccube_rfc_email_check']]),
  163.                     new Email(nullnull$this->eccubeConfig['eccube_rfc_email_check'] ? 'strict' null),
  164.                 ],
  165.             ])
  166.             ->add('my_page_regular_settings'EntityType::class, [
  167.                 'label' => 'gmo_epsilon.admin.config.my_page_regular_settings',
  168.                 'class' => MyPageRegularSetting::class,
  169.                 'multiple' => true,
  170.                 'expanded' => true,
  171.             ])
  172.             ->add('next_delivery_date_changeable_range_days'ChoiceType::class, [
  173.                 'choices' => array_combine(range(125), range(125)),
  174.                 'empty_data' => 5,
  175.                 'multiple' => false,
  176.                 'expanded' => false,
  177.                 'constraints' => [
  178.                     new NotBlank(),
  179.                 ],
  180.             ])
  181.             ->add('first_delivery_days'ChoiceType::class, [
  182.                 'choices' => array_combine(range(125), range(125)),
  183.                 'empty_data' => 5,
  184.                 'multiple' => false,
  185.                 'expanded' => false,
  186.                 'constraints' => [
  187.                     new NotBlank(),
  188.                 ],
  189.             ])
  190.             ->add('next_delivery_days_at_regular_resumption'ChoiceType::class, [
  191.                 'choices' => array_combine(range(125), range(125)),
  192.                 'empty_data' => 5,
  193.                 'multiple' => false,
  194.                 'expanded' => false,
  195.                 'constraints' => [
  196.                     new NotBlank(),
  197.                     new Assert\Callback(function (
  198.                         $object,
  199.                         ExecutionContextInterface $context
  200.                     ) {
  201.                         /** @var Form $form */
  202.                         $form $context->getRoot();
  203.                         /** @var Config $Config */
  204.                         $Config $this->configRepository->find(1);
  205.                         $deadLine $Config->getRegularOrderDeadline();
  206.                         if ($object <= $deadLine && $form['regular']->getData()) {
  207.                             $form['next_delivery_days_at_regular_resumption']->addError(new FormError('「定期受注注文締切日」よりも大きい日数を設定する必要があります。'));
  208.                         }
  209.                     }),
  210.                 ],
  211.             ])
  212.             ->add('next_delivery_days_after_re_payment'ChoiceType::class, [
  213.                 'label' => 'gmo_epsilon.admin.config.next_delivery_days_after_re_payment',
  214.                 'choices' => array_combine(range(125), range(125)),
  215.                 'empty_data' => 5,
  216.                 'multiple' => false,
  217.                 'expanded' => false,
  218.                 'constraints' => [
  219.                     new NotBlank(),
  220.                 ],
  221.             ])
  222.             ->add('regular_order_deadline'ChoiceType::class, [
  223.                 'label' => 'gmo_epsilon.admin.config.regular_order_deadline',
  224.                 'choices' => array_combine(range(125), range(125)),
  225.                 'empty_data' => 5,
  226.                 'multiple' => false,
  227.                 'expanded' => false,
  228.                 'constraints' => [
  229.                     new NotBlank(),
  230.                 ],
  231.             ])
  232.             ->add('regular_delivery_notification_email_days'ChoiceType::class, [
  233.                 'label' => 'gmo_epsilon.admin.config.regular_delivery_notification_email_days',
  234.                 'choices' => array_combine(range(125), range(125)),
  235.                 'empty_data' => 5,
  236.                 'multiple' => false,
  237.                 'expanded' => false,
  238.             ])
  239.             ->add('regular_stoppable_count'ChoiceType::class, [
  240.                 'label' => 'gmo_epsilon.admin.config.regular_stoppable_count',
  241.                 'choices' => array_combine(range(110), range(110)),
  242.                 'multiple' => false,
  243.                 'expanded' => false,
  244.                 'constraints' => [
  245.                     new NotBlank(),
  246.                 ],
  247.             ])
  248.             ->add('regular_cancelable_count'ChoiceType::class, [
  249.                 'label' => 'gmo_epsilon.admin.config.regular_cancelable_count',
  250.                 'choices' => array_combine(range(110), range(110)),
  251.                 'multiple' => false,
  252.                 'expanded' => false,
  253.                 'constraints' => [
  254.                     new NotBlank(),
  255.                 ],
  256.             ])
  257.             ->add('regular_resumable_period'NumberType::class, [
  258.                 'required' => false,
  259.                 'constraints' => [
  260.                     new Assert\GreaterThanOrEqual([
  261.                         'value' => 1,
  262.                     ]),
  263.                     new Assert\Regex([
  264.                         'pattern' => "/^\d+$/u",
  265.                         'message' => 'form_error.numeric_only',
  266.                     ]),
  267.                 ],
  268.             ])
  269.             ->add('regular_specified_count_notification_mail'NumberType::class, [
  270.                 'required' => false,
  271.                 'constraints' => [
  272.                     new Assert\GreaterThanOrEqual([
  273.                         'value' => 2,
  274.                         'message' => '※ 指定定期回数の通知は、2回目以降を設定してください。',
  275.                     ]),
  276.                     new Assert\Regex([
  277.                         'pattern' => "/^\d+$/u",
  278.                         'message' => 'form_error.numeric_only',
  279.                     ]),
  280.                 ],
  281.             ])
  282.             ->add('regular_point_magnification'ChoiceType::class, [
  283.                 'label' => 'gmo_epsilon.admin.config.regular_point_magnification',
  284.                 'choices' => array_combine(range(010), range(010)),
  285.                 'multiple' => false,
  286.                 'expanded' => false,
  287.                 'constraints' => [
  288.                     new NotBlank(),
  289.                 ],
  290.             ])
  291.             ->add('block_mode'ChoiceType::class, [
  292.                 'choices' => ['ON' => 1,
  293.                     'OFF' => 0,
  294.                 ],
  295.                 'constraints' => [
  296.                     new Assert\NotBlank(['message' => '※ ブロックモードが選択されていません。']),
  297.                 ],
  298.                 'multiple' => false,
  299.                 'expanded' => true,
  300.             ])
  301.             ->add('access_frequency_time'TextType::class, [
  302.                 'required' => false,
  303.                 'constraints' => [
  304.                     new Assert\Callback(function (
  305.                         $objcet,
  306.                         ExecutionContextInterface $context
  307.                     ) {
  308.                         $block_mode $context->getRoot()->get('block_mode')->getData();
  309.                         if ($block_mode && !$objcet) {
  310.                             $context->buildViolation('※ アクセス頻度が入力されていません。')
  311.                                 ->atPath('access_frequency_time')
  312.                                 ->addViolation();
  313.                         }
  314.                     }),
  315.                     new Assert\Length(['max' => $this->eccubeConfig['eccube_smtext_len']]),
  316.                 ],
  317.             ])
  318.             ->add('access_frequency'TextType::class, [
  319.                 'required' => false,
  320.                 'constraints' => [
  321.                     new Assert\Callback(function (
  322.                         $objcet,
  323.                         ExecutionContextInterface $context
  324.                     ) {
  325.                         $block_mode $context->getRoot()->get('block_mode')->getData();
  326.                         if ($block_mode && !$objcet) {
  327.                             $context->buildViolation('※ アクセス頻度が入力されていません。')
  328.                                 ->atPath('access_frequency')
  329.                                 ->addViolation();
  330.                         }
  331.                     }),
  332.                     new Assert\Length(['max' => $this->eccubeConfig['eccube_smtext_len']]),
  333.                 ],
  334.             ])
  335.             ->add('block_time'TextType::class, [
  336.                 'required' => false,
  337.                 'constraints' => [
  338.                     new Assert\Callback(function (
  339.                         $objcet,
  340.                         ExecutionContextInterface $context
  341.                     ) {
  342.                         $block_mode $context->getRoot()->get('block_mode')->getData();
  343.                         if ($block_mode && !$objcet) {
  344.                             $context->buildViolation('※ ブロック時間が入力されていません。')
  345.                                 ->atPath('block_time')
  346.                                 ->addViolation();
  347.                         }
  348.                     }),
  349.                     new Assert\Length(['max' => $this->eccubeConfig['eccube_smtext_len']]),
  350.                 ],
  351.             ])
  352.             ->add('white_list'TextareaType::class, [
  353.                 'required' => false,
  354.                 'constraints' => [
  355.                     new Assert\Callback(function (
  356.                         $objcet,
  357.                         ExecutionContextInterface $context
  358.                     ) {
  359.                         if ($objcet) {
  360.                             $ipAddresses explode(','$objcet);
  361.                             foreach ($ipAddresses as $ip_address) {
  362.                                 if (!preg_match('/(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/'$ip_address)) {
  363.                                     $context->buildViolation('※ IPアドレスの形式が不正です。')
  364.                                         ->atPath('white_list')
  365.                                         ->addViolation();
  366.                                 }
  367.                             }
  368.                         }
  369.                     }),
  370.                     new Assert\Length(['max' => $this->eccubeConfig['eccube_smtext_len']]),
  371.                 ],
  372.             ])
  373.             ->add('ip_black_list'CollectionType::class, [
  374.                 'required' => false,
  375.                 'entry_type' => IpBlackListType::class,
  376.                 'allow_add' => true,
  377.                 'allow_delete' => true,
  378.                 'prototype' => true,
  379.             ])
  380.         ;
  381.         $builder
  382.             ->addEventListener(
  383.                 FormEvents::POST_SUBMIT, [
  384.                     $this,
  385.                     'validateNextDeliveryDaysAfterRePayment',
  386.                 ]
  387.             )
  388.             ->addEventListener(
  389.                 FormEvents::POST_SUBMIT, [
  390.                     $this,
  391.                     'validateRegularDeliveryNotificationEmailDays',
  392.                 ]
  393.             )
  394.             ->addEventListener(
  395.                 FormEvents::POST_SUBMIT, [
  396.                     $this,
  397.                     'validateRegularOrderNotificationEmail',
  398.                 ]
  399.             )
  400.             ->addEventListener(
  401.                 FormEvents::POST_SUBMIT, [
  402.                     $this,
  403.                     'validateRegularOrderDeadline',
  404.                 ]
  405.             )
  406.             ->addEventListener(
  407.                 FormEvents::POST_SUBMIT, [
  408.                     $this,
  409.                     'validateConvenienceStoreForm',
  410.             ])
  411.         ;
  412.     }
  413.     public function validateNextDeliveryDaysAfterRePayment(FormEvent $event)
  414.     {
  415.         /** @var Form $form */
  416.         $form $event->getForm();
  417.         $nextDeliveryDaysAfterRePayment $form['next_delivery_days_after_re_payment']->getData();
  418.         $deadline $form['regular_order_deadline']->getData();
  419.         if ($deadline $nextDeliveryDaysAfterRePayment || !$form['regular']->getData()) {
  420.             return;
  421.         }
  422.         $form['next_delivery_days_after_re_payment']
  423.             ->addError(
  424.                 new FormError(
  425.                     '「再決済後の次回配送日数」は「定期受注注文締切日」よりも大きい値を設定する必要があります。'
  426.                 )
  427.             );
  428.     }
  429.     public function validateRegularDeliveryNotificationEmailDays(FormEvent $event)
  430.     {
  431.         /** @var Form $form */
  432.         $form $event->getForm();
  433.         $regularDeliveryNotificationEmailDays $form['regular_delivery_notification_email_days']->getData();
  434.         $deadline $form['regular_order_deadline']->getData();
  435.         if ($deadline $regularDeliveryNotificationEmailDays || !$form['regular']->getData()) {
  436.             return;
  437.         }
  438.         $form['regular_delivery_notification_email_days']
  439.             ->addError(
  440.                 new FormError(
  441.                     '「定期配送事前お知らせメール配信日数」は「定期受注注文締切日」よりも大きい値を設定する必要があります。'
  442.                 )
  443.             );
  444.     }
  445.     public function validateRegularOrderNotificationEmail(FormEvent $event)
  446.     {
  447.         /** @var Form $form */
  448.         $form $event->getForm();
  449.         if (is_null($form['regular_order_notification_email']->getData()) &&
  450.             $form['regular']->getData()) {
  451.             $form['regular_order_notification_email']
  452.                 ->addError(new FormError('定期受注作成時通知メールアドレスは必須入力です。'));
  453.         }
  454.     }
  455.     public function validateRegularOrderDeadline(FormEvent $event)
  456.     {
  457.         /** @var Form $form */
  458.         $form $event->getForm();
  459.         $deadline $form['regular_order_deadline']->getData();
  460.         /** @var RegularOrder[] $RegularOrders */
  461.         $RegularOrders $this->regularOrderRepository->findBy([
  462.             'RegularStatus' => [
  463.                 RegularStatus::CONTINUE,
  464.                 RegularStatus::SUSPEND,
  465.                 RegularStatus::PAYMENT_ERROR,
  466.                 RegularStatus::SYSTEM_ERROR,
  467.                 RegularStatus::WAITING_RE_PAYMENT,
  468.             ],
  469.         ]);
  470.         // 定期サイクルが日ごと、または週ごとの受注がチェック対象
  471.         foreach ($RegularOrders as $RegularOrder) {
  472.             /** @var RegularCycle $RegularCycle */
  473.             $RegularCycle $RegularOrder->getRegularCycle();
  474.             $regularCycleTypeId $RegularCycle->getRegularCycleType()->getId();
  475.             $cycleDays 30;
  476.             if ($regularCycleTypeId === RegularCycleType::REGULAR_DAILY_CYCLE) {
  477.                 $cycleDays $RegularCycle->getDay();
  478.             }
  479.             if ($regularCycleTypeId === RegularCycleType::REGULAR_WEEKLY_CYCLE) {
  480.                 $cycleDays $RegularCycle->getDay() * 7;
  481.             }
  482.             if ($cycleDays <= $deadline && $form['regular']->getData()) {
  483.                 $form['regular_order_deadline']->addError(new FormError('定期受注注文締切日は、定期ステータスが「解約」以外の定期受注の定期サイクル周期日数よりも少ない日数を入力する必要があります。'));
  484.                 return;
  485.             }
  486.         }
  487.     }
  488.     public function validateConvenienceStoreForm(FormEvent $event)
  489.     {
  490.         /** @var GmoEpsilonPayment $GmoEpsilonPayment */
  491.         $GmoEpsilonPayment $this->gmoEpsilonPaymentRepository->find(GmoEpsilonPayment::CONVENIENCE_STORE);
  492.         /** @var Form $form */
  493.         $form $event->getForm();
  494.         $form['gmo_epsilon_payments']->getData();
  495.         if ($form['gmo_epsilon_payments']->getData()->contains($GmoEpsilonPayment)) {
  496.             /** @var PersistentCollection $convenienceStores */
  497.             $convenienceStores $form['convenience_stores']->getData();
  498.             if ($convenienceStores->count() >= 1) {
  499.                 return;
  500.             }
  501.             $form['convenience_stores']->addError(new FormError('コンビニ種別は必須入力です。'));
  502.         }
  503.     }
  504.     public function configureOptions(OptionsResolver $resolver)
  505.     {
  506.         $resolver->setDefaults([
  507.             'data_class' => Config::class,
  508.         ]);
  509.     }
  510.     public function finishView(FormView $viewFormInterface $form, array $options)
  511.     {
  512.         usort($view['ip_black_list']->children, function (FormView $aFormView $b) {
  513.             /** @var IpBlackList $objectA */
  514.             $objectA $a->vars['data'];
  515.             /** @var IpBlackList $objectB */
  516.             $objectB $b->vars['data'];
  517.             return $objectA->getSortNo() < $objectB->getSortNo() ? -1;
  518.         });
  519.     }
  520. }