custom/plugins/AcrisCookieConsentCS/src/AcrisCookieConsentCS.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CookieConsent;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\Indexer\InheritanceIndexer;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\MessageQueue\IndexerMessageSender;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  14. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  15. use Shopware\Storefront\Framework\Cache\CacheResponseSubscriber;
  16. class AcrisCookieConsentCS extends Plugin
  17. {
  18.     const DEFAULT_SESSION_COOKIE "session-.*";
  19.     const DEFAULT_CSRF_COOKIE "csrf.*";
  20.     const DEFAULT_TIMEZONE_COOKIE "timezone";
  21.     const DEFAULT_REMEMBER_COOKIE "cookie-preference|acris_cookie_acc";
  22.     const DEFAULT_CACHE_COOKIES CacheResponseSubscriber::CONTEXT_CACHE_COOKIE."|".CacheResponseSubscriber::CURRENCY_COOKIE."|".CacheResponseSubscriber::SYSTEM_STATE_COOKIE;
  23.     const DEFAULT_REMEMBER_LANDING_REFERRER "acris_cookie_landing_page|acris_cookie_referrer";
  24.     const DEFAULT_REMEMBER_FIRST_ACTIVATED "acris_cookie_first_activated";
  25.     const DEFAULT_FUNCTIONAL_GROUP_IDENTIFICATION "functional";
  26.     const DEFAULT_MARKETING_GROUP_IDENTIFICATION "marketing";
  27.     const DEFAULT_TRACKING_GROUP_IDENTIFICATION "tracking";
  28.     const DEFAULT_PERSONAL_GROUP_IDENTIFICATION "personal";
  29.     const DEFAULT_RATING_GROUP_IDENTIFICATION "rating";
  30.     const DEFAULT_SERVICE_GROUP_IDENTIFICATION "service";
  31.     public function update(UpdateContext $updateContext): void
  32.     {
  33.         if($updateContext->getPlugin()->isActive() === true) {
  34.             if((version_compare($updateContext->getCurrentPluginVersion(), '2.0.0''<=') && version_compare($updateContext->getUpdatePluginVersion(), '2.0.0''>'))
  35.                 || (version_compare($updateContext->getCurrentPluginVersion(), '2.4.0''<=') && version_compare($updateContext->getUpdatePluginVersion(), '2.4.0''>'))) {
  36.                 $this->insertDefaultData($updateContext->getContext());
  37.             }
  38.             if((version_compare($updateContext->getCurrentPluginVersion(), '2.8.0''<') && version_compare($updateContext->getUpdatePluginVersion(), '2.8.0''>='))) {
  39.                 $this->insertDefaultData($updateContext->getContext());
  40.             }
  41.             if((version_compare($updateContext->getCurrentPluginVersion(), '2.8.7''<') && version_compare($updateContext->getUpdatePluginVersion(), '2.8.7''>='))) {
  42.                 $this->updateDefaultData($updateContext->getContext());
  43.             }
  44.         }
  45.     }
  46.     public function uninstall(UninstallContext $context): void
  47.     {
  48.         if ($context->keepUserData()) {
  49.             return;
  50.         }
  51.         $connection $this->container->get(Connection::class);
  52.         $connection->executeQuery('DROP TABLE IF EXISTS `acris_cookie_translation`');
  53.         $connection->executeQuery('DROP TABLE IF EXISTS `acris_cookie_group_translation`');
  54.         $connection->executeQuery('DROP TABLE IF EXISTS `acris_cookie_sales_channel`');
  55.         try {
  56.             $connection->executeQuery('ALTER TABLE `sales_channel` DROP `cookies`');
  57.         } catch (\Exception $e) { }
  58.         $connection->executeQuery('DROP TABLE IF EXISTS `acris_cookie`');
  59.         $connection->executeQuery('DROP TABLE IF EXISTS `acris_cookie_group`');
  60.     }
  61.     public function activate(ActivateContext $activateContext): void
  62.     {
  63.         $this->insertDefaultData($activateContext->getContext());
  64.     }
  65.     private function insertDefaultData(Context $context): void
  66.     {
  67.         /** @var EntityRepositoryInterface $cookieGroupRepository */
  68.         $cookieGroupRepository $this->container->get('acris_cookie_group.repository');
  69.         $cookieGroups = [
  70.             [
  71.                 'identification' => self::DEFAULT_FUNCTIONAL_GROUP_IDENTIFICATION,
  72.                 'translations' => [
  73.                     'en-GB' => [
  74.                         'title' => "Functional",
  75.                         'description' => "Functional cookies are absolutely necessary for the functionality of the web shop. These cookies assign a unique random ID to your browser so that your unhindered shopping experience can be guaranteed over several page views.",
  76.                     ],'de-DE' => [
  77.                         'title' => "Funktionale",
  78.                         'description' => "Funktionale Cookies sind für die Funktionalität des Webshops unbedingt erforderlich. Diese Cookies ordnen Ihrem Browser eine eindeutige zufällige ID zu damit Ihr ungehindertes Einkaufserlebnis über mehrere Seitenaufrufe hinweg gewährleistet werden kann.",
  79.                     ]],
  80.                 'title' => "Functional",
  81.                 'description' => "Functional cookies are absolutely necessary for the functionality of the web shop. These cookies assign a unique random ID to your browser so that your unhindered shopping experience can be guaranteed over several page views.",
  82.                 'isDefault' => true
  83.             ],[
  84.                 'identification' => self::DEFAULT_MARKETING_GROUP_IDENTIFICATION,
  85.                 'translations' => [
  86.                     'en-GB' => [
  87.                         'title' => "Marketing",
  88.                         'description' => "Marketing cookies are used to display advertisements on the website in a targeted and individualized manner across multiple page views and browser sessions.",
  89.                     ],'de-DE' => [
  90.                         'title' => "Marketing",
  91.                         'description' => "Marketing Cookies dienen dazu Werbeanzeigen auf der Webseite zielgerichtet und individuell über mehrere Seitenaufrufe und Browsersitzungen zu schalten.",
  92.                     ]],
  93.                 'title' => "Marketing",
  94.                 'description' => "Marketing cookies are used to display advertisements on the website in a targeted and individualized manner across multiple page views and browser sessions.",
  95.                 'isDefault' => false
  96.             ],[
  97.                 'identification' => self::DEFAULT_TRACKING_GROUP_IDENTIFICATION,
  98.                 'translations' => [
  99.                     'en-GB' => [
  100.                         'title' => "Tracking",
  101.                         'description' => "Tracking cookies help the shop operator to collect and evaluate information about the behaviour of users on their website.",
  102.                     ],'de-DE' => [
  103.                         'title' => "Tracking",
  104.                         'description' => "Tracking Cookies helfen dem Shopbetreiber Informationen über das Verhalten von Nutzern auf ihrer Webseite zu sammeln und auszuwerten.",
  105.                     ]],
  106.                 'title' => "Tracking",
  107.                 'description' => "Tracking cookies help the shop operator to collect and evaluate information about the behaviour of users on their website.",
  108.                 'isDefault' => false
  109.             ],[
  110.                 'identification' => self::DEFAULT_PERSONAL_GROUP_IDENTIFICATION,
  111.                 'translations' => [
  112.                     'en-GB' => [
  113.                         'title' => "Personalization",
  114.                         'description' => "These cookies are used to collect and process information about the use of the website by users, in order to subsequently personalise advertising and/or content in other contexts.",
  115.                     ],'de-DE' => [
  116.                         'title' => "Personalisierung",
  117.                         'description' => "Diese Cookies werden genutzt zur Erhebung und Verarbeitung von Informationen über die Verwendung der Webseite von Nutzern, um anschließend Werbung und/oder Inhalte in anderen Zusammenhängen, in weiterer Folge zu personalisieren.",
  118.                     ]],
  119.                 'title' => "Personalization",
  120.                 'description' => "These cookies are used to collect and process information about the use of the website by users, in order to subsequently personalise advertising and/or content in other contexts.",
  121.                 'isDefault' => false
  122.             ],[
  123.                 'identification' => self::DEFAULT_RATING_GROUP_IDENTIFICATION,
  124.                 'translations' => [
  125.                     'en-GB' => [
  126.                         'title' => "Rating",
  127.                         'description' => "These cookies are used to collect information about the use of the website and to link to previously collected information. This information is used to evaluate, understand and report on the use of the website services.",
  128.                     ],'de-DE' => [
  129.                         'title' => "Bewertung",
  130.                         'description' => "Diese Cookies dienen zur Erhebung von Informationen über die Nutzung der Webseite und die Verknüpfung mit zuvor erhobenen Informationen. Diese Informationen werden verwendet die Nutzung der Dienste der Webseite zu bewerten, zu verstehen und darüber zu berichten.",
  131.                     ]],
  132.                 'title' => "Rating",
  133.                 'description' => "These cookies are used to collect information about the use of the website and to link to previously collected information. This information is used to evaluate, understand and report on the use of the website services.",
  134.                 'isDefault' => false
  135.             ],[
  136.                 'identification' => self::DEFAULT_SERVICE_GROUP_IDENTIFICATION,
  137.                 'translations' => [
  138.                     'en-GB' => [
  139.                         'title' => "Service",
  140.                         'description' => "Service cookies are used to provide the user with additional offers (e.g. live chats) on the website. Information obtained via these service cookies may also be processed for site analysis.",
  141.                     ],'de-DE' => [
  142.                         'title' => "Service",
  143.                         'description' => "Service Cookies werden genutzt um dem Nutzer zusätzliche Angebote (z.B. Live Chats) auf der Webseite zur Verfügung zu stellen. Informationen, die über diese Service Cookies gewonnen werden, können möglicherweise auch zur Seitenanalyse weiterverarbeitet werden.",
  144.                     ]],
  145.                 'title' => "Service",
  146.                 'description' => "Service cookies are used to provide the user with additional offers (e.g. live chats) on the website. Information obtained via these service cookies may also be processed for site analysis.",
  147.                 'isDefault' => false
  148.             ]
  149.         ];
  150.         foreach ($cookieGroups as $cookieGroup) {
  151.             $this->createEntityIfNotExists($cookieGroupRepository$context'identification'$cookieGroup);
  152.         }
  153.         $defaultCookieGroupId $cookieGroupRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('isDefault'true))->setLimit(1), $context)->firstId();
  154.         /** @var EntityRepositoryInterface $cookieRepository */
  155.         $cookieRepository $this->container->get('acris_cookie.repository');
  156.         $cookies = [
  157.             [
  158.                 'cookieId' => self::DEFAULT_SESSION_COOKIE,
  159.                 'provider' => 'Shopware',
  160.                 'active' => true,
  161.                 'unknown' => false,
  162.                 'translations' => [
  163.                     'en-GB' => [
  164.                         'title' => "Session",
  165.                         'description' => "The session cookie stores your shopping data over several page views and is therefore essential for your personal shopping experience.",
  166.                     ],'de-DE' => [
  167.                         'title' => "Session",
  168.                         'description' => "Das Session Cookie speichert Ihre Einkaufsdaten über mehrere Seitenaufrufe hinweg und ist somit unerlässlich für Ihr persönliches Einkaufserlebnis.",
  169.                     ]],
  170.                 'title' => "Session",
  171.                 'description' => "The session cookie stores your shopping data over several page views and is therefore essential for your personal shopping experience.",
  172.                 'isDefault' => true,
  173.                 'cookieGroupId' => $defaultCookieGroupId
  174.             ],[
  175.                 'cookieId' => self::DEFAULT_CSRF_COOKIE,
  176.                 'provider' => 'Shopware',
  177.                 'active' => true,
  178.                 'unknown' => false,
  179.                 'translations' => [
  180.                     'en-GB' => [
  181.                         'title' => "CSRF token",
  182.                         'description' => "The CSRF token cookie contributes to your security. It strengthens the security of forms against unwanted hacker attacks.",
  183.                     ],'de-DE' => [
  184.                         'title' => "CSRF-Token",
  185.                         'description' => "Das CSRF-Token Cookie trägt zu Ihrer Sicherheit bei. Es verstärkt die Absicherung bei Formularen gegen unerwünschte Hackangriffe.",
  186.                     ]],
  187.                 'title' => "CSRF token",
  188.                 'description' => "The CSRF token cookie contributes to your security. It strengthens the security of forms against unwanted hacker attacks.",
  189.                 'isDefault' => true,
  190.                 'cookieGroupId' => $defaultCookieGroupId
  191.             ],[
  192.                 'cookieId' => self::DEFAULT_TIMEZONE_COOKIE,
  193.                 'provider' => 'Shopware',
  194.                 'active' => true,
  195.                 'unknown' => false,
  196.                 'translations' => [
  197.                     'en-GB' => [
  198.                         'title' => "Timezone",
  199.                         'description' => "The cookie is used to provide the system with the user's current time zone.",
  200.                     ],'de-DE' => [
  201.                         'title' => "Zeitzone",
  202.                         'description' => "Das Cookie wird verwendet um dem System die aktuelle Zeitzone des Benutzers zur Verfügung zu stellen.",
  203.                     ]],
  204.                 'title' => "Timezone",
  205.                 'description' => "The cookie is used to provide the system with the user's current time zone.",
  206.                 'isDefault' => true,
  207.                 'cookieGroupId' => $defaultCookieGroupId
  208.             ],[
  209.                 'cookieId' => self::DEFAULT_REMEMBER_COOKIE,
  210.                 'provider' => 'Shopware',
  211.                 'active' => true,
  212.                 'unknown' => false,
  213.                 'translations' => [
  214.                     'en-GB' => [
  215.                         'title' => "Cookie settings",
  216.                         'description' => "The cookie is used to store the cookie settings of the site user over several browser sessions.",
  217.                     ],'de-DE' => [
  218.                         'title' => "Cookie Einstellungen",
  219.                         'description' => "Das Cookie wird verwendet um die Cookie Einstellungen des Seitenbenutzers über mehrere Browsersitzungen zu speichern.",
  220.                     ]],
  221.                 'title' => "Cookie settings",
  222.                 'description' => "The cookie is used to store the cookie settings of the site user over several browser sessions.",
  223.                 'isDefault' => true,
  224.                 'cookieGroupId' => $defaultCookieGroupId
  225.             ],[
  226.                 'cookieId' => self::DEFAULT_CACHE_COOKIES,
  227.                 'provider' => 'Shopware',
  228.                 'active' => true,
  229.                 'unknown' => false,
  230.                 'translations' => [
  231.                     'en-GB' => [
  232.                         'title' => "Cache handling",
  233.                         'description' => "The cookie is used to differentiate the cache for different scenarios and page users.",
  234.                     ],'de-DE' => [
  235.                         'title' => "Cache Behandlung",
  236.                         'description' => "Das Cookie wird eingesetzt um den Cache für unterschiedliche Szenarien und Seitenbenutzer zu differenzieren.",
  237.                     ]],
  238.                 'title' => "Cache handling",
  239.                 'description' => "The cookie is used to differentiate the cache for different scenarios and page users.",
  240.                 'isDefault' => true,
  241.                 'cookieGroupId' => $defaultCookieGroupId
  242.             ],[
  243.                 'cookieId' => self::DEFAULT_REMEMBER_LANDING_REFERRER,
  244.                 'provider' => 'Shopware',
  245.                 'active' => true,
  246.                 'unknown' => false,
  247.                 'translations' => [
  248.                     'en-GB' => [
  249.                         'title' => "Information on origin",
  250.                         'description' => "The cookie stores the referrer and the first page visited by the user for further use.",
  251.                     ],'de-DE' => [
  252.                         'title' => "Herkunftsinformationen",
  253.                         'description' => "Das Cookie speichert die Herkunftsseite und die zuerst besuchte Seite des Benutzers für eine weitere Verwendung.",
  254.                     ]],
  255.                 'title' => "Information on origin",
  256.                 'description' => "The cookie stores the referrer and the first page visited by the user for further use.",
  257.                 'isDefault' => true,
  258.                 'cookieGroupId' => $defaultCookieGroupId
  259.             ],[
  260.                 'cookieId' => self::DEFAULT_REMEMBER_FIRST_ACTIVATED,
  261.                 'provider' => 'Shopware',
  262.                 'active' => true,
  263.                 'unknown' => false,
  264.                 'translations' => [
  265.                     'en-GB' => [
  266.                         'title' => "Activated cookies",
  267.                         'description' => "Saves which cookies have already been accepted by the user for the first time.",
  268.                     ],'de-DE' => [
  269.                         'title' => "Aktivierte Cookies",
  270.                         'description' => "Speichert welche Cookies bereits vom Benutzer zum ersten Mal akzeptiert wurden.",
  271.                     ]],
  272.                 'title' => "Activated cookies",
  273.                 'description' => "Saves which cookies have already been accepted by the user for the first time.",
  274.                 'isDefault' => true,
  275.                 'cookieGroupId' => $defaultCookieGroupId
  276.             ]
  277.         ];
  278.         foreach ($cookies as $cookie) {
  279.             $this->createEntityIfNotExists($cookieRepository$context'cookieId'$cookie);
  280.         }
  281.     }
  282.     private function updateDefaultData(Context $context): void
  283.     {
  284.         /** @var EntityRepositoryInterface $cookieGroupRepository */
  285.         $cookieGroupRepository $this->container->get('acris_cookie_group.repository');
  286.         $cookieGroups = [
  287.             [
  288.                 'identification' => self::DEFAULT_FUNCTIONAL_GROUP_IDENTIFICATION,
  289.                 'title' => "Functional",
  290.                 'description' => "Functional cookies are absolutely necessary for the functionality of the web shop. These cookies assign a unique random ID to your browser so that your unhindered shopping experience can be guaranteed over several page views."
  291.             ],[
  292.                 'identification' => self::DEFAULT_MARKETING_GROUP_IDENTIFICATION,
  293.                 'title' => "Marketing",
  294.                 'description' => "Marketing cookies are used to display advertisements on the website in a targeted and individualized manner across multiple page views and browser sessions."
  295.             ],[
  296.                 'identification' => self::DEFAULT_TRACKING_GROUP_IDENTIFICATION,
  297.                 'title' => "Tracking",
  298.                 'description' => "Tracking cookies help the shop operator to collect and evaluate information about the behaviour of users on their website."
  299.             ],[
  300.                 'identification' => self::DEFAULT_PERSONAL_GROUP_IDENTIFICATION,
  301.                 'title' => "Personalization",
  302.                 'description' => "These cookies are used to collect and process information about the use of the website by users, in order to subsequently personalise advertising and/or content in other contexts."
  303.             ],[
  304.                 'identification' => self::DEFAULT_RATING_GROUP_IDENTIFICATION,
  305.                 'title' => "Rating",
  306.                 'description' => "These cookies are used to collect information about the use of the website and to link to previously collected information. This information is used to evaluate, understand and report on the use of the website services."
  307.             ],[
  308.                 'identification' => self::DEFAULT_SERVICE_GROUP_IDENTIFICATION,
  309.                 'title' => "Service",
  310.                 'description' => "Service cookies are used to provide the user with additional offers (e.g. live chats) on the website. Information obtained via these service cookies may also be processed for site analysis."
  311.             ]
  312.         ];
  313.         foreach ($cookieGroups as $cookieGroup) {
  314.             $this->updateEntityIfExists($cookieGroupRepository$context'identification'$cookieGroup);
  315.         }
  316.         /** @var EntityRepositoryInterface $cookieRepository */
  317.         $cookieRepository $this->container->get('acris_cookie.repository');
  318.         $cookies = [
  319.             [
  320.                 'cookieId' => self::DEFAULT_SESSION_COOKIE,
  321.                 'title' => "Session",
  322.                 'description' => "The session cookie stores your shopping data over several page views and is therefore essential for your personal shopping experience."
  323.             ],[
  324.                 'cookieId' => self::DEFAULT_CSRF_COOKIE,
  325.                 'title' => "CSRF token",
  326.                 'description' => "The CSRF token cookie contributes to your security. It strengthens the security of forms against unwanted hacker attacks."
  327.             ],[
  328.                 'cookieId' => self::DEFAULT_TIMEZONE_COOKIE,
  329.                 'title' => "Timezone",
  330.                 'description' => "The cookie is used to provide the system with the user's current time zone."
  331.             ],[
  332.                 'cookieId' => self::DEFAULT_REMEMBER_COOKIE,
  333.                 'title' => "Cookie settings",
  334.                 'description' => "The cookie is used to store the cookie settings of the site user over several browser sessions."
  335.             ],[
  336.                 'cookieId' => self::DEFAULT_CACHE_COOKIES,
  337.                 'title' => "Cache handling",
  338.                 'description' => "The cookie is used to differentiate the cache for different scenarios and page users."
  339.             ],[
  340.                 'cookieId' => self::DEFAULT_REMEMBER_LANDING_REFERRER,
  341.                 'title' => "Information on origin",
  342.                 'description' => "The cookie stores the referrer and the first page visited by the user for further use."
  343.             ],[
  344.                 'cookieId' => self::DEFAULT_REMEMBER_FIRST_ACTIVATED,
  345.                 'title' => "Activated cookies",
  346.                 'description' => "Saves which cookies have already been accepted by the user for the first time."
  347.             ]
  348.         ];
  349.         foreach ($cookies as $cookie) {
  350.             $this->updateEntityIfExists($cookieRepository$context'cookieId'$cookie);
  351.         }
  352.     }
  353.     /**
  354.      * @param EntityRepositoryInterface $entityRepository
  355.      * @param Context $context
  356.      * @param string $identifierField
  357.      * @param array $groupData
  358.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  359.      */
  360.     private function createEntityIfNotExists(EntityRepositoryInterface $entityRepositoryContext $contextstring $identifierField, array $groupData): void
  361.     {
  362.         $exists $entityRepository->search((new Criteria())->addFilter(new EqualsFilter($identifierField$groupData[$identifierField])), $context);
  363.         if($exists->getTotal() === 0) {
  364.             $entityRepository->create([$groupData], $context);
  365.         }
  366.     }
  367.     /**
  368.      * @param EntityRepositoryInterface $entityRepository
  369.      * @param Context $context
  370.      * @param string $identifierField
  371.      * @param array $groupData
  372.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  373.      */
  374.     private function updateEntityIfExists(EntityRepositoryInterface $entityRepositoryContext $contextstring $identifierField, array $groupData): void
  375.     {
  376.         $exists $entityRepository->search((new Criteria())->addFilter(new EqualsFilter($identifierField$groupData[$identifierField])), $context);
  377.         if($exists->getTotal() > 0) {
  378.             $entity $exists->first();
  379.             $groupData['id'] = $entity->getId();
  380.             $entityRepository->update([$groupData], $context);
  381.         }
  382.     }
  383. }