lib/composer/LisaBundle/src/VisualMedia/LisaBundle/Controller/BaseController.php line 36

Open in your IDE?
  1. <?php
  2. /**
  3. * Base Controller
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\LisaBundle\Controller;
  8. use Exception;
  9. use Psr\Container\ContainerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use VisualMedia\FormBundle\Entity\BaseFormReply;
  14. use VisualMedia\FormBundle\Manager\BaseFormReplyManager;
  15. use VisualMedia\LisaBundle\Helper\Helper;
  16. use VisualMedia\LisaBundle\Service\Mailer;
  17. use VisualMedia\LisaBundle\Service\SettingProvider;
  18. use VisualMedia\PageBundle\Manager\BasePageManager;
  19. /**
  20. * Base Controller
  21. */
  22. class BaseController extends AbstractController
  23. {
  24. public static $controller = false;
  25. private static $staticcontainer = false;
  26. public function __construct()
  27. {
  28. static::$controller = $this;
  29. }
  30. public static function init(ContainerInterface $container = null)
  31. {
  32. if(isset($container) && $container){
  33. static::$staticcontainer = $container;
  34. }
  35. }
  36. public static function getService($id)
  37. {
  38. if(static::$staticcontainer){
  39. $service = static::$staticcontainer->get($id);
  40. return $service;
  41. }
  42. else{
  43. return false;
  44. }
  45. }
  46. public static function getDoctrineObject(){
  47. return static::$controller->getDoctrine();
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function sessionKey(string $parameter): string
  53. {
  54. $requestStack = $this->get('request_stack');
  55. $request = $requestStack->getMasterRequest();
  56. $matches = null;
  57. if (true === (bool)preg_match('/manage_(?<key>.*)__(?<other>.*)/', $request->get('_route'), $matches)) {
  58. return sprintf('%s.%s', $matches['key'], $parameter);
  59. }
  60. return sprintf('%s.%s', $request->get('_controller'), $parameter);
  61. }
  62. /**
  63. * Helper function for the 3 basic functions of handling forms;
  64. * 1. Add a form reply to the database.
  65. * 2. Send emails to client and website.
  66. * 3. Get or create thanks page
  67. * Returns the redirect response to the thanks page.
  68. *
  69. * @param FormInterface $form
  70. * @param string $data
  71. * @return Response
  72. */
  73. public function handleForm(FormInterface $form, array $data = []): Response
  74. {
  75. // Exception on empty variables.
  76. if (empty($key = $data['key'] ?? $form->getName())) {
  77. throw new Exception("Empty 'key' (second) argument when handling form.");
  78. }
  79. if (empty($email = $data['email'] ?? ($form->has('email') ? $form->get('email')->getData() : null))) {
  80. throw new Exception("Empty 'email' (third) argument when handling form.");
  81. }
  82. if (empty($contact = $data['contact'] ?? ($form->has('contact') ? $form->get('contact')->getData() : null))) {
  83. throw new Exception("Empty 'contact' (fourth) argument when handling form.");
  84. }
  85. // 1. Add form reply.
  86. $request = $this->get('request_stack')->getMasterRequest();
  87. $reply = new BaseFormReply();
  88. $reply->setForm($form->getName());
  89. $reply->setFormData(json_encode($form->getData()));
  90. $reply->setBrowser($request->server->get('HTTP_USER_AGENT'));
  91. $reply->setUrl("{$request->getScheme()}://{$request->getHost()}{$request->getRequestUri()}");
  92. $formReplyManager = $this->get(BaseFormReplyManager::class);
  93. $formReplyManager->persist($reply);
  94. $formReplyManager->flush();
  95. // 2. Send mail.
  96. $settingProvider = $this->get(SettingProvider::class);
  97. $data = array_merge($form->getData(), [
  98. 'mailer_sender_name' => $settingProvider->getValue($reference = 'mailer_sender_name', $domain = 'system') ?? Helper::settingPlaceholder($reference, $domain),
  99. ], $data);
  100. $translator = $this->get('translator');
  101. $subjectInternal = $translator->trans("{$key}.email_subject_int", [], 'forms');
  102. $subjectExternal = $translator->trans("{$key}.email_subject_ext", [], 'forms');
  103. $templateInternal = $data['template_int'] ?? "Email/{$key}_int.html.twig";
  104. $templateExternal = $data['template_ext'] ?? "Email/{$key}_ext.html.twig";
  105. $mailer = $this->get(Mailer::class);
  106. $mailer->sendReversed($subjectInternal, $email, $contact, $templateInternal, $data);
  107. $mailer->send($subjectExternal, $email, $contact, $templateExternal, $data);
  108. // 3. Thanks page.
  109. $title = $translator->trans("{$key}_thanks.title", [], 'pages');
  110. $pageManager = $this->get(BasePageManager::class);
  111. $page = $pageManager->getOrCreatePage("{$key}_thanks", [
  112. 'title' => $title,
  113. 'published' => true,
  114. 'indexable' => true,
  115. 'browserTitle' => $title,
  116. 'metaDescription' => "{$title}.",
  117. 'htmlContent' => "{$title}.",
  118. ]);
  119. return $this->redirect($page->getUrl());
  120. }
  121. public function hasBundle(string $bundle): bool
  122. {
  123. if(strpos($bundle,"VisualMedia") === false){
  124. $bundle = "VisualMedia".$bundle;
  125. }
  126. global $kernel;
  127. $bundles = $kernel->getBundles();
  128. if (array_key_exists($bundle, $bundles)) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. public function useComponents(): bool
  134. {
  135. $hasBundles = $this->hasBundle("ComponentBundle") && $this->hasBundle("BartPageBundle");
  136. if($hasBundles){
  137. //check settings
  138. $settingProvider = $this->get(SettingProvider::class);
  139. $useComponents = $settingProvider->getValue('componenten_use_componenten', 'componenten');
  140. return (bool)$useComponents;
  141. }
  142. return false;
  143. }
  144. }