1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the XabbuhPandaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Flothmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xabbuh\PandaBundle\Form\Extension; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FileType; |
16
|
|
|
use Symfony\Component\Form\FormInterface; |
17
|
|
|
use Symfony\Component\Form\FormView; |
18
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
19
|
|
|
use Symfony\Component\OptionsResolver\Options; |
20
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
22
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Extends the {@link http://api.symfony.com/master/Symfony/Component/Form/Extension/Core/Type/FileType.html FileType} |
26
|
|
|
* by adding an option to mark such a field as a widget for using the |
27
|
|
|
* {@link http://www.pandastream.com/docs/video_uploader Panda uploader}. |
28
|
|
|
* |
29
|
|
|
* @author Christian Flothmann <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class VideoUploaderExtension extends AbstractTypeExtension |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* The url generator |
35
|
|
|
* @var UrlGeneratorInterface |
36
|
|
|
*/ |
37
|
|
|
private $urlGenerator; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* General options to configure the display of widget components |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $defaultOptions; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @param UrlGeneratorInterface $urlGenerator The url generator |
49
|
|
|
* @param array $defaultOptions Default display options for widget objects |
50
|
|
|
*/ |
51
|
18 |
|
public function __construct(UrlGeneratorInterface $urlGenerator, array $defaultOptions) |
52
|
|
|
{ |
53
|
18 |
|
$this->urlGenerator = $urlGenerator; |
54
|
18 |
|
$this->defaultOptions = array_replace(array( |
55
|
18 |
|
'multiple_files' => false, |
56
|
|
|
'cancel_button' => true, |
57
|
|
|
'progress_bar' => true, |
58
|
|
|
), $defaultOptions); |
59
|
18 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
17 |
|
public function getExtendedType() |
65
|
|
|
{ |
66
|
17 |
|
return FileType::class; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
|
public static function getExtendedTypes(): iterable |
73
|
|
|
{ |
74
|
|
|
return [FileType::class]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
17 |
|
public function configureOptions(OptionsResolver $resolver) |
81
|
|
|
{ |
82
|
17 |
|
$resolver->setDefaults(array( |
83
|
17 |
|
'panda_widget' => false, |
84
|
|
|
'panda_widget_version' => 2, |
85
|
|
|
'cloud' => null, |
86
|
|
|
)); |
87
|
17 |
|
$resolver->setDefaults($this->defaultOptions); |
88
|
|
|
|
89
|
|
|
$cloudNormalizer = function (Options $options, $cloud) { |
90
|
16 |
|
if (!$options['panda_widget']) { |
91
|
4 |
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
12 |
|
if (null === $cloud) { |
95
|
1 |
|
throw new InvalidOptionsException('The "cloud" option is required when enabling the panda widget.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
11 |
|
return $cloud; |
99
|
17 |
|
}; |
100
|
|
|
|
101
|
17 |
|
$resolver->setNormalizer('cloud', $cloudNormalizer); |
102
|
17 |
|
$resolver->setAllowedValues('panda_widget', array(true, false, 'yes', 'no')); |
103
|
17 |
|
$resolver->setAllowedValues('panda_widget_version', array(1, 2)); |
104
|
17 |
|
$resolver->setAllowedTypes('cancel_button', 'bool'); |
105
|
17 |
|
$resolver->setAllowedTypes('cloud', array('null', 'string')); |
106
|
17 |
|
$resolver->setAllowedTypes('multiple_files', 'bool'); |
107
|
17 |
|
$resolver->setAllowedTypes('panda_widget', array('bool', 'string')); |
108
|
17 |
|
$resolver->setAllowedTypes('progress_bar', 'bool'); |
109
|
17 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritDoc} |
113
|
|
|
*/ |
114
|
12 |
|
public function buildView(FormView $view, FormInterface $form, array $options) |
115
|
|
|
{ |
116
|
12 |
|
if (!$options['panda_widget'] || !isset($view->vars['id'])) { |
117
|
1 |
|
$view->vars["panda_uploader"] = false; |
118
|
|
|
|
119
|
1 |
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
11 |
|
$view->vars["panda_uploader"] = true; |
123
|
|
|
|
124
|
11 |
|
$widgetVersion = $options["panda_widget_version"]; |
125
|
|
|
|
126
|
|
|
// generate widgets' ids |
127
|
11 |
|
$formId = $view->vars["id"]; |
128
|
11 |
|
$browseButtonId = "browse_button_$formId"; |
129
|
11 |
|
$cancelButtonId = "cancel_button_$formId"; |
130
|
11 |
|
$progressBarId = "progress_bar_$formId"; |
131
|
|
|
|
132
|
|
|
// set input field attributes accordingly (widget ids will be |
133
|
|
|
// read from there by the javascript uploader implementation) |
134
|
11 |
|
$view->vars["attr"]["panda-uploader"] = "v$widgetVersion"; |
135
|
11 |
|
$view->vars["attr"]["authorise-url"] = $this->urlGenerator->generate( |
136
|
11 |
|
"xabbuh_panda_authorise_upload", |
137
|
11 |
|
array("cloud" => $options["cloud"]) |
138
|
|
|
); |
139
|
11 |
|
$view->vars["attr"]["multiple_files"] = var_export($options["multiple_files"], true); |
140
|
11 |
|
$view->vars["attr"]["browse-button-id"] = $browseButtonId; |
141
|
11 |
|
$view->vars["attr"]["cancel-button-id"] = $cancelButtonId; |
142
|
11 |
|
$view->vars["attr"]["progress-bar-id"] = $progressBarId; |
143
|
|
|
|
144
|
11 |
|
$view->vars["browse_button_id"] = $browseButtonId; |
145
|
11 |
|
$view->vars["browse_button_label"] = "Browse"; |
146
|
11 |
|
$view->vars["cancel_button"] = $options["cancel_button"]; |
147
|
11 |
|
$view->vars["cancel_button_id"] = $cancelButtonId; |
148
|
11 |
|
$view->vars["cancel_button_label"] = "Cancel"; |
149
|
11 |
|
$view->vars["progress_bar"] = $options["progress_bar"]; |
150
|
11 |
|
$view->vars["progress_bar_id"] = $progressBarId; |
151
|
11 |
|
} |
152
|
|
|
} |
153
|
|
|
|