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
|
17 |
|
return 'Symfony\Component\Form\Extension\Core\Type\FileType'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
17 |
|
public function configureOptions(OptionsResolver $resolver) |
72
|
|
|
{ |
73
|
17 |
|
$resolver->setDefaults(array( |
74
|
17 |
|
'panda_widget' => false, |
75
|
|
|
'panda_widget_version' => 2, |
76
|
|
|
'cloud' => null, |
77
|
|
|
)); |
78
|
17 |
|
$resolver->setDefaults($this->defaultOptions); |
79
|
|
|
|
80
|
16 |
|
$cloudNormalizer = function (Options $options, $cloud) { |
81
|
16 |
|
if (!$options['panda_widget']) { |
82
|
4 |
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
12 |
|
if (null === $cloud) { |
86
|
1 |
|
throw new InvalidOptionsException('The "cloud" option is required when enabling the panda widget.'); |
87
|
|
|
} |
88
|
|
|
|
89
|
11 |
|
return $cloud; |
90
|
|
|
}; |
91
|
|
|
|
92
|
17 |
|
$resolver->setNormalizer('cloud', $cloudNormalizer); |
93
|
17 |
|
$resolver->setAllowedValues('panda_widget', array(true, false, 'yes', 'no')); |
94
|
17 |
|
$resolver->setAllowedValues('panda_widget_version', array(1, 2)); |
95
|
17 |
|
$resolver->setAllowedTypes('cancel_button', 'bool'); |
96
|
17 |
|
$resolver->setAllowedTypes('cloud', array('null', 'string')); |
97
|
17 |
|
$resolver->setAllowedTypes('multiple_files', 'bool'); |
98
|
17 |
|
$resolver->setAllowedTypes('panda_widget', array('bool', 'string')); |
99
|
17 |
|
$resolver->setAllowedTypes('progress_bar', 'bool'); |
100
|
17 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
12 |
|
public function buildView(FormView $view, FormInterface $form, array $options) |
106
|
|
|
{ |
107
|
12 |
|
if (!$options['panda_widget'] || !isset($view->vars['id'])) { |
108
|
1 |
|
$view->vars["panda_uploader"] = false; |
109
|
|
|
|
110
|
1 |
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
11 |
|
$view->vars["panda_uploader"] = true; |
114
|
|
|
|
115
|
11 |
|
$widgetVersion = $options["panda_widget_version"]; |
116
|
|
|
|
117
|
|
|
// generate widgets' ids |
118
|
11 |
|
$formId = $view->vars["id"]; |
119
|
11 |
|
$browseButtonId = "browse_button_$formId"; |
120
|
11 |
|
$cancelButtonId = "cancel_button_$formId"; |
121
|
11 |
|
$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
|
11 |
|
$view->vars["attr"]["panda-uploader"] = "v$widgetVersion"; |
126
|
11 |
|
$view->vars["attr"]["authorise-url"] = $this->urlGenerator->generate( |
127
|
11 |
|
"xabbuh_panda_authorise_upload", |
128
|
11 |
|
array("cloud" => $options["cloud"]) |
129
|
|
|
); |
130
|
11 |
|
$view->vars["attr"]["multiple_files"] = var_export($options["multiple_files"], true); |
131
|
11 |
|
$view->vars["attr"]["browse-button-id"] = $browseButtonId; |
132
|
11 |
|
$view->vars["attr"]["cancel-button-id"] = $cancelButtonId; |
133
|
11 |
|
$view->vars["attr"]["progress-bar-id"] = $progressBarId; |
134
|
|
|
|
135
|
11 |
|
$view->vars["browse_button_id"] = $browseButtonId; |
136
|
11 |
|
$view->vars["browse_button_label"] = "Browse"; |
137
|
11 |
|
$view->vars["cancel_button"] = $options["cancel_button"]; |
138
|
11 |
|
$view->vars["cancel_button_id"] = $cancelButtonId; |
139
|
11 |
|
$view->vars["cancel_button_label"] = "Cancel"; |
140
|
11 |
|
$view->vars["progress_bar"] = $options["progress_bar"]; |
141
|
11 |
|
$view->vars["progress_bar_id"] = $progressBarId; |
142
|
11 |
|
} |
143
|
|
|
} |
144
|
|
|
|