Compare commits

..

1 Commits

Author SHA1 Message Date
Walz
1b27d03c4d refactor to composer api
Non working state
2025-04-08 16:08:09 +02:00
12 changed files with 405 additions and 38 deletions

20
.gitignore vendored
View File

@ -1 +1,19 @@
.idea/ # IDE directories
.idea/
.vscode/
.DS_Store
# Composer
/vendor/
/composer.lock
# Temporary files
*.cache
*.log
*.tmp
# Node modules (if you use any JS build tools)
/node_modules/
# Generated files
/config.xml

27
composer.json Normal file
View File

@ -0,0 +1,27 @@
{
"name": "walzen665/gdprcookieconsent",
"description": "Adds a GDPR compliant cookie consent modal to your shop",
"license": "AFL-3.0",
"authors": [
{
"name": "Walzen665"
}
],
"autoload": {
"psr-4": {
"PrestaShop\\Module\\GdprCookieConsent\\": "src/"
}
},
"require": {
"php": ">=7.1.0",
"ext-json": "*"
},
"config": {
"preferred-install": "dist",
"prepend-autoloader": false,
"platform": {
"php": "7.1.0"
}
},
"type": "prestashop-module"
}

BIN
composer.phar Normal file

Binary file not shown.

7
config/routes.yml Normal file
View File

@ -0,0 +1,7 @@
gdpr_cookie_consent_configuration:
path: /gdprcookieconsent/configuration
methods: [GET, POST]
defaults:
_controller: 'PrestaShop\Module\GdprCookieConsent\Controller\GdprConfigurationController::indexAction'
_legacy_controller: AdminGdprCookieConsent
_legacy_link: AdminGdprCookieConsent

34
config/services.yml Normal file
View File

@ -0,0 +1,34 @@
services:
_defaults:
public: true
autowire: true
autoconfigure: true
# Form Type
prestashop.module.gdprcookieconsent.form.type.gdpr_configuration:
class: 'PrestaShop\Module\GdprCookieConsent\Form\GdprConfigurationFormType'
parent: 'form.type.translatable.aware'
public: true
tags:
- { name: form.type }
# Data Configuration
prestashop.module.gdprcookieconsent.form.gdpr_configuration_data_configuration:
class: PrestaShop\Module\GdprCookieConsent\Form\GdprConfigurationDataConfiguration
arguments: ['@prestashop.adapter.legacy.configuration']
# Form Data Provider
prestashop.module.gdprcookieconsent.form.gdpr_configuration_form_data_provider:
class: 'PrestaShop\Module\GdprCookieConsent\Form\GdprConfigurationFormDataProvider'
arguments:
- '@prestashop.module.gdprcookieconsent.form.gdpr_configuration_data_configuration'
# Form Handler
prestashop.module.gdprcookieconsent.form.gdpr_configuration_form_data_handler:
class: 'PrestaShop\PrestaShop\Core\Form\Handler'
arguments:
- '@form.factory'
- '@prestashop.core.hook.dispatcher'
- '@prestashop.module.gdprcookieconsent.form.gdpr_configuration_form_data_provider'
- 'PrestaShop\Module\GdprCookieConsent\Form\GdprConfigurationFormType'
- 'Configuration'

View File

@ -82,44 +82,13 @@ class GdprCookieConsent extends Module
*/ */
public function getContent() public function getContent()
{ {
$output = ''; try {
// Redirect to the Symfony controller
// If form submitted Tools::redirectAdmin($this->get('router')->generate('gdpr_cookie_consent_configuration'));
if (Tools::isSubmit('submitGdprCookieModule')) { } catch (\Exception $e) {
// Get configuration values from form // Fallback in case of error
$enabled = (int)Tools::getValue('GDPR_COOKIE_ENABLED'); return $this->displayError($this->l('An error occurred while trying to redirect to the configuration page.'));
$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();
} }
/** /**
@ -347,4 +316,19 @@ class GdprCookieConsent extends Module
return $this->display(__FILE__, 'views/templates/hook/footer.tpl'); return $this->display(__FILE__, 'views/templates/hook/footer.tpl');
} }
/**
* Load the dependency injection container
*
* @param array $params
*/
public function hookActionFrontControllerSetMedia(array $params)
{
// Do not load the container if it's already loaded
if ($this->container !== null) {
return;
}
$this->container = \PrestaShop\PrestaShop\Adapter\SymfonyContainer::getInstance();
}
} }

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace PrestaShop\Module\GdprCookieConsent\Controller;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class GdprConfigurationController extends FrameworkBundleAdminController
{
/**
* @param Request $request
* @return Response
*/
public function indexAction(Request $request): Response
{
$formDataHandler = $this->get('prestashop.module.gdprcookieconsent.form.gdpr_configuration_form_data_handler');
$form = $formDataHandler->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$errors = $formDataHandler->save($form->getData());
if (empty($errors)) {
$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));
return $this->redirectToRoute('gdpr_cookie_consent_configuration');
}
$this->flashErrors($errors);
}
return $this->render('@Modules/gdprcookieconsent/views/templates/admin/configuration.html.twig', [
'gdprConfigurationForm' => $form->createView(),
]);
}
}

