1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\MediaBundle\Twig\Extension; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
7
|
|
|
|
8
|
|
|
class MediaPreviewExtension extends \Twig_Extension |
9
|
|
|
{ |
10
|
|
|
protected $entityManager; |
11
|
|
|
protected $request; |
12
|
|
|
protected $previewIcons; |
13
|
|
|
|
14
|
|
|
public function __construct(RequestStack $requestStack, EntityManager $entityManager, $previewIcons) |
15
|
|
|
{ |
16
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
17
|
|
|
$this->entityManager = $entityManager; |
18
|
|
|
$this->previewIcon = $previewIcons; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getFilters() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
new \Twig_SimpleFilter('previewIcon', [$this, 'previewIconFilter'], [ |
25
|
|
|
'is_safe' => ['html'], |
26
|
|
|
'needs_environment' => true, |
27
|
|
|
] |
28
|
|
|
), |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function previewIconFilter(\Twig_Environment $twig, $secretKey = '') |
33
|
|
|
{ |
34
|
|
|
if ($secretKey == '') { |
35
|
|
|
return ''; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$mimeType = $this->getMimeType($secretKey); |
39
|
|
|
$icon = ''; |
40
|
|
|
|
41
|
|
|
$media = $this->entityManager->getRepository("AlpixelMediaBundle:Media")->findOneBySecretKey($secretKey); |
42
|
|
|
|
43
|
|
|
if (preg_match('/^image/', $mimeType) === 0) { |
44
|
|
|
$icon = $this->getIcon($mimeType); |
45
|
|
|
$link = $this->generatePath(true, $icon); |
46
|
|
|
} else { |
47
|
|
|
$link = $this->generatePath(false, $secretKey); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $twig->render('AlpixelMediaBundle:Form:blocks/show_icon.html.twig', [ |
51
|
|
|
'link' => $link, |
52
|
|
|
'icon' => $icon, |
53
|
|
|
'label' => $media->getName(), |
54
|
|
|
'secretKey' => $secretKey, |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function getIcon($mimeType) |
59
|
|
|
{ |
60
|
|
|
$explMime = explode('/', $mimeType); |
61
|
|
|
$mime = (isset($explMime[1])) ? $explMime[1] : ''; |
62
|
|
|
|
63
|
|
|
return (array_key_exists($mime, $this->previewIcon)) ? $this->previewIcon[$mime] : $this->previewIcon['unknown']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function generatePath($isIcon, $str) |
67
|
|
|
{ |
68
|
|
|
if ($isIcon === true) { |
69
|
|
|
return $this->request->getSchemeAndHttpHost().$this->request->getBasePath().'/bundles/alpixelmedia/images/'.$str; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->request->getSchemeAndHttpHost().$this->request->getBaseUrl().'/media/'.$str.'/admin'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function generatePathFromSecretKey($secretKey) |
76
|
|
|
{ |
77
|
|
|
if ($secretKey == '') { |
78
|
|
|
return ''; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$mimeType = $this->getMimeType($secretKey); |
82
|
|
|
|
83
|
|
|
if (preg_match('/^image/', $mimeType) === 0) { |
84
|
|
|
$icon = $this->getIcon($mimeType); |
85
|
|
|
|
86
|
|
|
return $this->generatePath(true, $icon); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->generatePath(false, $secretKey); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function getMimeType($secretKey) |
93
|
|
|
{ |
94
|
|
|
$mediaObject = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneBySecretKey($secretKey); |
95
|
|
|
|
96
|
|
|
return ($mediaObject !== null) ? $mediaObject->getMime() : null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getName() |
100
|
|
|
{ |
101
|
|
|
return 'alpixel_media_twig_media_preview_extension'; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|