Newer
Older
Guillaume Perréal
committed
1
2
3
4
5
6
7
8
9
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?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;
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
);
}
}