1 | <?php |
||
14 | class UploaderExtension extends \Twig_Extension |
||
15 | { |
||
16 | |||
17 | public $generator; |
||
18 | |||
19 | public function __construct(UrlGeneratorInterface $generator ) |
||
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() |
||
85 | } |
||
86 |