You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.8KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Form\Reduction;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\Extension\Core\Type\DateType;
  5. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. class EditGiftVoucherType extends AbstractType
  12. {
  13. public function buildForm(FormBuilderInterface $builder, array $options)
  14. {
  15. $builder->add('activationDate', DateType::class, [
  16. 'label' => 'Date d\'activation de votre bon cadeau :',
  17. 'widget'=> 'single_text',
  18. 'help'=> 'Date à partir de laquelle la personne pourra utiliser ce bon cadeau.'
  19. ])
  20. ->add('title', TextType::class, [
  21. 'label' => 'Message personnalisé affiché dans le bon cadeau :',
  22. 'help'=> 'Exemple "Joyeux Noël Tata Suzanne !"'
  23. ])
  24. ->add('email', EmailType::class, [
  25. 'label' => 'Email de la personne à qui vous souhaitez offrir ce bon cadeau :',
  26. 'help'=> 'Le bon ne sera utilisable qu\'avec cet adresse e-mail'
  27. ])
  28. ->add('ownerName', TextType::class,[
  29. 'label' => 'Offert par :',
  30. 'help'=> 'Sera affiché dans le bon cadeau'
  31. ]);
  32. $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($options) {
  33. if (isset($options['reduction_gift']) && $options['reduction_gift']) {
  34. $reductionGift = $options['reduction_gift'];
  35. $form = $event->getForm();
  36. if (!$form->isSubmitted()) {
  37. if ($reductionGift->getActivationDate()) {
  38. $form->get('activationDate')->setData($reductionGift->getActivationDate());
  39. }
  40. if ($reductionGift->getUsers()->count()) {
  41. $form->get('email')->setData($reductionGift->getUsers()[0]->getEmail());
  42. }
  43. if ($reductionGift->getOwnerName()) {
  44. $form->get('ownerName')->setData($reductionGift->getOwnerName());
  45. }
  46. $form->get('title')->setData($reductionGift->getTitle());
  47. }
  48. }
  49. });
  50. }
  51. public function configureOptions(OptionsResolver $resolver)
  52. {
  53. $resolver->setDefaults([
  54. 'reduction_gift' => null
  55. ]);
  56. }
  57. }