src/Bidcoz/Bundle/FrontendBundle/Form/Type/ItemBuyType.php line 19

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\FrontendBundle\Form\Type;
  3. use Bidcoz\Bundle\CoreBundle\Entity\Proxy\ItemPurchaseProxy;
  4. use RS\DiExtraBundle\Annotation as DI;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\Form\FormView;
  12. use Symfony\Component\OptionsResolver\OptionsResolver;
  13. /**
  14.  * @DI\FormType
  15.  */
  16. class ItemBuyType extends AbstractType
  17. {
  18.     public const MAX_QUANTITY 10;
  19.     public function buildForm(FormBuilderInterface $builder, array $options)
  20.     {
  21.         $item $options['item'];
  22.         if ($item->getCurrentQuantity() > self::MAX_QUANTITY) {
  23.             $formType IntegerType::class;
  24.             $options  = [
  25.                     'attr' => [
  26.                         'min' => 1,
  27.                         'max' => $item->getCurrentQuantity(),
  28.                     ],
  29.                 ];
  30.         } else {
  31.             $formType ChoiceType::class;
  32.             $qty      range(1$item->isFixedPrice() ? $item->getCurrentQuantity() : 1);
  33.             $qtyRange array_combine($qty$qty);
  34.             $options  = [
  35.                     'choices' => $qtyRange,
  36.                 ];
  37.         }
  38.         $builder
  39.                 ->add('quantity'$formTypearray_merge($options, []));
  40.         if ($item->getCollectEmail()) {
  41.             $builder->add('emails'TextareaType::class, [
  42.                 'label'    => 'Enter comma separated list of emails',
  43.                 'attr'     => [
  44.                     'class' => 'no-wysiwyg',
  45.                 ],
  46.             ]);
  47.         }
  48.     }
  49.     public function configureOptions(OptionsResolver $resolver)
  50.     {
  51.         $resolver->setDefaults([
  52.             'data_class' => ItemPurchaseProxy::class,
  53.             'item'       => null,
  54.         ]);
  55.     }
  56.     public function buildView(FormView $viewFormInterface $form, array $options)
  57.     {
  58.         $item                        $options['item'];
  59.         $view->vars['show_quantity'] = $item->isFixedPrice() && $item->getCurrentQuantity() > && !$item->getCollectEmail();
  60.         $view->vars['collect_email'] = $item->getCollectEmail();
  61.     }
  62.     public function getBlockPrefix()
  63.     {
  64.         return 'item_buy';
  65.     }
  66. }