|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[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 Sonata\MediaBundle\Block; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
15
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
|
16
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
|
17
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractBlockService; |
|
18
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
|
19
|
|
|
use Sonata\CoreBundle\Model\ManagerInterface; |
|
20
|
|
|
use Sonata\CoreBundle\Model\Metadata; |
|
21
|
|
|
use Sonata\MediaBundle\Admin\BaseMediaAdmin; |
|
22
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
|
23
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
|
24
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
25
|
|
|
use Symfony\Component\Form\FormBuilder; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
27
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
28
|
|
|
use Symfony\Component\Templating\EngineInterface; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @author Thomas Rabaix <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class MediaBlockService extends AbstractBlockService |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var BaseMediaAdmin |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $mediaAdmin; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var ManagerInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $mediaManager; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
* @param EngineInterface $templating |
|
48
|
|
|
* @param ContainerInterface $container |
|
49
|
|
|
* @param ManagerInterface $mediaManager |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct($name, EngineInterface $templating, ContainerInterface $container, ManagerInterface $mediaManager) |
|
52
|
|
|
{ |
|
53
|
|
|
parent::__construct($name, $templating); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$this->mediaManager = $mediaManager; |
|
56
|
|
|
$this->container = $container; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return Pool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getMediaPool() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->getMediaAdmin()->getPool(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return BaseMediaAdmin |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getMediaAdmin() |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$this->mediaAdmin) { |
|
73
|
|
|
$this->mediaAdmin = $this->container->get('sonata.media.admin.media'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->mediaAdmin; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function configureSettings(OptionsResolver $resolver) |
|
83
|
|
|
{ |
|
84
|
|
|
$resolver->setDefaults(array( |
|
85
|
|
|
'media' => false, |
|
86
|
|
|
'title' => false, |
|
87
|
|
|
'context' => false, |
|
88
|
|
|
'mediaId' => null, |
|
89
|
|
|
'format' => false, |
|
90
|
|
|
'template' => 'SonataMediaBundle:Block:block_media.html.twig', |
|
91
|
|
|
)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function buildEditForm(FormMapper $formMapper, BlockInterface $block) |
|
98
|
|
|
{ |
|
99
|
|
|
if (!$block->getSetting('mediaId') instanceof MediaInterface) { |
|
100
|
|
|
$this->load($block); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$formatChoices = $this->getFormatChoices($block->getSetting('mediaId')); |
|
104
|
|
|
|
|
105
|
|
|
$formMapper->add('settings', 'Sonata\CoreBundle\Form\Type\ImmutableArrayType', array( |
|
106
|
|
|
'keys' => array( |
|
107
|
|
|
array('title', 'Symfony\Component\Form\Extension\Core\Type\TextType', array( |
|
108
|
|
|
'required' => false, |
|
109
|
|
|
'label' => 'form.label_title', |
|
110
|
|
|
)), |
|
111
|
|
|
array($this->getMediaBuilder($formMapper), null, array()), |
|
112
|
|
|
array('format', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array( |
|
113
|
|
|
'required' => count($formatChoices) > 0, |
|
114
|
|
|
'choices' => $formatChoices, |
|
115
|
|
|
'label' => 'form.label_format', |
|
116
|
|
|
)), |
|
117
|
|
|
), |
|
118
|
|
|
'translation_domain' => 'SonataMediaBundle', |
|
119
|
|
|
)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function execute(BlockContextInterface $blockContext, Response $response = null) |
|
126
|
|
|
{ |
|
127
|
|
|
// make sure we have a valid format |
|
128
|
|
|
$media = $blockContext->getBlock()->getSetting('mediaId'); |
|
129
|
|
|
if ($media instanceof MediaInterface) { |
|
130
|
|
|
$choices = $this->getFormatChoices($media); |
|
131
|
|
|
|
|
132
|
|
|
if (!array_key_exists($blockContext->getSetting('format'), $choices)) { |
|
133
|
|
|
$blockContext->setSetting('format', key($choices)); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $this->renderResponse($blockContext->getTemplate(), array( |
|
138
|
|
|
'media' => $blockContext->getSetting('mediaId'), |
|
139
|
|
|
'block' => $blockContext->getBlock(), |
|
140
|
|
|
'settings' => $blockContext->getSettings(), |
|
141
|
|
|
), $response); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* {@inheritdoc} |
|
146
|
|
|
*/ |
|
147
|
|
|
public function load(BlockInterface $block) |
|
148
|
|
|
{ |
|
149
|
|
|
$media = $block->getSetting('mediaId', null); |
|
150
|
|
|
|
|
151
|
|
|
if (is_int($media)) { |
|
152
|
|
|
$media = $this->mediaManager->findOneBy(array('id' => $media)); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$block->setSetting('mediaId', $media); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
*/ |
|
161
|
|
|
public function prePersist(BlockInterface $block) |
|
162
|
|
|
{ |
|
163
|
|
|
$block->setSetting('mediaId', is_object($block->getSetting('mediaId')) ? $block->getSetting('mediaId')->getId() : null); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* {@inheritdoc} |
|
168
|
|
|
*/ |
|
169
|
|
|
public function preUpdate(BlockInterface $block) |
|
170
|
|
|
{ |
|
171
|
|
|
$block->setSetting('mediaId', is_object($block->getSetting('mediaId')) ? $block->getSetting('mediaId')->getId() : null); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritdoc} |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getBlockMetadata($code = null) |
|
178
|
|
|
{ |
|
179
|
|
|
return new Metadata($this->getName(), (!is_null($code) ? $code : $this->getName()), false, 'SonataMediaBundle', array( |
|
180
|
|
|
'class' => 'fa fa-picture-o', |
|
181
|
|
|
)); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param MediaInterface|null $media |
|
186
|
|
|
* |
|
187
|
|
|
* @return array |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function getFormatChoices(MediaInterface $media = null) |
|
190
|
|
|
{ |
|
191
|
|
|
$formatChoices = array(); |
|
192
|
|
|
|
|
193
|
|
|
if (!$media instanceof MediaInterface) { |
|
194
|
|
|
return $formatChoices; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$formats = $this->getMediaPool()->getFormatNamesByContext($media->getContext()); |
|
198
|
|
|
|
|
199
|
|
|
foreach ($formats as $code => $format) { |
|
200
|
|
|
$formatChoices[$code] = $code; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $formatChoices; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param FormMapper $formMapper |
|
208
|
|
|
* |
|
209
|
|
|
* @return FormBuilder |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function getMediaBuilder(FormMapper $formMapper) |
|
212
|
|
|
{ |
|
213
|
|
|
// simulate an association ... |
|
214
|
|
|
$fieldDescription = $this->getMediaAdmin()->getModelManager()->getNewFieldDescriptionInstance($this->mediaAdmin->getClass(), 'media', array( |
|
215
|
|
|
'translation_domain' => 'SonataMediaBundle', |
|
216
|
|
|
)); |
|
217
|
|
|
$fieldDescription->setAssociationAdmin($this->getMediaAdmin()); |
|
218
|
|
|
$fieldDescription->setAdmin($formMapper->getAdmin()); |
|
219
|
|
|
$fieldDescription->setOption('edit', 'list'); |
|
220
|
|
|
$fieldDescription->setAssociationMapping(array( |
|
221
|
|
|
'fieldName' => 'media', |
|
222
|
|
|
'type' => ClassMetadataInfo::MANY_TO_ONE, |
|
223
|
|
|
)); |
|
224
|
|
|
|
|
225
|
|
|
return $formMapper->create('mediaId', 'Sonata\AdminBundle\Form\Type\ModelListType', array( |
|
226
|
|
|
'sonata_field_description' => $fieldDescription, |
|
227
|
|
|
'class' => $this->getMediaAdmin()->getClass(), |
|
228
|
|
|
'model_manager' => $this->getMediaAdmin()->getModelManager(), |
|
229
|
|
|
'label' => 'form.label_media', |
|
230
|
|
|
)); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: