src/Controller/OmeloController.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. use Symfony\Component\DomCrawler\Crawler;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use App\Entity\OmeloPage;
  15. use App\Entity\OmeloPost;
  16. class OmeloController extends AbstractController
  17. {
  18.     /**
  19.      * @Route(
  20.      *      "/blogue", 
  21.      *      name="blog", 
  22.      *  )
  23.      * @Template("Omelo/Blog/archive.html.twig")
  24.      */
  25.     public function blog(EntityManagerInterface $em){
  26.         $twigData = array();
  27.         $twigData['hideSideMenu'] = true;
  28.         $twigData['forceFooter']=true;
  29.         $twigData['posts'] = $em->getRepository(OmeloPost::class)->getValidPosts();
  30.         return $twigData;
  31.     }
  32.     /**
  33.      * @Route("/blogue/article/{title}/{id}", 
  34.      *          name="viewPost",
  35.      *          defaults = {
  36.      *              "title" = false,
  37.      *          },
  38.      *      )
  39.      * @Template("Omelo/Blog/view.html.twig")
  40.      */
  41.     public function viewPost(EntityManagerInterface $em$titleOmeloPost $post){
  42.         $twigData = array();
  43.         $twigData['hideSideMenu'] = true;
  44.         $twigData['post'] = $post;
  45.         $twigData['forceFooter']=true;
  46.         return $twigData;
  47.     }
  48.     /**
  49.      * @Route("/info/{url}", name="page")
  50.      */
  51.     public function page(OmeloPage $page){
  52.         $twigData = array();
  53.         $twigData['page'] = $page;
  54.         $twigData['hideEdit'] = true;
  55.         if(!empty($page->getContent())){
  56.             $contents $page->getContentInJson();
  57.             if(!empty($contents['panelList'])){
  58.                 $twigData['panelList'] = json_decode($contents['panelList'], true);
  59.                 unset($contents['panelList']);
  60.             }
  61.             $twigData['data'] = $contents;
  62.         }
  63.         $twigData['forceFooter']=true;
  64.         return $this->render('Omelo/Templates/'.$page->getTemplate(), $twigData);
  65.     }
  66.     function isJson($string) {
  67.         json_decode($string);
  68.         return (json_last_error() == JSON_ERROR_NONE);
  69.     }
  70.     /**
  71.      * @Route("/omelo/template/edit/{id}", name="OmeloEditTemplate")
  72.      * @Security("has_role('ROLE_ADMIN_MATURIN')")
  73.      */
  74.     public function editTemplate(OmeloPage $page)
  75.     {
  76.         $twigData = array();
  77.         $twigData['hideChat'] = true;
  78.         $twigData['page'] = $page;
  79.         if(!empty($page->getContent())){
  80.             $contents $page->getContentInJson();
  81.             if(!empty($contents['panelList'])){
  82.                 $twigData['panelList'] = json_decode($contents['panelList'], true);
  83.                 unset($contents['panelList']);
  84.             }
  85.             $twigData['data'] = $contents;
  86.         }
  87.         return $this->render('Omelo/Templates/'.$page->getTemplate(), $twigData);
  88.     }
  89.     /**
  90.      * @Route("/omelo/template/save/{id}", name="OmeloSaveTemplate",
  91.      *      options = { "expose" = true },
  92.      * )
  93.      * @Security("has_role('ROLE_ADMIN_MATURIN')")
  94.      */
  95.     public function saveTemplate(OmeloPage $pageEntityManagerInterface $emRequest $requestUrlGeneratorInterface $router)
  96.     {
  97.         $contents $request->request->all();
  98.         //We replace all images link with a optimized version
  99.         //Issue are mostly related with the DOM Crawler that wasn'T able to achieve this
  100.         //Everything rely on the fact the plugin use \n for every lines of code
  101.         foreach($contents as $key => $content){
  102.             $rows explode("\n"$content);
  103.             foreach($rows as $nbr => $row){
  104.                 if(strstr($row'<img') && !strstr($row'/resize/') && !strstr($row'data-edited')){
  105.                     //Ok got one
  106.                     $cols explode(' '$row);
  107.                     $height $width $id false;
  108.                     $rawSrc false;
  109.                     foreach($cols as $col){
  110.                         if(strstr($col'height')){
  111.                             $raw explode('='$col);
  112.                             $height= (int) filter_var(trim(str_replace('>'''str_replace('"'''end($raw)))),FILTER_SANITIZE_NUMBER_INT) ;
  113.                         }
  114.                         if(strstr($col'width') && !strstr($col'data')){
  115.                             $raw explode('='$col);
  116.                             $width= (int)filter_var(trim(str_replace('>'''str_replace('"'''end($raw)))) , FILTER_SANITIZE_NUMBER_INT);
  117.                         }
  118.                         if(strstr($col'src')){
  119.                             $rawSrc $col;
  120.                             $raw explode('='$col);
  121.                             $url explode('/'str_replace('"'''end($raw)));
  122.                             $id trim(str_replace('>'''end($url)));
  123.                         }
  124.                     }
  125.                     //We got it all let's convert
  126.                     if($height && $width && $id ){
  127.                         $url $router->generate'getImageResized', array ('id' => $id'height' => $height'width' => $width));
  128.                         $row str_replace($rawSrc' src="'.$url.'" data-edited="true" '$row);
  129.                         $rows[$nbr] = $row;
  130.                     }
  131.                 }
  132.             }
  133.             $contents[$key] = implode("\n"$rows);
  134.         }
  135.         $json json_encode($contentstrue);
  136.         $page->setContent($json);
  137.         $em->persist($page);
  138.         $em->flush();
  139.         return new Response('saved');
  140.     }
  141.     
  142. }