vendor/wearemarketing/webcontentbundle/Service/EditInlineService.php line 124

Open in your IDE?
  1. <?php
  2. namespace WAM\Bundle\WebContentBundle\Service;
  3. use Doctrine\ORM\EntityManager;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Sonata\AdminBundle\Admin\Pool;
  6. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  7. use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
  9. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  10. use Twig\Environment;
  11. use WAM\Bundle\RoutingBundle\Entity\Interfaces\UrlInterface;
  12. use WAM\Bundle\WebContentBundle\Service\Interfaces\EditInlineServiceInterface;
  13. use WAM\Component\Core\Util\ClassTools;
  14. /**
  15.  * @author Edgar Tébar <etebar@wearemarketing.com>
  16.  */
  17. class EditInlineService implements EditInlineServiceInterface
  18. {
  19.     private AuthorizationCheckerInterface $checker;
  20.     private Pool $pool;
  21.     private EntityManagerInterface $em;
  22.     private array $config;
  23.     private Environment $templating;
  24.     private FragmentHandler $handler;
  25.     const TEMPLATE_TRANS_LINK_ADMIN 'WAMWebContentBundle:EditInline:path_to_admin_translation.html.twig';
  26.     const FULL_ADMIN_PATH_ACTION 'WAMWebContentBundle:EditInline:fullAdminEdit';
  27.     const RENDER_FORM_ACTION 'WAMWebContentBundle:EditInline:renderForm';
  28.     /**
  29.      * EditInlineService constructor.
  30.      * @param AuthorizationCheckerInterface $checker
  31.      * @param Pool $pool
  32.      * @param EntityManagerInterface $em
  33.      * @param array $config
  34.      */
  35.     public function __construct(AuthorizationCheckerInterface $checkerPool $poolEntityManagerInterface $em, array $config)
  36.     {
  37.         $this->checker $checker;
  38.         $this->pool $pool;
  39.         $this->em $em;
  40.         $this->config $config;
  41.     }
  42.     /**
  43.      * @param object $object
  44.      * @param string $routeType
  45.      * @param array  $parameters
  46.      * @param string $aditionalClass
  47.      * @param string $parentClass
  48.      *
  49.      * @return string
  50.      */
  51.     public function getFullAdminPath(object $objectstring $routeType, array $parametersstring $aditionalClassstring $parentClassstring $strategy): string
  52.     {
  53.         switch ($routeType) {
  54.             case 'create':
  55.                 $faIconClass 'fa-plus';
  56.                 $title 'Add new object';
  57.                 break;
  58.             case 'list':
  59.                 $faIconClass 'fa-list';
  60.                 $title 'For list all objects click here';
  61.                 break;
  62.             default:
  63.                 $faIconClass 'fa-pencil-square-o';
  64.                 if (method_exists($object'getTitle')) {
  65.                     $title 'Click here edit '.$object->getTitle();
  66.                 } else {
  67.                     $title 'For full edit click here';
  68.                 }
  69.                 break;
  70.         }
  71.         $route $this->getAdminRoute($object$routeType$parameters);
  72.         return $this->handler->render(new ControllerReference(self::FULL_ADMIN_PATH_ACTION, array(
  73.             'route' => $route,
  74.             'aditionalClass' => $aditionalClass,
  75.             'title' => $title,
  76.             'faIconClass' => $faIconClass,
  77.             'parentClass' => $parentClass,
  78.         )), $strategy, array());
  79.     }
  80.     public function getTransAdminPath(string $transKey): string
  81.     {
  82.         $params = array('filter[key][value]' => $transKey);
  83.         $absolute true;
  84.         $admin $this->pool->getAdminByAdminCode('ibrows_sonata_translation.admin.orm');
  85.         if (false === $this->isGranted()) {
  86.             return '';
  87.         }
  88.         $route $admin->generateUrl('list'$params$absolute);
  89.         if (!$route) {
  90.             return '';
  91.         }
  92.         return $route;
  93.     }
  94.     /**
  95.      * @return bool
  96.      */
  97.     public function isGranted(): bool
  98.     {
  99.         return $this->checker->isGranted($this->config['roles']);
  100.     }
  101.     /**
  102.      * @param array $parameters
  103.      *
  104.      * @return string
  105.      */
  106.     public function renderForm(array $parameters = array(), string $strategy): string
  107.     {
  108.         return $this->handler->render(new ControllerReference(self::RENDER_FORM_ACTION$parameters), $strategy, array());
  109.     }
  110.     public function renderTrans(array $parameters = array()): string
  111.     {
  112.         $object $this->em->getRepository('LexikTranslationBundle:TransUnit')->findBy(array('key_name' => $parameters['message']));
  113.         if (!$object) {
  114.             return $parameters['trans'];
  115.         }
  116.         $parameters array_merge($parameters, array('object' => $object));
  117.         return $this->templating->render(self::TEMPLATE_TRANS_LINK_ADMIN$parameters);
  118.     }
  119.     public function setEnvironment(Environment $templating)
  120.     {
  121.         $this->templating $templating;
  122.     }
  123.     public function setHandler(FragmentHandler $handler)
  124.     {
  125.         $this->handler $handler;
  126.         return $this;
  127.     }
  128.     public function getAdminRoute(object $objectstring $routeType, array $parametersbool $absolute false): ?string
  129.     {
  130.         return $this->generateAdminUrl($object$routeType$parameters$absolute);
  131.     }
  132.     public function generateAdminUrl(object $objectstring $routeType, array $parametersbool $absolute false): ?string
  133.     {
  134.         $params = array();
  135.         if (!empty($parameters['admin_code'])) {
  136.             $admin $this->pool->getAdminByAdminCode($parameters['admin_code']);
  137.         } else {
  138.             $admin $this->pool->getAdminByClass(ClassTools::realClass($object));
  139.         }
  140.         if (!$admin) {
  141.             return null;
  142.         }
  143.         if (in_array(get_class($object), $admin->getSubClasses())) {
  144.             $classInfo = new \ReflectionClass(get_class($object));
  145.             $params['subclass'] = $classInfo->getShortName();
  146.         }
  147.         if ('create' == $routeType) {
  148.             $route $admin->generateUrl($routeTypearray_merge($parameters$params), $absolute);
  149.         } else {
  150.             $route $admin->generateObjectUrl($routeType$objectarray_merge($parameters$params), $absolute);
  151.         }
  152.         return $route;
  153.     }
  154.     public function isUrlObject(object $object): bool
  155.     {
  156.         return $object instanceof UrlInterface;
  157.     }
  158. }