<?php
namespace App\Controller\Admin;
use App\Entity\Admin\Editor\Classes;
use App\Entity\Admin\Editor\CommonCss;
use App\Entity\Admin\Editor\Settings;
use App\Entity\Magazine\Magazine;
use App\Entity\Project\Publisher;
use App\Service\CssCompressor;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
use App\Entity\Magazine\MagazinePage;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class MagazinePageAdminController extends Controller
{
private $subject;
private $csscompressor;
public function __construct(CssCompressor $csscompressor)
{
$this->csscompressor = $csscompressor;
}
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function editorAction(Request $request)
{
$page = $this->admin->getSubject();
$viClassArr = [];
$commonStylesOrdered = [];
$em = $this->get('doctrine.orm.entity_manager');
// $settings = $em->getRepository(Settings::class)->find(1);
$globalclasses = $em->getRepository(Classes::class)->findBy(['global'=>true]);
// $commonStyles = $em->getRepository(CommonCss::class)->findBy(['publisher'=>$page->getMagazine()->getPublisher()]);
if ($page->getMagazine()->getPublisher())
$pubClasses = $page->getMagazine()->getPublisher()->getEditorclass();
/*foreach ($commonStyles as $commoncss) {
$temp = [];
$temp['id'] = $commoncss->getId();
$temp['name'] = $commoncss->getName();
$temp['css'] = $commoncss->getCss();
array_push($commonStylesOrdered, $temp);
}*/
if ($globalclasses) {
foreach ($globalclasses as $class) {
$temp = [];
$temp['id'] = $class->getId();
$temp['name'] = $class->getName();
$temp['css'] = $class->getCss();
array_push($viClassArr, $temp);
}
}
if ($pubClasses) {
foreach ($pubClasses as $class) {
$temp = [];
$temp['id'] = $class->getId();
$temp['name'] = $class->getName();
$temp['css'] = $class->getCss();
array_push($viClassArr, $temp);
}
}
if ($page->getMagazine()->getPublisher() && $page->getMagazine()->getPublisher()->getEditorclass()) {
foreach ($page->getMagazine()->getPublisher()->getEditorclass() as $pclass) {
array_push($viClassArr, $pclass->getName());
}
}
// $viClassArr = explode (";", $settings->getViclass());
// dump($page->getMagazine()->getPublisher()->getEditorclass());die;
$pages = $page->getMagazine()->getPages();
$magazineEditedCss = '';
foreach ($pages as $temppage) {
if($temppage->getId() !== $page->getId() && $temppage->getStylesheet() !== null && $temppage->getStylesheet() !== "") {
$magazineEditedCss .= "/***** page-". $temppage->getPosition() ."*****/";
$magazineEditedCss = $magazineEditedCss . $temppage->getStylesheet();
}
}
//dump(preg_replace('~[\r\n]+~', "", $page->getMagazine()->getCss()));die;
/*return $this->render('admin/visualeditor/editor.html.twig', [
]);*/
/*dump($request->get('id'));
dump($request->get('parent_id'));
die;*/
//dump(str_replace("\n", "", $page->getMagazine()->getCss()));die;
return $this->render(
'admin/visualeditor/editor.html.twig',
[
'action' => 'list',
'csrf_token' => $this->getCsrfToken('sonata.batch'),
'_sonata_admin' => $request->get('_sonata_admin'),
'page' => $page,
'otherpageCss' => preg_replace('~[\r\n]+~', "", $magazineEditedCss),
'commonCss' => preg_replace('~[\r\n]+~', "", $page->getMagazine()->getCss()),
// 'commonCssList' => $commonStylesOrdered,
'viClassArr' => $viClassArr
],
null,
$request
);
}
/**
* @Route("/admin/gjs/data")
*/
public function gjsDataAction()
{
$request = $this->getRequest();
/** @var EntityManager $em */
$em = $this->get('doctrine.orm.entity_manager');
//$page = $this->admin->getSubject();
$commonCssContent = false;
$gjsCssContent = $request->get('gjs-css');
$gjsHtmlContent = preg_replace('/(draggable\=\"+\w+\")|(data-highlightable\=\"+\w+\")/', "", $request->get('gjs-html'));
if ($request->get('gjs-commonCss'))
$commonCssContent = strip_tags($request->get('gjs-commonCss'));
// $commonCssContent = htmlspecialchars($commonCssContent, ENT_HTML5 | ENT_NOQUOTES | ENT_SUBSTITUTE, 'utf-8');
$page_id = $request->get('id');
$parent_id = $request->get('parent');
// dump($commonCssContent);die;
$magazine = $em->getRepository(Magazine::class)->find($parent_id);
$page = $em->getRepository(MagazinePage::class)->find($page_id);
if($commonCssContent || $page->getStylesheet() !== $gjsCssContent) {
/* generate hard copy */
$filesystem = new Filesystem();
$magazineCsspath = 'magazine/' . pathinfo('src-' . $magazine->getSlug(), PATHINFO_FILENAME) . '/assets/css/';
if ( is_dir($magazineCsspath) ) {
$magazineEditedCss = $magazine->getCss();
/* check for existing css */
if ($filesystem->exists(ltrim($magazine->getCommonstylesheet(), '/')))
$filesystem->remove(ltrim($magazine->getCommonstylesheet(), '/'));
/* create new */
$randomnamebytime = 'editor.' . date('dHis');
if ($commonCssContent) {
$magazine->setCss($commonCssContent);
$magazineEditedCss = $commonCssContent;
}
$pages = $magazine->getPages();
foreach ($pages as $temppage) {
if($temppage->getId() !== $page->getId() && $temppage->getStylesheet() !== null && $temppage->getStylesheet() !== "") {
$magazineEditedCss .= "/***** page-". $temppage->getPosition() ."*****/";
$magazineEditedCss .= $temppage->getStylesheet();
}
}
$magazineEditedCss .= "/***** page-". $page->getPosition() ."*****/";
$magazineEditedCss.= $gjsCssContent;
$filesystem->dumpFile($magazineCsspath . $randomnamebytime . '.css', $this->csscompressor->compress($magazineEditedCss));
$magazine->setCommonstylesheet('/' . $magazineCsspath . $randomnamebytime . '.css');
}
$this->admin->getModelManager()->update($magazine);
}
if($page->getContent() !== $gjsHtmlContent) {
$page->setContent($gjsHtmlContent);
}
if($page->getStylesheet() !== $gjsCssContent) {
$page->setStylesheet($gjsCssContent);
}
//dump($gjsHtmlContent);die;
$this->admin->getModelManager()->update($page);
/*$em->persist($magazine);
$em->persist($page);
$em->flush();*/
return new JsonResponse('done');
}
}