<?php
namespace App\Controller\Admin;
use App\Entity\Admin\Editor\Block;
use App\Entity\Magazine\Magazine;
use App\Entity\Magazine\MagazinePage;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
use App\Entity\Admin\Settings;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class EditorBlockAdminController extends Controller
{
/**
* @Route("/admin/editor/block/store")
* @throws \Sonata\AdminBundle\Exception\ModelManagerException
*/
public function editorBlockStoreAction()
{
$request = $this->getRequest();
/** @var EntityManager $em */
$em = $this->get('doctrine.orm.entity_manager');
//dump($request->get('data'));die;
$magazine_id = $request->get('magazine');
$mrmagzBlockId = $request->get('id');
$data = $request->get('data');
if ($mrmagzBlockId)
$block = $em->getRepository(Block::class)->find($mrmagzBlockId);
else
$block = new Block();
$block->setHtml($data['mrmagz-html']);
$block->setCss($data['mrmagz-css']);
$block->setLabel($data['mrmagz-label']);
$block->setTitle($data['mrmagz-title']);
// $block->setCategory($data['mrmagz-category']);
$block->setClass($data['mrmagz-bclass']);
switch ($data['mrmagz-asso']) {
case 'Global':
$block->setGlobal(true);
$block->setCategory('Global');
break;
case 'Magazine':
$magazine = $em->getRepository(Magazine::class)->find($magazine_id);
$block->addMagazine($magazine);
$block->setCategory('Magazine');
break;
case 'Publisher':
$magazine = $em->getRepository(Magazine::class)->find($magazine_id);
$publisher = $magazine->getPublisher();
$block->addPublisher($publisher);
$block->setCategory('Publisher');
break;
}
$this->admin->getModelManager()->update($block);
return new JsonResponse(['id' => $block->getId()]);
}
/**
* @Route("/admin/editor/block/load")
* @throws \Sonata\AdminBundle\Exception\ModelManagerException
*/
public function editorBlockLoadAction()
{
$request = $this->getRequest();
/** @var EntityManager $em */
$em = $this->get('doctrine.orm.entity_manager');
$magazine_id = $request->get('magazine');
$magazine = $em->getRepository(Magazine::class)->find($magazine_id);
$publisher = $magazine->getPublisher();
$allBlocks = $em->getRepository(Block::class)->findAllIn(1, $magazine->getId(), $publisher->getId());
$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer();
$normalizer->setIgnoredAttributes([
'updatedAt', 'user', 'createdAt', 'publishers', 'magazines'
]);
$normalizer->setCallbacks([
]);
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
$serializer = new Serializer(array($normalizer), array($encoder));
return $this->json($serializer->normalize($allBlocks, 'json'));
}
/**
* @Route("/admin/editor/block/delete")
* @throws \Sonata\AdminBundle\Exception\ModelManagerException
*/
public function editorBlockDeleteAction()
{
$request = $this->getRequest();
/** @var EntityManager $em */
$em = $this->get('doctrine.orm.entity_manager');
$mrmagzBlockId = $request->get('id');
$block = $em->getRepository(Block::class)->find($mrmagzBlockId);
if(!$block) {
return $this->json(['message' => 'Block not found'], Response::HTTP_NOT_FOUND);
}
$this->admin->getModelManager()->delete($block);
return new JsonResponse();
}
}