Skip to content

Commit

Permalink
Feature: Re-open banner after time
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikdro committed Sep 2, 2021
1 parent 39a6874 commit 0286480
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 25 deletions.
27 changes: 21 additions & 6 deletions Classes/Controller/JavaScriptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ public function initializeRenderJavaScriptAction()
$this->response->setComponentParameter(SetHeaderComponent::class, 'Cache-Control', 'max-age=0, private, must-revalidate');
}


public function renderJavaScriptAction(array $dimensions = [])
{

try {
$filteredDimensions =
array_filter($dimensions, function($key) {
return in_array($key, $this->consentDimensions);
},
ARRAY_FILTER_USE_KEY);
array_filter(
$dimensions,
function ($key) {
return in_array($key, $this->consentDimensions);
},
ARRAY_FILTER_USE_KEY
);

$dimensionIdentifier = implode(
'_',
Expand All @@ -78,12 +81,24 @@ public function renderJavaScriptAction(array $dimensions = [])

$cacheIdentifier = 'kd_gdpr_cc_' . sha1(json_encode($consents) . $dimensionIdentifier . $siteNode->getIdentifier());

$q = new FlowQuery([$siteNode]);

$consentDate = new \DateTime(isset($cookie['consentDates'][$dimensionIdentifier]) ? $cookie['consentDates'][$dimensionIdentifier] : $cookie['consentDate']);
$cookieSettings = $q->find('[instanceof KaufmannDigital.GDPR.CookieConsent:Content.CookieSettings]')->get(0);
$decisionTtl = $cookieSettings->getProperty('decisionTtl') ?? 0;
$expireDate = clone $consentDate;
$expireDate->add(\DateInterval::createFromDateString($decisionTtl . ' seconds'));
if ($expireDate < new \DateTime('now')) {
$this->response->setContentType('text/javascript');
$this->response->setContent('');
return;
}

if ($this->cache->has($cacheIdentifier)) {
$this->redirect('downloadGeneratedJavaScript', null, null, ['hash' => $cacheIdentifier]);
return;
}

$q = new FlowQuery([$siteNode]);
$cookieNodes = $q->find('[instanceof KaufmannDigital.GDPR.CookieConsent:Content.Cookie][javaScriptCode != ""]')->sort('priority', 'DESC')->get();

$javaScript = '';
Expand Down
2 changes: 1 addition & 1 deletion Configuration/NodeTypes.Content.CookieSettings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
label: i18n
inspector:
group: cookieSettings
cookieTtl:
decisionTtl:
type: integer
defaultValue: 31536000
ui:
Expand Down
Binary file added Documentation/Images/Decision_TTL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ To do this, you only need to edit the version date. You can find it in the inspe
After the date has been changed, the banner will be shown again to all visitors, who have submitted the cookie banner before this date. Old settings are used as presets.


#### Invalidating user-decisions after time
Sometimes it is necessary or advantageous to remind the user of his decision and ask him to confirm it again. For this purpose, a TTL for the decision can be set in the backend:
![decision-time setting](Documentation/Images/Decision_TTL.png)
The unit is seconds. After the set time has expired, the banner appears again with the default settings of the last decision, so the user can easily accept the old decision with just one click.
The value `0` (default) disables the repeated display of the banner.
### Styling
#### Custom Banner-Styles
The banner comes with a few basic-styles for positioning, which are getting included inline. To add your custom styles, just put a CSS-Files somewhere in your Resources-Folder and include it using Settings.yaml:
Expand Down
4 changes: 2 additions & 2 deletions Resources/Private/Fusion/Helper/JavaScriptSettings.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ prototype(KaufmannDigital.GDPR.CookieConsent:Helper.JavaScriptSettings) < protot

cookieSettingsNode = ${q(site).find('[instanceof KaufmannDigital.GDPR.CookieConsent:Content.CookieSettings]').get(0)}
@context.versionDate = ${q(this.cookieSettingsNode).property('versionDate')}
@context.cookieTtl = ${q(this.cookieSettingsNode).property('cookieTtl')}
@context.decisionTtl = ${q(this.cookieSettingsNode).property('decisionTtl')}

@context.apiUrl = Neos.Fusion:UriBuilder {
package = 'KaufmannDigital.GDPR.CookieConsent'
Expand All @@ -29,7 +29,7 @@ prototype(KaufmannDigital.GDPR.CookieConsent:Helper.JavaScriptSettings) < protot
cookieName: '" + cookieName +"',
cookieDomainName: '" + cookieDomainName +"',
versionTimestamp: " + versionDate.timestamp * 1000 + ",
cookieTtl: " + (cookieTtl ? (cookieTtl * 1000) : 0) + ",
decisionTtl: " + (decisionTtl ? (decisionTtl * 1000) : 0) + ",
dimensionsIdentifier: '" + (dimensionsIdentifier != '' ? dimensionsIdentifier : 'default') + "',
nodeTypeDisabled: " + (nodeTypeDisabled ? 'true' : 'false') +"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@
<target state="final">Versions-Datum</target>
</trans-unit>

<trans-unit id="properties.cookieTtl" xml:space="preserve">
<trans-unit id="properties.decisionTtl" xml:space="preserve">
<source>TTL (Decision time in seconds)</source>
<target state="final">TTL (Speicherdauer der Entscheidung in Sekunden)</target>
</trans-unit>

<trans-unit id="properties.cookieTtl.ui.help.message" xml:space="preserve">
<trans-unit id="properties.decisionTtl.ui.help.message" xml:space="preserve">
<source>Number of seconds until cookie settings are displayed and confirmed again. Example values are: 86400 (1 day), 604800 (1 week), 2592000 (30 days), 31536000 (1 year)</source>
<target state="final">Anzahl der Sekunden, bis die Cookie-Einstellungen erneut angezeigt und bestätigt werden. Beispielwerte sind: 86400 (1 Tag), 604800 (1 Woche), 2592000 (30 Tage), 31536000 (1 Jahr)</target>
</trans-unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
<source>Version date</source>
</trans-unit>

<trans-unit id="properties.cookieTtl" xml:space="preserve">
<trans-unit id="properties.decisionTtl" xml:space="preserve">
<source>TTL (Decision time in seconds)</source>
</trans-unit>

<trans-unit id="properties.cookieTtl.ui.help.message" xml:space="preserve">
<trans-unit id="properties.decisionTtl.ui.help.message" xml:space="preserve">
<source>Number of seconds until cookie settings are displayed and confirmed again. Example values are: 86400 (1 day), 604800 (1 week), 2592000 (30 days), 31536000 (1 year)</source>
</trans-unit>

Expand Down
13 changes: 7 additions & 6 deletions Resources/Public/JavaScript/Initialize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function loadCookiebannerHtml() {
function loadCookiebannerHtml(openSettings = false) {
if (document.body.classList.contains('neos-backend')) return;
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function() {
Expand All @@ -10,7 +10,7 @@ function loadCookiebannerHtml() {
eval(scriptTags[n].innerHTML);
}
if (typeof initializeCookieConsent === 'function') {
initializeCookieConsent();
initializeCookieConsent(openSettings);
}
});

Expand Down Expand Up @@ -42,12 +42,13 @@ if (KD_GDPR_CC.nodeTypeDisabled === false && document.cookie.indexOf(KD_GDPR_CC.
loadCookiebannerHtml();
}

if (!cookieObject.expireDates || !cookieObject.expireDates[KD_GDPR_CC.dimensionsIdentifier]) {
loadCookiebannerHtml();
} else if (new Date(cookieObject.expireDates[KD_GDPR_CC.dimensionsIdentifier]) < new Date()) {
loadCookiebannerHtml();
//Re-Open Cookie-Consent, if TTL is expired
var decisionExpiry = cookieConsentDate.getTime() + KD_GDPR_CC.decisionTtl;
if (KD_GDPR_CC.decisionTtl > 0 && decisionExpiry < new Date()) {
loadCookiebannerHtml(true);
}


window.dataLayer.push({
event: 'KD_GDPR_CC_consent',
KD_GDPR_CC: {
Expand Down
13 changes: 7 additions & 6 deletions Resources/Public/JavaScript/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);

function initializeCookieConsent() {
function initializeCookieConsent(openSettings = false) {

var kd_gdpr_cc_userid;
var cookieSettingsContainer = document.querySelector('.gdpr-cookieconsent-settings');
Expand Down Expand Up @@ -56,6 +56,10 @@ function initializeCookieConsent() {
btnAcceptAll.style.display = 'none';
btnSaveSettings.style.display = 'block';
});
if (openSettings) {
let clickEvent = new MouseEvent('click');
btnIndividualSettingsEnable.dispatchEvent(clickEvent);
}

btnIndividualSettingsDisable.addEventListener('click', function() {
individualSettingsContainer.style.display = 'none';
Expand Down Expand Up @@ -190,8 +194,8 @@ function dispatchEventsForCookies(inputs) {
function saveConsentToCookie(inputs, userId) {
var cookie = decodeCookie();
var currentDate = new Date();
if (KD_GDPR_CC.cookieTtl && KD_GDPR_CC.cookieTtl > 0) {
expireDate = new Date(currentDate.getTime() + KD_GDPR_CC.cookieTtl);
if (KD_GDPR_CC.decisionTtl && KD_GDPR_CC.decisionTtl > 0) {
expireDate = new Date(currentDate.getTime() + KD_GDPR_CC.decisionTtl);
} else {
var expireDate = new Date(currentDate.getFullYear() + 1, currentDate.getMonth(), currentDate.getDate());
}
Expand All @@ -205,14 +209,11 @@ function saveConsentToCookie(inputs, userId) {
var consentDates = cookie && cookie.consentDates ? cookie.consentDates : {};
consentDates[KD_GDPR_CC.dimensionsIdentifier] = currentDate.toUTCString();

var expireDates = cookie && cookie.expireDates ? cookie.expireDates : {};
expireDates[KD_GDPR_CC.dimensionsIdentifier] = expireDate.toUTCString();

var cookieData = {
userId: userId,
consents: consents,
consentDates: consentDates,
expireDates: expireDates,
consentDate: currentDate.toUTCString(),
expireDate: expireDate.toUTCString()
};
Expand Down

0 comments on commit 0286480

Please sign in to comment.