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.

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