Skip to content

Commit

Permalink
renamed GalleryHasMedia to GalleryItem (sonata-project#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark authored and Virgile Vivier committed Jun 27, 2016
1 parent 8d0403e commit c59febf
Show file tree
Hide file tree
Showing 61 changed files with 364 additions and 277 deletions.
8 changes: 4 additions & 4 deletions Admin/GalleryAdmin.php
Expand Up @@ -47,7 +47,7 @@ public function prePersist($gallery)
$gallery->setContext($parameters['context']);

// fix weird bug with setter object not being call
$gallery->setGalleryHasMedias($gallery->getGalleryHasMedias());
$gallery->setGalleryItems($gallery->getGalleryItems());
}

/**
Expand All @@ -56,7 +56,7 @@ public function prePersist($gallery)
public function preUpdate($gallery)
{
// fix weird bug with setter object not being call
$gallery->setGalleryHasMedias($gallery->getGalleryHasMedias());
$gallery->setGalleryItems($gallery->getGalleryItems());
}

/**
Expand Down Expand Up @@ -127,14 +127,14 @@ protected function configureFormFields(FormMapper $formMapper)
->add('defaultFormat', 'choice', array('choices' => $formats))
->end()
->with('Gallery')
->add('galleryHasMedias', 'sonata_type_collection', array(
->add('galleryItems', 'sonata_type_collection', array(
'cascade_validation' => true,
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'link_parameters' => array('context' => $context),
'admin_code' => 'sonata.media.admin.gallery_has_media',
'admin_code' => 'sonata.media.admin.gallery_item',
)
)
->end()
Expand Down
Expand Up @@ -15,7 +15,7 @@
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;

class GalleryHasMediaAdmin extends AbstractAdmin
class GalleryItemAdmin extends AbstractAdmin
{
/**
* {@inheritdoc}
Expand Down
12 changes: 6 additions & 6 deletions Block/GalleryBlockService.php
Expand Up @@ -226,22 +226,22 @@ public function getBlockMetadata($code = null)
private function buildElements(GalleryInterface $gallery)
{
$elements = array();
foreach ($gallery->getGalleryHasMedias() as $galleryHasMedia) {
if (!$galleryHasMedia->getEnabled()) {
foreach ($gallery->getGalleryItems() as $galleryItem) {
if (!$galleryItem->getEnabled()) {
continue;
}

$type = $this->getMediaType($galleryHasMedia->getMedia());
$type = $this->getMediaType($galleryItem->getMedia());

if (!$type) {
continue;
}

$elements[] = array(
'title' => $galleryHasMedia->getMedia()->getName(),
'caption' => $galleryHasMedia->getMedia()->getDescription(),
'title' => $galleryItem->getMedia()->getName(),
'caption' => $galleryItem->getMedia()->getDescription(),
'type' => $type,
'media' => $galleryHasMedia->getMedia(),
'media' => $galleryItem->getMedia(),
);
}

Expand Down
107 changes: 65 additions & 42 deletions Controller/Api/GalleryController.php
Expand Up @@ -18,8 +18,8 @@
use JMS\Serializer\SerializationContext;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Sonata\DatagridBundle\Pager\PagerInterface;
use Sonata\MediaBundle\Model\GalleryHasMediaInterface;
use Sonata\MediaBundle\Model\GalleryInterface;
use Sonata\MediaBundle\Model\GalleryItemInterface;
use Sonata\MediaBundle\Model\GalleryManagerInterface;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Model\MediaManagerInterface;
Expand Down Expand Up @@ -53,22 +53,22 @@ class GalleryController
/**
* @var string
*/
protected $galleryHasMediaClass;
protected $galleryItemClass;

/**
* Constructor.
*
* @param GalleryManagerInterface $galleryManager
* @param MediaManagerInterface $mediaManager
* @param FormFactoryInterface $formFactory
* @param string $galleryHasMediaClass
* @param string $galleryItemClass
*/
public function __construct(GalleryManagerInterface $galleryManager, MediaManagerInterface $mediaManager, FormFactoryInterface $formFactory, $galleryHasMediaClass)
public function __construct(GalleryManagerInterface $galleryManager, MediaManagerInterface $mediaManager, FormFactoryInterface $formFactory, $galleryItemClass)
{
$this->galleryManager = $galleryManager;
$this->mediaManager = $mediaManager;
$this->formFactory = $formFactory;
$this->galleryHasMediaClass = $galleryHasMediaClass;
$this->galleryItemClass = $galleryItemClass;
}

