app/Plugin/TeikiOrder42/Form/Extension/AddCartTypeExtension.php line 86

Open in your IDE?
  1. <?php
  2. namespace Plugin\TeikiOrder42\Form\Extension;
  3. use Eccube\Form\Type\AddCartType;
  4. use Eccube\Entity\Product;
  5. use Eccube\Repository\ProductRepository;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. use Symfony\Component\Form\AbstractTypeExtension;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. class AddCartTypeExtension extends AbstractTypeExtension
  14. {
  15.     private $productRepository;
  16.     /**
  17.      * @var \Eccube\Entity\Product
  18.      */
  19.     protected $Product null;
  20.     /**
  21.      * @var SessionInterface
  22.      */
  23.     protected $session;
  24.     public function __construct(
  25.         ProductRepository $productRepository,
  26.         ContainerInterface $container,
  27.         SessionInterface $session
  28.     ){
  29.         $this->productRepository $productRepository;
  30.         $this->container $container;
  31.         $this->session $session;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function buildForm(FormBuilderInterface $builder, array $options)
  37.     {
  38.         /* @var $Product \Eccube\Entity\Product */
  39.         $Product $options['product'];
  40.         $this->Product $Product;
  41.         $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
  42.             $form $event->getForm();
  43.             $TEIKI_PRODUCT_ID $this->container->getParameter('TEIKI_PRODUCT_ID');
  44.             if ($form->get('product_id')->getData() == $TEIKI_PRODUCT_ID) {
  45.                 $timestamp time();
  46.                 //$timestamp = strtotime("2024-3-10");
  47.                 // 前年4月号〜今月号までをプルダウンで表示
  48.                 // 今月号を取得 10日以降なら次月号
  49.                 $day date('j'$timestamp);
  50.                 $start_month '+4 month';
  51.                 if ($day >= 10) {
  52.                     $start_month '+5 month';
  53.                 }
  54.                 //$start_book = date('Y年n月号', strtotime(+ $mon1 .' month', $timestamp));
  55.                 $start_book date('Y年n月号'strtotime($start_month$timestamp));
  56.                 // 前年4月号を取得
  57.                 $month date('n'$timestamp);
  58.                 $end_month '-1 year';
  59.                 if ($month 4) {
  60.                     $end_month '-2 year';
  61.                 }
  62.                 $end_date date("Y-4-01"strtotime($end_month));
  63.                 $end_timestamp strtotime($end_date);
  64.                 $end_book date('Y年n月号'$end_timestamp);
  65.                 $Product $this->productRepository->find($TEIKI_PRODUCT_ID);
  66.                 $Choices $Product->getClassCategories1AsFlip();
  67.                 arsort($Choices);
  68.                 $change_choices array_flip(array_keys($Choices));
  69.                 if (array_key_exists($start_book$change_choices) == false) {
  70.                     $change_choices[$start_book] = 0;
  71.                 }
  72.                 if ($this->session->has('_security_admin')) {
  73.                     $Choices array_slice($Choices$change_choices[$start_book]);
  74.                 } else {
  75.                     $Choices array_slice($Choices$change_choices[$start_book], $change_choices[$end_book] - $change_choices[$start_book] + 1);
  76.                 }
  77.                 $classcategory_id1 $form['classcategory_id1'];
  78.                 $options1 $classcategory_id1->getConfig()->getOptions();
  79.                 $options1['choices'] = ['common.select' => '__unselected'] + $Choices;
  80.                 $form->add('classcategory_id1'ChoiceType::class, $options1);
  81.             }
  82.         });
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function getExtendedType()
  88.     {
  89.         return AddCartType::class;
  90.     }
  91.     /**
  92.      * Return the class of the type being extended.
  93.      */
  94.     public static function getExtendedTypes(): iterable
  95.     {
  96.         return [AddCartType::class];
  97.     }
  98. }