src/CmsBundle/Twig/Extension/PageInfo.php line 1000

Open in your IDE?
  1. <?php
  2. namespace App\CmsBundle\Twig\Extension;
  3. use Symfony\Component\HttpKernel\KernelInterface;
  4. use Symfony\Component\Routing\RouterInterface;
  5. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Twig\Extension\AbstractExtension;
  11. class PageInfo extends AbstractExtension {
  12.     /**
  13.      * @var EntityManager
  14.      */
  15.     private $em;
  16.     private $generator;
  17.     private $authCheck;
  18.     private $ActivePage;
  19.     private $requestStack;
  20.     private $container;
  21.     public function __construct(EntityManagerInterface $emRouterInterface $generatorAuthorizationCheckerInterface $authCheckRequestStack $requestStackContainerInterface $container){
  22.         $this->em $em;
  23.         $this->generator $generator;
  24.         $this->authCheck $authCheck;
  25.         $this->requestStack $requestStack;
  26.         $this->container $container;
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public function getFunctions() {
  32.         return array(
  33.             new \Twig\TwigFunction('sitename', array($this'getSitename'), array('is_safe' => array('html'))),
  34.             new \Twig\TwigFunction('mailheader', array($this'getMailheader'), array('is_safe' => array('html'))),
  35.             new \Twig\TwigFunction('mailfooter', array($this'getMailfooter'), array('is_safe' => array('html'))),
  36.             new \Twig\TwigFunction('logo', array($this'getLogo'), array('is_safe' => array('html'))),
  37.             new \Twig\TwigFunction('logo_alt', array($this'getLogoAlt'), array('is_safe' => array('html'))),
  38.             new \Twig\TwigFunction('breadcrumbs', array($this'getBreadcrumbs'), array('is_safe' => array('html'))),
  39.             new \Twig\TwigFunction('page_label', array($this'getPageLabel'), array('is_safe' => array('html'))),
  40.             new \Twig\TwigFunction('page_subtitle', array($this'getPageSubtitle'), array('is_safe' => array('html'))),
  41.             new \Twig\TwigFunction('pagehref', array($this'getPageHref'), array('is_safe' => array('html'))),
  42.             new \Twig\TwigFunction('pageurl', array($this'getPageUrl'), array('is_safe' => array('html'))),
  43.             new \Twig\TwigFunction('pageimage', array($this'getPageImage'), array('is_safe' => array('html'))),
  44.             new \Twig\TwigFunction('pagecontent', array($this'getPageContent'), array('is_safe' => array('html'))),
  45.             new \Twig\TwigFunction('pageblocks', array($this'getPageBlocks'), array('is_safe' => array('html'), 'needs_environment' => true)),
  46.             new \Twig\TwigFunction('pageblocks_meta', array($this'getPageBlocksMeta'), array('is_safe' => array('html'), 'needs_environment' => true)),
  47.             new \Twig\TwigFunction('trinity_render', array($this'doRender'), array('is_safe' => array('html'), 'needs_environment' => true)),
  48.             new \Twig\TwigFunction('phone', array($this'getPhone'), array('is_safe' => array('html'))),
  49.             new \Twig\TwigFunction('facebook_url', array($this'getFacebookUrl'), array('is_safe' => array('html'))),
  50.             new \Twig\TwigFunction('twitter_url', array($this'getTwitterUrl'), array('is_safe' => array('html'))),
  51.             new \Twig\TwigFunction('youtube_url', array($this'getYoutubeUrl'), array('is_safe' => array('html'))),
  52.             new \Twig\TwigFunction('instagram_url', array($this'getInstagramUrl'), array('is_safe' => array('html'))),
  53.             new \Twig\TwigFunction('linkedin_url', array($this'getLinkedinUrl'), array('is_safe' => array('html'))),
  54.             new \Twig\TwigFunction('fetch_entity', array($this'fetchEntity'), array('is_safe' => array('html'))),
  55.         );
  56.     }
  57.     public function getFacebookUrl(){
  58.         $request  $this->container->get('request_stack')->getCurrentRequest();
  59.         if(!empty($request)){
  60.             $locale   $request->getLocale();
  61.         }else{
  62.             $locale 'nl';
  63.             $Settings null;
  64.         }
  65.         if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  66.         if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  67.         if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  68.         return (!empty($Settings->getFacebook()) ? 'https://www.facebook.com/' $Settings->getFacebook() : null);
  69.     }
  70.     public function getTwitterUrl(){
  71.         $request  $this->container->get('request_stack')->getCurrentRequest();
  72.         if(!empty($request)){
  73.             $locale   $request->getLocale();
  74.         }else{
  75.             $locale 'nl';
  76.             $Settings null;
  77.         }
  78.         if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  79.         if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  80.         if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  81.         return (!empty($Settings->getTwitter()) ? 'https://twitter.com/' $Settings->getTwitter() : null);
  82.     }
  83.     public function getYoutubeUrl(){
  84.         $request  $this->container->get('request_stack')->getCurrentRequest();
  85.         if(!empty($request)){
  86.             $locale   $request->getLocale();
  87.         }else{
  88.             $locale 'nl';
  89.             $Settings null;
  90.         }
  91.         if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  92.         if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  93.         if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  94.         return (!empty($Settings->getYoutube()) ? 'https://www.youtube.com/' $Settings->getYoutube() : null);
  95.     }
  96.     public function getInstagramUrl(){
  97.         $request  $this->container->get('request_stack')->getCurrentRequest();
  98.         if(!empty($request)){
  99.             $locale   $request->getLocale();
  100.         }else{
  101.             $locale 'nl';
  102.             $Settings null;
  103.         }
  104.         if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  105.         if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  106.         if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  107.         return (!empty($Settings->getInstagram()) ? 'https://www.instagram.com/' $Settings->getInstagram() : null);
  108.     }
  109.     public function getLinkedinUrl(){
  110.         $request  $this->container->get('request_stack')->getCurrentRequest();
  111.         if(!empty($request)){
  112.             $locale   $request->getLocale();
  113.         }else{
  114.             $locale 'nl';
  115.             $Settings null;
  116.         }
  117.         if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  118.         if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  119.         if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  120.         return (!empty($Settings->getLinkedin()) ? 'https://www.linkedin.com/' $Settings->getLinkedin() : null);
  121.     }
  122.     public function fetchEntity($bundle$entity$id){
  123.         $Entity null;
  124.         if(is_numeric($id)){
  125.             $selector 'Trinity' ucfirst(strtolower($bundle)) . 'Bundle:' ucfirst(strtolower($entity));
  126.             $Entity $this->em->getRepository($selector)->find((int)$id);
  127.         }
  128.         return $Entity;
  129.     }
  130.     public function getPhone($clean false){
  131.         $request  $this->container->get('request_stack')->getCurrentRequest();
  132.         if(!empty($request)){
  133.             $locale   $request->getLocale();
  134.         }else{
  135.             $locale 'nl';
  136.             $Settings null;
  137.         }
  138.         if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  139.         if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  140.         if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  141.         $phone $Settings->getPhone();
  142.         if($clean){
  143.             $phone preg_replace('/[^0-9]+/'''$phone);
  144.         }
  145.         return (!empty($phone) ? $phone null);
  146.     }
  147.     /**
  148.      * @return string
  149.      */
  150.     public function getPageHref ($slugkey) {
  151.         try{
  152.             if(is_numeric($slugkey)){
  153.                 $tmp_page $this->em->getRepository('CmsBundle:Page')->findOneById($slugkey);
  154.                 if($tmp_page == null) throw new \Exception('Invalid page');
  155.                 $slugkey $tmp_page->getSlugKey();
  156.             }
  157.             $url $this->generator->generate($slugkey);
  158.             // Find page by slugkey
  159.             $tmp_pages $this->em->getRepository('CmsBundle:Page')->findBy(array('slugkey' => $slugkey'enabled' => true), array('sort' => 'ASC'), 1);
  160.             if(!empty($tmp_pages)){
  161.                 $Page $tmp_pages[0];
  162.                 return '<a href="' $url '">' $Page->getTitle() . '</a>';
  163.             }
  164.         }catch(\Exception $e){}
  165.         return '';
  166.     }
  167.     /**
  168.      * @return string
  169.      */
  170.     public function getPageUrl ($slugkey) {
  171.         try{
  172.             if(is_numeric($slugkey)){
  173.                 $tmp_page $this->em->getRepository('CmsBundle:Page')->findOneById($slugkey);
  174.                 if($tmp_page == null) throw new \Exception('Invalid page');
  175.                 $slugkey $tmp_page->getSlugKey();
  176.             }
  177.             $url $this->generator->generate($slugkey);
  178.             return $url;
  179.         }catch(\Exception $e){}
  180.         return '';
  181.     }
  182.     /**
  183.      * @return string
  184.      */
  185.     public function getPageImage ($slugkey) {
  186.         try{
  187.             $url $this->generator->generate($slugkey);
  188.             // Find page by slugkey
  189.             $tmp_pages $this->em->getRepository('CmsBundle:Page')->findBy(array('slugkey' => $slugkey'enabled' => true), array('sort' => 'ASC'), 1);
  190.             if(!empty($tmp_pages)){
  191.                 $Page $tmp_pages[0];
  192.                 if($Page->hasImage()){
  193.                     return $Page->getImage()->getWebPath();
  194.                 }
  195.             }
  196.         }catch(\Exception $e){}
  197.         return '';
  198.     }
  199.     /**
  200.      * @return string
  201.      */
  202.     public function getBreadcrumbs ($Page$seperator '') {
  203.         $crumbs $Page->getParents();
  204.         $crumbs array_reverse($crumbs);
  205.         $crumbs[] = $Page;
  206.         $return = [];
  207.         foreach($crumbs as $i => $Page){
  208.             if($i == 0){
  209.                 $return[] = '<a class="breadcrumb-item" href="' $this->generator->generate('homepage') . ($Page substr($Page->getSettings()->getBaseUri(), 1) : '') . '">Home</a>';
  210.             }
  211.             if($Page){
  212.                 $return[] = '<a class="breadcrumb-item" href="' $this->generator->generate($Page->getSlugKey()) . '">' $Page->getTitle() . '</a>';
  213.             }
  214.         }
  215.         return '<div class="breadcrumb">' implode($seperator$return) . '</div>';
  216.     }
  217.     /**
  218.      * @return string
  219.      */
  220.     public function getPageLabel ($slugkey) {
  221.         // Find page by slugkey
  222.         $tmp_pages $this->em->getRepository('CmsBundle:Page')->findBy(array('slugkey' => $slugkey'enabled' => true), array('sort' => 'ASC'), 1);
  223.         if(!empty($tmp_pages)){
  224.             $Page $tmp_pages[0];
  225.             return $Page->getTitle();
  226.         }
  227.         return '';
  228.     }
  229.     /**
  230.      * @return string
  231.      */
  232.     public function getPageSubtitle ($slugkey) {
  233.         // Find page by slugkey
  234.         $tmp_pages $this->em->getRepository('CmsBundle:Page')->findBy(array('slugkey' => $slugkey'enabled' => true), array('sort' => 'ASC'), 1);
  235.         if(!empty($tmp_pages)){
  236.             $Page $tmp_pages[0];
  237.             return $Page->getSubtitle();
  238.         }
  239.         return '';
  240.     }
  241.     /**
  242.      * @return string
  243.      */
  244.     public function getSitename($Settings null){
  245.         if (empty($Settings)) {
  246.             $request  $this->container->get('request_stack')->getCurrentRequest();
  247.             if(!empty($request)){
  248.                 $locale   $request->getLocale();
  249.             }else{
  250.                 $locale 'nl';
  251.                 $Settings null;
  252.             }
  253.             if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  254.             if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  255.             if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  256.         }
  257.         return $Settings->getLabel();
  258.     }
  259.     /**
  260.      * @return string
  261.      */
  262.     public function getMailheader($Settings null){
  263.         if (empty($Settings)) {
  264.             $request  $this->container->get('request_stack')->getCurrentRequest();
  265.             if(!empty($request)){
  266.                 $locale   $request->getLocale();
  267.             }else{
  268.                 $locale 'nl';
  269.                 $Settings null;
  270.             }
  271.             if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  272.             if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  273.             if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  274.         }
  275.         return $Settings->getMailHeader();
  276.     }
  277.     /**
  278.      * @return string
  279.      */
  280.     public function getMailfooter($Settings null){
  281.         if (empty($Settings)) {
  282.             $request  $this->container->get('request_stack')->getCurrentRequest();
  283.             if(!empty($request)){
  284.                 $locale   $request->getLocale();
  285.             }else{
  286.                 $locale 'nl';
  287.                 $Settings null;
  288.             }
  289.             if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  290.             if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  291.             if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  292.         }
  293.         return $Settings->getMailFooter();
  294.     }
  295.     /**
  296.      * @return string
  297.      */
  298.     public function getLogo($Settings null){
  299.         if (empty($Settings)) {
  300.             $request  $this->container->get('request_stack')->getCurrentRequest();
  301.             if(!empty($request)){
  302.                 $locale   $request->getLocale();
  303.             }else{
  304.                 $locale 'nl';
  305.                 $Settings null;
  306.             }
  307.             if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  308.             if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  309.             if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  310.         }
  311.         return $Settings->getLogo();
  312.     }
  313.     /**
  314.      * @return string
  315.      */
  316.     public function getLogoAlt($Settings null){
  317.         if (empty($Settings)) {
  318.             $request  $this->container->get('request_stack')->getCurrentRequest();
  319.             if(!empty($request)){
  320.                 $locale   $request->getLocale();
  321.             }else{
  322.                 $locale 'nl';
  323.                 $Settings null;
  324.             }
  325.             if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  326.             if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  327.             if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  328.         }
  329.         return $Settings->getLogoAlt();
  330.     }
  331.     /**
  332.      * @return string
  333.      */
  334.     public function getPageContent ($Page$locale$name '') {
  335.         $name preg_replace('/[^0-9a-z-]*/'''str_replace(' ''-'strtolower($name)));
  336.         // $session = new Session();
  337.         $request  $this->container->get('request_stack')->getCurrentRequest();
  338.         if(!empty($request)){
  339.             $locale   $request->getLocale();
  340.         }else{
  341.             $locale 'nl';
  342.             $Settings null;
  343.         }
  344.         if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  345.         if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  346.         if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage($Language); }
  347.         $Content $this->em->getRepository('CmsBundle:PageContent')->findOneBy([
  348.             'page' => $Page,
  349.             'name' => $name,
  350.             'published' => true
  351.         ], [
  352.             'revision' => 'desc'
  353.         ]);
  354.         if($Content){
  355.             if(!empty($this->authCheck) && $this->authCheck->isGranted('ROLE_ADMIN') && $Settings->getInlineEdit() && $session->get('live_edit') == true){
  356.                 return '<div id="contentid-' $Content->getId() . '" class="front-editor" contenteditable="true">' $Content->getContent() . '</div>';
  357.             }else{
  358.                 return $Content->getContent();
  359.             }
  360.         }
  361.         return '';
  362.     }
  363.     /**
  364.      * @return string
  365.      */
  366.     public function doRender (\Twig\Environment $environment$ct){
  367.         $request $this->requestStack->getCurrentRequest();
  368.         $params = array();
  369.         $params[] = $request->get('param1');
  370.         $params[] = $request->get('param2');
  371.         $params[] = $request->get('param3');
  372.         $params[] = $request->get('param4');
  373.         $params[] = $request->get('param5');
  374.         $ct $this->parseModuleContent($ct$params$request);
  375.         $ct $this->parseModuleContent($ct$params$request);
  376.         if(preg_match_all('/\[page:([a-zA-Z0-9\-\_]+)(\#[a-zA-Z0-9\-].*?)?\]/'$ct$matches)){
  377.             $idToSlug = [];
  378.             $cacheFile preg_replace('/\/src.*/''/var/cache/prod/'__DIR__) . 'page_id_slug';
  379.             if(file_exists($cacheFile)){
  380.                 $idToSlug json_decode(file_get_contents($cacheFile), true);
  381.             }
  382.             foreach($matches[0] as $index => $tag){
  383.                 $key $matches[1][$index];
  384.                 if(is_numeric($key)){ if(array_key_exists($key$idToSlug)){ $key $idToSlug[$key]; }else{ $key 'homepage'; } }
  385.                 $key str_replace('-''_'$key);
  386.                 $ct str_replace('http://' $tag$this->generator->generate($key), $ct);
  387.                 $ct str_replace('https://' $tag$this->generator->generate($key), $ct);
  388.                 $ct str_replace($tag$this->generator->generate($key), $ct);
  389.             }
  390.         }
  391.         if(preg_match_all('/page:(\d+)/'$ct$m)){
  392.             $foundIds $m[1];
  393.             arsort($foundIds);
  394.             foreach($foundIds as $index => $id){
  395.                 $Page $this->em->getRepository('CmsBundle:Page')->find($id);
  396.                 if ($Page) {
  397.                     $ct str_replace($m[0][$index], $this->generator->generate($Page->getSlugKey()), $ct);
  398.                 }else{
  399.                     $ct str_replace($m[0][$index], $this->generator->generate('homepage'), $ct);
  400.                 }
  401.             }
  402.         }
  403.         return $ct;
  404.     }
  405.     /**
  406.      * @return string
  407.      */
  408.     public function getPageBlocks (\Twig\Environment $environment$Page$locale$name '') {
  409.         if($Page && $Page->requireAuth){
  410.             return '';
  411.         }
  412.         $request $this->requestStack->getCurrentRequest();
  413.         $params = array();
  414.         $params[] = $request->get('param1');
  415.         $params[] = $request->get('param2');
  416.         $params[] = $request->get('param3');
  417.         $params[] = $request->get('param4');
  418.         $params[] = $request->get('param5');
  419.         
  420.         if(!empty($Page->exception)){
  421.             // EXCEPTION HANDLER
  422.             $locale $Page->getLanguage()->getLocale();
  423.             if (empty($locale)) {
  424.                 $request  $this->container->get('request_stack')->getCurrentRequest();
  425.                 if(!empty($request)){
  426.                     $locale   $request->getLocale();
  427.                 }else{
  428.                     $locale 'nl';
  429.                     $Settings null;
  430.                 }
  431.             }
  432.             $Settings $Page->getSettings();
  433.             $return '';
  434.             $ct '';
  435.             if($Page->getSlug() == 'error'){
  436.                 $blocks $this->em->getRepository('CmsBundle:PageBlockWrapper')->findBy([
  437.                     'page' => $Page,
  438.                     'enabled' => true
  439.                 ], ['pos' => 'asc']);
  440.                 if(!empty($blocks)){
  441.                     foreach($blocks as $pageBlock){
  442.                         $blockData null;
  443.                         $templateKey $pageBlock->getTemplateKey();
  444.                         if(empty($templateKey)) continue;
  445.                         $layoutPathOverride '../templates/blocks/';
  446.                         if(!file_exists($layoutPathOverride $templateKey '.html.twig')){
  447.                             $layoutPath '../src/CmsBundle/Resources/views/blocks/';
  448.                             $twigPath '@Cms/blocks/';
  449.                         }else{
  450.                             $layoutPath $layoutPathOverride;
  451.                             $twigPath '/blocks/';
  452.                         }
  453.                         $f $layoutPath $templateKey '.html.twig';
  454.                         if(file_exists($f)){
  455.                             $content file_get_contents($f);
  456.                             if(preg_match('/\{\#(.*?)\#\}/is'$content$m)){
  457.                                 $conf $m[1];
  458.                                 $conf json_decode($conf);
  459.                                 if(!empty($conf)){
  460.                                     $blockData $conf;
  461.                                     $conf->blocks_q = [];
  462.                                     foreach($conf->blocks as $b){
  463.                                         if(!isset($b->contained) || empty($b->contained)){
  464.                                             $b->contained false;
  465.                                         }
  466.                                         $conf->blocks_q[$b->key] = $b;
  467.                                     }
  468.                                 }
  469.                             }
  470.                             // Prepare contained blocks
  471.                             $parsed_blocks = [];
  472.                             $containments = [];
  473.                             foreach($pageBlock->getBlocks() as $block){
  474.                                 if($block->getContained()){
  475.                                     $containments[$block->getContained()][] = $block;
  476.                                     if(!in_array('contained:' $block->getContained(), $parsed_blocks)){
  477.                                         $parsed_blocks[] = 'contained:' $block->getContained();
  478.                                     }
  479.                                 }else{
  480.                                     $parsed_blocks[] = $block;
  481.                                 }
  482.                             }
  483.                             $childblocks $pageBlock->getBlocks();
  484.                             $bundle null;
  485.                             foreach($childblocks as $block){
  486.                                 if($block->getBundle()){
  487.                                     $bundle str_replace('Trinity'''$block->getBundle());
  488.                                     $bundle preg_split('/(?=[A-Z])/',$bundle);
  489.                                     if(isset($bundle[0]) && empty($bundle[0])){
  490.                                         unset($bundle[0]);
  491.                                     }
  492.                                     $bundle strtolower(implode('_'$bundle));
  493.                                 }
  494.                             }
  495.                             $anchor '';
  496.                             if($pageBlock->getAnchor()){
  497.                                 $anchor '<a class="anchor" style="clear: both;display: block;" name="' $pageBlock->getAnchor() . '"></a>';
  498.                             }
  499.                             $blockHtml $anchor $environment->render($twigPath $templateKey '.html.twig', ['page' => $Page'locale' => $locale'bundle' => $bundle'wrapper' => $pageBlock'containments' => $containments'firstBlock' => $pageBlock->getBlocks()->first(), 'blocks' => $parsed_blocks'data' => $blockData'Page' => $Page]);
  500.                             $ct .= $blockHtml;
  501.                             if(preg_match_all('/\[get\.(.*?)\]/'$ct$matches)){
  502.                                 foreach($matches[0] as $index => $tag){
  503.                                     $key $matches[1][$index];
  504.                                     if(!empty($_GET[$key])){
  505.                                         $ct str_replace($tag$_GET[$key], $ct);
  506.                                     }else{
  507.                                         $ct str_replace($tag''$ct);
  508.                                     }
  509.                                 }
  510.                             }
  511.                             if(preg_match_all('/media:(\d+)/'$ct$m)){
  512.                                 $foundIds $m[1];
  513.                                 arsort($foundIds);
  514.                                 foreach($foundIds as $index => $id){
  515.                                     $Media $this->em->getRepository('CmsBundle:Media')->find($id);
  516.                                     $ct str_replace($m[0][$index], '/' $Media->getWebPath(), $ct);
  517.                                 }
  518.                             }
  519.                             $ct $this->parseModuleContent($ct$params$request);
  520.                             $ct $this->parseModuleContent($ct$params$request);
  521.                             if(preg_match_all('/\[page:([a-zA-Z0-9\-\_]+)(\#[a-zA-Z0-9\-].*?)?\]/'$ct$matches)){
  522.                                 $idToSlug = [];
  523.                                 $cacheFile preg_replace('/\/src.*/''/var/cache/prod/'__DIR__) . 'page_id_slug';
  524.                                 if(file_exists($cacheFile)){
  525.                                     $idToSlug json_decode(file_get_contents($cacheFile), true);
  526.                                 }
  527.                                 foreach($matches[0] as $index => $tag){
  528.                                     $key $matches[1][$index];
  529.                                     if(is_numeric($key)){ if(array_key_exists($key$idToSlug)){ $key $idToSlug[$key]; }else{ $key 'homepage'; } }
  530.                                     $key str_replace('-''_'$key);
  531.                                     $ct str_replace('http://' $tag$this->generator->generate($key), $ct);
  532.                                     $ct str_replace('https://' $tag$this->generator->generate($key), $ct);
  533.                                     $ct str_replace($tag$this->generator->generate($key), $ct);
  534.                                 }
  535.                             }
  536.                             if(preg_match_all('/page:(\d+)/'$ct$m)){
  537.                                 $foundIds $m[1];
  538.                                 arsort($foundIds);
  539.                                 foreach($foundIds as $index => $id){
  540.                                     $Page $this->em->getRepository('CmsBundle:Page')->find($id);
  541.                                     if ($Page) {
  542.                                         $ct str_replace($m[0][$index], $this->generator->generate($Page->getSlugKey()), $ct);
  543.                                     }else{
  544.                                         $ct str_replace($m[0][$index], $this->generator->generate('homepage'), $ct);
  545.                                     }
  546.                                 }
  547.                             }
  548.                         }
  549.                     }
  550.                 }
  551.             }
  552.             try{
  553.                 if($Page->exception->getStatusCode() == 404){
  554.                     $return $Settings->getErrorNotFound();
  555.                 }
  556.                 if($Page->exception->getStatusCode() == 403){
  557.                     $return $Settings->getErrorNoAccess();
  558.                 }
  559.                 if($Page->exception->getStatusCode() == 500){
  560.                     $return $Settings->getErrorSystem();
  561.                 }
  562.             }catch(\Throwable $e){
  563.                 $return $Settings->getErrorSystem();
  564.             }
  565.             return $ct '<!-- ERROR DEBUG | pid: ' $Page->getId() . ' | sid: ' $Settings->getId() . ' | lid: ' $Settings->getLanguage()->getId() . ' | ' $Settings->getLanguage()->getLocale() . ' -->
  566.             <section id="trinity-exception"><div class="container">' $return '</div></section>';
  567.         }
  568.         // dump(func_get_args());die();
  569.         $name preg_replace('/[^0-9a-z-]*/'''str_replace(' ''-'strtolower($name)));
  570.         $session = new Session();
  571.         $request  $this->container->get('request_stack')->getCurrentRequest();
  572.         if(!empty($request)){
  573.             $locale   $request->getLocale();
  574.         }else{
  575.             $locale 'nl';
  576.             $Settings null;
  577.         }
  578.         if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  579.         if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  580.         if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findOneByLanguage($Language); }
  581.         $canEdit false;
  582.         if(!empty($this->authCheck) && $this->authCheck->isGranted('ROLE_ADMIN') && $Settings->getInlineEdit() && $session->get('live_edit') == true){
  583.             $canEdit $Settings->getInlineEdit();
  584.         }
  585.         $use_materialize false;
  586.         $use_bootstrap false;
  587.         $libraries = (array)$Settings->getLayoutIncludeJs();
  588.         foreach($libraries as $lib){
  589.             if(preg_match('/materialize/'strtolower($lib))){
  590.                 $use_materialize true;
  591.                 $use_bootstrap false;
  592.                 break;
  593.             }else if(preg_match('/bootstrap/'strtolower($lib))){
  594.                 $use_materialize false;
  595.                 $use_bootstrap true;
  596.                 break;
  597.             }
  598.         }
  599.         $ct '';
  600.         // $blocks = $Page->getBlocks();
  601.         $blocks $this->em->getRepository('CmsBundle:PageBlockWrapper')->findBy([
  602.             'page' => $Page,
  603.             'enabled' => true
  604.         ], ['pos' => 'asc']);
  605.         if(!empty($blocks)){
  606.             foreach($blocks as $pageBlock){
  607.                 $blockData null;
  608.                 $templateKey $pageBlock->getTemplateKey();
  609.                 if(empty($templateKey)) continue;
  610.                 $layoutPathOverride '../templates/blocks/';
  611.                 if(!file_exists($layoutPathOverride $templateKey '.html.twig')){
  612.                     $layoutPath '../src/CmsBundle/Resources/views/blocks/';
  613.                     $twigPath '@Cms/blocks/';
  614.                 }else{
  615.                     $layoutPath $layoutPathOverride;
  616.                     $twigPath 'blocks/';
  617.                 }
  618.                 // Search for native blocks provided by bundles
  619.                 $bundleDir $this->container->get('kernel')->getProjectDir() . '/src/Trinity/';
  620.                 if(file_exists($bundleDir)){
  621.                     foreach(scandir($bundleDir) as $bundleKey){
  622.                         if(substr($bundleKey01) == '.') continue;
  623.                         $bundleRoot $bundleDir $bundleKey;
  624.                         if(file_exists($bundleRoot '/Resources/views/Cms/Blocks')){
  625.                             // Has custom layout dir
  626.                             if(file_exists($bundleRoot '/Resources/views/Cms/Blocks/' $templateKey '.html.twig')){
  627.                                 $layoutPath $bundleRoot '/Resources/views/Cms/Blocks/';
  628.                                 $twigPath 'Trinity' $bundleKey ':Cms/Blocks:';
  629.                             }
  630.                         }
  631.                     }
  632.                 }
  633.                 $f $layoutPath $templateKey '.html.twig';
  634.                 if(file_exists($f)){
  635.                     $content file_get_contents($f);
  636.                     if(preg_match('/\{\#(.*?)\#\}/is'$content$m)){
  637.                         $conf $m[1];
  638.                         $conf json_decode($conf);
  639.                         if(!empty($conf)){
  640.                             $blockData $conf;
  641.                             $conf->blocks_q = [];
  642.                             foreach($conf->blocks as $b){
  643.                                 if(!isset($b->contained) || empty($b->contained)){
  644.                                     $b->contained false;
  645.                                 }
  646.                                 $conf->blocks_q[$b->key] = $b;
  647.                             }
  648.                         }
  649.                     }
  650.                     // Prepare contained blocks
  651.                     $parsed_blocks = [];
  652.                     $containments = [];
  653.                     foreach($pageBlock->getBlocks() as $block){
  654.                         if($block->getContained()){
  655.                             $containments[$block->getContained()][] = $block;
  656.                             if(!in_array('contained:' $block->getContained(), $parsed_blocks)){
  657.                                 $parsed_blocks[] = 'contained:' $block->getContained();
  658.                             }
  659.                         }else{
  660.                             $parsed_blocks[] = $block;
  661.                         }
  662.                     }
  663.                     $childblocks $pageBlock->getBlocks();
  664.                     $bundle null;
  665.                     foreach($childblocks as $block){
  666.                         if($block->getBundle()){
  667.                             $bundle str_replace('Trinity'''$block->getBundle());
  668.                             $bundle preg_split('/(?=[A-Z])/',$bundle);
  669.                             if(isset($bundle[0]) && empty($bundle[0])){
  670.                                 unset($bundle[0]);
  671.                             }
  672.                             $bundle strtolower(implode('_'$bundle));
  673.                         }
  674.                     }
  675.                     $anchor '';
  676.                     if($pageBlock->getAnchor()){
  677.                         $anchor '<a class="anchor" style="clear: both;display: block;" name="' $pageBlock->getAnchor() . '"></a>';
  678.                     }
  679.                     $blockHtml $anchor $environment->render($twigPath $templateKey '.html.twig', ['page' => $Page'locale' => $locale'bundle' => $bundle'wrapper' => $pageBlock'containments' => $containments'firstBlock' => $pageBlock->getBlocks()->first(), 'blocks' => $parsed_blocks'data' => $blockData'Page' => $Page]);
  680.                     $ct .= $blockHtml;
  681.                     if(preg_match_all('/\[get\.(.*?)\]/'$ct$matches)){
  682.                         foreach($matches[0] as $index => $tag){
  683.                             $key $matches[1][$index];
  684.                             if(!empty($_GET[$key])){
  685.                                 $ct str_replace($tag$_GET[$key], $ct);
  686.                             }else{
  687.                                 $ct str_replace($tag''$ct);
  688.                             }
  689.                         }
  690.                     }
  691.                     if(preg_match_all('/media:(\d+)/'$ct$m)){
  692.                         $foundIds $m[1];
  693.                         arsort($foundIds);
  694.                         foreach($foundIds as $index => $id){
  695.                             $Media $this->em->getRepository('CmsBundle:Media')->find($id);
  696.                             $ct str_replace($m[0][$index], '/' $Media->getWebPath(), $ct);
  697.                         }
  698.                     }
  699.                     $ct $this->parseModuleContent($ct$params$request);
  700.                     $ct $this->parseModuleContent($ct$params$request);
  701.                     if(preg_match_all('/\[page:([a-zA-Z0-9\-\_]+)(\#[a-zA-Z0-9\-].*?)?\]/'$ct$matches)){
  702.                         $idToSlug = [];
  703.                         $cacheFile preg_replace('/\/src.*/''/var/cache/prod/'__DIR__) . 'page_id_slug';
  704.                         if(file_exists($cacheFile)){
  705.                             $idToSlug json_decode(file_get_contents($cacheFile), true);
  706.                         }
  707.                         foreach($matches[0] as $index => $tag){
  708.                             $key $matches[1][$index];
  709.                             if(is_numeric($key)){ if(array_key_exists($key$idToSlug)){ $key $idToSlug[$key]; }else{ $key 'homepage'; } }
  710.                             $key str_replace('-''_'$key);
  711.                             $ct str_replace('http://' $tag$this->generator->generate($key), $ct);
  712.                             $ct str_replace('https://' $tag$this->generator->generate($key), $ct);
  713.                             $ct str_replace($tag$this->generator->generate($key), $ct);
  714.                         }
  715.                     }
  716.                     if(preg_match_all('/page:(\d+)/'$ct$m)){
  717.                         $foundIds $m[1];
  718.                         arsort($foundIds);
  719.                         foreach($foundIds as $index => $id){
  720.                             $Page $this->em->getRepository('CmsBundle:Page')->find($id);
  721.                             if ($Page) {
  722.                                 $ct str_replace($m[0][$index], $this->generator->generate($Page->getSlugKey()), $ct);
  723.                             }else{
  724.                                 $ct str_replace($m[0][$index], $this->generator->generate('homepage'), $ct);
  725.                             }
  726.                         }
  727.                     }
  728.                 }
  729.             }
  730.         }
  731.         if($canEdit){
  732.             $ct .= '
  733.                 <script>
  734.                     var savePath = \'' $this->generator->generate('admin_page_save_front') . '\';
  735.                 </script>
  736.                 <script src="/bundles/cms/js/front-editor.js"></script>
  737.                 <script src="/bundles/cms/ckeditor/ckeditor.js"></script>
  738.             ';
  739.         }
  740.         // die();
  741.         /*$ct = $this->parseModuleContent($ct, $params, $request);
  742.         if(preg_match_all('/\[page:([a-zA-Z0-9-_]+)\]/', $ct, $matches)){
  743.             foreach($matches[0] as $index => $tag){
  744.                 $ct = str_replace('http://' . $tag, $this->generateUrl($matches[1][$index]), $ct);
  745.                 $ct = str_replace('https://' . $tag, $this->generateUrl($matches[1][$index]), $ct);
  746.                 $ct = str_replace($tag, $this->generateUrl($matches[1][$index]), $ct);
  747.             }
  748.         }*/
  749.         return $ct;
  750.     }
  751.     /**
  752.      * @return string
  753.      */
  754.     public function getPageBlocksMeta (\Twig\Environment $environment$Page) {
  755.         if($Page && $Page->requireAuth){ return null; }
  756.         if($Page && !empty($Page->exception)){ return null; }
  757.         $request $this->requestStack->getCurrentRequest();
  758.         $params = array();
  759.         $params[] = $request->get('param1');
  760.         $params[] = $request->get('param2');
  761.         $params[] = $request->get('param3');
  762.         $params[] = $request->get('param4');
  763.         $params[] = $request->get('param5');
  764.         // $blocks = $Page->getBlocks();
  765.         $blocks $this->em->getRepository('CmsBundle:PageBlockWrapper')->findBy([
  766.             'page' => $Page,
  767.             'enabled' => true
  768.         ], ['pos' => 'asc']);
  769.         $seo_data null;
  770.         if(!empty($blocks)){
  771.             foreach($blocks as $pageBlock){
  772.                 foreach($pageBlock->getBlocks() as $block){
  773.                     $bundle $block->getBundle();
  774.                     if(!empty($bundle)){
  775.                         if(preg_match_all('/((?:^|[A-Z])[a-z]+)/',$bundle,$split)){
  776.                             if(!empty($split[1])){
  777.                                 try {
  778.                                     $config $block->getBundleData(true);
  779.                                     $namespace implode('\\'$split[1]);
  780.                                     $namespace '\\' str_replace('\\Bundle''Bundle'$namespace) . '\\Classes\\Metadata';
  781.                                     if(class_exists($namespace)){
  782.                                         $cl = new $namespace($this->em$params$config$this->container);
  783.                                         if(method_exists($cl'parse')){
  784.                                             $seo_data $cl->parse();
  785.                                             if(!is_array($seo_data) || empty($seo_data)){
  786.                                                 $seo_data null;
  787.                                             }
  788.                                         }
  789.                                     }
  790.                                 } catch (\Exception $e) {}
  791.                                 if(!empty($seo_data)){
  792.                                     break;
  793.                                 }
  794.                             }
  795.                         }
  796.                     }
  797.                 }
  798.             }
  799.             return $seo_data;
  800.         }
  801.         return null;
  802.     }
  803.     private function parseModuleContent($content$params = array(), $request null){
  804.         // Legacy
  805.         if(preg_match_all('/(qinox(\w+bundle)_show)/'$content$m)){
  806.             foreach($m[0] as $key => $str){
  807.                 $content str_replace($m[0][$key], 'trinity' $m[2][$key] . '_show'$content);
  808.             }
  809.         }
  810.         if(preg_match_all('/trinity:(\w+):(\{.*?\})/'$content$m)){
  811.             foreach($m[1] as $k => $bundle){
  812.                 $call 'trinity' strtolower($bundle) . 'bundle_show';
  813.                 $cfg json_decode($m[2][$k], true);
  814.                 if(is_null($cfg)) $cfg = array();
  815.                 try{
  816.                     $func 'ShowAction';
  817.                     $content str_replace($m[0][$k], $this->container->get($call)->$func($cfg$params$request), $content);
  818.                 }catch(\Exception $e){
  819.                     $content str_replace($m[0][$k], $e->getMessage(), $content);
  820.                 }
  821.             }
  822.         }
  823.         if(preg_match_all('/\[(.*?_show):(.*?)\((.*?)\)\]/'$content$m)){
  824.             foreach($m[1] as $i => $call){
  825.                 // $call = preg_replace('/^trinity/', 'qinox', $call);
  826.                 $action $m[2][$i];
  827.                 $value $m[3][$i];
  828.                 $cfg json_decode($valuetrue);
  829.                 if(is_null($cfg)) $cfg = array();
  830.                 try{
  831.                     $func $action 'Action';
  832.                     $content str_replace($m[0][$i], $this->container->get($call)->$func($cfg$params$request), $content);
  833.                 }catch(\Exception $e){
  834.                     $content str_replace($m[0][$i], $e->getMessage(), $content);
  835.                 }
  836.             }
  837.         }
  838.         return $content;
  839.     }
  840.     /**
  841.      * {@inheritdoc}
  842.      */
  843.     public function getName() {
  844.         return 'Page info';
  845.     }
  846. }