Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

278 lines
9.1KB

  1. <?php
  2. namespace Lc\SovBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  5. use Knp\Component\Pager\PaginatorInterface;
  6. use Lc\SovBundle\Component\FileComponent;
  7. use Lc\SovBundle\Container\ComponentContainer;
  8. use Lc\SovBundle\Container\File\FileContainer;
  9. use Lc\SovBundle\Container\Newsletter\NewsletterContainer;
  10. use Lc\SovBundle\Container\Reminder\ReminderContainer;
  11. use Lc\SovBundle\Container\Setting\SiteSettingContainer;
  12. use Lc\SovBundle\Container\Site\NewsContainer;
  13. use Lc\SovBundle\Container\Site\PageContainer;
  14. use Lc\SovBundle\Container\Site\SiteContainer;
  15. use Lc\SovBundle\Container\Ticket\TicketContainer;
  16. use Lc\SovBundle\Container\Ticket\TicketMessageContainer;
  17. use Lc\SovBundle\Container\User\GroupUserContainer;
  18. use Lc\SovBundle\Container\User\UserContainer;
  19. use Lc\SovBundle\Field\Filter\FilterManager;
  20. use Lc\SovBundle\Generator\PdfGenerator;
  21. use Lc\SovBundle\Repository\EntityRepository;
  22. use Lc\SovBundle\Solver\Setting\SettingSolver;
  23. use Lc\SovBundle\Translation\FlashBagTranslator;
  24. use Lc\SovBundle\Translation\TranslatorAdmin;
  25. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  26. use Psr\Log\LoggerInterface;
  27. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  28. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpFoundation\RequestStack;
  31. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  32. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  33. use Symfony\Component\Security\Core\Security;
  34. use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
  35. use Symfony\Contracts\Translation\TranslatorInterface;
  36. use Twig\Environment;
  37. trait ControllerTrait
  38. {
  39. public static function getSubscribedServices()
  40. {
  41. return array_merge(
  42. parent::getSubscribedServices(),
  43. [
  44. Environment::class => Environment::class,
  45. Security::class => Security::class,
  46. EntityManagerInterface::class => EntityManagerInterface::class,
  47. UrlGeneratorInterface::class => UrlGeneratorInterface::class,
  48. SessionInterface::class => SessionInterface::class,
  49. PaginatorInterface::class => PaginatorInterface::class,
  50. RequestStack::class => RequestStack::class,
  51. EventDispatcherInterface::class => EventDispatcherInterface::class,
  52. LoggerInterface::class => LoggerInterface::class,
  53. ParameterBagInterface::class => ParameterBagInterface::class,
  54. TranslatorInterface::class => TranslatorInterface::class,
  55. PdfGenerator::class => PdfGenerator::class,
  56. TranslatorAdmin::class => TranslatorAdmin::class,
  57. FilterManager::class => FilterManager::class,
  58. FlashBagTranslator::class => FlashBagTranslator::class,
  59. MailjetTransport::class => MailjetTransport::class,
  60. AdminUrlGenerator::class => AdminUrlGenerator::class,
  61. SettingSolver::class => SettingSolver::class,
  62. ComponentContainer::class => ComponentContainer::class,
  63. FileContainer::class => FileContainer::class,
  64. NewsletterContainer::class => NewsletterContainer::class,
  65. ReminderContainer::class => ReminderContainer::class,
  66. NewsContainer::class => NewsContainer::class,
  67. PageContainer::class => PageContainer::class,
  68. SiteContainer::class => SiteContainer::class,
  69. TicketContainer::class => TicketContainer::class,
  70. TicketMessageContainer::class => TicketMessageContainer::class,
  71. GroupUserContainer::class => GroupUserContainer::class,
  72. UserContainer::class => UserContainer::class,
  73. SiteSettingContainer::class => SiteSettingContainer::class,
  74. EntityRepository::class => EntityRepository::class,
  75. FileComponent::class => FileComponent::class,
  76. ]
  77. );
  78. }
  79. public function addFlashTranslator(
  80. string $type,
  81. $translationKeyName,
  82. $translationEntityName = null,
  83. $translationParam = array()
  84. ): void
  85. {
  86. if ($translationEntityName === null && method_exists($this, 'getTranslationEntityName')) {
  87. $translationEntityName = $this->getTranslationEntityName();
  88. }
  89. $this->get(FlashBagTranslator::class)->add(
  90. $type,
  91. $translationKeyName,
  92. $translationEntityName,
  93. $translationParam
  94. );
  95. }
  96. public function isInstanceOf(string $interfaceName): bool
  97. {
  98. return in_array($interfaceName, class_implements($this->getEntityFqcn()));
  99. }
  100. public function generateEaUrl(string $controller = null, string $action = null, int $entityId = null, array $extraParam = array()): string
  101. {
  102. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  103. if ($controller) {
  104. $adminUrlGenerator->setController($controller);
  105. }
  106. if ($action) {
  107. $adminUrlGenerator->setAction($action);
  108. }
  109. if ($entityId) {
  110. $adminUrlGenerator->setEntityId($entityId);
  111. }
  112. if ($extraParam) {
  113. foreach ($extraParam as $key => $value) {
  114. $adminUrlGenerator->set($key, $value);
  115. }
  116. }
  117. return $adminUrlGenerator->generateUrl();
  118. }
  119. public function getReferer(Request $request): ?string
  120. {
  121. return $request->headers->get('referer');
  122. }
  123. public function getTemplating(): Environment
  124. {
  125. return $this->get(Environment::class);
  126. }
  127. public function getEntityManager(): EntityManagerInterface
  128. {
  129. return $this->get(EntityManagerInterface::class);
  130. }
  131. public function getRouter(): UrlGeneratorInterface
  132. {
  133. return $this->get(UrlGeneratorInterface::class);
  134. }
  135. public function getSession(): SessionInterface
  136. {
  137. return $this->get(SessionInterface::class);
  138. }
  139. public function getSecurity(): Security
  140. {
  141. return $this->get(Security::class);
  142. }
  143. public function getPaginator(): PaginatorInterface
  144. {
  145. return $this->get(PaginatorInterface::class);
  146. }
  147. public function getRequestStack(): RequestStack
  148. {
  149. return $this->get(RequestStack::class);
  150. }
  151. public function getEventDispatcher(): EventDispatcherInterface
  152. {
  153. return $this->get(EventDispatcherInterface::class);
  154. }
  155. public function getParameterBag(): ParameterBagInterface
  156. {
  157. return $this->get(ParameterBagInterface::class);
  158. }
  159. public function getTranslator(): TranslatorInterface
  160. {
  161. return $this->get(TranslatorInterface::class);
  162. }
  163. public function getPdfGenerator(): PdfGenerator
  164. {
  165. return $this->get(PdfGenerator::class);
  166. }
  167. public function getTranslatorAdmin(): TranslatorAdmin
  168. {
  169. return $this->get(TranslatorAdmin::class);
  170. }
  171. public function getLogger(): LoggerInterface
  172. {
  173. return $this->get(LoggerInterface::class);
  174. }
  175. public function getMailjetTransport(): MailjetTransport
  176. {
  177. return $this->get(MailjetTransport::class);
  178. }
  179. public function getAdminUrlGenerator(): AdminUrlGenerator
  180. {
  181. return $this->get(AdminUrlGenerator::class);
  182. }
  183. public function getSettingSolver(): SettingSolver
  184. {
  185. return $this->get(SettingSolver::class);
  186. }
  187. public function getSettingValue($entity, $settingName)
  188. {
  189. return $this->getSettingSolver()->getSettingValue($entity, $settingName);
  190. }
  191. public function getComponentContainer(): ComponentContainer
  192. {
  193. return $this->get(ComponentContainer::class);
  194. }
  195. public function getFileContainer(): FileContainer
  196. {
  197. return $this->get(FileContainer::class);
  198. }
  199. public function getNewsletterContainer(): NewsletterContainer
  200. {
  201. return $this->get(NewsletterContainer::class);
  202. }
  203. public function getReminderContainer(): ReminderContainer
  204. {
  205. return $this->get(ReminderContainer::class);
  206. }
  207. public function getNewsContainer(): NewsContainer
  208. {
  209. return $this->get(NewsContainer::class);
  210. }
  211. public function getPageContainer(): PageContainer
  212. {
  213. return $this->get(PageContainer::class);
  214. }
  215. public function getSiteContainer(): SiteContainer
  216. {
  217. return $this->get(SiteContainer::class);
  218. }
  219. public function getTicketContainer(): TicketContainer
  220. {
  221. return $this->get(TicketContainer::class);
  222. }
  223. public function getTicketMessageContainer(): TicketMessageContainer
  224. {
  225. return $this->get(TicketMessageContainer::class);
  226. }
  227. public function getGroupUserContainer(): GroupUserContainer
  228. {
  229. return $this->get(GroupUserContainer::class);
  230. }
  231. public function getUserContainer(): UserContainer
  232. {
  233. return $this->get(UserContainer::class);
  234. }
  235. public function getSiteSettingContainer(): SiteSettingContainer
  236. {
  237. return $this->get(SiteSettingContainer::class);
  238. }
  239. }