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.

270 line
8.8KB

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