<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\DomCrawler\Crawler;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\OmeloPage;
use App\Entity\OmeloPost;
class OmeloController extends AbstractController
{
/**
* @Route(
* "/blogue",
* name="blog",
* )
* @Template("Omelo/Blog/archive.html.twig")
*/
public function blog(EntityManagerInterface $em){
$twigData = array();
$twigData['hideSideMenu'] = true;
$twigData['forceFooter']=true;
$twigData['posts'] = $em->getRepository(OmeloPost::class)->getValidPosts();
return $twigData;
}
/**
* @Route("/blogue/article/{title}/{id}",
* name="viewPost",
* defaults = {
* "title" = false,
* },
* )
* @Template("Omelo/Blog/view.html.twig")
*/
public function viewPost(EntityManagerInterface $em, $title, OmeloPost $post){
$twigData = array();
$twigData['hideSideMenu'] = true;
$twigData['post'] = $post;
$twigData['forceFooter']=true;
return $twigData;
}
/**
* @Route("/info/{url}", name="page")
*/
public function page(OmeloPage $page){
$twigData = array();
$twigData['page'] = $page;
$twigData['hideEdit'] = true;
if(!empty($page->getContent())){
$contents = $page->getContentInJson();
if(!empty($contents['panelList'])){
$twigData['panelList'] = json_decode($contents['panelList'], true);
unset($contents['panelList']);
}
$twigData['data'] = $contents;
}
$twigData['forceFooter']=true;
return $this->render('Omelo/Templates/'.$page->getTemplate(), $twigData);
}
function isJson($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
/**
* @Route("/omelo/template/edit/{id}", name="OmeloEditTemplate")
* @Security("has_role('ROLE_ADMIN_MATURIN')")
*/
public function editTemplate(OmeloPage $page)
{
$twigData = array();
$twigData['hideChat'] = true;
$twigData['page'] = $page;
if(!empty($page->getContent())){
$contents = $page->getContentInJson();
if(!empty($contents['panelList'])){
$twigData['panelList'] = json_decode($contents['panelList'], true);
unset($contents['panelList']);
}
$twigData['data'] = $contents;
}
return $this->render('Omelo/Templates/'.$page->getTemplate(), $twigData);
}
/**
* @Route("/omelo/template/save/{id}", name="OmeloSaveTemplate",
* options = { "expose" = true },
* )
* @Security("has_role('ROLE_ADMIN_MATURIN')")
*/
public function saveTemplate(OmeloPage $page, EntityManagerInterface $em, Request $request, UrlGeneratorInterface $router)
{
$contents = $request->request->all();
//We replace all images link with a optimized version
//Issue are mostly related with the DOM Crawler that wasn'T able to achieve this
//Everything rely on the fact the plugin use \n for every lines of code
foreach($contents as $key => $content){
$rows = explode("\n", $content);
foreach($rows as $nbr => $row){
if(strstr($row, '<img') && !strstr($row, '/resize/') && !strstr($row, 'data-edited')){
//Ok got one
$cols = explode(' ', $row);
$height = $width = $id = false;
$rawSrc = false;
foreach($cols as $col){
if(strstr($col, 'height')){
$raw = explode('=', $col);
$height= (int) filter_var(trim(str_replace('>', '', str_replace('"', '', end($raw)))),FILTER_SANITIZE_NUMBER_INT) ;
}
if(strstr($col, 'width') && !strstr($col, 'data')){
$raw = explode('=', $col);
$width= (int)filter_var(trim(str_replace('>', '', str_replace('"', '', end($raw)))) , FILTER_SANITIZE_NUMBER_INT);
}
if(strstr($col, 'src')){
$rawSrc = $col;
$raw = explode('=', $col);
$url = explode('/', str_replace('"', '', end($raw)));
$id = trim(str_replace('>', '', end($url)));
}
}
//We got it all let's convert
if($height && $width && $id ){
$url = $router->generate( 'getImageResized', array ('id' => $id, 'height' => $height, 'width' => $width));
$row = str_replace($rawSrc, ' src="'.$url.'" data-edited="true" ', $row);
$rows[$nbr] = $row;
}
}
}
$contents[$key] = implode("\n", $rows);
}
$json = json_encode($contents, true);
$page->setContent($json);
$em->persist($page);
$em->flush();
return new Response('saved');
}
}