src/Controller/Admin/EditorBlockAdminController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Admin\Editor\Block;
  4. use App\Entity\Magazine\Magazine;
  5. use App\Entity\Magazine\MagazinePage;
  6. use Sonata\AdminBundle\Controller\CRUDController as Controller;
  7. use App\Entity\Admin\Settings;
  8. use Symfony\Component\Filesystem\Filesystem;
  9. use Symfony\Component\Finder\Exception\AccessDeniedException;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  15. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  16. use Symfony\Component\Serializer\Serializer;
  17. class EditorBlockAdminController extends Controller
  18. {
  19.     /**
  20.      * @Route("/admin/editor/block/store")
  21.      * @throws \Sonata\AdminBundle\Exception\ModelManagerException
  22.      */
  23.     public function editorBlockStoreAction()
  24.     {
  25.         $request $this->getRequest();
  26.         /** @var EntityManager $em */
  27.         $em $this->get('doctrine.orm.entity_manager');
  28.         //dump($request->get('data'));die;
  29.         $magazine_id $request->get('magazine');
  30.         $mrmagzBlockId $request->get('id');
  31.         $data $request->get('data');
  32.         if ($mrmagzBlockId)
  33.             $block $em->getRepository(Block::class)->find($mrmagzBlockId);
  34.         else
  35.             $block = new Block();
  36.         $block->setHtml($data['mrmagz-html']);
  37.         $block->setCss($data['mrmagz-css']);
  38.         $block->setLabel($data['mrmagz-label']);
  39.         $block->setTitle($data['mrmagz-title']);
  40.         // $block->setCategory($data['mrmagz-category']);
  41.         $block->setClass($data['mrmagz-bclass']);
  42.         switch ($data['mrmagz-asso']) {
  43.             case 'Global':
  44.                 $block->setGlobal(true);
  45.                 $block->setCategory('Global');
  46.                 break;
  47.             case 'Magazine':
  48.                 $magazine $em->getRepository(Magazine::class)->find($magazine_id);
  49.                 $block->addMagazine($magazine);
  50.                 $block->setCategory('Magazine');
  51.                 break;
  52.             case 'Publisher':
  53.                 $magazine $em->getRepository(Magazine::class)->find($magazine_id);
  54.                 $publisher $magazine->getPublisher();
  55.                 $block->addPublisher($publisher);
  56.                 $block->setCategory('Publisher');
  57.                 break;
  58.         }
  59.         $this->admin->getModelManager()->update($block);
  60.         return new JsonResponse(['id' => $block->getId()]);
  61.     }
  62.     /**
  63.      * @Route("/admin/editor/block/load")
  64.      * @throws \Sonata\AdminBundle\Exception\ModelManagerException
  65.      */
  66.     public function editorBlockLoadAction()
  67.     {
  68.         $request $this->getRequest();
  69.         /** @var EntityManager $em */
  70.         $em $this->get('doctrine.orm.entity_manager');
  71.         $magazine_id $request->get('magazine');
  72.         $magazine $em->getRepository(Magazine::class)->find($magazine_id);
  73.         $publisher $magazine->getPublisher();
  74.         $allBlocks $em->getRepository(Block::class)->findAllIn(1$magazine->getId(), $publisher->getId());
  75.         $encoder = new JsonEncoder();
  76.         $normalizer = new ObjectNormalizer();
  77.         $normalizer->setIgnoredAttributes([
  78.             'updatedAt''user''createdAt''publishers''magazines'
  79.         ]);
  80.         $normalizer->setCallbacks([
  81.         ]);
  82.         $normalizer->setCircularReferenceHandler(function ($object) {
  83.             return $object->getId();
  84.         });
  85.         $serializer = new Serializer(array($normalizer), array($encoder));
  86.         return $this->json($serializer->normalize($allBlocks'json'));
  87.     }
  88.     /**
  89.      * @Route("/admin/editor/block/delete")
  90.      * @throws \Sonata\AdminBundle\Exception\ModelManagerException
  91.      */
  92.     public function editorBlockDeleteAction()
  93.     {
  94.         $request $this->getRequest();
  95.         /** @var EntityManager $em */
  96.         $em $this->get('doctrine.orm.entity_manager');
  97.         $mrmagzBlockId $request->get('id');
  98.         $block $em->getRepository(Block::class)->find($mrmagzBlockId);
  99.         if(!$block) {
  100.             return $this->json(['message' => 'Block not found'], Response::HTTP_NOT_FOUND);
  101.         }
  102.         $this->admin->getModelManager()->delete($block);
  103.         return new JsonResponse();
  104.     }
  105. }