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.

ControllerTrait.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace Lc\SovBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  5. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  6. use Knp\Component\Pager\PaginatorInterface;
  7. use Lc\SovBundle\Component\FileComponent;
  8. use Lc\SovBundle\Container\ComponentContainer;
  9. use Lc\SovBundle\Container\File\FileContainer;
  10. use Lc\SovBundle\Container\Newsletter\NewsletterContainer;
  11. use Lc\SovBundle\Container\Reminder\ReminderContainer;
  12. use Lc\SovBundle\Container\Setting\SiteSettingContainer;
  13. use Lc\SovBundle\Container\Site\NewsContainer;
  14. use Lc\SovBundle\Container\Site\PageContainer;
  15. use Lc\SovBundle\Container\Site\SiteContainer;
  16. use Lc\SovBundle\Container\Ticket\TicketContainer;
  17. use Lc\SovBundle\Container\Ticket\TicketMessageContainer;
  18. use Lc\SovBundle\Container\User\GroupUserContainer;
  19. use Lc\SovBundle\Container\User\UserContainer;
  20. use Lc\SovBundle\Definition\ApplicationDefinition;
  21. use Lc\SovBundle\Field\Filter\FilterManager;
  22. use Lc\SovBundle\Generator\PdfGenerator;
  23. use Lc\SovBundle\Repository\EntityRepository;
  24. use Lc\SovBundle\Solver\Setting\SettingSolver;
  25. use Lc\SovBundle\Translation\FlashBagTranslator;
  26. use Lc\SovBundle\Translation\TranslatorAdmin;
  27. //use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  28. use Psr\Log\LoggerInterface;
  29. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  30. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpFoundation\RequestStack;
  33. use Symfony\Component\HttpKernel\KernelInterface;
  34. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  35. use Symfony\Component\Security\Core\Security;
  36. use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
  37. use Symfony\Contracts\Translation\TranslatorInterface;
  38. use Twig\Environment;
  39. trait ControllerTrait
  40. {
  41. protected TranslatorAdmin $translatorAdmin;
  42. protected RequestStack $requestStack;
  43. public function __construct(
  44. TranslatorAdmin $translatorAdmin,
  45. RequestStack $requestStack
  46. ){
  47. $this->translatorAdmin = $translatorAdmin;
  48. $this->requestStack = $requestStack;
  49. }
  50. /*
  51. * Fonctions privées
  52. */
  53. protected function _setNoLimitMemoryAndTime()
  54. {
  55. ini_set('memory_limit', '-1');
  56. set_time_limit(0);
  57. }
  58. //Si vous avez une erreur avec le :array c'est qu'il faut passer à symfony 5.4.
  59. //Suivez la procédure suivante : https://symfony.com/doc/current/setup/unstable_versions.html#upgrading-your-project-to-an-unstable-symfony-version
  60. //En gros il faut changer tout les dépendances symfony/ qui sont en 5.3 par 5.4 dans composer.json
  61. public static function getSubscribedServices(): array
  62. {
  63. return array_merge(
  64. parent::getSubscribedServices(),
  65. ApplicationDefinition::getSubscribedContainerServices(),
  66. [
  67. ApplicationDefinition::class => ApplicationDefinition::class,
  68. Environment::class => Environment::class,
  69. Security::class => Security::class,
  70. EntityManagerInterface::class => EntityManagerInterface::class,
  71. UrlGeneratorInterface::class => UrlGeneratorInterface::class,
  72. // SessionInterface::class => SessionInterface::class,
  73. PaginatorInterface::class => PaginatorInterface::class,
  74. RequestStack::class => RequestStack::class,
  75. EventDispatcherInterface::class => EventDispatcherInterface::class,
  76. LoggerInterface::class => LoggerInterface::class,
  77. ParameterBagInterface::class => ParameterBagInterface::class,
  78. TranslatorInterface::class => TranslatorInterface::class,
  79. PdfGenerator::class => PdfGenerator::class,
  80. TranslatorAdmin::class => TranslatorAdmin::class,
  81. FilterManager::class => FilterManager::class,
  82. FlashBagTranslator::class => FlashBagTranslator::class,
  83. // MailjetTransport::class => MailjetTransport::class,
  84. AdminUrlGenerator::class => AdminUrlGenerator::class,
  85. SettingSolver::class => SettingSolver::class,
  86. ComponentContainer::class => ComponentContainer::class,
  87. EntityRepository::class => EntityRepository::class,
  88. FileComponent::class => FileComponent::class,
  89. KernelInterface::class => KernelInterface::class
  90. ]
  91. );
  92. }
  93. public function addFlashTranslator(
  94. string $type,
  95. $translationKeyName,
  96. $translationEntityName = null,
  97. $translationParam = array()
  98. ): void {
  99. if ($translationEntityName === null && method_exists($this, 'getTranslationEntityName')) {
  100. $translationEntityName = $this->getTranslationEntityName();
  101. }
  102. $this->container->get(FlashBagTranslator::class)->add(
  103. $type,
  104. $translationKeyName,
  105. $translationEntityName,
  106. $translationParam
  107. );
  108. }
  109. public function isInstanceOf(string $interfaceName): bool
  110. {
  111. return in_array($interfaceName, class_implements($this->getEntityFqcn()));
  112. }
  113. public function generateEaUrl(
  114. string $controller = null,
  115. string $action = null,
  116. int $entityId = null,
  117. array $extraParam = array()
  118. ): string {
  119. $adminUrlGenerator = $this->container->get(AdminUrlGenerator::class);
  120. if ($controller) {
  121. $adminUrlGenerator->setController($controller);
  122. }
  123. if ($action) {
  124. $adminUrlGenerator->setAction($action);
  125. }
  126. if ($entityId) {
  127. $adminUrlGenerator->setEntityId($entityId);
  128. }
  129. if ($extraParam) {
  130. foreach ($extraParam as $key => $value) {
  131. $adminUrlGenerator->set($key, $value);
  132. }
  133. }
  134. return $adminUrlGenerator->generateUrl();
  135. }
  136. public function getReferer(Request $request): ?string
  137. {
  138. return $request->headers->get('referer');
  139. }
  140. public function getTemplating(): Environment
  141. {
  142. return $this->container->get(Environment::class);
  143. }
  144. public function getApplicationDefinition(): ApplicationDefinition
  145. {
  146. return $this->container->get(ApplicationDefinition::class);
  147. }
  148. public function getEntityManager(): EntityManagerInterface
  149. {
  150. return $this->container->get(EntityManagerInterface::class);
  151. }
  152. public function getRouter(): UrlGeneratorInterface
  153. {
  154. return $this->container->get(UrlGeneratorInterface::class);
  155. }
  156. public function getSecurity(): Security
  157. {
  158. return $this->container->get(Security::class);
  159. }
  160. public function getPaginator(): PaginatorInterface
  161. {
  162. return $this->container->get(PaginatorInterface::class);
  163. }
  164. public function getRequestStack(): RequestStack
  165. {
  166. return $this->container->get(RequestStack::class);
  167. }
  168. public function getEventDispatcher(): EventDispatcherInterface
  169. {
  170. return $this->container->get(EventDispatcherInterface::class);
  171. }
  172. public function getParameterBag(): ParameterBagInterface
  173. {
  174. return $this->container->get(ParameterBagInterface::class);
  175. }
  176. public function getTranslator(): TranslatorInterface
  177. {
  178. return $this->container->get(TranslatorInterface::class);
  179. }
  180. public function getPdfGenerator(): PdfGenerator
  181. {
  182. return $this->container->get(PdfGenerator::class);
  183. }
  184. public function getTranslatorAdmin(): TranslatorAdmin
  185. {
  186. return $this->container->get(TranslatorAdmin::class);
  187. }
  188. public function getLogger(): LoggerInterface
  189. {
  190. return $this->container->get(LoggerInterface::class);
  191. }
  192. // public function getMailjetTransport(): MailjetTransport
  193. // {
  194. // return $this->container->get(MailjetTransport::class);
  195. // }
  196. public function getAdminUrlGenerator(): AdminUrlGenerator
  197. {
  198. return $this->container->get(AdminUrlGenerator::class);
  199. }
  200. public function getKernel(): KernelInterface
  201. {
  202. return $this->container->get(KernelInterface::class);
  203. }
  204. public function getSettingSolver(): SettingSolver
  205. {
  206. return $this->container->get(SettingSolver::class);
  207. }
  208. public function getSettingValue($entity, $settingName)
  209. {
  210. return $this->container->getSettingSolver()->getSettingValue($entity, $settingName);
  211. }
  212. public function getComponentContainer(): ComponentContainer
  213. {
  214. return $this->container->get(ComponentContainer::class);
  215. }
  216. public function getFileContainer(): FileContainer
  217. {
  218. return $this->container->get(FileContainer::class);
  219. }
  220. public function getNewsletterContainer(): NewsletterContainer
  221. {
  222. return $this->container->get(NewsletterContainer::class);
  223. }
  224. public function getReminderContainer(): ReminderContainer
  225. {
  226. return $this->container->get(ReminderContainer::class);
  227. }
  228. public function getNewsContainer(): NewsContainer
  229. {
  230. return $this->container->get(NewsContainer::class);
  231. }
  232. public function getPageContainer(): PageContainer
  233. {
  234. return $this->container->get(PageContainer::class);
  235. }
  236. public function getSiteContainer(): SiteContainer
  237. {
  238. return $this->container->get(SiteContainer::class);
  239. }
  240. public function getTicketContainer(): TicketContainer
  241. {
  242. return $this->container->get(TicketContainer::class);
  243. }
  244. public function getTicketMessageContainer(): TicketMessageContainer
  245. {
  246. return $this->container->get(TicketMessageContainer::class);
  247. }
  248. public function getGroupUserContainer(): GroupUserContainer
  249. {
  250. return $this->container->get(GroupUserContainer::class);
  251. }
  252. public function getUserContainer(): UserContainer
  253. {
  254. return $this->container->get(UserContainer::class);
  255. }
  256. public function getSiteSettingContainer(): SiteSettingContainer
  257. {
  258. return $this->container->get(SiteSettingContainer::class);
  259. }
  260. public function setNoMemoryAndTimeLimit(): void
  261. {
  262. ini_set('memory_limit', '-1');
  263. set_time_limit(0);
  264. }
  265. public function getFilterManager():FilterManager
  266. {
  267. return $this->container->get(FilterManager::class);
  268. }
  269. public function getAdminContextProvider(): AdminContextProvider
  270. {
  271. return $this->container->get(AdminContextProvider::class);
  272. }
  273. public function getFlashBagTranslator(): FlashBagTranslator
  274. {
  275. return $this->container->get(FlashBagTranslator::class);
  276. }
  277. }