src/Controller/Admin/MagazinePageAdminController.php line 128

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Admin\Editor\Classes;
  4. use App\Entity\Admin\Editor\CommonCss;
  5. use App\Entity\Admin\Editor\Settings;
  6. use App\Entity\Magazine\Magazine;
  7. use App\Entity\Project\Publisher;
  8. use App\Service\CssCompressor;
  9. use Sonata\AdminBundle\Controller\CRUDController as Controller;
  10. use App\Entity\Magazine\MagazinePage;
  11. use Symfony\Component\Filesystem\Filesystem;
  12. use Symfony\Component\Finder\Finder;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class MagazinePageAdminController extends Controller
  19. {
  20.     private $subject;
  21.     private $csscompressor;
  22.     public function __construct(CssCompressor $csscompressor)
  23.     {
  24.         $this->csscompressor $csscompressor;
  25.     }
  26.     /**
  27.      * @param Request $request
  28.      * @return \Symfony\Component\HttpFoundation\Response
  29.      */
  30.     public function editorAction(Request $request)
  31.     {
  32.         $page $this->admin->getSubject();
  33.         $viClassArr = [];
  34.         $commonStylesOrdered = [];
  35.         $em $this->get('doctrine.orm.entity_manager');
  36.         // $settings = $em->getRepository(Settings::class)->find(1);
  37.         $globalclasses $em->getRepository(Classes::class)->findBy(['global'=>true]);
  38.         // $commonStyles = $em->getRepository(CommonCss::class)->findBy(['publisher'=>$page->getMagazine()->getPublisher()]);
  39.         if ($page->getMagazine()->getPublisher())
  40.             $pubClasses $page->getMagazine()->getPublisher()->getEditorclass();
  41.         /*foreach ($commonStyles as $commoncss) {
  42.             $temp = [];
  43.             $temp['id'] = $commoncss->getId();
  44.             $temp['name'] = $commoncss->getName();
  45.             $temp['css'] = $commoncss->getCss();
  46.             array_push($commonStylesOrdered, $temp);
  47.         }*/
  48.         if ($globalclasses) {
  49.             foreach ($globalclasses as $class) {
  50.                 $temp = [];
  51.                 $temp['id'] = $class->getId();
  52.                 $temp['name'] = $class->getName();
  53.                 $temp['css'] = $class->getCss();
  54.                 array_push($viClassArr$temp);
  55.             }
  56.         }
  57.         if ($pubClasses) {
  58.             foreach ($pubClasses as $class) {
  59.                 $temp = [];
  60.                 $temp['id'] = $class->getId();
  61.                 $temp['name'] = $class->getName();
  62.                 $temp['css'] = $class->getCss();
  63.                 array_push($viClassArr$temp);
  64.             }
  65.         }
  66.         if ($page->getMagazine()->getPublisher() && $page->getMagazine()->getPublisher()->getEditorclass()) {
  67.             foreach ($page->getMagazine()->getPublisher()->getEditorclass() as $pclass) {
  68.                 array_push($viClassArr$pclass->getName());
  69.             }
  70.         }
  71.         // $viClassArr = explode (";", $settings->getViclass());
  72.         // dump($page->getMagazine()->getPublisher()->getEditorclass());die;
  73.         $pages $page->getMagazine()->getPages();
  74.         $magazineEditedCss '';
  75.         foreach ($pages as $temppage) {
  76.             if($temppage->getId() !== $page->getId() && $temppage->getStylesheet() !== null && $temppage->getStylesheet() !== "") {
  77.                 $magazineEditedCss .= "/***** page-"$temppage->getPosition() ."*****/";
  78.                 $magazineEditedCss $magazineEditedCss $temppage->getStylesheet();
  79.             }
  80.         }
  81.         //dump(preg_replace('~[\r\n]+~', "", $page->getMagazine()->getCss()));die;
  82.         /*return $this->render('admin/visualeditor/editor.html.twig', [
  83.         ]);*/
  84.         /*dump($request->get('id'));
  85.         dump($request->get('parent_id'));
  86.         die;*/
  87.         //dump(str_replace("\n", "", $page->getMagazine()->getCss()));die;
  88.         return $this->render(
  89.             'admin/visualeditor/editor.html.twig',
  90.             [
  91.                 'action' => 'list',
  92.                 'csrf_token' => $this->getCsrfToken('sonata.batch'),
  93.                 '_sonata_admin' => $request->get('_sonata_admin'),
  94.                 'page' => $page,
  95.                 'otherpageCss' => preg_replace('~[\r\n]+~'""$magazineEditedCss),
  96.                 'commonCss' => preg_replace('~[\r\n]+~'""$page->getMagazine()->getCss()),
  97.                 // 'commonCssList' => $commonStylesOrdered,
  98.                 'viClassArr' => $viClassArr
  99.             ],
  100.             null,
  101.             $request
  102.         );
  103.     }
  104.     /**
  105.      * @Route("/admin/gjs/data")
  106.      */
  107.     public function gjsDataAction()
  108.     {
  109.         $request $this->getRequest();
  110.         /** @var EntityManager $em */
  111.         $em $this->get('doctrine.orm.entity_manager');
  112.         //$page = $this->admin->getSubject();
  113.         $commonCssContent false;
  114.         $gjsCssContent $request->get('gjs-css');
  115.         $gjsHtmlContent preg_replace('/(draggable\=\"+\w+\")|(data-highlightable\=\"+\w+\")/'""$request->get('gjs-html'));
  116.         if ($request->get('gjs-commonCss'))
  117.             $commonCssContent strip_tags($request->get('gjs-commonCss'));
  118.         // $commonCssContent = htmlspecialchars($commonCssContent, ENT_HTML5 | ENT_NOQUOTES | ENT_SUBSTITUTE, 'utf-8');
  119.         $page_id $request->get('id');
  120.         $parent_id $request->get('parent');
  121.         // dump($commonCssContent);die;
  122.         $magazine $em->getRepository(Magazine::class)->find($parent_id);
  123.         $page $em->getRepository(MagazinePage::class)->find($page_id);
  124.         if($commonCssContent || $page->getStylesheet() !== $gjsCssContent) {
  125.             /* generate hard copy */
  126.             $filesystem = new Filesystem();
  127.             $magazineCsspath 'magazine/' pathinfo('src-' $magazine->getSlug(), PATHINFO_FILENAME) . '/assets/css/';
  128.             if ( is_dir($magazineCsspath) ) {
  129.                 $magazineEditedCss $magazine->getCss();
  130.                 /* check for existing css */
  131.                 if ($filesystem->exists(ltrim($magazine->getCommonstylesheet(), '/')))
  132.                     $filesystem->remove(ltrim($magazine->getCommonstylesheet(), '/'));
  133.                 /* create new */
  134.                 $randomnamebytime 'editor.' .  date('dHis');
  135.                 if ($commonCssContent) {
  136.                     $magazine->setCss($commonCssContent);
  137.                     $magazineEditedCss $commonCssContent;
  138.                 }
  139.                 $pages $magazine->getPages();
  140.                 foreach ($pages as $temppage) {
  141.                     if($temppage->getId() !== $page->getId() && $temppage->getStylesheet() !== null && $temppage->getStylesheet() !== "") {
  142.                         $magazineEditedCss .= "/***** page-"$temppage->getPosition() ."*****/";
  143.                         $magazineEditedCss .= $temppage->getStylesheet();
  144.                     }
  145.                 }
  146.                 $magazineEditedCss .= "/***** page-"$page->getPosition() ."*****/";
  147.                 $magazineEditedCss.= $gjsCssContent;
  148.                 $filesystem->dumpFile($magazineCsspath $randomnamebytime '.css'$this->csscompressor->compress($magazineEditedCss));
  149.                 $magazine->setCommonstylesheet('/' $magazineCsspath $randomnamebytime '.css');
  150.             }
  151.             $this->admin->getModelManager()->update($magazine);
  152.         }
  153.         if($page->getContent() !== $gjsHtmlContent) {
  154.             $page->setContent($gjsHtmlContent);
  155.         }
  156.         if($page->getStylesheet() !== $gjsCssContent) {
  157.             $page->setStylesheet($gjsCssContent);
  158.         }
  159.         //dump($gjsHtmlContent);die;
  160.         $this->admin->getModelManager()->update($page);
  161.         /*$em->persist($magazine);
  162.         $em->persist($page);
  163.         $em->flush();*/
  164.         return new JsonResponse('done');
  165.     }
  166. }