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.

297 line
9.6KB

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