<?php
namespace App\Controller\Admin;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use WAM\Bundle\MediaBundle\Manager\Interfaces\FileManagerInterface;
final class CkeditorAdminController extends CRUDController
{
private FileManagerInterface $fileManager;
/**
* CkeditorAdminController constructor.
* @param FileManagerInterface $fileManager
*/
public function __construct(FileManagerInterface $fileManager)
{
$this->fileManager = $fileManager;
}
/**
* @throws AccessDeniedException
*/
public function browserAction(Request $request): Response
{
$this->admin->checkAccess('list');
$datagrid = $this->admin->getDatagrid();
$datagrid->setValue('providerName', null, $this->admin->getPersistentParameter('provider'));
$formView = $datagrid->getForm()->createView();
$this->setFormTheme($formView, $this->admin->getFilterTheme());
return $this->renderWithExtraParams('extensions/ckeditor/browser.html.twig', [
'action' => 'browser',
'form' => $formView,
'datagrid' => $datagrid,
]);
}
/**
* @throws AccessDeniedException
* @throws NotFoundHttpException
*/
public function uploadAction(Request $request): Response
{
$this->admin->checkAccess('create');
$files = $this->fileManager->createFromRequest($request);
if (!$request->isMethod('POST') || [] === $files) {
throw $this->createNotFoundException();
}
$media = $files[0];
$this->fileManager->processAndSave($media);
return $this->renderWithExtraParams('extensions/ckeditor/upload.html.twig', [
'action' => 'list',
'object' => $media,
]);
}
/**
* Sets the admin form theme to form view. Used for compatibility between Symfony versions.
*/
private function setFormTheme(FormView $formView, array $theme): void
{
$this->get('twig')->getRuntime(FormRenderer::class)->setTheme($formView, $theme);
}
}