<?php
namespace Bidcoz\Bundle\FrontendBundle\Form\Type;
use Bidcoz\Bundle\CoreBundle\Entity\Proxy\ItemPurchaseProxy;
use RS\DiExtraBundle\Annotation as DI;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @DI\FormType
*/
class ItemBuyType extends AbstractType
{
public const MAX_QUANTITY = 10;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$item = $options['item'];
if ($item->getCurrentQuantity() > self::MAX_QUANTITY) {
$formType = IntegerType::class;
$options = [
'attr' => [
'min' => 1,
'max' => $item->getCurrentQuantity(),
],
];
} else {
$formType = ChoiceType::class;
$qty = range(1, $item->isFixedPrice() ? $item->getCurrentQuantity() : 1);
$qtyRange = array_combine($qty, $qty);
$options = [
'choices' => $qtyRange,
];
}
$builder
->add('quantity', $formType, array_merge($options, []));
if ($item->getCollectEmail()) {
$builder->add('emails', TextareaType::class, [
'label' => 'Enter comma separated list of emails',
'attr' => [
'class' => 'no-wysiwyg',
],
]);
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ItemPurchaseProxy::class,
'item' => null,
]);
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$item = $options['item'];
$view->vars['show_quantity'] = $item->isFixedPrice() && $item->getCurrentQuantity() > 0 && !$item->getCollectEmail();
$view->vars['collect_email'] = $item->getCollectEmail();
}
public function getBlockPrefix()
{
return 'item_buy';
}
}