Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

187 lines
6.9KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Form\Ticket;
  3. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  4. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  5. use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
  6. use Lc\CaracoleBundle\Solver\Price\PriceSolver;
  7. use Lc\SovBundle\Component\FormComponent;
  8. use Lc\SovBundle\Doctrine\EntityManager;
  9. use Lc\SovBundle\Model\Ticket\TicketInterface;
  10. use Lc\SovBundle\Solver\Ticket\TicketSolver;
  11. use Lc\SovBundle\Translation\TranslatorAdmin;
  12. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  15. use Symfony\Component\Form\Extension\Core\Type\FileType;
  16. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextType;
  18. use Symfony\Component\Form\FormBuilderInterface;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. use Symfony\Component\Security\Core\Security;
  21. use Symfony\Component\Validator\Constraints\File;
  22. use Lc\SovBundle\Form\Ticket\TicketFormType as SovTicketFormType;
  23. class TicketFormType extends SovTicketFormType
  24. {
  25. protected Security $security;
  26. protected FormComponent $formComponent;
  27. protected OrderShopStore $orderShopStore;
  28. protected PriceSolver $priceSolver;
  29. public function __construct(
  30. Security $security,
  31. EntityManager $entityManager,
  32. TranslatorAdmin $translatorAdmin,
  33. TicketSolver $ticketSolver,
  34. FormComponent $formComponent,
  35. OrderShopStore $orderShopStore,
  36. PriceSolver $priceSolver
  37. ) {
  38. parent::__construct($entityManager, $translatorAdmin, $ticketSolver);
  39. $this->security = $security;
  40. $this->formComponent = $formComponent;
  41. $this->orderShopStore = $orderShopStore;
  42. $this->priceSolver = $priceSolver;
  43. }
  44. public function buildForm(FormBuilderInterface $builder, array $options)
  45. {
  46. $isConnected = $this->security->getUser();
  47. $entityName = $this->entityManager->getEntityName(TicketInterface::class);
  48. $builder->add(
  49. 'section',
  50. EntityType::class,
  51. [
  52. 'class' => $this->entityManager->getEntityName(SectionInterface::class),
  53. ]
  54. );
  55. if (!$isConnected) {
  56. $builder->add(
  57. 'visitorFirstname',
  58. TextType::class,
  59. [
  60. 'label' => 'Prénom'
  61. ]
  62. )
  63. ->add(
  64. 'visitorLastname',
  65. TextType::class,
  66. [
  67. 'label' => 'Nom'
  68. ]
  69. )
  70. ->add(
  71. 'visitorEmail',
  72. EmailType::class,
  73. [
  74. 'label' => 'Email'
  75. ]
  76. );
  77. }
  78. $builder->add(
  79. 'type',
  80. ChoiceType::class,
  81. [
  82. 'label' => 'Type',
  83. 'choices' => $this->translatorAdmin->transChoices(
  84. $this->ticketSolver->getTypeChoices(),
  85. 'Ticket',
  86. 'type'
  87. ),
  88. ]
  89. );
  90. if ($isConnected) {
  91. $builder->add(
  92. 'orderShop',
  93. EntityType::class,
  94. [
  95. 'class' => $this->entityManager->getEntityName(OrderShopInterface::class),
  96. 'multiple' => false,
  97. 'expanded' => false,
  98. 'choices' => $this->orderShopStore->getBy(
  99. [
  100. 'user' => $this->security->getUser(),
  101. 'isValid' => true
  102. ]
  103. ),
  104. 'label' => 'entity.default.fields.order',
  105. 'placeholder' => '-- Choisissez une commande --',
  106. 'required' => false,
  107. 'choice_label' => function ($orderShop, $key, $value) {
  108. $dateString = '';
  109. $dateObject = $orderShop->getValidationDate() ? $orderShop->getValidationDate(
  110. ) : $orderShop->getCreatedAt();
  111. if ($orderShop->getValidationDate()) {
  112. $dateString .= 'du ' . $dateObject->format('d/m/Y') . ' ';
  113. }
  114. return 'Commande ' . $dateString .
  115. '(' . number_format($this->priceSolver->getTotalWithTax($orderShop), 2) .
  116. ' €)';
  117. },
  118. 'translation_domain' => 'admin',
  119. ]
  120. );
  121. } else {
  122. $this->formComponent->addCaptchaType($builder);
  123. }
  124. $builder->add(
  125. 'subject',
  126. TextType::class
  127. );
  128. $builder->add(
  129. 'message',
  130. TextareaType::class,
  131. [
  132. 'mapped' => false,
  133. 'label' => 'Message'
  134. ]
  135. );
  136. $builder->add(
  137. 'image',
  138. FileType::class,
  139. [
  140. 'label' => 'Photo',
  141. 'mapped' => false,
  142. 'required' => false,
  143. 'constraints' => [
  144. new File(
  145. [
  146. 'maxSize' => '2048k',
  147. 'mimeTypes' => [
  148. 'image/png',
  149. 'image/jpeg',
  150. 'image/jpg',
  151. 'image/gif',
  152. ],
  153. 'mimeTypesMessage' => "Mauvais format d'image (formats acceptés : jpeg, png, gif)",
  154. ]
  155. )
  156. ],
  157. ]
  158. );
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function configureOptions(OptionsResolver $resolver)
  164. {
  165. $resolver->setDefaults(
  166. [
  167. 'data_class' => $this->entityManager->getEntityName(TicketInterface::class),
  168. ]
  169. );
  170. }
  171. }