gdprcookieconsent/gdprcookieconsent.php

356 lines
16 KiB
PHP

<?php
/**
* 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)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class GdprCookieConsent extends Module
{
public function __construct()
{
$this->name = 'gdprcookieconsent';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Walzen665';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '1.7.0.0',
'max' => _PS_VERSION_
];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('GDPR Cookie Consent');
$this->description = $this->l('Adds a GDPR compliant cookie consent modal to your shop');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
}
/**
* Install the module
*/
public function install()
{
return parent::install() &&
$this->registerHook('displayHeader') &&
$this->registerHook('displayFooter') &&
Configuration::updateValue('GDPR_COOKIE_ENABLED', 1) &&
Configuration::updateValue('GDPR_COOKIE_MESSAGE', 'This website uses cookies to ensure you get the best experience. We collect and process your data for website functionality, analytics, and personalized advertising.') &&
Configuration::updateValue('GDPR_COOKIE_ACCEPT', 'Accept All Cookies') &&
Configuration::updateValue('GDPR_COOKIE_DECLINE', 'Decline Non-Essential') &&
Configuration::updateValue('GDPR_COOKIE_SETTINGS', 'Cookie Settings') &&
Configuration::updateValue('GDPR_COOKIE_MORE_INFO', 'More Information') &&
Configuration::updateValue('GDPR_COOKIE_MORE_INFO_URL', 'content/2-privacy-policy') &&
Configuration::updateValue('GDPR_COOKIE_DATA_CONTROLLER', 'Your Company Name') &&
Configuration::updateValue('GDPR_COOKIE_RETENTION_PERIOD', '365 days') &&
Configuration::updateValue('GDPR_COOKIE_THIRD_PARTIES', 'Google Analytics, Facebook, etc.') &&
Configuration::updateValue('GDPR_COOKIE_MANAGE_TEXT', 'Manage Cookie Preferences') &&
Configuration::updateValue('GDPR_COOKIE_ONLY_REQUIRED', 1);
}
/**
* Uninstall the module
*/
public function uninstall()
{
return parent::uninstall() &&
Configuration::deleteByName('GDPR_COOKIE_ENABLED') &&
Configuration::deleteByName('GDPR_COOKIE_MESSAGE') &&
Configuration::deleteByName('GDPR_COOKIE_ACCEPT') &&
Configuration::deleteByName('GDPR_COOKIE_DECLINE') &&
Configuration::deleteByName('GDPR_COOKIE_SETTINGS') &&
Configuration::deleteByName('GDPR_COOKIE_MORE_INFO') &&
Configuration::deleteByName('GDPR_COOKIE_MORE_INFO_URL') &&
Configuration::deleteByName('GDPR_COOKIE_DATA_CONTROLLER') &&
Configuration::deleteByName('GDPR_COOKIE_RETENTION_PERIOD') &&
Configuration::deleteByName('GDPR_COOKIE_THIRD_PARTIES') &&
Configuration::deleteByName('GDPR_COOKIE_MANAGE_TEXT') &&
Configuration::deleteByName('GDPR_COOKIE_ONLY_REQUIRED');
}
/**
* Load the configuration form
*/
public function getContent()
{
$output = '';
// If form submitted
if (Tools::isSubmit('submitGdprCookieModule')) {
// Get configuration values from form
$enabled = (int)Tools::getValue('GDPR_COOKIE_ENABLED');
$message = Tools::getValue('GDPR_COOKIE_MESSAGE');
$accept = Tools::getValue('GDPR_COOKIE_ACCEPT');
$decline = Tools::getValue('GDPR_COOKIE_DECLINE');
$settings = Tools::getValue('GDPR_COOKIE_SETTINGS');
$moreInfo = Tools::getValue('GDPR_COOKIE_MORE_INFO');
$moreInfoUrl = Tools::getValue('GDPR_COOKIE_MORE_INFO_URL');
$dataController = Tools::getValue('GDPR_COOKIE_DATA_CONTROLLER');
$retentionPeriod = Tools::getValue('GDPR_COOKIE_RETENTION_PERIOD');
$thirdParties = Tools::getValue('GDPR_COOKIE_THIRD_PARTIES');
$manageText = Tools::getValue('GDPR_COOKIE_MANAGE_TEXT');
$onlyRequired = Tools::getValue('GDPR_COOKIE_ONLY_REQUIRED');
// Update configuration values
Configuration::updateValue('GDPR_COOKIE_ENABLED', $enabled);
Configuration::updateValue('GDPR_COOKIE_MESSAGE', $message);
Configuration::updateValue('GDPR_COOKIE_ACCEPT', $accept);
Configuration::updateValue('GDPR_COOKIE_DECLINE', $decline);
Configuration::updateValue('GDPR_COOKIE_SETTINGS', $settings);
Configuration::updateValue('GDPR_COOKIE_MORE_INFO', $moreInfo);
Configuration::updateValue('GDPR_COOKIE_MORE_INFO_URL', $moreInfoUrl);
Configuration::updateValue('GDPR_COOKIE_DATA_CONTROLLER', $dataController);
Configuration::updateValue('GDPR_COOKIE_RETENTION_PERIOD', $retentionPeriod);
Configuration::updateValue('GDPR_COOKIE_THIRD_PARTIES', $thirdParties);
Configuration::updateValue('GDPR_COOKIE_MANAGE_TEXT', $manageText);
Configuration::updateValue('GDPR_COOKIE_ONLY_REQUIRED', $onlyRequired);
// Display confirmation
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
// Display the configuration form
return $output . $this->displayForm();
}
/**
* Create the configuration form
*/
protected function displayForm()
{
// Init Fields form array
$form = [
'form' => [
'legend' => [
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
],
'input' => [
[
'type' => 'switch',
'label' => $this->l('Enable Cookie Consent'),
'name' => 'GDPR_COOKIE_ENABLED',
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
]
],
],
[
'type' => 'textarea',
'label' => $this->l('Cookie Consent Message'),
'name' => 'GDPR_COOKIE_MESSAGE',
'required' => true,
],
[
'type' => 'text',
'label' => $this->l('Data Controller'),
'name' => 'GDPR_COOKIE_DATA_CONTROLLER',
'desc' => $this->l('Your company/organization name'),
'required' => true,
],
[
'type' => 'text',
'label' => $this->l('Cookie Retention Period'),
'name' => 'GDPR_COOKIE_RETENTION_PERIOD',
'desc' => $this->l('How long cookies will be stored (e.g., 365 days)'),
'required' => true,
],
[
'type' => 'textarea',
'label' => $this->l('Third-Party Recipients'),
'name' => 'GDPR_COOKIE_THIRD_PARTIES',
'desc' => $this->l('List third parties that receive cookie data'),
'required' => true,
],
[
'type' => 'text',
'label' => $this->l('Accept Button Text'),
'name' => 'GDPR_COOKIE_ACCEPT',
'required' => true,
],
[
'type' => 'text',
'label' => $this->l('Decline Button Text'),
'name' => 'GDPR_COOKIE_DECLINE',
'required' => true,
],
[
'type' => 'text',
'label' => $this->l('Settings Button Text'),
'name' => 'GDPR_COOKIE_SETTINGS',
'required' => true,
],
[
'type' => 'text',
'label' => $this->l('Manage Preferences Text'),
'name' => 'GDPR_COOKIE_MANAGE_TEXT',
'desc' => $this->l('Text for the manage preferences button (displayed after consent is given)'),
'required' => true,
],
[
'type' => 'text',
'label' => $this->l('More Info Button Text'),
'name' => 'GDPR_COOKIE_MORE_INFO',
'required' => true,
],
[
'type' => 'text',
'label' => $this->l('More Info URL'),
'desc' => $this->l('URL to your Privacy Policy page'),
'name' => 'GDPR_COOKIE_MORE_INFO_URL',
'required' => true,
],
[
'type' => 'switch',
'label' => $this->l('This cookie types are used'),
'name' => 'GDPR_COOKIE_ONLY_REQUIRED',
'desc' => $this->l('If this option is enabled, no config modal will be rendered and there will only be a single button to hide the cookie notice. (Only enable this, if you don\'t track any user data)'),
'is_bool' => true,
'required' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Nessessary only')
],
[
'id' => 'active_off',
'value' => 0,
'label' => $this->l('All cookies')
]
],
],
],
'submit' => [
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right',
],
],
];
$helper = new HelperForm();
// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
// Language
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true;
$helper->toolbar_scroll = true;
$helper->submit_action = 'submitGdprCookieModule';
// Load current values
$helper->fields_value['GDPR_COOKIE_ENABLED'] = Configuration::get('GDPR_COOKIE_ENABLED');
$helper->fields_value['GDPR_COOKIE_MESSAGE'] = Configuration::get('GDPR_COOKIE_MESSAGE');
$helper->fields_value['GDPR_COOKIE_ACCEPT'] = Configuration::get('GDPR_COOKIE_ACCEPT');
$helper->fields_value['GDPR_COOKIE_DECLINE'] = Configuration::get('GDPR_COOKIE_DECLINE');
$helper->fields_value['GDPR_COOKIE_SETTINGS'] = Configuration::get('GDPR_COOKIE_SETTINGS');
$helper->fields_value['GDPR_COOKIE_MORE_INFO'] = Configuration::get('GDPR_COOKIE_MORE_INFO');
$helper->fields_value['GDPR_COOKIE_MORE_INFO_URL'] = Configuration::get('GDPR_COOKIE_MORE_INFO_URL');
$helper->fields_value['GDPR_COOKIE_DATA_CONTROLLER'] = Configuration::get('GDPR_COOKIE_DATA_CONTROLLER');
$helper->fields_value['GDPR_COOKIE_RETENTION_PERIOD'] = Configuration::get('GDPR_COOKIE_RETENTION_PERIOD');
$helper->fields_value['GDPR_COOKIE_THIRD_PARTIES'] = Configuration::get('GDPR_COOKIE_THIRD_PARTIES');
$helper->fields_value['GDPR_COOKIE_MANAGE_TEXT'] = Configuration::get('GDPR_COOKIE_MANAGE_TEXT');
$helper->fields_value['GDPR_COOKIE_ONLY_REQUIRED'] = Configuration::get('GDPR_COOKIE_ONLY_REQUIRED');
return $helper->generateForm([$form]);
}
/**
* Add CSS and JS to the header
*/
public function hookDisplayHeader()
{
if (!Configuration::get('GDPR_COOKIE_ENABLED')) {
return;
}
// Add CSS
$this->context->controller->addCSS($this->_path . 'views/css/gdpr_cookie.css');
// Add JS
$this->context->controller->addJS($this->_path . 'views/js/gdpr_cookie.js');
// Add JS variables for modal
Media::addJsDef([
'gdprCookieMessage' => Configuration::get('GDPR_COOKIE_MESSAGE'),
'gdprCookieAccept' => Configuration::get('GDPR_COOKIE_ACCEPT'),
'gdprCookieDecline' => Configuration::get('GDPR_COOKIE_DECLINE'),
'gdprCookieSettings' => Configuration::get('GDPR_COOKIE_SETTINGS'),
'gdprCookieMoreInfo' => Configuration::get('GDPR_COOKIE_MORE_INFO'),
'gdprCookieMoreInfoUrl' => $this->context->link->getCMSLink(
Configuration::get('GDPR_COOKIE_MORE_INFO_URL')
),
'gdprCookieDataController' => Configuration::get('GDPR_COOKIE_DATA_CONTROLLER', ''),
'gdprCookieRetentionPeriod' => Configuration::get('GDPR_COOKIE_RETENTION_PERIOD', '365 days'),
'gdprCookieThirdParties' => Configuration::get('GDPR_COOKIE_THIRD_PARTIES', ''),
'gdprCookieManageText' => Configuration::get('GDPR_COOKIE_MANAGE_TEXT', 'Manage Cookies'),
'gdprCookieOnlyRequired' => Configuration::get('GDPR_COOKIE_ONLY_REQUIRED'),
]);
// Assign variables to Smarty for the manage button template
$this->context->smarty->assign([
'gdprCookieManageText' => Configuration::get('GDPR_COOKIE_MANAGE_TEXT', 'Manage Cookies'),
]);
// Return the template content - but don't throw an error if it doesn't exist yet
if (file_exists(_PS_MODULE_DIR_ . $this->name . '/views/templates/hook/manage_button.tpl')) {
return $this->display(__FILE__, 'views/templates/hook/manage_button.tpl');
}
}
/**
* Add the cookie modal to the footer
*/
public function hookDisplayFooter()
{
if (!Configuration::get('GDPR_COOKIE_ENABLED')) {
return;
}
$this->smarty->assign([
'gdprCookieMessage' => Configuration::get('GDPR_COOKIE_MESSAGE'),
'gdprCookieAccept' => Configuration::get('GDPR_COOKIE_ACCEPT'),
'gdprCookieDecline' => Configuration::get('GDPR_COOKIE_DECLINE'),
'gdprCookieSettings' => Configuration::get('GDPR_COOKIE_SETTINGS'),
'gdprCookieMoreInfo' => Configuration::get('GDPR_COOKIE_MORE_INFO'),
'gdprCookieMoreInfoUrl' => $this->context->link->getCMSLink(
Configuration::get('GDPR_COOKIE_MORE_INFO_URL')
),
// New variables
'gdprCookieDataController' => Configuration::get('GDPR_COOKIE_DATA_CONTROLLER'),
'gdprCookieRetentionPeriod' => Configuration::get('GDPR_COOKIE_RETENTION_PERIOD'),
'gdprCookieThirdParties' => Configuration::get('GDPR_COOKIE_THIRD_PARTIES'),
'gdprCookieManageText' => Configuration::get('GDPR_COOKIE_MANAGE_TEXT'),
'gdprCookieOnlyRequired' => Configuration::get('GDPR_COOKIE_ONLY_REQUIRED'),
]);
return $this->display(__FILE__, 'views/templates/hook/footer.tpl');
}
}