Skip to content

Commit

Permalink
Use template file to make the notice translatable and move stuff out …
Browse files Browse the repository at this point in the history
…of JS
  • Loading branch information
andersju committed May 28, 2021
1 parent 2342905 commit 06e407f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 27 deletions.
44 changes: 33 additions & 11 deletions cookieNotice/CookieNoticePlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,43 @@ public function register($category, $path, $mainContextId = NULL) {
$success = parent::register($category, $path);

if ($success && $this->getEnabled()) {
HookRegistry::register('TemplateManager::display', array($this, 'registerResources'));
HookRegistry::register('Templates::Common::Footer::PageFooter', array($this, 'insertHTML'));
}

return $success;
}

function registerResources($hookName, $params) {
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);

$templateMgr->addStyleSheet(
'cookieNoticeStyles',
$request->getBaseUrl() . '/' . $this->getPluginPath() . '/css/cookieNotice.css',
['context' => ['backend', 'frontend']]
);

$templateMgr->addJavaScript(
'cookieNoticeScript',
$request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/cookieNotice.js',
['context' => ['backend', 'frontend']]
);

return false;
}

function insertHTML($hookName, $params) {
$request = Application::get()->getRequest();

if ($request->getCookieVar('cookieNotice') != 1) {
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
$templateMgr->addStyleSheet(
'cookieNoticeStyles',
$request->getBaseUrl() . '/' . $this->getPluginPath() . '/css/cookieNotice.css',
['context' => ['backend', 'frontend']]
);
$templateMgr->addJavaScript(
'cookieNoticeScript',
$request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/cookieNotice.js',
['context' => ['backend', 'frontend']]
);
$output =& $params[2];
$output .= $templateMgr->fetch($this->getTemplateResource('cookieNotice.tpl'));
}

return $success;
return false;
}

public function getDisplayName() {
Expand Down
19 changes: 5 additions & 14 deletions cookieNotice/js/cookieNotice.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
let cookieInfoUrl = 'https://publicera.kb.se/kakor';
let cookieDurationDays = 365;

document.addEventListener('DOMContentLoaded', function() {
if (!document.cookie.split(';').some((item) => item.trim().startsWith('cookieNotice='))) {
const confirmationHtml = `
<div id="cookie-notice" class="alert alert-primary" role="alert">
<div class="container">
Genom att använda Publicera godkänner du de kakor (cookies) som finns på webbplatsen. <a href="${cookieInfoUrl}">Läs mer om hur vi använder kakor</a>.
<button id="cookie-notice-confirmation" type="button" class="btn btn-secondary btn-light btn-sm">OK, jag förstår</button>
</div>
</div>
`;
document.body.insertAdjacentHTML('afterbegin', confirmationHtml);
let cookieNotice = document.getElementById('cookie-notice');
document.body.insertAdjacentElement('afterbegin', cookieNotice);

document.getElementById('cookie-notice-confirmation').addEventListener('click', function() {
document.cookie = `cookieNotice=1; max-age=${60*60*24*cookieDurationDays}`;
document.cookie = `cookieNotice=1; max-age=${60*60*24*365}; SameSite=Strict`;
document.getElementById('cookie-notice').classList.add('cookie-notice-hidden');
});
}
});
});
6 changes: 6 additions & 0 deletions cookieNotice/locale/en_US/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ msgstr "Cookie Notice Plugin"

msgid "plugins.generic.cookieNotice.description"
msgstr "Turns on a site-wide cookie notice."

msgid "plugins.generic.cookieNotice.infoText"
msgstr "By using Publicera you accept the cookies that we use on the website. <a href=\"/index/cookies\">Read more about how we use cookies</a>."

msgid "plugins.generic.cookieNotice.confirmationButtonText"
msgstr "OK, I understand"
6 changes: 6 additions & 0 deletions cookieNotice/locale/sv_SE/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ msgstr "Cookie-notifieringsplugin"

msgid "plugins.generic.cookieNotice.description"
msgstr "Informerar besökare om att cookies används."

msgid "plugins.generic.cookieNotice.infoText"
msgstr "Genom att använda Publicera godkänner du de kakor (cookies) som finns på webbplatsen. <a href=\"/index/cookies\">Läs mer om hur vi använder kakor</a>."

msgid "plugins.generic.cookieNotice.confirmationButtonText"
msgstr "OK, jag förstår"
8 changes: 8 additions & 0 deletions cookieNotice/templates/cookieNotice.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div id="cookie-notice" class="alert alert-primary" role="alert">
<div class="container">
{translate key='plugins.generic.cookieNotice.infoText'}
<button id="cookie-notice-confirmation" type="button" class="btn btn-secondary btn-light btn-sm">
{translate key='plugins.generic.cookieNotice.confirmationButtonText'}
</button>
</div>
</div>
4 changes: 2 additions & 2 deletions cookieNotice/version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<version>
<application>cookieNotice</application>
<type>plugins.generic</type>
<release>1.0.0</release>
<date>2021-05-25</date>
<release>1.0.1</release>
<date>2021-05-28</date>
<lazy-load>1</lazy-load>
<sitewide>1</sitewide>
<class>CookieNoticePlugin</class>
Expand Down

0 comments on commit 06e407f

Please sign in to comment.