/**
Expand All @@ -79,10 +79,33 @@ public function __construct(GalleryManagerInterface $galleryManager, MediaManage
* output={"class"="Sonata\DatagridBundle\Pager\PagerInterface", "groups"="sonata_api_read"}
* )
*
* @QueryParam(name="page", requirements="\d+", default="1", description="Page for gallery list pagination")
* @QueryParam(name="count", requirements="\d+", default="10", description="Number of galleries by page")
* @QueryParam(name="enabled", requirements="0|1", nullable=true, strict=true, description="Enabled/Disabled galleries filter")
* @QueryParam(name="orderBy", array=true, requirements="ASC|DESC", nullable=true, strict=true, description="Order by array (key is field, value is direction)")
* @QueryParam(
* name="page",
* requirements="\d+",
* default="1",
* description="Page for gallery list pagination"
* )
* @QueryParam(
* name="count",
* requirements="\d+",
* default="10",
* description="Number of galleries by page"
* )
* @QueryParam(
* name="enabled",
* requirements="0|1",
* nullable=true,
* strict=true,
* description="Enabled/Disabled galleries filter"
* )
* @QueryParam(
* name="orderBy",
* array=true,
* requirements="ASC|DESC",
* nullable=true,
* strict=true,
* description="Order by array (key is field, value is direction)"
* )
*
* @View(serializerGroups="sonata_api_read", serializerEnableMaxDepthChecks=true)
*
Expand Down Expand Up @@ -163,24 +186,24 @@ public function getGalleryAction($id)
*/
public function getGalleryMediasAction($id)
{
$ghms = $this->getGallery($id)->getGalleryHasMedias();
$galleryItems = $this->getGallery($id)->getGalleryItems();

$media = array();
foreach ($ghms as $ghm) {
$media[] = $ghm->getMedia();
foreach ($galleryItems as $galleryItem) {
$media[] = $galleryItem->getMedia();
}

return $media;
}

/**
* Retrieves the galleryhasmedias of specified gallery.
* Retrieves the gallery items of specified gallery.
*
* @ApiDoc(
* requirements={
* {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="gallery id"}
* },
* output={"class"="Sonata\MediaBundle\Model\GalleryHasMedia", "groups"="sonata_api_read"},
* output={"class"="Sonata\MediaBundle\Model\GalleryItem", "groups"="sonata_api_read"},
* statusCodes={
* 200="Returned when successful",
* 404="Returned when gallery is not found"
Expand All @@ -191,11 +214,11 @@ public function getGalleryMediasAction($id)
*
* @param $id
*
* @return GalleryHasMediaInterface[]
* @return GalleryItemInterface[]
*/
public function getGalleryGalleryhasmediasAction($id)
public function getGalleryGalleryItemAction($id)
{
return $this->getGallery($id)->getGalleryHasMedias();
return $this->getGallery($id)->getGalleryItems();
}

/**
Expand Down Expand Up @@ -257,7 +280,7 @@ public function putGalleryAction($id, Request $request)
* {"name"="galleryId", "dataType"="integer", "requirement"="\d+", "description"="gallery identifier"},
* {"name"="mediaId", "dataType"="integer", "requirement"="\d+", "description"="media identifier"}
* },
* input={"class"="sonata_media_api_form_gallery_has_media", "name"="", "groups"={"sonata_api_write"}},
* input={"class"="sonata_media_api_form_gallery_item", "name"="", "groups"={"sonata_api_write"}},
* output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}},
* statusCodes={
* 200="Returned when successful",
Expand All @@ -273,20 +296,20 @@ public function putGalleryAction($id, Request $request)
*
* @throws NotFoundHttpException
*/
public function postGalleryMediaGalleryhasmediaAction($galleryId, $mediaId, Request $request)
public function postGalleryMediaGalleryItemAction($galleryId, $mediaId, Request $request)
{
$gallery = $this->getGallery($galleryId);
$media = $this->getMedia($mediaId);

foreach ($gallery->getGalleryHasMedias() as $galleryHasMedia) {
if ($galleryHasMedia->getMedia()->getId() == $media->getId()) {
foreach ($gallery->getGalleryItems() as $galleryItem) {
if ($galleryItem->getMedia()->getId() == $media->getId()) {
return FOSRestView::create(array(
'error' => sprintf('Gallery "%s" already has media "%s"', $galleryId, $mediaId),
), 400);
}
}

return $this->handleWriteGalleryhasmedia($gallery, $media, null, $request);
return $this->handleWriteGalleryItem($gallery, $media, null, $request);
}

/**
Expand All @@ -297,7 +320,7 @@ public function postGalleryMediaGalleryhasmediaAction($galleryId, $mediaId, Requ
* {"name"="galleryId", "dataType"="integer", "requirement"="\d+", "description"="gallery identifier"},
* {"name"="mediaId", "dataType"="integer", "requirement"="\d+", "description"="media identifier"}
* },
* input={"class"="sonata_media_api_form_gallery_has_media", "name"="", "groups"={"sonata_api_write"}},
* input={"class"="sonata_media_api_form_gallery_item", "name"="", "groups"={"sonata_api_write"}},
* output={"class"="sonata_media_api_form_gallery", "groups"={"sonata_api_read"}},
* statusCodes={
* 200="Returned when successful",
Expand All @@ -313,14 +336,14 @@ public function postGalleryMediaGalleryhasmediaAction($galleryId, $mediaId, Requ
*
* @throws NotFoundHttpException
*/
public function putGalleryMediaGalleryhasmediaAction($galleryId, $mediaId, Request $request)
public function putGalleryMediaGalleryItemAction($galleryId, $mediaId, Request $request)
{
$gallery = $this->getGallery($galleryId);
$media = $this->getMedia($mediaId);

foreach ($gallery->getGalleryHasMedias() as $galleryHasMedia) {
if ($galleryHasMedia->getMedia()->getId() == $media->getId()) {
return $this->handleWriteGalleryhasmedia($gallery, $media, $galleryHasMedia, $request);
foreach ($gallery->getGalleryItems() as $galleryItem) {
if ($galleryItem->getMedia()->getId() == $media->getId()) {
return $this->handleWriteGalleryItem($gallery, $media, $galleryItem, $request);
}
}

Expand Down Expand Up @@ -349,14 +372,14 @@ public function putGalleryMediaGalleryhasmediaAction($galleryId, $mediaId, Reque
*
* @throws NotFoundHttpException
*/
public function deleteGalleryMediaGalleryhasmediaAction($galleryId, $mediaId)
public function deleteGalleryMediaGalleryItemAction($galleryId, $mediaId)
{
$gallery = $this->getGallery($galleryId);
$media = $this->getMedia($mediaId);

foreach ($gallery->getGalleryHasMedias() as $key => $galleryHasMedia) {
if ($galleryHasMedia->getMedia()->getId() == $media->getId()) {
$gallery->getGalleryHasMedias()->remove($key);
foreach ($gallery->getGalleryItems() as $key => $galleryItem) {
if ($galleryItem->getMedia()->getId() == $media->getId()) {
$gallery->getGalleryItems()->remove($key);
$this->getGalleryManager()->save($gallery);

return array('deleted' => true);
Expand Down Expand Up @@ -398,31 +421,31 @@ public function deleteGalleryAction($id)
}

/**
* Write a GalleryHasMedia, this method is used by both POST and PUT action methods.
* Write a GalleryItem, this method is used by both POST and PUT action methods.
*
* @param GalleryInterface $gallery
* @param MediaInterface $media
* @param GalleryHasMediaInterface $galleryHasMedia
* @param Request $request
* @param GalleryInterface $gallery
* @param MediaInterface $media
* @param GalleryItemInterface $galleryItem
* @param Request $request
*
* @return FormInterface
*/
protected function handleWriteGalleryhasmedia(GalleryInterface $gallery, MediaInterface $media, GalleryHasMediaInterface $galleryHasMedia = null, Request $request)
protected function handleWriteGalleryItem(GalleryInterface $gallery, MediaInterface $media, GalleryItemInterface $galleryItem = null, Request $request)
{
$form = $this->formFactory->createNamed(null, 'sonata_media_api_form_gallery_has_media', $galleryHasMedia, array(
$form = $this->formFactory->createNamed(null, 'sonata_media_api_form_gallery_item', $galleryItem, array(
'csrf_protection' => false,
));

$form->handleRequest($request);

if ($form->isValid()) {
$galleryHasMedia = $form->getData();
$galleryHasMedia->setMedia($media);
$galleryItem = $form->getData();
$galleryItem->setMedia($media);

$gallery->addGalleryHasMedias($galleryHasMedia);
$gallery->addGalleryItem($galleryItem);
$this->galleryManager->save($gallery);

$view = FOSRestView::create($galleryHasMedia);
$view = FOSRestView::create($galleryItem);
$serializationContext = SerializationContext::create();
$serializationContext->setGroups(array('sonata_api_read'));
$serializationContext->enableMaxDepthChecks();
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Expand Up @@ -417,7 +417,7 @@ private function addModelSection(ArrayNodeDefinition $node)
->children()
->scalarNode('media')->defaultValue('Application\\Sonata\\MediaBundle\\Entity\\Media')->end()
->scalarNode('gallery')->defaultValue('Application\\Sonata\\MediaBundle\\Entity\\Gallery')->end()
->scalarNode('gallery_has_media')->defaultValue('Application\\Sonata\\MediaBundle\\Entity\\GalleryHasMedia')->end()
->scalarNode('gallery_item')->defaultValue('Application\\Sonata\\MediaBundle\\Entity\\GalleryItem')->end()
->scalarNode('category')->defaultValue('Application\\Sonata\\ClassificationBundle\\Entity\\Category')->end()
->end()
->end()
Expand Down

0 comments on commit c59febf

Please sign in to comment.