open-orchestra/open-orchestra-media-admin-bundle

View on GitHub
MediaAdminBundle/Controller/Admin/FolderController.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php

namespace OpenOrchestra\MediaAdminBundle\Controller\Admin;

use OpenOrchestra\Backoffice\BusinessRules\Strategies\BusinessActionInterface;
use OpenOrchestra\BackofficeBundle\Controller\AbstractAdminController;
use OpenOrchestra\MediaAdmin\FolderEvents;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as Config;
use Symfony\Component\HttpFoundation\Response;
use OpenOrchestra\Backoffice\Security\ContributionActionInterface;

/**
 * Class FolderController
 */
class FolderController extends AbstractAdminController
{
    /**
     * @param Request $request
     * @param string  $folderId
     *
     * @Config\Route("/folder/form/{folderId}", name="open_orchestra_media_admin_folder_form")
     * @Config\Method({"GET", "POST"})
     *
     * @return Response
     */
    public function formAction(Request $request, $folderId)
    {
        $folderRepository = $this->get('open_orchestra_media.repository.media_folder');
        $folder = $folderRepository->find($folderId);
        $this->denyAccessUnlessGranted(ContributionActionInterface::EDIT, $folder);
        $form = $this->createForm('oo_folder', $folder, array(
            'action' => $this->generateUrl('open_orchestra_media_admin_folder_form', array('folderId' => $folderId)),
            'enable_delete_button' => $this->get('open_orchestra_backoffice.business_rules_manager')->isGranted(BusinessActionInterface::DELETE, $folder),
            'delete_button' => $this->isGranted(ContributionActionInterface::DELETE, $folder)
        ));
        $form->handleRequest($request);
        if ($this->handleForm($form, $this->get('translator')->trans('open_orchestra_media_admin.form.folder.success'), $folder)) {
            $event = $this->get("open_orchestra_media_admin.event.folder_event");
            $event->setFolder($folder);
            $this->dispatchEvent(FolderEvents::FOLDER_UPDATE, $event);
        }
        return $this->renderAdminForm($form);
    }

    /**
     * @param Request $request
     * @param string  $parentId
     *
     * @Config\Route("/folder/new", name="open_orchestra_media_admin_folder_new")
     * @Config\Route("/folder/new/{parentId}", name="open_orchestra_media_admin_folder_add")
     * @Config\Method({"GET", "POST"})
     *
     * @return Response
     */
    public function newAction(Request $request, $parentId=null)
    {
        $folderClass = $this->getParameter('open_orchestra_media.document.media_folder.class');
        $folder = new $folderClass();
        if (!is_null($parentId)) {
            $parentFolder = $this->get('open_orchestra_media.repository.media_folder')->find($parentId);
            $this->denyAccessUnlessGranted(ContributionActionInterface::CREATE, $parentFolder);
            if ($parentFolder) {
                $folder->setParent($parentFolder);
            }
        }
        $siteId = $this->get('open_orchestra_backoffice.context_backoffice_manager')->getSiteId();
        $folder->setSiteId($siteId);
        $url = $this->generateUrl('open_orchestra_media_admin_folder_new');
        if (!is_null($parentId)) {
            $url = $this->generateUrl('open_orchestra_media_admin_folder_add', array('parentId' => $parentId));
        }
        $form = $this->createForm('oo_folder', $folder, array(
            'action' => $url,
            'new_button' => true,
        ));
        $form->handleRequest($request);

        if ($form->isValid()) {
            $documentManager = $this->get('object_manager');
            $documentManager->persist($folder);
            $documentManager->flush();
            $event = $this->get("open_orchestra_media_admin.event.folder_event");
            $event->setFolder($folder);
            $this->dispatchEvent(FolderEvents::FOLDER_CREATE, $event);
            $message = $this->get('translator')->trans('open_orchestra_media_admin.form.folder.success');
            $this->get('session')->getFlashBag()->add('success', $message);

            $response = new Response(
                '',
                Response::HTTP_CREATED,
                array('Content-type' => 'text/plain; charset=utf-8', 'folderId' => $folder->getId())
            );
            return $response;
        }
        return $this->renderAdminForm($form);
    }
}