Commit c0624a8f authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Mise en place d'une extension Twig pour faire le rendu des UploadedFile.

Showing with 165 additions and 0 deletions
+165 -0
......@@ -66,3 +66,11 @@ services:
- @cl_tissue.scanner
tags:
- { name: kernel.event_listener, event: file_upload.complete, method: onFileUploadCompleted }
# Extension Twig
irstea_file_upload.twig_extension:
class: Irstea\FileUploadBundle\Twig\FileUploadExtension
arguments:
- @translator
tags:
- { name: twig.extension }
......@@ -7,3 +7,6 @@ form:
max_number_of_files(%num%): %num% fichiers maximum
min_file_size: Taille minimale
max_file_size: Taille maximale
file_upload:
file_size(%size%,%unit%): %size% %unit%o
{%- spaceless %}
<span class="uploaded-file">
{{- irstea_file_icon(file.type|default(null)) -}}
&nbsp;<span class="name">
<a href="{{
path('file_upload_get_content', {
'id': file.id|default('__id__'),
'token': csrf_token(constant('Irstea\\FileUploadBundle\\Controller\\UploadController::CSRF_INTENTION'))
})
}}">
{{- file.name|default('') -}}
</a>
</span>&nbsp;(<span class="size">
{{- file.size|default(0)|irstea_file_size -}}
</span>)
</span>
{% endspaceless -%}
<?php
/*
* Copyright (C) 2015 IRSTEA
* All rights reserved.
*/
namespace Irstea\FileUploadBundle\Twig;
use Irstea\FileUploadBundle\Model\UploadedFileInterface;
use Irstea\FileUploadBundle\Utils\MimeTypeIcon;
use \NumberFormatter;
use Symfony\Component\Translation\TranslatorInterface;
use Twig_Environment;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
* Description of FileUploadExtension
*
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
*/
class FileUploadExtension extends Twig_Extension
{
/**
*
* @var TranslatorInterface
*/
private $translator;
/**
*
* @param TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function getFunctions()
{
return [
new Twig_SimpleFunction(
'irstea_uploaded_file',
[$this, 'formatUploadedFile'],
[
'needs_environment' => true,
'is_safe' => ['html']
]
),
new Twig_SimpleFunction(
'irstea_file_icon',
[$this, 'formatFileIcon'],
['is_safe' => ['html']]
),
];
}
public function getFilters()
{
return [
new Twig_SimpleFilter(
'irstea_file_size',
[$this, 'formatFileSize'],
['needs_context' => true]
),
];
}
public function getName()
{
return "irstea_file_upload";
}
public function formatUploadedFile(Twig_Environment $env, $file = null)
{
if(null === $file) {
return '';
} elseif($file instanceof \Irstea\FileUploadBundle\Entity\UploadedFile) {
$file = $file->toArray();
} elseif(!is_array($file)) {
throw new \InvalidArgumentException("irstea_uploaded_file expects an UploadedFile instance, array or null");
}
return $env->render('IrsteaFileUploadBundle:Extension:uploaded_file.html.twig', ['file' => $file]);
}
public function formatFileIcon($mimeType)
{
$icon = MimeTypeIcon::getMimeTypeIcon($mimeType);
return sprintf('<i class="icon fa fa-file%s-o"></i>', ($icon && $icon !== 'file') ? ('-'.$icon) : '');
}
public function formatFileSize($context, $size, $precision = null, $locale = null)
{
if(!is_numeric($size)) {
return '';
}
if($size > 1000000000) {
$size /= 1000000000.0;
$unit = 'Gi';
} elseif($size > 1000000) {
$size /= 1000000.0;
$unit = 'Mi';
} elseif($size > 1000) {
$size /= 1000.0;
$unit = 'Ki';
} else {
$unit = '';
}
if(null === $locale) {
$locale = $context['app']->getRequest()->getLocale();
}
$formatter = NumberFormatter::create($locale, NumberFormatter::DECIMAL);
if(null !== $precision) {
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $precision);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $precision);
} else {
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 0);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2);
}
return $this->translator->trans(
'file_upload.file_size(%size%,%unit%)',
[
'%size%' => $formatter->format($size),
'%unit%' => $unit,
],
null,
$locale
);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment