Newer
Older
Guillaume Perréal
committed
<?php
/*
* Copyright (C) 2015 IRSTEA
* All rights reserved.
*
* @package irstea/file-upload-bundle
* @copyright 2015 Irstea
* @author Guillaume Perréal <guillaume.perreal@irstea.fr>
Guillaume Perréal
committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
*/
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;
}
Guillaume Perréal
committed
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']]
),
];
}
Guillaume Perréal
committed
public function getFilters()
{
return [
new Twig_SimpleFilter(
'irstea_file_size',
[$this, 'formatFileSize'],
['needs_context' => true]
),
];
}
Guillaume Perréal
committed
public function getName()
{
return "irstea_file_upload";
}
/** Genère un tag HTML pour représenter le fichier.
*
* Inclut un lien sécurisé qui permet de télécharger le fichier, mais uniquement à l'utilisateur qui a affiché la page.
*
* @param Twig_Environment $env
* @param UploadedFileInterface $file
*
* @return string
*/
Guillaume Perréal
committed
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]);
}
/** Génère un tag "icône" Font-Awesome pour le type MIME indiqué.
*
* @param string $mimeType Type MIME.
*
* @return string Un tag <i></i> avec les classes Font-Awesome.
*
* @uses MimeTypeIcon::getMimeTypeIcon
*/
Guillaume Perréal
committed
public function formatFileIcon($mimeType)
{
$icon = MimeTypeIcon::getMimeTypeIcon($mimeType);
return sprintf('<i class="icon fa fa-file%s-o"></i>', ($icon && $icon !== 'file') ? ('-'.$icon) : '');
}
/** Formate une taille de fichier.
*
* @param array $context Contexte Twig.
* @param integer $size Taille du fichier.
* @param integer $precision Nombre de chiffres après la virgule.
* @param string $locale Langue
*
* @return string
*
* @uses \NumberFormatter
*/
Guillaume Perréal
committed
public function formatFileSize($context, $size, $precision = null, $locale = null)
{
if(!is_numeric($size)) {
return '';
}
if($size > 1000000000) {
$size /= 1000000000.0;
Guillaume Perréal
committed
$defaultPrecision = 2;
Guillaume Perréal
committed
$unit = 'Gi';
} elseif($size > 1000000) {
$size /= 1000000.0;
Guillaume Perréal
committed
$defaultPrecision = 1;
Guillaume Perréal
committed
$unit = 'Mi';
} elseif($size > 1000) {
$size /= 1000.0;
Guillaume Perréal
committed
$defaultPrecision = 1;
Guillaume Perréal
committed
$unit = 'Ki';
} else {
$unit = '';
Guillaume Perréal
committed
$defaultPrecision = 0;
}
Guillaume Perréal
committed
if(null === $locale) {
$locale = $context['app']->getRequest()->getLocale();
}
$formatter = NumberFormatter::create($locale, NumberFormatter::DECIMAL);
Guillaume Perréal
committed
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, null !== $precision ? $precision : 0);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, null !== $precision ? $precision : $defaultPrecision);
Guillaume Perréal
committed
return $this->translator->trans(
Guillaume Perréal
committed
'file_size(%size%,%unit%)',
Guillaume Perréal
committed
[
'%size%' => $formatter->format($size),
'%unit%' => $unit,
],
Guillaume Perréal
committed
'file_upload',
Guillaume Perréal
committed
$locale
);
}
}