<?php
namespace WAM\Bundle\WebContentBundle\Service;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Sonata\AdminBundle\Admin\Pool;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Twig\Environment;
use WAM\Bundle\RoutingBundle\Entity\Interfaces\UrlInterface;
use WAM\Bundle\WebContentBundle\Service\Interfaces\EditInlineServiceInterface;
use WAM\Component\Core\Util\ClassTools;
/**
* @author Edgar Tébar <etebar@wearemarketing.com>
*/
class EditInlineService implements EditInlineServiceInterface
{
private AuthorizationCheckerInterface $checker;
private Pool $pool;
private EntityManagerInterface $em;
private array $config;
private Environment $templating;
private FragmentHandler $handler;
const TEMPLATE_TRANS_LINK_ADMIN = 'WAMWebContentBundle:EditInline:path_to_admin_translation.html.twig';
const FULL_ADMIN_PATH_ACTION = 'WAMWebContentBundle:EditInline:fullAdminEdit';
const RENDER_FORM_ACTION = 'WAMWebContentBundle:EditInline:renderForm';
/**
* EditInlineService constructor.
* @param AuthorizationCheckerInterface $checker
* @param Pool $pool
* @param EntityManagerInterface $em
* @param array $config
*/
public function __construct(AuthorizationCheckerInterface $checker, Pool $pool, EntityManagerInterface $em, array $config)
{
$this->checker = $checker;
$this->pool = $pool;
$this->em = $em;
$this->config = $config;
}
/**
* @param object $object
* @param string $routeType
* @param array $parameters
* @param string $aditionalClass
* @param string $parentClass
*
* @return string
*/
public function getFullAdminPath(object $object, string $routeType, array $parameters, string $aditionalClass, string $parentClass, string $strategy): string
{
switch ($routeType) {
case 'create':
$faIconClass = 'fa-plus';
$title = 'Add new object';
break;
case 'list':
$faIconClass = 'fa-list';
$title = 'For list all objects click here';
break;
default:
$faIconClass = 'fa-pencil-square-o';
if (method_exists($object, 'getTitle')) {
$title = 'Click here edit '.$object->getTitle();
} else {
$title = 'For full edit click here';
}
break;
}
$route = $this->getAdminRoute($object, $routeType, $parameters);
return $this->handler->render(new ControllerReference(self::FULL_ADMIN_PATH_ACTION, array(
'route' => $route,
'aditionalClass' => $aditionalClass,
'title' => $title,
'faIconClass' => $faIconClass,
'parentClass' => $parentClass,
)), $strategy, array());
}
public function getTransAdminPath(string $transKey): string
{
$params = array('filter[key][value]' => $transKey);
$absolute = true;
$admin = $this->pool->getAdminByAdminCode('ibrows_sonata_translation.admin.orm');
if (false === $this->isGranted()) {
return '';
}
$route = $admin->generateUrl('list', $params, $absolute);
if (!$route) {
return '';
}
return $route;
}
/**
* @return bool
*/
public function isGranted(): bool
{
return $this->checker->isGranted($this->config['roles']);
}
/**
* @param array $parameters
*
* @return string
*/
public function renderForm(array $parameters = array(), string $strategy): string
{
return $this->handler->render(new ControllerReference(self::RENDER_FORM_ACTION, $parameters), $strategy, array());
}
public function renderTrans(array $parameters = array()): string
{
$object = $this->em->getRepository('LexikTranslationBundle:TransUnit')->findBy(array('key_name' => $parameters['message']));
if (!$object) {
return $parameters['trans'];
}
$parameters = array_merge($parameters, array('object' => $object));
return $this->templating->render(self::TEMPLATE_TRANS_LINK_ADMIN, $parameters);
}
public function setEnvironment(Environment $templating)
{
$this->templating = $templating;
}
public function setHandler(FragmentHandler $handler)
{
$this->handler = $handler;
return $this;
}
public function getAdminRoute(object $object, string $routeType, array $parameters, bool $absolute = false): ?string
{
return $this->generateAdminUrl($object, $routeType, $parameters, $absolute);
}
public function generateAdminUrl(object $object, string $routeType, array $parameters, bool $absolute = false): ?string
{
$params = array();
if (!empty($parameters['admin_code'])) {
$admin = $this->pool->getAdminByAdminCode($parameters['admin_code']);
} else {
$admin = $this->pool->getAdminByClass(ClassTools::realClass($object));
}
if (!$admin) {
return null;
}
if (in_array(get_class($object), $admin->getSubClasses())) {
$classInfo = new \ReflectionClass(get_class($object));
$params['subclass'] = $classInfo->getShortName();
}
if ('create' == $routeType) {
$route = $admin->generateUrl($routeType, array_merge($parameters, $params), $absolute);
} else {
$route = $admin->generateObjectUrl($routeType, $object, array_merge($parameters, $params), $absolute);
}
return $route;
}
public function isUrlObject(object $object): bool
{
return $object instanceof UrlInterface;
}
}