View File

@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace PrestaShop\Module\GdprCookieConsent\Form;
use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
/**
* Configuration is used to save data to configuration table and retrieve from it.
*/
final class GdprConfigurationDataConfiguration implements DataConfigurationInterface
{
/**
* @var ConfigurationInterface
*/
private $configuration;
/**
* @var array Configuration keys
*/
private $configurationKeys = [
'GDPR_COOKIE_ENABLED',
'GDPR_COOKIE_MESSAGE',
'GDPR_COOKIE_ACCEPT',
'GDPR_COOKIE_DECLINE',
'GDPR_COOKIE_SETTINGS',
'GDPR_COOKIE_MORE_INFO',
'GDPR_COOKIE_MORE_INFO_URL',
'GDPR_COOKIE_DATA_CONTROLLER',
'GDPR_COOKIE_RETENTION_PERIOD',
'GDPR_COOKIE_THIRD_PARTIES',
'GDPR_COOKIE_MANAGE_TEXT',
'GDPR_COOKIE_ONLY_REQUIRED',
];
public function __construct(ConfigurationInterface $configuration)
{
$this->configuration = $configuration;
}
/**
* @return array
*/
public function getConfiguration(): array
{
$config = [];
foreach ($this->configurationKeys as $key) {
$config[$key] = $this->configuration->get($key);
}
return $config;
}
/**
* @param array $configuration
* @return array
*/
public function updateConfiguration(array $configuration): array
{
$errors = [];
if ($this->validateConfiguration($configuration)) {
foreach ($this->configurationKeys as $key) {
if (isset($configuration[$key])) {
$this->configuration->set($key, $configuration[$key]);
}
}
}
return $errors;
}
/**
* @param array $configuration
* @return bool
*/
public function validateConfiguration(array $configuration): bool
{
return isset($configuration['GDPR_COOKIE_MESSAGE']) &&
isset($configuration['GDPR_COOKIE_ACCEPT']) &&
isset($configuration['GDPR_COOKIE_DECLINE']) &&
isset($configuration['GDPR_COOKIE_SETTINGS']) &&
isset($configuration['GDPR_COOKIE_MORE_INFO']) &&
isset($configuration['GDPR_COOKIE_MORE_INFO_URL']) &&
isset($configuration['GDPR_COOKIE_DATA_CONTROLLER']) &&
isset($configuration['GDPR_COOKIE_RETENTION_PERIOD']) &&
isset($configuration['GDPR_COOKIE_THIRD_PARTIES']) &&
isset($configuration['GDPR_COOKIE_MANAGE_TEXT']);
}
}

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace PrestaShop\Module\GdprCookieConsent\Form;
use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;
/**
* Provider is responsible for providing form data
*/
class GdprConfigurationFormDataProvider implements FormDataProviderInterface
{
/**
* @var DataConfigurationInterface
*/
private $gdprConfigurationDataConfiguration;
public function __construct(DataConfigurationInterface $gdprConfigurationDataConfiguration)
{
$this->gdprConfigurationDataConfiguration = $gdprConfigurationDataConfiguration;
}
/**
* @return array
*/
public function getData(): array
{
return $this->gdprConfigurationDataConfiguration->getConfiguration();
}
/**
* @param array $data
* @return array
*/
public function setData(array $data): array
{
return $this->gdprConfigurationDataConfiguration->updateConfiguration($data);
}
}

