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.

254 lines
8.3KB

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