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.

198 line
7.4KB

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