/**
  * Create a media folder
  * and if a parent is provided added has its last child
  *
  * @param MediaFolder $mediaFolder
  *
  * @Rest\RequestParam(name="title", description="media title", requirements={
  *   @Assert\NotBlank()
  * })
  * @ParamConverter(
  *   name="parent", id_name="parent_uid", id_source="request", class="BackBee\NestedNode\MediaFolder", required=false
  * )
  * @Rest\Security("is_fully_authenticated() & has_role('ROLE_API_USER') & is_granted('CREATE', '\BackBee\NestedNode\MediaFolder')")
  */
 public function postAction(Request $request, $parent = null)
 {
     try {
         $title = trim($request->request->get('title'));
         $uid = $request->request->get('uid', null);
         if (null !== $uid) {
             $mediaFolder = $this->getMediaFolderRepository()->find($uid);
             $mediaFolder->setTitle($title);
         } else {
             $mediaFolder = new MediaFolder();
             $mediaFolder->setUrl($request->request->get('url', StringUtils::urlize($title)));
             $mediaFolder->setTitle($title);
             if (null === $parent) {
                 $parent = $this->getMediaFolderRepository()->getRoot();
             }
             if ($this->mediaFolderAlreadyExists($title, $parent)) {
                 throw new BadRequestHttpException(sprintf('A MediaFolder named `%s` already exists in `%s`.', $title, $parent->getTitle()));
             }
             $mediaFolder->setParent($parent);
             $this->getMediaFolderRepository()->insertNodeAsLastChildOf($mediaFolder, $parent);
         }
         $this->getEntityManager()->persist($mediaFolder);
         $this->getEntityManager()->flush();
         $response = $this->createJsonResponse(null, 201, ['BB-RESOURCE-UID' => $mediaFolder->getUid(), 'Location' => $this->getApplication()->getRouting()->getUrlByRouteName('bb.rest.media-folder.get', ['version' => $request->attributes->get('version'), 'uid' => $mediaFolder->getUid()], '', false)]);
     } catch (\Exception $e) {
         $response = $this->createResponse(sprintf('Internal server error: %s', $e->getMessage()));
     }
     return $response;
 }
Пример #2
0
 /**
  * @covers BackBee\NestedNode\MediaFolder::getUrl
  * @covers BackBee\NestedNode\MediaFolder::setUrl
  */
 public function testGetAndSetUrl()
 {
     $this->assertEquals($this->folder, $this->folder->setUrl('new url'));
     $this->assertEquals('new url', $this->folder->getUrl());
 }