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.

277 lines
9.0KB

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