public function dispatchLoopShutdown()
{
$config = \Pimcore\Config::getSystemConfig();
if (!$config->general->show_cookie_notice || !Tool::useFrontendOutputFilters($this->getRequest()) || !Tool::isHtmlResponse($this->getResponse())) {
return;
}
$template = $this->getTemplateCode();
// cleanup code
$template = preg_replace('/[\\r\\n\\t]+/', ' ', $template);
//remove new lines, spaces, tabs
$template = preg_replace('/>[\\s]+</', '><', $template);
//remove new lines, spaces, tabs
$template = preg_replace('/[\\s]+/', ' ', $template);
//remove new lines, spaces, tabs
$translations = $this->getTranslations();
foreach ($translations as $key => &$value) {
$value = htmlentities($value, ENT_COMPAT, "UTF-8");
$template = str_replace("%" . $key . "%", $value, $template);
}
$linkContent = "";
if (array_key_exists("linkTarget", $translations)) {
$linkContent = '<a href="' . $translations["linkTarget"] . '" data-content="' . $translations["linkText"] . '"></a>';
}
$template = str_replace("%link%", $linkContent, $template);
$templateCode = \Zend_Json::encode($template);
$code = '
<script>
(function () {
var ls = window["localStorage"];
if(ls && !ls.getItem("pc-cookie-accepted")) {
var code = ' . $templateCode . ';
var ci = window.setInterval(function () {
if(document.body) {
clearInterval(ci);
document.body.insertAdjacentHTML("beforeend", code);
document.getElementById("pc-button").onclick = function () {
document.getElementById("pc-cookie-notice").style.display = "none";
ls.setItem("pc-cookie-accepted", "true");
};
}
}, 100);
}
})();
</script>
';
$body = $this->getResponse()->getBody();
// search for the end <head> tag, and insert the google analytics code before
// this method is much faster than using simple_html_dom and uses less memory
$headEndPosition = stripos($body, "</head>");
if ($headEndPosition !== false) {
$body = substr_replace($body, $code . "</head>", $headEndPosition, 7);
}
$this->getResponse()->setBody($body);
}