<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Form\Extension\Front;
use Eccube\Form\Type\AddCartType;
use Customize\Entity\ProductTrait;
use Eccube\Entity\CartItem;
use Eccube\Entity\ProductClass;
use Eccube\Form\DataTransformer\EntityToIdTransformer;
use Eccube\Repository\ProductClassRepository;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContext;
class AddCartTypeExtension extends AbstractTypeExtension
{
/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return AddCartType::class;
}
/**
* {@inheritdoc}
*/
public static function getExtendedTypes(): iterable
{
yield AddCartType::class;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
/* @var $Product \Eccube\Entity\Product */
$Product = $options['product'];
$this->Product = $Product;
$ProductClasses = $Product->getProductClasses();
if ($Product->getStockFind()) {
if ($Product && $Product->getProductClasses()) {
if (!is_null($Product->getClassName1())) {
if (!is_null($Product->getClassName2())) {
$builder->add('classcategory_id1', ChoiceType::class, [
'label' => $Product->getClassName1(),
'choices' => ['common.select' => '__unselected'] + $Product->getClassCategories1AsFlip(),
'mapped' => false,
]);
} else {
$builder->add('classcategory_id1', ChoiceType::class, [
'label' => $Product->getClassName1(),
'choices' => ['common.select' => '__unselected'] + $Product->getCombinedProductClass1(),
'mapped' => false,
]);
}
}
if (!is_null($Product->getClassName2())) {
$builder->add('classcategory_id2', ChoiceType::class, [
'label' => $Product->getClassName2(),
'choices' => ['common.select' => '__unselected'],
'mapped' => false,
]);
}
}
}
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($Product) {
$data = $event->getData();
$form = $event->getForm();
if (isset($data['classcategory_id1']) && !is_null($Product->getClassName2())) {
if ($data['classcategory_id1']) {
$form->add('classcategory_id2', ChoiceType::class, [
'label' => $Product->getClassName2(),
'choices' => ['common.select' => '__unselected'] + $Product->getClassCategories2AsFlip($data['classcategory_id1']),
'mapped' => false,
]);
}
}
});
}
}