Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

325 lines
11KB

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