|
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\Controller\Api; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\RestBundle\Context\Context; |
|
15
|
|
|
use FOS\RestBundle\Controller\Annotations\QueryParam; |
|
16
|
|
|
use FOS\RestBundle\Controller\Annotations\View; |
|
17
|
|
|
use FOS\RestBundle\Request\ParamFetcherInterface; |
|
18
|
|
|
use FOS\RestBundle\View\View as FOSRestView; |
|
19
|
|
|
use JMS\Serializer\SerializationContext; |
|
20
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
21
|
|
|
use Sonata\DatagridBundle\Pager\PagerInterface; |
|
22
|
|
|
use Sonata\MediaBundle\Model\GalleryInterface; |
|
23
|
|
|
use Sonata\MediaBundle\Model\GalleryItemInterface; |
|
24
|
|
|
use Sonata\MediaBundle\Model\GalleryManagerInterface; |
|
25
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
|
26
|
|
|
use Sonata\MediaBundle\Model\MediaManagerInterface; |
|
27
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
28
|
|
|
use Symfony\Component\Form\FormInterface; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
30
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @author Hugo Briand <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
class GalleryController |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var GalleryManagerInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $galleryManager; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var MediaManagerInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $mediaManager; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var FormFactoryInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $formFactory; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $galleryItemClass; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Constructor. |
|
59
|
|
|
* |
|
60
|
|
|
* @param GalleryManagerInterface $galleryManager |
|
61
|
|
|
* @param MediaManagerInterface $mediaManager |
|
62
|
|
|
* @param FormFactoryInterface $formFactory |
|
63
|
|
|
* @param string $galleryItemClass |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct(GalleryManagerInterface $galleryManager, MediaManagerInterface $mediaManager, FormFactoryInterface $formFactory, $galleryItemClass) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->galleryManager = $galleryManager; |
|
68
|
|
|
$this->mediaManager = $mediaManager; |
|
69
|
|
|
$this->formFactory = $formFactory; |
|
70
|
|
|
$this->galleryItemClass = $galleryItemClass; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Retrieves the list of galleries (paginated). |
|
75
|
|
|
* |
|
76
|
|
|
* @ApiDoc( |
|
77
|
|
|
* resource=true, |
|
78
|
|
|
* output={"class"="Sonata\DatagridBundle\Pager\PagerInterface", "groups"={"sonata_api_read"}} |
|
79
|
|
|
* ) |
|
80
|
|
|
* |
|
81
|
|
|
* @QueryParam( |
|
82
|
|
|
* name="page", |
|
83
|
|
|
* requirements="\d+", |
|
84
|
|
|
* default="1", |
|
85
|
|
|
* description="Page for gallery list pagination" |
|
86
|
|
|
* ) |
|
87
|
|
|
* @QueryParam( |
|
88
|
|
|
* name="count", |
|
89
|
|
|
* requirements="\d+", |
|
90
|
|
|
* default="10", |
|
91
|
|
|
* description="Number of galleries by page" |
|
92
|
|
|
* ) |
|
93
|
|
|
* @QueryParam( |
|
94
|
|
|
* name="enabled", |
|
95
|
|
|
* requirements="0|1", |
|
96
|
|
|
* nullable=true, |
|
97
|
|
|
* strict=true, |
|
98
|
|
|
* description="Enabled/Disabled galleries filter" |
|
99
|
|
|
* ) |
|
100
|
|
|
* |
|
101
|
|
|
* @View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
|
102
|
|
|
* |
|
103
|
|
|
* @param ParamFetcherInterface $paramFetcher |
|
104
|
|
|
* |
|
105
|
|
|
* @return PagerInterface |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getGalleriesAction(ParamFetcherInterface $paramFetcher) |
|
108
|
|
|
{ |
|
109
|
|
|
$orderByQueryParam = new QueryParam(); |
|
110
|
|
|
$orderByQueryParam->name = 'orderBy'; |
|
111
|
|
|
$orderByQueryParam->requirements = 'ASC|DESC'; |
|
112
|
|
|
$orderByQueryParam->nullable = true; |
|
113
|
|
|
$orderByQueryParam->strict = true; |
|
114
|
|
|
$orderByQueryParam->description = 'Query groups order by clause (key is field, value is direction)'; |
|
115
|
|
|
if (property_exists($orderByQueryParam, 'map')) { |
|
116
|
|
|
$orderByQueryParam->map = true; |
|
117
|
|
|
} else { |
|
118
|
|
|
$orderByQueryParam->array = true; |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$paramFetcher->addParam($orderByQueryParam); |
|
122
|
|
|
|
|
123
|
|
|
$supportedCriteria = array( |
|
124
|
|
|
'enabled' => '', |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$page = $paramFetcher->get('page'); |
|
128
|
|
|
$limit = $paramFetcher->get('count'); |
|
129
|
|
|
$sort = $paramFetcher->get('orderBy'); |
|
130
|
|
|
$criteria = array_intersect_key($paramFetcher->all(), $supportedCriteria); |
|
131
|
|
|
|
|
132
|
|
|
foreach ($criteria as $key => $value) { |
|
133
|
|
|
if (null === $value) { |
|
134
|
|
|
unset($criteria[$key]); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if (!$sort) { |
|
139
|
|
|
$sort = array(); |
|
140
|
|
|
} elseif (!is_array($sort)) { |
|
141
|
|
|
$sort = array($sort => 'asc'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this->getGalleryManager()->getPager($criteria, $page, $limit, $sort); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Retrieves a specific gallery. |
|
149
|
|
|
* |
|
150
|
|
|
* @ApiDoc( |
|
151
|
|
|
* requirements={ |
|
152
|
|
|
* {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="gallery id"} |
|
153
|
|
|
* }, |
|
154
|
|
|
* output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}}, |
|
155
|
|
|
* statusCodes={ |
|
156
|
|
|
* 200="Returned when successful", |
|
157
|
|
|
* 404="Returned when gallery is not found" |
|
158
|
|
|
* } |
|
159
|
|
|
* ) |
|
160
|
|
|
* |
|
161
|
|
|
* @View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
|
162
|
|
|
* |
|
163
|
|
|
* @param $id |
|
164
|
|
|
* |
|
165
|
|
|
* @return GalleryInterface |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getGalleryAction($id) |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->getGallery($id); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Retrieves the medias of specified gallery. |
|
174
|
|
|
* |
|
175
|
|
|
* @ApiDoc( |
|
176
|
|
|
* requirements={ |
|
177
|
|
|
* {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="gallery id"} |
|
178
|
|
|
* }, |
|
179
|
|
|
* output={"class"="Sonata\MediaBundle\Model\Media", "groups"={"sonata_api_read"}}, |
|
180
|
|
|
* statusCodes={ |
|
181
|
|
|
* 200="Returned when successful", |
|
182
|
|
|
* 404="Returned when gallery is not found" |
|
183
|
|
|
* } |
|
184
|
|
|
* ) |
|
185
|
|
|
* |
|
186
|
|
|
* @View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
|
187
|
|
|
* |
|
188
|
|
|
* @param $id |
|
189
|
|
|
* |
|
190
|
|
|
* @return MediaInterface[] |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getGalleryMediasAction($id) |
|
193
|
|
|
{ |
|
194
|
|
|
$galleryItems = $this->getGallery($id)->getGalleryItems(); |
|
195
|
|
|
|
|
196
|
|
|
$media = array(); |
|
197
|
|
|
foreach ($galleryItems as $galleryItem) { |
|
198
|
|
|
$media[] = $galleryItem->getMedia(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $media; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Retrieves the gallery items of specified gallery. |
|
206
|
|
|
* |
|
207
|
|
|
* @ApiDoc( |
|
208
|
|
|
* requirements={ |
|
209
|
|
|
* {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="gallery id"} |
|
210
|
|
|
* }, |
|
211
|
|
|
* output={"class"="Sonata\MediaBundle\Model\GalleryItem", "groups"={"sonata_api_read"}}, |
|
212
|
|
|
* statusCodes={ |
|
213
|
|
|
* 200="Returned when successful", |
|
214
|
|
|
* 404="Returned when gallery is not found" |
|
215
|
|
|
* } |
|
216
|
|
|
* ) |
|
217
|
|
|
* |
|
218
|
|
|
* @View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
|
219
|
|
|
* |
|
220
|
|
|
* @param $id |
|
221
|
|
|
* |
|
222
|
|
|
* @return GalleryItemInterface[] |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getGalleryGalleryItemAction($id) |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->getGallery($id)->getGalleryItems(); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Adds a gallery. |
|
231
|
|
|
* |
|
232
|
|
|
* @ApiDoc( |
|
233
|
|
|
* input={"class"="sonata_media_api_form_gallery", "name"="", "groups"={"sonata_api_write"}}, |
|
234
|
|
|
* output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}}, |
|
235
|
|
|
* statusCodes={ |
|
236
|
|
|
* 200="Returned when successful", |
|
237
|
|
|
* 400="Returned when an error has occurred while gallery creation", |
|
238
|
|
|
* } |
|
239
|
|
|
* ) |
|
240
|
|
|
* |
|
241
|
|
|
* @param Request $request A Symfony request |
|
242
|
|
|
* |
|
243
|
|
|
* @return GalleryInterface |
|
244
|
|
|
* |
|
245
|
|
|
* @throws NotFoundHttpException |
|
246
|
|
|
*/ |
|
247
|
|
|
public function postGalleryAction(Request $request) |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->handleWriteGallery($request); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Updates a gallery. |
|
254
|
|
|
* |
|
255
|
|
|
* @ApiDoc( |
|
256
|
|
|
* requirements={ |
|
257
|
|
|
* {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="gallery identifier"} |
|
258
|
|
|
* }, |
|
259
|
|
|
* input={"class"="sonata_media_api_form_gallery", "name"="", "groups"={"sonata_api_write"}}, |
|
260
|
|
|
* output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}}, |
|
261
|
|
|
* statusCodes={ |
|
262
|
|
|
* 200="Returned when successful", |
|
263
|
|
|
* 400="Returned when an error has occurred while gallery creation", |
|
264
|
|
|
* 404="Returned when unable to find gallery" |
|
265
|
|
|
* } |
|
266
|
|
|
* ) |
|
267
|
|
|
* |
|
268
|
|
|
* @param int $id User id |
|
269
|
|
|
* @param Request $request A Symfony request |
|
270
|
|
|
* |
|
271
|
|
|
* @return GalleryInterface |
|
272
|
|
|
* |
|
273
|
|
|
* @throws NotFoundHttpException |
|
274
|
|
|
*/ |
|
275
|
|
|
public function putGalleryAction($id, Request $request) |
|
276
|
|
|
{ |
|
277
|
|
|
return $this->handleWriteGallery($request, $id); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Adds a media to a gallery. |
|
282
|
|
|
* |
|
283
|
|
|
* @ApiDoc( |
|
284
|
|
|
* requirements={ |
|
285
|
|
|
* {"name"="galleryId", "dataType"="integer", "requirement"="\d+", "description"="gallery identifier"}, |
|
286
|
|
|
* {"name"="mediaId", "dataType"="integer", "requirement"="\d+", "description"="media identifier"} |
|
287
|
|
|
* }, |
|
288
|
|
|
* input={"class"="sonata_media_api_form_gallery_item", "name"="", "groups"={"sonata_api_write"}}, |
|
289
|
|
|
* output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}}, |
|
290
|
|
|
* statusCodes={ |
|
291
|
|
|
* 200="Returned when successful", |
|
292
|
|
|
* 400="Returned when an error has occurred while gallery/media attachment", |
|
293
|
|
|
* } |
|
294
|
|
|
* ) |
|
295
|
|
|
* |
|
296
|
|
|
* @param int $galleryId A gallery identifier |
|
297
|
|
|
* @param int $mediaId A media identifier |
|
298
|
|
|
* @param Request $request A Symfony request |
|
299
|
|
|
* |
|
300
|
|
|
* @return GalleryInterface |
|
301
|
|
|
* |
|
302
|
|
|
* @throws NotFoundHttpException |
|
303
|
|
|
*/ |
|
304
|
|
|
public function postGalleryMediaGalleryItemAction($galleryId, $mediaId, Request $request) |
|
305
|
|
|
{ |
|
306
|
|
|
$gallery = $this->getGallery($galleryId); |
|
307
|
|
|
$media = $this->getMedia($mediaId); |
|
308
|
|
|
|
|
309
|
|
|
foreach ($gallery->getGalleryItems() as $galleryItem) { |
|
310
|
|
|
if ($galleryItem->getMedia()->getId() == $media->getId()) { |
|
311
|
|
|
return FOSRestView::create(array( |
|
312
|
|
|
'error' => sprintf('Gallery "%s" already has media "%s"', $galleryId, $mediaId), |
|
313
|
|
|
), 400); |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
return $this->handleWriteGalleryItem($gallery, $media, null, $request); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Updates a media to a gallery. |
|
322
|
|
|
* |
|
323
|
|
|
* @ApiDoc( |
|
324
|
|
|
* requirements={ |
|
325
|
|
|
* {"name"="galleryId", "dataType"="integer", "requirement"="\d+", "description"="gallery identifier"}, |
|
326
|
|
|
* {"name"="mediaId", "dataType"="integer", "requirement"="\d+", "description"="media identifier"} |
|
327
|
|
|
* }, |
|
328
|
|
|
* input={"class"="sonata_media_api_form_gallery_item", "name"="", "groups"={"sonata_api_write"}}, |
|
329
|
|
|
* output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}}, |
|
330
|
|
|
* statusCodes={ |
|
331
|
|
|
* 200="Returned when successful", |
|
332
|
|
|
* 404="Returned when an error if media cannot be found in gallery", |
|
333
|
|
|
* } |
|
334
|
|
|
* ) |
|
335
|
|
|
* |
|
336
|
|
|
* @param int $galleryId A gallery identifier |
|
337
|
|
|
* @param int $mediaId A media identifier |
|
338
|
|
|
* @param Request $request A Symfony request |
|
339
|
|
|
* |
|
340
|
|
|
* @return GalleryInterface |
|
341
|
|
|
* |
|
342
|
|
|
* @throws NotFoundHttpException |
|
343
|
|
|
*/ |
|
344
|
|
|
public function putGalleryMediaGalleryItemAction($galleryId, $mediaId, Request $request) |
|
345
|
|
|
{ |
|
346
|
|
|
$gallery = $this->getGallery($galleryId); |
|
347
|
|
|
$media = $this->getMedia($mediaId); |
|
348
|
|
|
|
|
349
|
|
|
foreach ($gallery->getGalleryItems() as $galleryItem) { |
|
350
|
|
|
if ($galleryItem->getMedia()->getId() == $media->getId()) { |
|
351
|
|
|
return $this->handleWriteGalleryItem($gallery, $media, $galleryItem, $request); |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
throw new NotFoundHttpException(sprintf('Gallery "%s" does not have media "%s"', $galleryId, $mediaId)); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Deletes a media association to a gallery. |
|
360
|
|
|
* |
|
361
|
|
|
* @ApiDoc( |
|
362
|
|
|
* requirements={ |
|
363
|
|
|
* {"name"="galleryId", "dataType"="integer", "requirement"="\d+", "description"="gallery identifier"}, |
|
364
|
|
|
* {"name"="mediaId", "dataType"="integer", "requirement"="\d+", "description"="media identifier"} |
|
365
|
|
|
* }, |
|
366
|
|
|
* statusCodes={ |
|
367
|
|
|
* 200="Returned when media is successfully deleted from gallery", |
|
368
|
|
|
* 400="Returned when an error has occurred while media deletion of gallery", |
|
369
|
|
|
* 404="Returned when unable to find gallery or media" |
|
370
|
|
|
* } |
|
371
|
|
|
* ) |
|
372
|
|
|
* |
|
373
|
|
|
* @param int $galleryId A gallery identifier |
|
374
|
|
|
* @param int $mediaId A media identifier |
|
375
|
|
|
* |
|
376
|
|
|
* @return View |
|
377
|
|
|
* |
|
378
|
|
|
* @throws NotFoundHttpException |
|
379
|
|
|
*/ |
|
380
|
|
|
public function deleteGalleryMediaGalleryItemAction($galleryId, $mediaId) |
|
381
|
|
|
{ |
|
382
|
|
|
$gallery = $this->getGallery($galleryId); |
|
383
|
|
|
$media = $this->getMedia($mediaId); |
|
384
|
|
|
|
|
385
|
|
|
foreach ($gallery->getGalleryItems() as $key => $galleryItem) { |
|
386
|
|
|
if ($galleryItem->getMedia()->getId() == $media->getId()) { |
|
387
|
|
|
$gallery->getGalleryItems()->remove($key); |
|
|
|
|
|
|
388
|
|
|
$this->getGalleryManager()->save($gallery); |
|
389
|
|
|
|
|
390
|
|
|
return array('deleted' => true); |
|
|
|
|
|
|
391
|
|
|
} |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
return FOSRestView::create(array( |
|
395
|
|
|
'error' => sprintf('Gallery "%s" does not have media "%s" associated', $galleryId, $mediaId), |
|
396
|
|
|
), 400); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* Deletes a gallery. |
|
401
|
|
|
* |
|
402
|
|
|
* @ApiDoc( |
|
403
|
|
|
* requirements={ |
|
404
|
|
|
* {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="gallery identifier"} |
|
405
|
|
|
* }, |
|
406
|
|
|
* statusCodes={ |
|
407
|
|
|
* 200="Returned when gallery is successfully deleted", |
|
408
|
|
|
* 400="Returned when an error has occurred while gallery deletion", |
|
409
|
|
|
* 404="Returned when unable to find gallery" |
|
410
|
|
|
* } |
|
411
|
|
|
* ) |
|
412
|
|
|
* |
|
413
|
|
|
* @param int $id A Gallery identifier |
|
414
|
|
|
* |
|
415
|
|
|
* @return View |
|
416
|
|
|
* |
|
417
|
|
|
* @throws NotFoundHttpException |
|
418
|
|
|
*/ |
|
419
|
|
|
public function deleteGalleryAction($id) |
|
420
|
|
|
{ |
|
421
|
|
|
$gallery = $this->getGallery($id); |
|
422
|
|
|
|
|
423
|
|
|
$this->galleryManager->delete($gallery); |
|
424
|
|
|
|
|
425
|
|
|
return array('deleted' => true); |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* Write a GalleryItem, this method is used by both POST and PUT action methods. |
|
430
|
|
|
* |
|
431
|
|
|
* @param GalleryInterface $gallery |
|
432
|
|
|
* @param MediaInterface $media |
|
433
|
|
|
* @param GalleryItemInterface $galleryItem |
|
434
|
|
|
* @param Request $request |
|
435
|
|
|
* |
|
436
|
|
|
* @return FormInterface |
|
437
|
|
|
*/ |
|
438
|
|
|
protected function handleWriteGalleryItem(GalleryInterface $gallery, MediaInterface $media, GalleryItemInterface $galleryItem = null, Request $request) |
|
439
|
|
|
{ |
|
440
|
|
|
$form = $this->formFactory->createNamed(null, 'sonata_media_api_form_gallery_item', $galleryItem, array( |
|
441
|
|
|
'csrf_protection' => false, |
|
442
|
|
|
)); |
|
443
|
|
|
|
|
444
|
|
|
$form->handleRequest($request); |
|
445
|
|
|
|
|
446
|
|
|
if ($form->isValid()) { |
|
447
|
|
|
$galleryItem = $form->getData(); |
|
448
|
|
|
$galleryItem->setMedia($media); |
|
449
|
|
|
|
|
450
|
|
|
$gallery->addGalleryItem($galleryItem); |
|
451
|
|
|
$this->galleryManager->save($gallery); |
|
452
|
|
|
|
|
453
|
|
|
$view = FOSRestView::create($galleryItem); |
|
454
|
|
|
|
|
455
|
|
|
// BC for FOSRestBundle < 2.0 |
|
456
|
|
|
if (method_exists($view, 'setSerializationContext')) { |
|
457
|
|
|
$serializationContext = SerializationContext::create(); |
|
458
|
|
|
$serializationContext->setGroups(array('sonata_api_read')); |
|
459
|
|
|
$serializationContext->enableMaxDepthChecks(); |
|
460
|
|
|
$view->setSerializationContext($serializationContext); |
|
|
|
|
|
|
461
|
|
|
} else { |
|
462
|
|
|
$context = new Context(); |
|
463
|
|
|
$context->setGroups(array('sonata_api_read')); |
|
464
|
|
|
$context->setMaxDepth(0); |
|
|
|
|
|
|
465
|
|
|
$view->setContext($context); |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
return $view; |
|
|
|
|
|
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
return $form; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
/** |
|
475
|
|
|
* Retrieves gallery with id $id or throws an exception if it doesn't exist. |
|
476
|
|
|
* |
|
477
|
|
|
* @param $id |
|
478
|
|
|
* |
|
479
|
|
|
* @return GalleryInterface |
|
480
|
|
|
* |
|
481
|
|
|
* @throws NotFoundHttpException |
|
482
|
|
|
*/ |
|
483
|
|
|
protected function getGallery($id) |
|
484
|
|
|
{ |
|
485
|
|
|
$gallery = $this->getGalleryManager()->findOneBy(array('id' => $id)); |
|
486
|
|
|
|
|
487
|
|
|
if (null === $gallery) { |
|
488
|
|
|
throw new NotFoundHttpException(sprintf('Gallery (%d) not found', $id)); |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
return $gallery; |
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
|
|
/** |
|
495
|
|
|
* Retrieves media with id $id or throws an exception if it doesn't exist. |
|
496
|
|
|
* |
|
497
|
|
|
* @param $id |
|
498
|
|
|
* |
|
499
|
|
|
* @return MediaInterface |
|
500
|
|
|
* |
|
501
|
|
|
* @throws NotFoundHttpException |
|
502
|
|
|
*/ |
|
503
|
|
|
protected function getMedia($id) |
|
504
|
|
|
{ |
|
505
|
|
|
$media = $this->getMediaManager()->findOneBy(array('id' => $id)); |
|
506
|
|
|
|
|
507
|
|
|
if (null === $media) { |
|
508
|
|
|
throw new NotFoundHttpException(sprintf('Media (%d) not found', $id)); |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
return $media; |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* @return GalleryManagerInterface |
|
516
|
|
|
*/ |
|
517
|
|
|
protected function getGalleryManager() |
|
518
|
|
|
{ |
|
519
|
|
|
return $this->galleryManager; |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
|
|
/** |
|
523
|
|
|
* @return MediaManagerInterface |
|
524
|
|
|
*/ |
|
525
|
|
|
protected function getMediaManager() |
|
526
|
|
|
{ |
|
527
|
|
|
return $this->mediaManager; |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
/** |
|
531
|
|
|
* Write a Gallery, this method is used by both POST and PUT action methods. |
|
532
|
|
|
* |
|
533
|
|
|
* @param Request $request Symfony request |
|
534
|
|
|
* @param int|null $id A Gallery identifier |
|
535
|
|
|
* |
|
536
|
|
|
* @return View|FormInterface |
|
537
|
|
|
*/ |
|
538
|
|
|
protected function handleWriteGallery($request, $id = null) |
|
539
|
|
|
{ |
|
540
|
|
|
$gallery = $id ? $this->getGallery($id) : null; |
|
541
|
|
|
|
|
542
|
|
|
$form = $this->formFactory->createNamed(null, 'sonata_media_api_form_gallery', $gallery, array( |
|
543
|
|
|
'csrf_protection' => false, |
|
544
|
|
|
)); |
|
545
|
|
|
|
|
546
|
|
|
$form->handleRequest($request); |
|
547
|
|
|
|
|
548
|
|
|
if ($form->isValid()) { |
|
549
|
|
|
$gallery = $form->getData(); |
|
550
|
|
|
$this->galleryManager->save($gallery); |
|
551
|
|
|
|
|
552
|
|
|
$view = FOSRestView::create($gallery); |
|
553
|
|
|
|
|
554
|
|
|
// BC for FOSRestBundle < 2.0 |
|
555
|
|
|
if (method_exists($view, 'setSerializationContext')) { |
|
556
|
|
|
$serializationContext = SerializationContext::create(); |
|
557
|
|
|
$serializationContext->setGroups(array('sonata_api_read')); |
|
558
|
|
|
$serializationContext->enableMaxDepthChecks(); |
|
559
|
|
|
$view->setSerializationContext($serializationContext); |
|
|
|
|
|
|
560
|
|
|
} else { |
|
561
|
|
|
$context = new Context(); |
|
562
|
|
|
$context->setGroups(array('sonata_api_read')); |
|
563
|
|
|
$context->setMaxDepth(0); |
|
|
|
|
|
|
564
|
|
|
$view->setContext($context); |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
return $view; |
|
568
|
|
|
} |
|
569
|
|
|
|
|
570
|
|
|
return $form; |
|
571
|
|
|
} |
|
572
|
|
|
} |
|
573
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.