View File

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace PrestaShop\Module\GdprCookieConsent\Form;
use PrestaShopBundle\Form\Admin\Type\SwitchType;
use PrestaShopBundle\Form\Admin\Type\TranslatableType;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
class GdprConfigurationFormType extends TranslatorAwareType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('GDPR_COOKIE_ENABLED', SwitchType::class, [
'label' => $this->trans('Enable Cookie Consent', 'Modules.Gdprcookieconsent.Admin'),
'help' => $this->trans('Enable or disable the cookie consent banner', 'Modules.Gdprcookieconsent.Admin'),
])
->add('GDPR_COOKIE_MESSAGE', TranslatableType::class, [
'label' => $this->trans('Cookie Consent Message', 'Modules.Gdprcookieconsent.Admin'),
'type' => TextareaType::class,
'required' => true,
])
->add('GDPR_COOKIE_DATA_CONTROLLER', TranslatableType::class, [
'label' => $this->trans('Data Controller', 'Modules.Gdprcookieconsent.Admin'),
'help' => $this->trans('Your company/organization name', 'Modules.Gdprcookieconsent.Admin'),
'type' => TextType::class,
'required' => true,
])
->add('GDPR_COOKIE_RETENTION_PERIOD', TranslatableType::class, [
'label' => $this->trans('Cookie Retention Period', 'Modules.Gdprcookieconsent.Admin'),
'help' => $this->trans('How long cookies will be stored (e.g., 365 days)', 'Modules.Gdprcookieconsent.Admin'),
'type' => TextType::class,
'required' => true,
])
->add('GDPR_COOKIE_THIRD_PARTIES', TranslatableType::class, [
'label' => $this->trans('Third-Party Recipients', 'Modules.Gdprcookieconsent.Admin'),
'help' => $this->trans('List third parties that receive cookie data', 'Modules.Gdprcookieconsent.Admin'),
'type' => TextareaType::class,
'required' => true,
])
->add('GDPR_COOKIE_ACCEPT', TranslatableType::class, [
'label' => $this->trans('Accept Button Text', 'Modules.Gdprcookieconsent.Admin'),
'type' => TextType::class,
'required' => true,
])
->add('GDPR_COOKIE_DECLINE', TranslatableType::class, [
'label' => $this->trans('Decline Button Text', 'Modules.Gdprcookieconsent.Admin'),
'type' => TextType::class,
'required' => true,
])
->add('GDPR_COOKIE_SETTINGS', TranslatableType::class, [
'label' => $this->trans('Settings Button Text', 'Modules.Gdprcookieconsent.Admin'),
'type' => TextType::class,
'required' => true,
])
->add('GDPR_COOKIE_MANAGE_TEXT', TranslatableType::class, [
'label' => $this->trans('Manage Preferences Text', 'Modules.Gdprcookieconsent.Admin'),
'help' => $this->trans('Text for the manage preferences button (displayed after consent is given)', 'Modules.Gdprcookieconsent.Admin'),
'type' => TextType::class,
'required' => true,
])
->add('GDPR_COOKIE_MORE_INFO', TranslatableType::class, [
'label' => $this->trans('More Info Button Text', 'Modules.Gdprcookieconsent.Admin'),
'type' => TextType::class,
'required' => true,
])
->add('GDPR_COOKIE_MORE_INFO_URL', UrlType::class, [
'label' => $this->trans('More Info URL', 'Modules.Gdprcookieconsent.Admin'),
'help' => $this->trans('URL to your Privacy Policy page', 'Modules.Gdprcookieconsent.Admin'),
'required' => true,
])
->add('GDPR_COOKIE_ONLY_REQUIRED', SwitchType::class, [
'label' => $this->trans('This cookie types are used', 'Modules.Gdprcookieconsent.Admin'),
'help' => $this->trans('If this option is enabled, no config modal will be rendered and there will only be a single button to hide the cookie notice.', 'Modules.Gdprcookieconsent.Admin'),
'choices' => [
$this->trans('Nessessary only', 'Modules.Gdprcookieconsent.Admin') => true,
$this->trans('All cookies', 'Modules.Gdprcookieconsent.Admin') => false,
],
]);
}
}

9
views/js/admin.js Normal file
View File

@ -0,0 +1,9 @@
$(document).ready(function () {
// Initialize PrestaShop UI components
window.prestashop.component.initComponents(
[
'TranslatableField',
'TranslatableInput',
],
);
});

View File

@ -0,0 +1,28 @@
{% extends '@PrestaShop/Admin/layout.html.twig' %}
{% block content %}
{{ form_start(gdprConfigurationForm) }}
<div class="card">
<h3 class="card-header">
<i class="material-icons">settings</i> {{ 'GDPR Cookie Consent Configuration'|trans({}, 'Modules.Gdprcookieconsent.Admin') }}
</h3>
<div class="card-body">
<div class="form-wrapper">
{{ form_widget(gdprConfigurationForm) }}
</div>
</div>
<div class="card-footer">
<div class="d-flex justify-content-end">
<button class="btn btn-primary float-right" id="save-button">
{{ 'Save'|trans({}, 'Admin.Actions') }}
</button>
</div>
</div>
</div>
{{ form_end(gdprConfigurationForm) }}
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('../modules/gdprcookieconsent/views/js/admin.js') }}"></script>
{% endblock %}