gdprcookieconsent/views/js/gdpr_cookie.js
Walz 677b2481ce Initial commit
- Functional module
2025-04-07 16:41:38 +02:00

198 lines
6.5 KiB
JavaScript

/**
* GDPR Cookie Consent Module for PrestaShop
*
* @author Walzen665
* @copyright Copyright (c) 2025
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
document.addEventListener('DOMContentLoaded', function() {
// Cookie functions
function setCookie(name, value, days) {
var expires = '';
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = '; expires=' + date.toUTCString();
}
document.cookie = name + '=' + (value || '') + expires + '; path=/; SameSite=Lax';
}
function getCookie(name) {
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
// Check if cookie consent is already set
var cookieConsent = getCookie('gdpr_cookie_consent');
if (!cookieConsent) {
// Show the cookie banner if consent is not set
document.getElementById('gdpr-cookie-banner').style.display = 'block';
} else {
// Apply cookie preferences
var consentPreferences = JSON.parse(cookieConsent);
applyConsentPreferences(consentPreferences);
}
// Banner buttons
document.getElementById('gdpr-cookie-banner-accept').addEventListener('click', function() {
acceptAllCookies();
});
document.getElementById('gdpr-cookie-banner-decline').addEventListener('click', function() {
declineAllCookies();
});
document.getElementById('gdpr-cookie-banner-settings').addEventListener('click', function() {
showModal();
});
// Modal buttons
document.getElementById('gdpr-cookie-accept-all').addEventListener('click', function() {
acceptAllCookies();
});
document.getElementById('gdpr-cookie-decline').addEventListener('click', function() {
declineAllCookies();
});
document.getElementById('gdpr-cookie-accept-selected').addEventListener('click', function() {
acceptSelectedCookies();
});
// Functions
function showModal() {
document.getElementById('gdpr-cookie-banner').style.display = 'none';
document.getElementById('gdpr-cookie-modal').style.display = 'block';
// Load saved preferences if they exist
var cookieConsent = getCookie('gdpr_cookie_consent');
if (cookieConsent) {
var preferences = JSON.parse(cookieConsent);
// Set checkboxes based on saved preferences
document.querySelectorAll('.gdpr-cookie-checkbox').forEach(function(checkbox) {
var category = checkbox.getAttribute('data-cookie-category');
if (preferences[category]) {
checkbox.checked = true;
} else {
checkbox.checked = false;
}
});
}
}
function hideModal() {
document.getElementById('gdpr-cookie-modal').style.display = 'none';
}
function acceptAllCookies() {
var preferences = {
necessary: true,
functional: true,
analytics: true,
marketing: true
};
setCookie('gdpr_cookie_consent', JSON.stringify(preferences), 365);
applyConsentPreferences(preferences);
hideModal();
document.getElementById('gdpr-cookie-banner').style.display = 'none';
}
function declineAllCookies() {
var preferences = {
necessary: true, // Necessary cookies are always accepted
functional: false,
analytics: false,
marketing: false
};
setCookie('gdpr_cookie_consent', JSON.stringify(preferences), 365);
applyConsentPreferences(preferences);
hideModal();
document.getElementById('gdpr-cookie-banner').style.display = 'none';
}
function acceptSelectedCookies() {
var preferences = {
necessary: true // Necessary cookies are always accepted
};
// Get selected preferences
document.querySelectorAll('.gdpr-cookie-checkbox').forEach(function(checkbox) {
var category = checkbox.getAttribute('data-cookie-category');
preferences[category] = checkbox.checked;
});
setCookie('gdpr_cookie_consent', JSON.stringify(preferences), 365);
applyConsentPreferences(preferences);
hideModal();
document.getElementById('gdpr-cookie-banner').style.display = 'none';
}
function applyConsentPreferences(preferences) {
// Example implementation
// You would need to adapt this based on your specific cookie usage
// Functional cookies
if (!preferences.functional) {
// Disable functional cookies
removeFunctionalCookies();
}
// Analytics cookies
if (!preferences.analytics) {
// Disable analytics cookies (like Google Analytics)
disableAnalytics();
} else {
// Enable analytics
enableAnalytics();
}
// Marketing cookies
if (!preferences.marketing) {
// Disable marketing cookies
disableMarketing();
} else {
// Enable marketing
enableMarketing();
}
}
// Helper functions to implement consent preferences
function removeFunctionalCookies() {
// This is just an example - implement based on your specific needs
var functionalCookies = ['prefs', 'language', 'theme'];
functionalCookies.forEach(function(cookie) {
document.cookie = cookie + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
});
}
function disableAnalytics() {
// Example: Disable Google Analytics
window['ga-disable-UA-XXXXXXXX-X'] = true;
}
function enableAnalytics() {
// Example: Enable Google Analytics
window['ga-disable-UA-XXXXXXXX-X'] = false;
}
function disableMarketing() {
// Example implementation
// You would implement according to your specific marketing cookies
}
function enableMarketing() {
// Example implementation
// You would implement according to your specific marketing cookies
}
});