Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

244 lines
8.1KB

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