src/Web/Core/Engine/Page.php line 345

Open in your IDE?
  1. <?php
  2. namespace Web\Core\Engine;
  3. use Application\Modules\Advertisement\Model\Advertisement as ApplicationAdvertisementModel;
  4. use Common\Exception\RedirectException;
  5. use IcebirdCMS\App\KernelLoader;
  6. use Web\Core\Engine\Block\ModuleExtraInterface;
  7. use Web\Core\Header\Header;
  8. use Web\Core\Language\Language;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\HttpKernel\KernelInterface;
  13. use Web\Core\Engine\Block\ExtraInterface as WebBlockExtra;
  14. use Web\Core\Engine\Block\Widget as WebBlockWidget;
  15. use Application\Core\Engine\Model as ApplicationModel;
  16. use Web\Modules\Advertisement\Model\Advertisement as WebAdvertisementModel;
  17. use Web\Modules\Profiles\Model\Authentication as WebAuthenticationModel;
  18. use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException;
  19. class Page extends KernelLoader
  20. {
  21.     protected $breadcrumb;
  22.     protected $extras = [];
  23.     protected $footer;
  24.     protected $header;
  25.     protected $pageId;
  26.     protected $record = [];
  27.     protected $templatePath;
  28.     protected $statusCode Response::HTTP_OK;
  29.     protected $template;
  30.     protected $url;
  31.     public function __construct(KernelInterface $kernel)
  32.     {
  33.         parent::__construct($kernel);
  34.         $this->getContainer()->set('page'$this);
  35.         $this->template $this->getContainer()->get('templating');
  36.         $this->url $this->getContainer()->get('url');
  37.     }
  38.     public function load(): void
  39.     {
  40.         Model::getVisitorId();
  41.         $this->header = new Header($this->getKernel());
  42.         try {
  43.             $this->handlePage(Navigation::getPageId(implode('/'$this->url->getPages())));
  44.         } catch (NotFoundHttpException $notFoundHttpException) {
  45.             $this->handlePage(Response::HTTP_NOT_FOUND);
  46.         } catch (InsufficientAuthenticationException $insufficientAuthenticationException) {
  47.             $this->redirectToLogin();
  48.         }
  49.     }
  50.     private function handlePage(int $pageId)
  51.     {
  52.         $this->record $this->getPageContent($pageId);
  53.         if (empty($this->record)) {
  54.             throw new NotFoundHttpException('No page was found for the page id:' $pageId);
  55.         }
  56.         $this->checkAuthentication();
  57.         $this->pageId = (int)$this->record['id'];
  58.         if ($this->pageId === Response::HTTP_NOT_FOUND) {
  59.             $this->statusCode Response::HTTP_NOT_FOUND;
  60.             if (extension_loaded('newrelic')) {
  61.                 newrelic_name_transaction('404');
  62.             }
  63.         }
  64.         $this->breadcrumb = new Breadcrumb($this->getKernel());
  65.         $this->footer = new Footer($this->getKernel());
  66.         $this->processPage();
  67.         array_map([$this'processExtra'], $this->extras);
  68.     }
  69.     private function checkAuthentication(): void
  70.     {
  71.         if (!isset($this->record['data']['auth_required'])
  72.             || !$this->record['data']['auth_required']
  73.             || !ApplicationModel::isModuleInstalled('Profiles')
  74.         ) {
  75.             return;
  76.         }
  77.         if (!WebAuthenticationModel::isLoggedIn()) {
  78.             throw new InsufficientAuthenticationException('You must log in to see this page');
  79.         }
  80.         if (empty($this->record['data']['auth_groups'])) {
  81.             return;
  82.         }
  83.         foreach ($this->record['data']['auth_groups'] as $group) {
  84.             if (WebAuthenticationModel::getProfile()->isInGroup($group)) {
  85.                 return;
  86.             }
  87.         }
  88.         throw new NotFoundHttpException('The current page is not available to the logged in profile');
  89.     }
  90.     public function getCookie()
  91.     {
  92.         $cookie $this->getContainer()->get('database')->getRecord(
  93.             'SELECT a.*
  94.          FROM cookie_consent AS a
  95.          WHERE a.language = ?',
  96.             array(WEB_LANGUAGE)
  97.         );
  98.         $cookie['exclude_pages'] = unserialize($cookie['exclude_pages']);
  99.         $this->template->assignGlobal('cookieConsent'$cookie);
  100.     }
  101.     public function isMarketingCookie()
  102.     {
  103.         $this->template->assignGlobal('isMarketingCookie', isset($_COOKIE['COOKIE_MARKETING']) ? $_COOKIE['COOKIE_MARKETING'] : false);
  104.     }
  105.     public function display(): Response
  106.     {
  107.         try {
  108.             $this->template->assignGlobal('isPage' $this->pageIdtrue);
  109.             $this->template->assignGlobal('isChildOfPage' $this->record['parent_id'], true);
  110.             //CookieConsent module
  111.             if (ApplicationModel::isModuleInstalled('CookieConsent')) {
  112.                 $this->getCookie();
  113.                 $this->isMarketingCookie();
  114.             }
  115.             $this->template->assignGlobal(
  116.                 'cookieBarHide',
  117.                 !$this->get('icebird.settings')->get('Core''show_cookie_bar'false)
  118.                 || $this->getContainer()->get('icebird.cookie')->hasHiddenCookieBar()
  119.             );
  120.             $this->parsePositions();
  121.             $unusedPositions array_diff(
  122.                 $this->record['template_data']['names'],
  123.                 array_keys($this->record['positions'])
  124.             );
  125.             foreach ($unusedPositions as $position) {
  126.                 $this->template->assign('position' . \SpoonFilter::ucfirst($position), []);
  127.             }
  128.             $this->header->parse();
  129.             $this->breadcrumb->parse();
  130.             $this->parseLanguages();
  131.             $this->footer->parse();
  132.             return new Response(
  133.                 $this->template->getContent($this->templatePath),
  134.                 $this->statusCode
  135.             );
  136.         } catch (NotFoundHttpException $notFoundHttpException) {
  137.             $this->handlePage(Response::HTTP_NOT_FOUND);
  138.             return $this->display();
  139.         } catch (InsufficientAuthenticationException $insufficientAuthenticationException) {
  140.             $this->redirectToLogin();
  141.         }
  142.     }
  143.     private function redirectToLogin()
  144.     {
  145.         $this->redirect(
  146.             Navigation::getUrlForBlock('Profiles''Login') . '?queryString=' Model::getRequest()->getRequestUri(),
  147.             Response::HTTP_TEMPORARY_REDIRECT
  148.         );
  149.     }
  150.     public function getExtras(): array
  151.     {
  152.         return $this->extras;
  153.     }
  154.     public function getId(): int
  155.     {
  156.         return $this->pageId;
  157.     }
  158.     private function getPageRecord(int $pageId): array
  159.     {
  160.         if ($this->url->getParameter('page_revision''int') === null) {
  161.             return Model::getPage($pageId);
  162.         }
  163.         $this->header->addMetaData(['name' => 'robots''content' => 'noindex, nofollow'], true);
  164.         return Model::getPageRevision($this->url->getParameter('page_revision''int'));
  165.     }
  166.     protected function getPageContent(int $pageId): array
  167.     {
  168.         $record $this->getPageRecord($pageId);
  169.         if (empty($record)) {
  170.             return [];
  171.         }
  172.         if ($this->allPositionsAreEmpty($record['positions'])) {
  173.             $firstChildId Navigation::getFirstChildId($record['id']);
  174.             if ($firstChildId !== 0) {
  175.                 $this->redirect(Navigation::getUrl($firstChildId));
  176.             }
  177.         }
  178.         return $record;
  179.     }
  180.     private function allPositionsAreEmpty(array $positions): bool
  181.     {
  182.         foreach ($positions as $blocks) {
  183.             foreach ($blocks as $block) {
  184.                 if ($block['extra_type'] === 'block'
  185.                     || $block['extra_type'] === 'widget'
  186.                     || trim($block['html']) !== ''
  187.                 ) {
  188.                     return false;
  189.                 }
  190.             }
  191.         }
  192.         return true;
  193.     }
  194.     public function getRecord(): array
  195.     {
  196.         return $this->record;
  197.     }
  198.     public function getStatusCode(): int
  199.     {
  200.         return $this->statusCode;
  201.     }
  202.     protected function parseLanguages(): void
  203.     {
  204.         if (!$this->getContainer()->getParameter('site.multilanguage') || count(Language::getActiveLanguages()) === 1) {
  205.             return;
  206.         }
  207.         $this->template->assignGlobal(
  208.             'languages',
  209.             array_map(
  210.                 function (string $language) {
  211.                     return [
  212.                         'url' => $language != SITE_DEFAULT_LANGUAGE '/' $language '/' '/',
  213.                         'label' => $language,
  214.                         'name' => Language::msg(mb_strtoupper($language)),
  215.                         'current' => $language === LANGUAGE,
  216.                     ];
  217.                 },
  218.                 Language::getActiveLanguages()
  219.             )
  220.         );
  221.     }
  222.     protected function parsePositions(): void
  223.     {
  224.         $positions = [];
  225.         $mainVariables $this->template->getAssignedVariables();
  226.         foreach ($this->record['positions'] as $position => $blocks) {
  227.             foreach ($blocks as $i => $block) {
  228.                 $positions[$position][$i] = $this->parseBlock($block$mainVariables);
  229.             }
  230.             $this->template->assign('position' . \SpoonFilter::ucfirst($position), $positions[$position]);
  231.         }
  232.         $category '';
  233.         if (!empty($this->url->getParameter(1)))
  234.             $category WebAdvertisementModel::getCategory($this->url->getParameter(1));
  235.         elseif ($this->getContainer()->get('request_stack')->getCurrentRequest()->getPathInfo() == '/bedrijven' && $this->getContainer()->get('request_stack')->getCurrentRequest()->query->has('adres')) {
  236.             $category ApplicationAdvertisementModel::getCategoryByName($this->getContainer()->get('request_stack')->getCurrentRequest()->query->get('adres'));
  237.         }
  238.         if (!empty($category)) {
  239.             $banners ApplicationAdvertisementModel::getMediaByRubriek($category['id'], 'banner');
  240.             $bannersBig ApplicationAdvertisementModel::getMediaByRubriek($category['id'], 'banner_big');
  241.             $leaderboards ApplicationAdvertisementModel::getMediaByRubriek($category['id'], 'leaderboard');
  242.             if (!empty($banners)) {
  243.                 unset($positions['logogridsidebar']);
  244.                 foreach ($banners as $banner) {
  245.                     $title $banner['title'];
  246.                     $url '/src/Web/Files/Advertisement/banner/' $category['id'] . '/source/' $banner['filename'];
  247.                     $html '<div onclick="stats(\'banner\', false, \'' $title '\')" class="single-logo-overzichtspagina-grid" data-href="' strtolower($banner['url']) . '">
  248.     <img class="logo-bedrijf" src="' $url '" alt="placeholder" data-label="Logo bedrijf (225px X 110px)" data-type="image" style="display: block;">
  249.     <div class="single-logo-overzichtspagina-grid-link">
  250.         <a href="' strtolower($banner['url']) . '" target="_blank" class="hidden" data-label="Link naar bedrijf advertentie" data-type="link">' $banner['title'] . '</a>
  251.     </div>    
  252. </div>';
  253.                     $positions['logogridsidebar'][]['html'] = $html;
  254.                 }
  255.             }
  256.             if (!empty($bannersBig)) {
  257.                 unset($positions['biglogossidebar']);
  258.                 foreach ($bannersBig as $bannerBig) {
  259.                     $title $bannerBig['title'];
  260.                     $url '/src/Web/Files/Advertisement/banner_big/' $category['id'] . '/source/' $bannerBig['filename'];
  261.                     $html '<div onclick="stats(\'banner_big\', false, \'' $title '\')" class="single-logo-overzichtspagina-groot" data-href="' strtolower($bannerBig['url']) . '">
  262.     <img class="logo-bedrijf-groot" src="' $url '" alt="placeholder" data-label="Logo bedrijf (480px X 380px)" data-type="image" style="display: block;">
  263.     <div class="single-logo-overzichtspagina-groot-link">
  264.         <a href="' strtolower($bannerBig['url']) . '" class="hidden" data-label="Link naar bedrijf advertentie" data-type="link">' $bannerBig['title'] . '</a>
  265.     </div>        
  266. </div>';
  267.                     $positions['biglogossidebar'][]['html'] = $html;
  268.                 }
  269.             }
  270.             if (!empty($leaderboards)) {
  271.                 unset($positions['advertentieafbeelding']);
  272.                 foreach ($leaderboards as $leaderboard) {
  273.                     $title $leaderboard['title'];
  274.                     $url '/src/Web/Files/Advertisement/leaderboard/' $category['id'] . '/source/' $leaderboard['filename'];
  275.                     $html '<div onclick="stats(\'leaderboard\', false, \'' $title '\')" class="slider-overzichtspagina-top-container blank-data-href"><img class="single-advertentie-overzichtspagina hide-670" src="'.$url.'" alt="placeholder" data-label="'.$title.'" data-type="image" style="display: block;">
  276.     <div class="single-advertentie-overzichtspagina-link">
  277.         <a href="' strtolower($leaderboard['url']) . '" class="hidden" data-label="Link naar bedrijf '.$title.'" data-type="link">' strtolower($leaderboard['url']) . '</a>
  278.     </div></div>';
  279.                     $positions['advertentieafbeelding'][]['html'] = $html;
  280.                 }
  281.             }
  282.         }
  283.         $this->template->assign('positions'$positions);
  284.     }
  285.     private function parseBlock(array $block, array $mainVariables): array
  286.     {
  287.         if (!isset($block['extra'])) {
  288.             $parsedBlock $block;
  289.             if (array_key_exists('blockContent'$block)) {
  290.                 $parsedBlock['html'] = $block['blockContent'];
  291.             }
  292.             return $parsedBlock;
  293.         }
  294.         $block['extra']->execute();
  295.         $extraVariables $block['extra']->getTemplate()->getAssignedVariables();
  296.         $block['extra']->getTemplate()->assignArray($mainVariables);
  297.         $block['extra']->getTemplate()->assignArray($extraVariables);
  298.         return [
  299.             'variables' => $block['extra']->getTemplate()->getAssignedVariables(),
  300.             'blockIsEditor' => false,
  301.             'html' => $block['extra']->getContent(),
  302.         ];
  303.     }
  304.     protected function processExtra(ModuleExtraInterface $extra): void
  305.     {
  306.         $this->getContainer()->get('logger.public')->info(
  307.             'Executing ' get_class($extra) . " '{$extra->getAction()}' for module '{$extra->getModule()}'."
  308.         );
  309.         if (is_callable([$extra'getOverwrite']) && $extra->getOverwrite()) {
  310.             $this->templatePath $extra->getTemplatePath();
  311.         }
  312.     }
  313.     private function addAlternateLinks(): void
  314.     {
  315.         if (!$this->getContainer()->getParameter('site.multilanguage')) {
  316.             return;
  317.         }
  318.         array_map([$this'addAlternateLinkForLanguage'], Language::getActiveLanguages());
  319.     }
  320.     private function addAlternateLinkForLanguage(string $language): void
  321.     {
  322.         if ($language === LANGUAGE) {
  323.             return;
  324.         }
  325.         $pageInfo Model::getPage($this->pageId);
  326.         if (isset($pageInfo['data']['hreflang_' $language])) {
  327.             $url Navigation::getUrl($pageInfo['data']['hreflang_' $language], $language);
  328.         } else {
  329.             $url Navigation::getUrl($this->pageId$language);
  330.         }
  331.         if ($this->pageId !== Response::HTTP_NOT_FOUND
  332.             && $url === Navigation::getUrl(Response::HTTP_NOT_FOUND$language)) {
  333.             return;
  334.         }
  335.         if (strpos($url'/') === 0) {
  336.             $url SITE_URL $url;
  337.         }
  338.         $this->header->addLink(['rel' => 'alternate''hreflang' => $language'href' => $url]);
  339.     }
  340.     private function assignPageMeta(): void
  341.     {
  342.         $this->header->setPageTitle(
  343.             $this->record['meta_title'],
  344.             $this->record['meta_title_overwrite']
  345.         );
  346.         $this->header->addMetaDescription(
  347.             $this->record['meta_description'],
  348.             $this->record['meta_description_overwrite']
  349.         );
  350.         $this->header->addMetaKeywords(
  351.             $this->record['meta_keywords'],
  352.             $this->record['meta_keywords_overwrite']
  353.         );
  354.         $this->header->setMetaCustom($this->record['meta_custom']);
  355.         if (isset($this->record['meta_seo_index'])) {
  356.             $this->header->addMetaData(
  357.                 ['name' => 'robots''content' => $this->record['meta_seo_index']]
  358.             );
  359.         }
  360.         if (isset($this->record['meta_seo_follow'])) {
  361.             $this->header->addMetaData(
  362.                 ['name' => 'robots''content' => $this->record['meta_seo_follow']]
  363.             );
  364.         }
  365.     }
  366.     protected function processPage(): void
  367.     {
  368.         $this->assignPageMeta();
  369.         new Navigation($this->getKernel());
  370.         $this->addAlternateLinks();
  371.         $pageInfo Navigation::getPageInfo($this->record['id']);
  372.         $this->record['has_children'] = $pageInfo['has_children'];
  373.         $this->template->assignGlobal('page'$this->record);
  374.         $this->templatePath $this->record['template_path'];
  375.         foreach ($this->record['positions'] as $position => &$blocks) {
  376.             if (!in_array($position$this->record['template_data']['names'])) {
  377.                 continue;
  378.             }
  379.             $blocks array_map(
  380.                 function (array $block) {
  381.                     if ($block['extra_id'] === null) {
  382.                         return [
  383.                             'blockIsEditor' => true,
  384.                             'blockContent' => $block['html'],
  385.                         ];
  386.                     }
  387.                     $block = ['extra' => $this->getExtraForBlock($block)];
  388.                     $this->extras[] = $block['extra'];
  389.                     return $block;
  390.                 },
  391.                 $blocks
  392.             );
  393.         }
  394.     }
  395.     private function getExtraForBlock(array $block): ModuleExtraInterface
  396.     {
  397.         if ($block['extra_type'] === 'block') {
  398.             if (extension_loaded('newrelic')) {
  399.                 newrelic_name_transaction($block['extra_module'] . '::' $block['extra_action']);
  400.             }
  401.             return new WebBlockExtra(
  402.                 $this->getKernel(),
  403.                 $block['extra_module'],
  404.                 $block['extra_action'],
  405.                 $block['extra_data']
  406.             );
  407.         }
  408.         return new WebBlockWidget(
  409.             $this->getKernel(),
  410.             $block['extra_module'],
  411.             $block['extra_action'],
  412.             $block['extra_data']
  413.         );
  414.     }
  415.     private function redirect(string $urlint $code RedirectResponse::HTTP_FOUND): void
  416.     {
  417.         throw new RedirectException('Redirect', new RedirectResponse($url$code));
  418.     }
  419. }