|
- <?php
-
- namespace Lc\SovBundle\Twig;
-
- use Doctrine\ORM\EntityManagerInterface;
- use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
- use Liip\ImagineBundle\Imagine\Cache\CacheManager;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\HttpKernel\KernelInterface;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
- use Symfony\Contracts\Translation\TranslatorInterface;
- use Twig\Extension\AbstractExtension;
- use Twig\TwigFilter;
- use Twig\TwigFunction;
-
- class TwigExtension extends AbstractExtension
- {
- protected $em;
- protected $kernel;
- protected $parameterBag;
- protected $cacheManager;
- protected $requestStack;
- protected $router;
- protected $translator;
-
- public function __construct(
- KernelInterface $kernel,
- ParameterBagInterface $parameterBag,
- CacheManager $cacheManager,
- EntityManagerInterface $entityManager,
- RequestStack $requestStack,
- UrlGeneratorInterface $router,
- TranslatorInterface $translator
- ) {
- $this->kernel = $kernel;
- $this->parameterBag = $parameterBag;
- $this->cacheManager = $cacheManager;
- $this->em = $entityManager;
- $this->requestStack = $requestStack;
- $this->router = $router;
- $this->translator = $translator;
- }
-
- public function getFunctions()
- {
- return array(
- new TwigFunction('lc_liip', [$this, 'lcLiip']),
- new TwigFunction('homepage_route', [$this, 'homepageRoute']),
- new TwigFunction('page_by_dev_alias', [$this, 'getPageByDevAlias']),
- new TwigFunction('translated_urls', [$this, 'getTranslatedUrls'])
- );
- }
-
-
- public function getFilters()
- {
- return [
- new TwigFilter('lc_cache', [$this, 'lcCache']),
- new TwigFilter('lc_trans_admin_field', [$this, 'lcTransAdminField']),
- new TwigFilter('lc_trans_admin_panel', [$this, 'lcTransAdminPanel']),
- new TwigFilter('lc_trans_admin_menu', [$this, 'lcTransAdminMenu']),
- new TwigFilter('lc_trans_admin_title', [$this, 'lcTransAdminTitle']),
- ];
- }
-
- public function lcCache($file)
- {
- $cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file);
- if ($cacheTime) {
- return $file . '?c=' . $cacheTime;
- } else {
- return $file . "?c=0";
- }
- }
-
- public function lcTransAdminField($fieldName, $entityClass, $isHelp = false)
- {
- if ($isHelp) {
- $fieldName = $fieldName . '_help';
- }
-
- $entityName = $this->getEntityName($entityClass) ;
-
- $entityLabel = 'entity.' . $entityName . '.fields.' . $fieldName;
- $translatedLabel = $this->translator->trans($entityLabel, [], 'admin');
-
- if ($translatedLabel == $entityLabel) {
- $defaultLabel = 'entity.default.fields.' . $fieldName;
- $translatedLabel = $this->translator->trans($defaultLabel, [], 'admin');
-
- if ($translatedLabel == $defaultLabel) {
- if ($isHelp) {
- $translatedLabel = '';
- } else {
- $translatedLabel = $entityLabel;
- }
- }
- }
-
- return $translatedLabel;
- }
-
- public function lcTransAdminPanel($panelName, $entityClass)
- {
- $entityName = $this->getEntityName($entityClass) ;
-
- $panelLabel = 'entity.' . $entityName . '.panels.' . $panelName;
- $translatedLabel = $this->translator->trans($panelLabel, [], 'admin');
-
- if ($translatedLabel == $panelLabel) {
- $defaultLabel = 'entity.default.panels.' . $panelName;
- $translatedLabel = $this->translator->trans($defaultLabel, [], 'admin');
-
- if ($translatedLabel == $defaultLabel) {
- $translatedLabel = $panelLabel;
- }
- }
-
- return $translatedLabel;
- }
-
- public function lcTransAdminMenu($menuName)
- {
- $menuLabel = 'menu.'.$menuName;
- $translatedLabel = $this->translator->trans($menuLabel, [], 'admin');
-
- return $translatedLabel ;
- }
-
- public function lcTransAdminTitle($pageName, $entityClass)
- {
- $entityName = $this->getEntityName($entityClass) ;
- $translatedLabel = '' ;
-
- if(Crud::PAGE_INDEX == $pageName) {
- $titleLabel = 'entity.'.$entityName.'.label_plurial' ;
- $translatedLabel = $this->translator->trans($titleLabel, [], 'admin');
- }
- elseif(Crud::PAGE_EDIT == $pageName) {
- $entityLabel = 'entity.'.$entityName.'.label' ;
- $translatedEntityLabel = $this->translator->trans($entityLabel, [],'admin');
- $translatedLabel = $this->translator->trans('form.title.edit', ['%name%' => $translatedEntityLabel], 'admin');
- }
- elseif(Crud::PAGE_NEW == $pageName) {
- $entityLabel = 'entity.'.$entityName.'.label' ;
- $translatedEntityLabel = $this->translator->trans($entityLabel, [], 'admin');
- $translatedLabel = $this->translator->trans('form.title.new', ['%name%' => $translatedEntityLabel], 'admin');
- }
-
- return $translatedLabel ;
- }
-
- public function getEntityName($entityClass)
- {
- $entityNameExplode = explode('\\', $entityClass);
- $entityName = strtolower($entityNameExplode[count($entityNameExplode) - 1]);
- return $entityName ;
- }
-
- public function lcLiip($path, $thumb = 'tile', $default = 'default.jpg')
- {
- if (substr($path, 0, 1) === '/') {
- $path = substr($path, 1);
- }
-
- if ($path) {
- $fileManagerFolder = substr($this->getFileManagerFolder(), 1);
-
- if (strpos($path, $fileManagerFolder) === false) {
- $path = $fileManagerFolder . '/' . $path;
- }
-
- if (file_exists($path)) {
- return $this->cacheManager->getBrowserPath($path, $thumb);
- }
- }
-
- return $this->cacheManager->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
- }
-
- function getTranslatedUrls()
- {
- $ret = array();
- $langs = $this->parameterBag->get('app.locales');
- $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
-
- $params = array_merge((array)$this->requestStack->getCurrentRequest()->get('_route_params'), $_GET);
-
- if ($currentRoute) {
- foreach ($langs as $lg) {
- $ret[$lg] = $this->router->generate($currentRoute, array_merge($params, array('_locale' => $lg)));
- }
- }
-
- return $ret;
- }
-
- public function getFileManagerFolder()
- {
- return $this->parameterBag->get('app.path_uploads');
- }
-
- public function homepageRoute()
- {
- return $this->parameterBag->get('lc_sov.homepage_route');
- }
-
- }
|