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