->setProperty($propertyName) | ->setProperty($propertyName) | ||||
->setLabel($label) | ->setLabel($label) | ||||
->setTemplatePath('@LcAdmin/crud/field/image.html.twig') | ->setTemplatePath('@LcAdmin/crud/field/image.html.twig') | ||||
->addJsFiles('bundles/lcadmin/js/form-type-collection.js') | |||||
->addJsFiles('bundles/lcadmin/js/form-type-file-manager.js') | |||||
->setFormType(FileManagerType::class) | ->setFormType(FileManagerType::class) | ||||
->addCssClass('field-text') | ->addCssClass('field-text') | ||||
->setCustomOption(self::OPTION_MAX_LENGTH, null) | ->setCustomOption(self::OPTION_MAX_LENGTH, null) |
public function buildForm(FormBuilderInterface $builder, array $options) | public function buildForm(FormBuilderInterface $builder, array $options) | ||||
{ | { | ||||
$builder->add('image', HiddenType::class, array( | |||||
$builder->add('path', HiddenType::class, array( | |||||
'block_prefix' => 'file_manager_image', | 'block_prefix' => 'file_manager_image', | ||||
'label' => false | 'label' => false | ||||
)); | )); |
/** | /** | ||||
* @ORM\Column(type="string", length=255, nullable=true) | * @ORM\Column(type="string", length=255, nullable=true) | ||||
*/ | */ | ||||
protected $image; | |||||
protected $path; | |||||
/** | /** | ||||
* @Gedmo\Translatable | * @Gedmo\Translatable | ||||
public function __toString(){ | public function __toString(){ | ||||
return 'dfe'; | |||||
return $this->getLegend(); | |||||
} | } | ||||
public function getImage(): ?string | |||||
public function getPath(): ?string | |||||
{ | { | ||||
return $this->image; | |||||
return $this->path; | |||||
} | } | ||||
public function setImage(?string $image): self | |||||
public function setPath(?string $path): self | |||||
{ | { | ||||
$this->image = $image; | |||||
$this->path = $path; | |||||
return $this; | return $this; | ||||
} | } | ||||
public function getLegend(): ?string | public function getLegend(): ?string | ||||
{ | { | ||||
return $this->legend; | return $this->legend; |
{% set html_id = 'ea-lightbox-' ~ field.uniqueId %} | {% set html_id = 'ea-lightbox-' ~ field.uniqueId %} | ||||
{% if field.value is not null %} | {% if field.value is not null %} | ||||
<a href="#" class="ea-lightbox-thumbnail" title="{{ field.value.legend }}" data-featherlight="#{{ html_id }}" data-featherlight-close-on-click="anywhere"> | <a href="#" class="ea-lightbox-thumbnail" title="{{ field.value.legend }}" data-featherlight="#{{ html_id }}" data-featherlight-close-on-click="anywhere"> | ||||
<img src="{{ asset(field.value.image) }}" class="img-fluid"> | |||||
<img src="{{ asset(field.value.path) }}" class="img-fluid"> | |||||
</a> | </a> | ||||
<div id="{{ html_id }}" class="ea-lightbox"> | <div id="{{ html_id }}" class="ea-lightbox"> | ||||
<img src="{{ asset(field.value.image) }}"> | |||||
<img src="{{ asset(field.value.path) }}"> | |||||
</div> | </div> | ||||
{% endif %} | {% endif %} |
<div class="lc-filemanager col-xs-12"> | <div class="lc-filemanager col-xs-12"> | ||||
<div class="col-md-6 col-xs-12 nopadding" style="padding: 0px;"> | <div class="col-md-6 col-xs-12 nopadding" style="padding: 0px;"> | ||||
<img style="width: 200px; height: 150px; object-fit: contain; background: #ddd; " src="{{ form.image.vars.value }}" class="lc-filemenager-preview" id="{{ form.image.vars.id }}_preview" alt=""> | |||||
<img style="width: 200px; height: 150px; object-fit: contain; background: #ddd; " src="{{ form.path.vars.value }}" class="lc-filemenager-preview" id="{{ form.path.vars.id }}_preview" alt=""> | |||||
</div> | </div> | ||||
<div class="input-group"> | <div class="input-group"> | ||||
{{ form_widget(form) }} | {{ form_widget(form) }} | ||||
<div class="input-group-append"> | <div class="input-group-append"> | ||||
<button type="button" class="btn btn-sm lc-filemanager-open" data-id="{{ form.image.vars.id }}" | |||||
<button type="button" class="btn btn-sm lc-filemanager-open" data-id="{{ form.path.vars.id }}" | |||||
data-target="{{ path('file_manager', {module:1, conf:'default'})|raw }}"> | data-target="{{ path('file_manager', {module:1, conf:'default'})|raw }}"> | ||||
<i class="fa fa-folder-open-o"></i> | <i class="fa fa-folder-open-o"></i> | ||||
</button> | </button> | ||||
{% if value %} | {% if value %} | ||||
<button type="button" class="btn btn-sm lc-filemanager-delete" data-id="{{ form.image.vars.id }}"> | |||||
<button type="button" class="btn btn-sm lc-filemanager-delete" data-id="{{ form.path.vars.id }}"> | |||||
<i class="fa fa-trash-o"></i> | <i class="fa fa-trash-o"></i> | ||||
</button> | </button> | ||||
{% endif %} | {% endif %} |
<?php | |||||
namespace Lc\AdminBundle\Twig; | |||||
use App\Entity\Page; | |||||
use Doctrine\ORM\EntityManagerInterface; | |||||
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 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; | |||||
public function __construct(KernelInterface $kernel, ParameterBagInterface $parameterBag, CacheManager $cacheManager, EntityManagerInterface $entityManager, RequestStack $requestStack,UrlGeneratorInterface $router) | |||||
{ | |||||
$this->kernel = $kernel; | |||||
$this->parameterBag = $parameterBag; | |||||
$this->cacheManager = $cacheManager; | |||||
$this->em = $entityManager; | |||||
$this->requestStack = $requestStack; | |||||
$this->router = $router; | |||||
} | |||||
public function getFunctions() | |||||
{ | |||||
return array( | |||||
new TwigFunction('lc_liip', [$this, 'lcLiip']), | |||||
new TwigFunction('page_by_dev_alias', [$this, 'getPageByDevAlias']), | |||||
new TwigFunction('translated_urls', [$this, 'getTranslatedUrls']) | |||||
); | |||||
} | |||||
public function getFilters() | |||||
{ | |||||
return [ | |||||
new TwigFilter('lc_cache', [$this, 'lcCache']), | |||||
]; | |||||
} | |||||
public function lcCache($file) | |||||
{ | |||||
$cacheTime = filemtime($this->kernel->getProjectDir() . '/public' . $file); | |||||
if ($cacheTime) { | |||||
return $file . '?c=' . $cacheTime; | |||||
} else { | |||||
return $file . "?c=0"; | |||||
} | |||||
} | |||||
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 getPageByDevAlias($devAlias) | |||||
{ | |||||
return $this->em->getRepository(Page::class)->findOneByDevAlias($devAlias); | |||||
} | |||||
} |