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

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 $ct '';
  434.             try{
  435.                 if($Page->getSlug() == 'error' || in_array($Page->exception->getStatusCode(),array(404,403,500))){
  436.                     $pageError $this->em->getRepository('CmsBundle:Page')->findOneBy(array('slug' => $Page->exception->getStatusCode()));
  437.                     if(!empty($pageError) || $Page->getSlug() == 'error'){
  438.                         if(!empty($pageError) &&  $Page->getSlug() != 'error')$Page $pageError;
  439.                         $blocks $this->em->getRepository('CmsBundle:PageBlockWrapper')->findBy([
  440.                             'page' => $Page,
  441.                             'enabled' => true
  442.                         ], ['pos' => 'asc']);
  443.                         
  444.                         if(!empty($blocks)){
  445.                             foreach($blocks as $pageBlock){
  446.                                 $blockData null;
  447.                                 $templateKey $pageBlock->getTemplateKey();
  448.                                 if(empty($templateKey)) continue;
  449.                                 $layoutPathOverride '../templates/blocks/';
  450.                                 if(!file_exists($layoutPathOverride $templateKey '.html.twig')){
  451.                                     $layoutPath '../src/CmsBundle/Resources/views/blocks/';
  452.                                     $twigPath '@Cms/blocks/';
  453.                                 }else{
  454.                                     $layoutPath $layoutPathOverride;
  455.                                     $twigPath '/blocks/';
  456.                                 }
  457.                                 $f $layoutPath $templateKey '.html.twig';
  458.                                 if(file_exists($f)){
  459.                                     $content file_get_contents($f);
  460.                                     if(preg_match('/\{\#(.*?)\#\}/is'$content$m)){
  461.                                         $conf $m[1];
  462.                                         $conf json_decode($conf);
  463.                                         if(!empty($conf)){
  464.                                             $blockData $conf;
  465.                                             $conf->blocks_q = [];
  466.                                             foreach($conf->blocks as $b){
  467.                                                 if(!isset($b->contained) || empty($b->contained)){
  468.                                                     $b->contained false;
  469.                                                 }
  470.                                                 $conf->blocks_q[$b->key] = $b;
  471.                                             }
  472.                                         }
  473.                                     }
  474.                                     // Prepare contained blocks
  475.                                     $parsed_blocks = [];
  476.                                     $containments = [];
  477.                                     foreach($pageBlock->getBlocks() as $block){
  478.                                         if($block->getContained()){
  479.                                             $containments[$block->getContained()][] = $block;
  480.                                             if(!in_array('contained:' $block->getContained(), $parsed_blocks)){
  481.                                                 $parsed_blocks[] = 'contained:' $block->getContained();
  482.                                             }
  483.                                         }else{
  484.                                             $parsed_blocks[] = $block;
  485.                                         }
  486.                                     }
  487.                                     $childblocks $pageBlock->getBlocks();
  488.                                     $bundle null;
  489.                                     foreach($childblocks as $block){
  490.                                         if($block->getBundle()){
  491.                                             $bundle str_replace('Trinity'''$block->getBundle());
  492.                                             $bundle preg_split('/(?=[A-Z])/',$bundle);
  493.                                             if(isset($bundle[0]) && empty($bundle[0])){
  494.                                                 unset($bundle[0]);
  495.                                             }
  496.                                             $bundle strtolower(implode('_'$bundle));
  497.                                         }
  498.                                     }
  499.                                     $anchor '';
  500.                                     if($pageBlock->getAnchor()){
  501.                                         $anchor '<a class="anchor" style="clear: both;display: block;" name="' $pageBlock->getAnchor() . '"></a>';
  502.                                     }
  503.                                     $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]);
  504.                                     $ct .= $blockHtml;
  505.                                     if(preg_match_all('/\[get\.(.*?)\]/'$ct$matches)){
  506.                                         foreach($matches[0] as $index => $tag){
  507.                                             $key $matches[1][$index];
  508.                                             if(!empty($_GET[$key])){
  509.                                                 $ct str_replace($tag$_GET[$key], $ct);
  510.                                             }else{
  511.                                                 $ct str_replace($tag''$ct);
  512.                                             }
  513.                                         }
  514.                                     }
  515.                                     if(preg_match_all('/media:(\d+)/'$ct$m)){
  516.                                         $foundIds $m[1];
  517.                                         arsort($foundIds);
  518.                                         foreach($foundIds as $index => $id){
  519.                                             $Media $this->em->getRepository('CmsBundle:Media')->find($id);
  520.                                             $ct str_replace($m[0][$index], '/' $Media->getWebPath(), $ct);
  521.                                         }
  522.                                     }
  523.                                     $ct $this->parseModuleContent($ct$params$request);
  524.                                     $ct $this->parseModuleContent($ct$params$request);
  525.                                     if(preg_match_all('/\[page:([a-zA-Z0-9\-\_]+)(\#[a-zA-Z0-9\-].*?)?\]/'$ct$matches)){
  526.                                         $idToSlug = [];
  527.                                         $cacheFile preg_replace('/\/src.*/''/var/cache/prod/'__DIR__) . 'page_id_slug';
  528.                                         if(file_exists($cacheFile)){
  529.                                             $idToSlug json_decode(file_get_contents($cacheFile), true);
  530.                                         }
  531.                                         foreach($matches[0] as $index => $tag){
  532.                                             $key $matches[1][$index];
  533.                                             if(is_numeric($key)){ if(array_key_exists($key$idToSlug)){ $key $idToSlug[$key]; }else{ $key 'homepage'; } }
  534.                                             $key str_replace('-''_'$key);
  535.                                             $ct str_replace('http://' $tag$this->generator->generate($key), $ct);
  536.                                             $ct str_replace('https://' $tag$this->generator->generate($key), $ct);
  537.                                             $ct str_replace($tag$this->generator->generate($key), $ct);
  538.                                         }
  539.                                     }
  540.                                     if(preg_match_all('/page:(\d+)/'$ct$m)){
  541.                                         $foundIds $m[1];
  542.                                         arsort($foundIds);
  543.                                         foreach($foundIds as $index => $id){
  544.                                             $Page $this->em->getRepository('CmsBundle:Page')->find($id);
  545.                                             if ($Page) {
  546.                                                 $ct str_replace($m[0][$index], $this->generator->generate($Page->getSlugKey()), $ct);
  547.                                             }else{
  548.                                                 $ct str_replace($m[0][$index], $this->generator->generate('homepage'), $ct);
  549.                                             }
  550.                                         }
  551.                                     }
  552.                                 }
  553.                             }
  554.                         }                    
  555.                     }else{
  556.                         if(in_array($Page->exception->getStatusCode(),array(404,403,500))){
  557.                             if($Page->exception->getStatusCode() == 404){
  558.                                 $return $Settings->getErrorNotFound();
  559.                             }
  560.                             if($Page->exception->getStatusCode() == 403){
  561.                                 $return $Settings->getErrorNoAccess();
  562.                             }
  563.                             if($Page->exception->getStatusCode() == 500){
  564.                                 $return $Settings->getErrorSystem();
  565.                             }
  566.                         }
  567.                     }
  568.                 }
  569.             }catch(\Throwable $e){
  570.                 $return $Settings->getErrorSystem();
  571.             }
  572.             if(empty($return)){
  573.                 return $ct '<!-- ERROR DEBUG | pid: ' $Page->getId() . ' | sid: ' $Settings->getId() . ' | lid: ' $Settings->getLanguage()->getId() . ' | ' $Settings->getLanguage()->getLocale() . ' -->';
  574.             }else{
  575.                 return $ct '<!-- ERROR DEBUG | pid: ' $Page->getId() . ' | sid: ' $Settings->getId() . ' | lid: ' $Settings->getLanguage()->getId() . ' | ' $Settings->getLanguage()->getLocale() . ' -->
  576.                     <section id="trinity-exception"><div class="container">' $return '</div></section>';
  577.             }
  578.         }
  579.         // dump(func_get_args());die();
  580.         $name preg_replace('/[^0-9a-z-]*/'''str_replace(' ''-'strtolower($name)));
  581.         $session = new Session();
  582.         $request  $this->container->get('request_stack')->getCurrentRequest();
  583.         if(!empty($request)){
  584.             $locale   $request->getLocale();
  585.         }else{
  586.             $locale 'nl';
  587.             $Settings null;
  588.         }
  589.         if($locale){ $Language $this->em->getRepository('CmsBundle:Language')->findOneByLocale($locale); }else{ $Languages $this->em->getRepository('CmsBundle:Language')->findAll(); $Language $Languages[0]; }
  590.         if (!empty($request)) { $Settings $this->em->getRepository('CmsBundle:Settings')->findByLanguage(nullstr_replace('www.'''$request->getHttpHost())); }
  591.         if(is_null($Settings) || is_null($Settings->getId())){ $Settings $this->em->getRepository('CmsBundle:Settings')->findOneByLanguage($Language); }
  592.         $canEdit false;
  593.         if(!empty($this->authCheck) && $this->authCheck->isGranted('ROLE_ADMIN') && $Settings->getInlineEdit() && $session->get('live_edit') == true){
  594.             $canEdit $Settings->getInlineEdit();
  595.         }
  596.         $use_materialize false;
  597.         $use_bootstrap false;
  598.         $libraries = (array)$Settings->getLayoutIncludeJs();
  599.         foreach($libraries as $lib){
  600.             if(preg_match('/materialize/'strtolower($lib))){
  601.                 $use_materialize true;
  602.                 $use_bootstrap false;
  603.                 break;
  604.             }else if(preg_match('/bootstrap/'strtolower($lib))){
  605.                 $use_materialize false;
  606.                 $use_bootstrap true;
  607.                 break;
  608.             }
  609.         }
  610.         $ct '';
  611.         // $blocks = $Page->getBlocks();
  612.         $blocks $this->em->getRepository('CmsBundle:PageBlockWrapper')->findBy([
  613.             'page' => $Page,
  614.             'enabled' => true
  615.         ], ['pos' => 'asc']);
  616.         if(!empty($blocks)){
  617.             foreach($blocks as $pageBlock){
  618.                 $blockData null;
  619.                 $templateKey $pageBlock->getTemplateKey();
  620.                 if(empty($templateKey)) continue;
  621.                 $layoutPathOverride '../templates/blocks/';
  622.                 if(!file_exists($layoutPathOverride $templateKey '.html.twig')){
  623.                     $layoutPath '../src/CmsBundle/Resources/views/blocks/';
  624.                     $twigPath '@Cms/blocks/';
  625.                 }else{
  626.                     $layoutPath $layoutPathOverride;
  627.                     $twigPath 'blocks/';
  628.                 }
  629.                 // Search for native blocks provided by bundles
  630.                 $bundleDir $this->container->get('kernel')->getProjectDir() . '/src/Trinity/';
  631.                 if(file_exists($bundleDir)){
  632.                     foreach(scandir($bundleDir) as $bundleKey){
  633.                         if(substr($bundleKey01) == '.') continue;
  634.                         $bundleRoot $bundleDir $bundleKey;
  635.                         if(file_exists($bundleRoot '/Resources/views/Cms/Blocks')){
  636.                             // Has custom layout dir
  637.                             if(file_exists($bundleRoot '/Resources/views/Cms/Blocks/' $templateKey '.html.twig')){
  638.                                 $layoutPath $bundleRoot '/Resources/views/Cms/Blocks/';
  639.                                 $twigPath 'Trinity' $bundleKey ':Cms/Blocks:';
  640.                             }
  641.                         }
  642.                     }
  643.                 }
  644.                 $f $layoutPath $templateKey '.html.twig';
  645.                 if(file_exists($f)){
  646.                     $content file_get_contents($f);
  647.                     if(preg_match('/\{\#(.*?)\#\}/is'$content$m)){
  648.                         $conf $m[1];
  649.                         $conf json_decode($conf);
  650.                         if(!empty($conf)){
  651.                             $blockData $conf;
  652.                             $conf->blocks_q = [];
  653.                             foreach($conf->blocks as $b){
  654.                                 if(!isset($b->contained) || empty($b->contained)){
  655.                                     $b->contained false;
  656.                                 }
  657.                                 $conf->blocks_q[$b->key] = $b;
  658.                             }
  659.                         }
  660.                     }
  661.                     // Prepare contained blocks
  662.                     $parsed_blocks = [];
  663.                     $containments = [];
  664.                     foreach($pageBlock->getBlocks() as $block){
  665.                         if($block->getContained()){
  666.                             $containments[$block->getContained()][] = $block;
  667.                             if(!in_array('contained:' $block->getContained(), $parsed_blocks)){
  668.                                 $parsed_blocks[] = 'contained:' $block->getContained();
  669.                             }
  670.                         }else{
  671.                             $parsed_blocks[] = $block;
  672.                         }
  673.                     }
  674.                     $childblocks $pageBlock->getBlocks();
  675.                     $bundle null;
  676.                     foreach($childblocks as $block){
  677.                         if($block->getBundle()){
  678.                             $bundle str_replace('Trinity'''$block->getBundle());
  679.                             $bundle preg_split('/(?=[A-Z])/',$bundle);
  680.                             if(isset($bundle[0]) && empty($bundle[0])){
  681.                                 unset($bundle[0]);
  682.                             }
  683.                             $bundle strtolower(implode('_'$bundle));
  684.                         }
  685.                     }
  686.                     $anchor '';
  687.                     if($pageBlock->getAnchor()){
  688.                         $anchor '<a class="anchor" style="clear: both;display: block;" name="' $pageBlock->getAnchor() . '"></a>';
  689.                     }
  690.                     $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]);
  691.                     $ct .= $blockHtml;
  692.                     if(preg_match_all('/\[get\.(.*?)\]/'$ct$matches)){
  693.                         foreach($matches[0] as $index => $tag){
  694.                             $key $matches[1][$index];
  695.                             if(!empty($_GET[$key])){
  696.                                 $ct str_replace($tag$_GET[$key], $ct);
  697.                             }else{
  698.                                 $ct str_replace($tag''$ct);
  699.                             }
  700.                         }
  701.                     }
  702.                     if(preg_match_all('/media:(\d+)/'$ct$m)){
  703.                         $foundIds $m[1];
  704.                         arsort($foundIds);
  705.                         foreach($foundIds as $index => $id){
  706.                             $Media $this->em->getRepository('CmsBundle:Media')->find($id);
  707.                             $ct str_replace($m[0][$index], '/' $Media->getWebPath(), $ct);
  708.                         }
  709.                     }
  710.                     $ct $this->parseModuleContent($ct$params$request);
  711.                     $ct $this->parseModuleContent($ct$params$request);
  712.                     if(preg_match_all('/\[page:([a-zA-Z0-9\-\_]+)(\#[a-zA-Z0-9\-].*?)?\]/'$ct$matches)){
  713.                         $idToSlug = [];
  714.                         $cacheFile preg_replace('/\/src.*/''/var/cache/prod/'__DIR__) . 'page_id_slug';
  715.                         if(file_exists($cacheFile)){
  716.                             $idToSlug json_decode(file_get_contents($cacheFile), true);
  717.                         }
  718.                         foreach($matches[0] as $index => $tag){
  719.                             $key $matches[1][$index];
  720.                             if(is_numeric($key)){ if(array_key_exists($key$idToSlug)){ $key $idToSlug[$key]; }else{ $key 'homepage'; } }
  721.                             $key str_replace('-''_'$key);
  722.                             $ct str_replace('http://' $tag$this->generator->generate($key), $ct);
  723.                             $ct str_replace('https://' $tag$this->generator->generate($key), $ct);
  724.                             $ct str_replace($tag$this->generator->generate($key), $ct);
  725.                         }
  726.                     }
  727.                     if(preg_match_all('/page:(\d+)/'$ct$m)){
  728.                         $foundIds $m[1];
  729.                         arsort($foundIds);
  730.                         foreach($foundIds as $index => $id){
  731.                             $Page $this->em->getRepository('CmsBundle:Page')->find($id);
  732.                             if ($Page) {
  733.                                 $ct str_replace($m[0][$index], $this->generator->generate($Page->getSlugKey()), $ct);
  734.                             }else{
  735.                                 $ct str_replace($m[0][$index], $this->generator->generate('homepage'), $ct);
  736.                             }
  737.                         }
  738.                     }
  739.                 }
  740.             }
  741.         }
  742.         if($canEdit){
  743.             $ct .= '
  744.                 <script>
  745.                     var savePath = \'' $this->generator->generate('admin_page_save_front') . '\';
  746.                 </script>
  747.                 <script src="/bundles/cms/js/front-editor.js"></script>
  748.                 <script src="/bundles/cms/ckeditor/ckeditor.js"></script>
  749.             ';
  750.         }
  751.         // die();
  752.         /*$ct = $this->parseModuleContent($ct, $params, $request);
  753.         if(preg_match_all('/\[page:([a-zA-Z0-9-_]+)\]/', $ct, $matches)){
  754.             foreach($matches[0] as $index => $tag){
  755.                 $ct = str_replace('http://' . $tag, $this->generateUrl($matches[1][$index]), $ct);
  756.                 $ct = str_replace('https://' . $tag, $this->generateUrl($matches[1][$index]), $ct);
  757.                 $ct = str_replace($tag, $this->generateUrl($matches[1][$index]), $ct);
  758.             }
  759.         }*/
  760.         return $ct;
  761.     }
  762.     /**
  763.      * @return string
  764.      */
  765.     public function getPageBlocksMeta (\Twig\Environment $environment$Page) {
  766.         if($Page && $Page->requireAuth){ return null; }
  767.         if($Page && !empty($Page->exception)){ return null; }
  768.         $request $this->requestStack->getCurrentRequest();
  769.         $params = array();
  770.         $params[] = $request->get('param1');
  771.         $params[] = $request->get('param2');
  772.         $params[] = $request->get('param3');
  773.         $params[] = $request->get('param4');
  774.         $params[] = $request->get('param5');
  775.         // $blocks = $Page->getBlocks();
  776.         $blocks $this->em->getRepository('CmsBundle:PageBlockWrapper')->findBy([
  777.             'page' => $Page,
  778.             'enabled' => true
  779.         ], ['pos' => 'asc']);
  780.         $seo_data null;
  781.         if(!empty($blocks)){
  782.             foreach($blocks as $pageBlock){
  783.                 foreach($pageBlock->getBlocks() as $block){
  784.                     $bundle $block->getBundle();
  785.                     if(!empty($bundle)){
  786.                         if(preg_match_all('/((?:^|[A-Z])[a-z]+)/',$bundle,$split)){
  787.                             if(!empty($split[1])){
  788.                                 try {
  789.                                     $config $block->getBundleData(true);
  790.                                     $namespace implode('\\'$split[1]);
  791.                                     $namespace '\\' str_replace('\\Bundle''Bundle'$namespace) . '\\Classes\\Metadata';
  792.                                     if(class_exists($namespace)){
  793.                                         $cl = new $namespace($this->em$params$config$this->container);
  794.                                         if(method_exists($cl'parse')){
  795.                                             $seo_data $cl->parse();
  796.                                             if(!is_array($seo_data) || empty($seo_data)){
  797.                                                 $seo_data null;
  798.                                             }
  799.                                         }
  800.                                     }
  801.                                 } catch (\Exception $e) {}
  802.                                 if(!empty($seo_data)){
  803.                                     break;
  804.                                 }
  805.                             }
  806.                         }
  807.                     }
  808.                 }
  809.             }
  810.             return $seo_data;
  811.         }
  812.         return null;
  813.     }
  814.     private function parseModuleContent($content$params = array(), $request null){
  815.         // Legacy
  816.         if(preg_match_all('/(qinox(\w+bundle)_show)/'$content$m)){
  817.             foreach($m[0] as $key => $str){
  818.                 $content str_replace($m[0][$key], 'trinity' $m[2][$key] . '_show'$content);
  819.             }
  820.         }
  821.         if(preg_match_all('/trinity:(\w+):(\{.*?\})/'$content$m)){
  822.             foreach($m[1] as $k => $bundle){
  823.                 $call 'trinity' strtolower($bundle) . 'bundle_show';
  824.                 $cfg json_decode($m[2][$k], true);
  825.                 if(is_null($cfg)) $cfg = array();
  826.                 try{
  827.                     $func 'ShowAction';
  828.                     $content str_replace($m[0][$k], $this->container->get($call)->$func($cfg$params$request), $content);
  829.                 }catch(\Exception $e){
  830.                     $content str_replace($m[0][$k], $e->getMessage(), $content);
  831.                 }
  832.             }
  833.         }
  834.         if(preg_match_all('/\[(.*?_show):(.*?)\((.*?)\)\]/'$content$m)){
  835.             foreach($m[1] as $i => $call){
  836.                 // $call = preg_replace('/^trinity/', 'qinox', $call);
  837.                 $action $m[2][$i];
  838.                 $value $m[3][$i];
  839.                 $cfg json_decode($valuetrue);
  840.                 if(is_null($cfg)) $cfg = array();
  841.                 try{
  842.                     $func $action 'Action';
  843.                     $content str_replace($m[0][$i], $this->container->get($call)->$func($cfg$params$request), $content);
  844.                 }catch(\Exception $e){
  845.                     $content str_replace($m[0][$i], $e->getMessage(), $content);
  846.                 }
  847.             }
  848.         }
  849.         return $content;
  850.     }
  851.     /**
  852.      * {@inheritdoc}
  853.      */
  854.     public function getName() {
  855.         return 'Page info';
  856.     }
  857. }