src/Frontend/Core/Engine/Footer.php line 44

Open in your IDE?
  1. <?php
  2. namespace Frontend\Core\Engine;
  3. use ForkCMS\App\KernelLoader;
  4. use Symfony\Component\HttpKernel\KernelInterface;
  5. use Frontend\Core\Engine\Navigation as FrontendNavigation;
  6. /**
  7.  * This class will be used to alter the footer-part of the HTML-document that will be created by the frontend.
  8.  */
  9. class Footer extends KernelLoader
  10. {
  11.     /**
  12.      * TwigTemplate instance
  13.      *
  14.      * @var TwigTemplate
  15.      */
  16.     protected $template;
  17.     /**
  18.      * URL instance
  19.      *
  20.      * @var Url
  21.      */
  22.     protected $url;
  23.     public function __construct(KernelInterface $kernel)
  24.     {
  25.         parent::__construct($kernel);
  26.         $this->template $this->getContainer()->get('templating');
  27.         $this->url $this->getContainer()->get('url');
  28.         $this->getContainer()->set('footer'$this);
  29.     }
  30.     /**
  31.      * Parse the footer into the template
  32.      */
  33.     public function parse(): void
  34.     {
  35.         $footerLinks = (array) Navigation::getFooterLinks();
  36.         dump($footerLinks);
  37.         $this->template->assignGlobal('footerLinks'$footerLinks);
  38.         $siteHTMLFooter = (string) $this->get('fork.settings')->get('Core''site_html_footer'null);
  39.         $facebookAppId $this->get('fork.settings')->get('Core''facebook_app_id'null);
  40.         // facebook admins given?
  41.         if ($facebookAppId !== null) {
  42.             // add Facebook container
  43.             $siteHTMLFooter .= $this->getFacebookHtml($facebookAppId);
  44.         }
  45.         // add Google sitelinks search box code if wanted.
  46.         if ($this->get('fork.settings')->get('Search''use_sitelinks_search_box'true)) {
  47.             $searchUrl FrontendNavigation::getUrlForBlock('Search');
  48.             $url404 FrontendNavigation::getUrl(Model::ERROR_PAGE_ID);
  49.             if ($searchUrl !== $url404) {
  50.                 $siteHTMLFooter .= $this->getSiteLinksCode($searchUrl);
  51.             }
  52.         }
  53.         // assign site wide html
  54.         $this->template->assignGlobal('siteHTMLFooter'$siteHTMLFooter);
  55.     }
  56.     /**
  57.      * Builds the HTML needed for Facebook to be initialized
  58.      *
  59.      * @param string $facebookAppId The application id used to interact with FB
  60.      *
  61.      * @return string  HTML and JS needed to initialize FB JavaScript
  62.      */
  63.     protected function getFacebookHtml(string $facebookAppId): string
  64.     {
  65.         // add the fb-root div
  66.         $facebookHtml "\n" '<div id="fb-root"></div>' "\n";
  67.         // add facebook JavaScript
  68.         $facebookHtml .= '<script>' "\n";
  69.         if (!empty($facebookAppId)) {
  70.             $facebookHtml .= '    window.fbAsyncInit = function() {' "\n";
  71.             $facebookHtml .= '        FB.init({' "\n";
  72.             $facebookHtml .= '            appId: "' $facebookAppId '",' "\n";
  73.             $facebookHtml .= '            status: true,' "\n";
  74.             $facebookHtml .= '            cookie: true,' "\n";
  75.             $facebookHtml .= '            xfbml: true,' "\n";
  76.             $facebookHtml .= '            oauth: true' "\n";
  77.             $facebookHtml .= '        });' "\n";
  78.             $facebookHtml .= '        jsFrontend.facebook.afterInit();' "\n";
  79.             $facebookHtml .= '    };' "\n";
  80.         }
  81.         $facebookHtml .= '    (function(d, s, id){' "\n";
  82.         $facebookHtml .= '        var js, fjs = d.getElementsByTagName(s)[0];' "\n";
  83.         $facebookHtml .= '        if (d.getElementById(id)) {return;}' "\n";
  84.         $facebookHtml .= '        js = d.createElement(s); js.id = id;' "\n";
  85.         $facebookHtml .= '        js.src = "//connect.facebook.net/' $this->getFacebookLocale() . '/all.js";' "\n";
  86.         $facebookHtml .= '        fjs.parentNode.insertBefore(js, fjs);' "\n";
  87.         $facebookHtml .= '    }(document, \'script\', \'facebook-jssdk\'));' "\n";
  88.         $facebookHtml .= '</script>';
  89.         return $facebookHtml;
  90.     }
  91.     private function getFacebookLocale(): string
  92.     {
  93.         $specialCases = [
  94.             'en' => 'en_US'// sorry uk :( I prefer you too
  95.             'zh' => 'zh_CN',
  96.             'cs' => 'cs_CZ',
  97.             'el' => 'el_GR',
  98.             'ja' => 'ja_JP',
  99.             'sv' => 'sv_SE',
  100.             'uk' => 'uk_UA',
  101.         ];
  102.         // check if it is a special case, otherwise return [language]_[language]
  103.         return $specialCases[LANGUAGE] ?? mb_strtolower(LANGUAGE) . '_' mb_strtoupper(LANGUAGE);
  104.     }
  105.     /**
  106.      * Returns the code needed to get a site links search box in Google.
  107.      * More information can be found on the offical Google documentation:
  108.      * https://developers.google.com/webmasters/richsnippets/sitelinkssearch
  109.      *
  110.      * @param string $searchUrl The url to the search page
  111.      *
  112.      * @return string The script needed for google
  113.      */
  114.     protected function getSiteLinksCode(string $searchUrl): string
  115.     {
  116.         $siteLinksCode '<script type="application/ld+json">' "\n";
  117.         $siteLinksCode .= '{' "\n";
  118.         $siteLinksCode .= '    "@context": "https://schema.org",' "\n";
  119.         $siteLinksCode .= '    "@type": "WebSite",' "\n";
  120.         $siteLinksCode .= '    "url": "' SITE_URL '",' "\n";
  121.         $siteLinksCode .= '    "potentialAction": {' "\n";
  122.         $siteLinksCode .= '        "@type": "SearchAction",' "\n";
  123.         $siteLinksCode .= '        "target": "' SITE_URL $searchUrl '?form=search&q_widget={q_widget}",' "\n";
  124.         $siteLinksCode .= '        "query-input": "name=q_widget"' "\n";
  125.         $siteLinksCode .= '    }' "\n";
  126.         $siteLinksCode .= '}' "\n";
  127.         $siteLinksCode .= '</script>';
  128.         return $siteLinksCode;
  129.     }
  130. }