|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Block; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
|
17
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
|
18
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractBlockService; |
|
19
|
|
|
use Sonata\BlockBundle\Meta\Metadata; |
|
20
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
|
21
|
|
|
use Sonata\Form\Type\ImmutableArrayType; |
|
22
|
|
|
use Sonata\Form\Validator\ErrorElement; |
|
23
|
|
|
use Sonata\MediaBundle\Model\GalleryManagerInterface; |
|
24
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
|
25
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
26
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
27
|
|
|
use Symfony\Component\Form\Extension\Core\Type\IntegerType; |
|
28
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
30
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
31
|
|
|
use Twig\Environment; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @final since sonata-project/media-bundle 3.21.0 |
|
35
|
|
|
*/ |
|
36
|
|
|
class GalleryListBlockService extends AbstractBlockService |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var GalleryManagerInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $galleryManager; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var Pool |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $pool; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* NEXT_MAJOR: Remove `$templating` argument. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Environment|string $twigOrName |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($twigOrName, ?EngineInterface $templating, GalleryManagerInterface $galleryManager, Pool $pool) |
|
54
|
|
|
{ |
|
55
|
|
|
parent::__construct($twigOrName, $templating); |
|
56
|
|
|
|
|
57
|
|
|
$this->galleryManager = $galleryManager; |
|
58
|
|
|
$this->pool = $pool; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* NEXT_MAJOR: Remove this method. |
|
63
|
|
|
* |
|
64
|
|
|
* @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. You should use |
|
65
|
|
|
* `Sonata\BlockBundle\Block\Service\EditableBlockService` interface instead. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$contextChoices = []; |
|
70
|
|
|
|
|
71
|
|
|
foreach ($this->pool->getContexts() as $name => $context) { |
|
72
|
|
|
$contextChoices[$name] = $name; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$formMapper->add('settings', ImmutableArrayType::class, [ |
|
76
|
|
|
'keys' => [ |
|
77
|
|
|
['title', TextType::class, [ |
|
78
|
|
|
'label' => 'form.label_title', |
|
79
|
|
|
'required' => false, |
|
80
|
|
|
]], |
|
81
|
|
|
['translation_domain', TextType::class, [ |
|
82
|
|
|
'label' => 'form.label_translation_domain', |
|
83
|
|
|
'required' => false, |
|
84
|
|
|
]], |
|
85
|
|
|
['icon', TextType::class, [ |
|
86
|
|
|
'label' => 'form.label_icon', |
|
87
|
|
|
'required' => false, |
|
88
|
|
|
]], |
|
89
|
|
|
['class', TextType::class, [ |
|
90
|
|
|
'label' => 'form.label_class', |
|
91
|
|
|
'required' => false, |
|
92
|
|
|
]], |
|
93
|
|
|
['number', IntegerType::class, [ |
|
94
|
|
|
'label' => 'form.label_number', |
|
95
|
|
|
'required' => true, |
|
96
|
|
|
]], |
|
97
|
|
|
['context', ChoiceType::class, [ |
|
98
|
|
|
'required' => true, |
|
99
|
|
|
'label' => 'form.label_context', |
|
100
|
|
|
'choices' => $contextChoices, |
|
101
|
|
|
]], |
|
102
|
|
|
['mode', ChoiceType::class, [ |
|
103
|
|
|
'label' => 'form.label_mode', |
|
104
|
|
|
'choices' => [ |
|
105
|
|
|
'public' => 'form.label_mode_public', |
|
106
|
|
|
'admin' => 'form.label_mode_admin', |
|
107
|
|
|
], |
|
108
|
|
|
]], |
|
109
|
|
|
['order', ChoiceType::class, [ |
|
110
|
|
|
'label' => 'form.label_order', |
|
111
|
|
|
'choices' => [ |
|
112
|
|
|
'name' => 'form.label_order_name', |
|
113
|
|
|
'createdAt' => 'form.label_order_created_at', |
|
114
|
|
|
'updatedAt' => 'form.label_order_updated_at', |
|
115
|
|
|
], |
|
116
|
|
|
]], |
|
117
|
|
|
['sort', ChoiceType::class, [ |
|
118
|
|
|
'label' => 'form.label_sort', |
|
119
|
|
|
'choices' => [ |
|
120
|
|
|
'desc' => 'form.label_sort_desc', |
|
121
|
|
|
'asc' => 'form.label_sort_asc', |
|
122
|
|
|
], |
|
123
|
|
|
]], |
|
124
|
|
|
], |
|
125
|
|
|
'translation_domain' => 'SonataMediaBundle', |
|
126
|
|
|
]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
|
|
public function execute(BlockContextInterface $blockContext, ?Response $response = null) |
|
133
|
|
|
{ |
|
134
|
|
|
$context = $blockContext->getBlock()->getSetting('context'); |
|
135
|
|
|
|
|
136
|
|
|
$criteria = [ |
|
137
|
|
|
'mode' => $blockContext->getSetting('mode'), |
|
138
|
|
|
'context' => $context, |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
$order = [ |
|
142
|
|
|
$blockContext->getSetting('order') => $blockContext->getSetting('sort'), |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
|
|
return $this->renderResponse($blockContext->getTemplate(), [ |
|
146
|
|
|
'context' => $blockContext, |
|
147
|
|
|
'settings' => $blockContext->getSettings(), |
|
148
|
|
|
'block' => $blockContext->getBlock(), |
|
149
|
|
|
'pager' => $this->galleryManager->getPager( |
|
150
|
|
|
$criteria, |
|
151
|
|
|
1, |
|
152
|
|
|
$blockContext->getSetting('number'), |
|
153
|
|
|
$order |
|
154
|
|
|
), |
|
155
|
|
|
], $response); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
*/ |
|
161
|
|
|
public function configureSettings(OptionsResolver $resolver): void |
|
162
|
|
|
{ |
|
163
|
|
|
$resolver->setDefaults([ |
|
164
|
|
|
'number' => 15, |
|
165
|
|
|
'mode' => 'public', |
|
166
|
|
|
'order' => 'createdAt', |
|
167
|
|
|
'sort' => 'desc', |
|
168
|
|
|
'context' => false, |
|
169
|
|
|
'title' => null, |
|
170
|
|
|
'translation_domain' => null, |
|
171
|
|
|
'icon' => 'fa fa-images', |
|
172
|
|
|
'class' => null, |
|
173
|
|
|
'template' => '@SonataMedia/Block/block_gallery_list.html.twig', |
|
174
|
|
|
]); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* NEXT_MAJOR: Remove this method. |
|
179
|
|
|
* |
|
180
|
|
|
* @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. You should use |
|
181
|
|
|
* `Sonata\BlockBundle\Block\Service\EditableBlockService` interface instead. |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getBlockMetadata($code = null) |
|
184
|
|
|
{ |
|
185
|
|
|
return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataMediaBundle', [ |
|
186
|
|
|
'class' => 'fa fa-picture-o', |
|
187
|
|
|
]); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* NEXT_MAJOR: Remove this method. |
|
192
|
|
|
* |
|
193
|
|
|
* @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. You should use |
|
194
|
|
|
* `Sonata\BlockBundle\Block\Service\EditableBlockService` interface instead. |
|
195
|
|
|
*/ |
|
196
|
|
|
public function buildCreateForm(FormMapper $formMapper, BlockInterface $block) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->buildEditForm($formMapper, $block); |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* NEXT_MAJOR: Remove this method. |
|
203
|
|
|
* |
|
204
|
|
|
* @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. |
|
205
|
|
|
*/ |
|
206
|
|
|
public function prePersist(BlockInterface $block) |
|
|
|
|
|
|
207
|
|
|
{ |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* NEXT_MAJOR: Remove this method. |
|
212
|
|
|
* |
|
213
|
|
|
* @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. |
|
214
|
|
|
*/ |
|
215
|
|
|
public function postPersist(BlockInterface $block) |
|
|
|
|
|
|
216
|
|
|
{ |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* NEXT_MAJOR: Remove this method. |
|
221
|
|
|
* |
|
222
|
|
|
* @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. |
|
223
|
|
|
*/ |
|
224
|
|
|
public function preUpdate(BlockInterface $block) |
|
|
|
|
|
|
225
|
|
|
{ |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* NEXT_MAJOR: Remove this method. |
|
230
|
|
|
* |
|
231
|
|
|
* @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. |
|
232
|
|
|
*/ |
|
233
|
|
|
public function postUpdate(BlockInterface $block) |
|
|
|
|
|
|
234
|
|
|
{ |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* NEXT_MAJOR: Remove this method. |
|
239
|
|
|
* |
|
240
|
|
|
* @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. |
|
241
|
|
|
*/ |
|
242
|
|
|
public function preRemove(BlockInterface $block) |
|
|
|
|
|
|
243
|
|
|
{ |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* NEXT_MAJOR: Remove this method. |
|
248
|
|
|
* |
|
249
|
|
|
* @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. |
|
250
|
|
|
*/ |
|
251
|
|
|
public function postRemove(BlockInterface $block) |
|
|
|
|
|
|
252
|
|
|
{ |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* NEXT_MAJOR: Remove this method. |
|
257
|
|
|
* |
|
258
|
|
|
* @deprecated since sonata-project/media-bundle 3.25, to be removed in 4.0. You should use |
|
259
|
|
|
* `Sonata\BlockBundle\Block\Service\EditableBlockService` interface instead. |
|
260
|
|
|
*/ |
|
261
|
|
|
public function validateBlock(ErrorElement $errorElement, BlockInterface $block) |
|
|
|
|
|
|
262
|
|
|
{ |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.