1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Rafidion Michael |
5
|
|
|
* Date: 30/11/2014 |
6
|
|
|
* Time: 17:41 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Mykees\MediaBundle\Twig\Extension; |
10
|
|
|
use Mykees\MediaBundle\Interfaces\Mediable; |
11
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
12
|
|
|
use Mykees\MediaBundle\Util\Reflection; |
13
|
|
|
|
14
|
|
|
class UploaderExtension extends \Twig_Extension |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
public $generator; |
18
|
|
|
|
19
|
|
|
public function __construct(UrlGeneratorInterface $generator ) |
20
|
|
|
{ |
21
|
|
|
$this->generator = $generator; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
public function getFunctions() |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
|
|
new \Twig_SimpleFunction('iframe_uploader', [$this, 'iframeUploader'], [ |
29
|
|
|
'is_safe'=>array('html') |
30
|
|
|
]), |
31
|
|
|
new \Twig_SimpleFunction('editor_uploader', [$this, 'wysiwygUploader'], [ |
32
|
|
|
'is_safe'=>array('html'), |
33
|
|
|
'needs_environment'=>true |
34
|
|
|
]) |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Display the iframe of uploader |
40
|
|
|
* @param $entity |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function iframeUploader(Mediable $entity ) |
44
|
|
|
{ |
45
|
|
|
$model = Reflection::getClassShortName($entity); |
46
|
|
|
$model_id = $entity->getId(); |
47
|
|
|
$bundle = Reflection::getShortBundleRepository($entity); |
48
|
|
|
if($model_id) |
49
|
|
|
{ |
50
|
|
|
$url = $this->generator->generate('mykees_media',[ |
51
|
|
|
'model' => $model, |
52
|
|
|
'bundle'=> $bundle, |
53
|
|
|
'model_id'=> $model_id |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
return '<iframe src="'.$url.'" style="width:100%;border: none;min-height:100%;" class="iframe-uploader"></iframe>'; |
57
|
|
|
}else{ |
58
|
|
|
|
59
|
|
|
return '<h3 style="font-weight: bold;text-align: center;color:#777">The <span style="color:#DD6F6F;border-bottom:2px dashed #777;">ID</span> from your entity <span style="color:#DD6F6F;border-bottom:2px dashed #777;">'.$model.'</span> is required to use the uploader</h3>'; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Display a wysiwyg uploader |
65
|
|
|
* @param $entity |
66
|
|
|
* @return |
67
|
|
|
*/ |
68
|
|
|
public function wysiwygUploader( \Twig_Environment $env, Mediable $entity ) |
69
|
|
|
{ |
70
|
|
|
$model = Reflection::getClassShortName($entity); |
71
|
|
|
$model_id = $entity->getId(); |
72
|
|
|
$bundle = Reflection::getShortBundleRepository($entity); |
73
|
|
|
|
74
|
|
|
return $env->render('MykeesMediaBundle:Media:editor/tinymce.html.twig',[ |
75
|
|
|
'model'=>$model, |
76
|
|
|
'model_id'=>$model_id, |
77
|
|
|
'bundle'=> $bundle, |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getName() |
82
|
|
|
{ |
83
|
|
|
return "mykees_uploader"; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|