bundle/w3des/AdminBundle/src/NodeModule/ContactModule.php line 50

Open in your IDE?
  1. <?php
  2. namespace w3des\AdminBundle\NodeModule;
  3. use Symfony\Component\Form\FormFactoryInterface;
  4. use Symfony\Component\Form\FormInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Component\Mime\Email;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use w3des\AdminBundle\Form\Type\CKEditorType;
  11. use w3des\AdminBundle\Form\Type\ContactType;
  12. use w3des\AdminBundle\Model\AbstractNodeModule;
  13. use w3des\AdminBundle\Model\NodeView;
  14. use w3des\AdminBundle\Service\Settings;
  15. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  16. use w3des\AdminBundle\Validator\IsTrueValidatorV3;
  17. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  18. use Symfony\Component\Form\Extension\Core\Type\TextType;
  19. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  20. class ContactModule extends AbstractNodeModule
  21. {
  22. private array $forms = [];
  23. private FormFactoryInterface $factory;
  24. private Settings $settings;
  25. private MailerInterface $mailer;
  26. private TranslatorInterface $translator;
  27. public function __construct(FormFactoryInterface $factory, Settings $settings, MailerInterface $mailer, TranslatorInterface $translator)
  28. {
  29. $this->factory = $factory;
  30. $this->settings = $settings;
  31. $this->mailer = $mailer;
  32. $this->translator = $translator;
  33. }
  34. public static function name(): string
  35. {
  36. return 'contact_form';
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function frontend(Request $request, NodeView $module, array $options)
  42. {
  43. $form = $this->getForm($module);
  44. $form->handleRequest($request);
  45. if ($form->isSubmitted() && $form->isValid()) {
  46. $view = $form->createView();
  47. $body = '<p>Dane formularza:</p>';
  48. foreach ($form as $field) {
  49. if ($field->getName() == 'to' || $field->getName() == '_module' || $field->getName() == 'recaptcha') {
  50. continue;
  51. }
  52. $ff = $view->children[$field->getName()];
  53. $label = $this->translator->trans($ff->vars['label'], [], $ff->vars['translation_domain'] ?? 'messages');
  54. $body .= '<p><strong>' . $label . '</strong>: ' . $field->getData() . '</p>';
  55. }
  56. $msg = new Email();
  57. $msg->subject('Formularz kontaktowy');
  58. $msg->html($body);
  59. $msg->from($this->settings->get('mail_from') ?? 'exmaple@example.com');
  60. if ($form->has('to')) {
  61. $to = $this->nodes->fetch($form['to']->getData());
  62. if ($to) {
  63. $msg->addTo($this->nodes->getVariable($to, 'address'));
  64. } else {
  65. $msg->addTo($this->settings->get('mail_to'));
  66. }
  67. } else {
  68. foreach (\explode(',', $this->settings->get('mail_to') ?? 'exmaple@example.com') as $mail) {
  69. $msg->addTo($mail);
  70. }
  71. }
  72. $this->mailer->send($msg);
  73. $request->getSession()->getFlashBag()
  74. ->add('info' . $module->id, $this->translator->trans('form.sent'));
  75. return new RedirectResponse($request->getRequestUri() . '#contact' . $module->id);
  76. }
  77. return [
  78. 'module' => $module,
  79. 'form' => $form->createView()
  80. ];
  81. }
  82. /**
  83. * @param NodeView $module
  84. * @return FormInterface
  85. */
  86. public function getForm(NodeView $module)
  87. {
  88. if (! isset($this->forms[$module->id])) {
  89. $this->forms[$module->id] = $this->factory->create(ContactType::class, [
  90. '_module' => $module->id
  91. ], [
  92. 'module' => 'contact_' . ($module->id > 0 ? 'module_' . $module->id : 'main')
  93. ]);
  94. }
  95. return $this->forms[$module->id];
  96. }
  97. public static function settings(): array
  98. {
  99. return [
  100. 'integrations' => [
  101. 'recaptcha' => [
  102. 'fields' => [
  103. IsTrueValidatorV3::SETTINGS_ENABLED => [
  104. 'type' => CheckboxType::class,
  105. 'default' => false,
  106. 'locale' => false
  107. ],
  108. IsTrueValidatorV3::SETTINGS_PUBLIC => [
  109. 'type' => TextType::class,
  110. 'locale' => false
  111. ],
  112. IsTrueValidatorV3::SETTINGS_SECRET => [
  113. 'type' => TextType::class,
  114. 'locale' => false
  115. ],
  116. IsTrueValidatorV3::SETTINGS_THRESHOLD => [
  117. 'type' => NumberType::class,
  118. 'locale' => false,
  119. 'default' => 0.5,
  120. 'storeType' => 'float'
  121. ]
  122. ]
  123. ]
  124. ]
  125. ];
  126. }
  127. }