Skip to content

Commit

Permalink
Adds compatibility, removes mandatory condition
Browse files Browse the repository at this point in the history
  • Loading branch information
davide-alghi committed Jan 5, 2022
1 parent be19b9e commit 5cb8b6d
Show file tree
Hide file tree
Showing 28 changed files with 219 additions and 297 deletions.
58 changes: 58 additions & 0 deletions Classes/Compatibility/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types = 1);

/**
* This file is part of the TYPO3 CMS extension.
* The extension name is: Cookie Consent Plus.
* The extension key is: cookieconsent_plus.
* Cookie Consent Plus extends dp_cookieconsent TYPO3 extension
* The developer is Davide Alghi (Abbiategrasso - Italy).
* Cookie Consent Plus Copyright (C) 2021 Davide Alghi.
* All Rights Reserved.
* Cookie Consent Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Cookie Consent Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Cookie Consent Plus. If not, see https://www.gnu.org/licenses/gpl-3.0.en.html.
* See the file LICENSE.md for copying conditions.
* Website: https://www.penguinable.it
*
* @category TYPO3
* @copyright 2021 Davide Alghi
* @author Davide Alghi <[email protected]>
* @license GPLv3
*/

namespace PAD\CookieconsentPlus\Compatibility;

use \TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

class Version
{
const DP_COOKIECONSENT_KEY = 'dp_cookieconsent';

/**
* If dp_cookieconsent is the new version
* returns true
* else false
* It is the new version, if it is eq to 11.2.1 or gte to 11.4.0
*
* @param none
* @return bool
*/
public function isTheNewVersion(): bool
{
$version = ExtensionManagementUtility::getExtensionVersion(self::DP_COOKIECONSENT_KEY);
$result = false;
if (\version_compare($version, '11.2.1', '==') || \version_compare($version, '11.4.0', '>=')) {
$result = true;
}
return $result;
}
}
100 changes: 65 additions & 35 deletions Classes/Cookie/CookieManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* along with Cookie Consent Plus. If not, see https://www.gnu.org/licenses/gpl-3.0.en.html.
* See the file LICENSE.md for copying conditions.
* Website: https://www.penguinable.it
*
*
* @category TYPO3
* @copyright 2021 Davide Alghi
* @author Davide Alghi <[email protected]>
Expand All @@ -31,41 +31,73 @@

namespace PAD\CookieconsentPlus\Cookie;

use \PAD\CookieconsentPlus\Compatibility\Version;
use \TYPO3\CMS\Core\Utility\GeneralUtility;

class CookieManager
{
const COOKIECONSENTSTATUS_NAME = 'cookieconsent_status';
const COOKIECONSENTSTATUS_OPTOUT_NAME = 'dp_cookieconsent_status';
const COOKIECONSENTSTATUS_DP_NAME = 'dp_cookieconsent_status';
const COOKIEDISMISSVALUE = 'dismiss';
const COOKIEALLOWVALUE = 'allow';
const COOKIEDENYVALUE = 'deny';
const COOKIEDIALOGSTATUSOPEN = 'open';
const COOKIEDIALOGSTATUSAPPROVED = 'approved';

protected $cookieValue = '';
protected $optoutCookieValue = '';
protected $dpCookieValue = [];
protected $cookieStatus = false;
protected $mandatoryCookieStatus = false;
protected $dpCookieStatus = '';
protected $statisticsCookieStatus = false;
protected $marketingCookieStatus = false;

/**
* Sets statuses from cookies value
*
*
* @param void
*/
public function __construct()
{
if (isset($_COOKIE[self::COOKIECONSENTSTATUS_NAME])) {
$this->cookieValue = $_COOKIE[self::COOKIECONSENTSTATUS_NAME];
if ($this->cookieValue) {
$this->cookieStatus = $_COOKIE[self::COOKIECONSENTSTATUS_NAME] == self::COOKIEDENYVALUE ? false : true;
if ($this->cookieStatus) {
$this->mandatoryCookieStatus = true;
if ($this->cookieValue != self::COOKIEDISMISSVALUE && isset($_COOKIE[self::COOKIECONSENTSTATUS_OPTOUT_NAME])) {
$this->optoutCookieValue = $_COOKIE[self::COOKIECONSENTSTATUS_OPTOUT_NAME];
$optoutCookieValueArray = json_decode($_COOKIE[self::COOKIECONSENTSTATUS_OPTOUT_NAME], true);
$this->statisticsCookieStatus = (boolean) $optoutCookieValueArray['dp--cookie-statistics'];
$this->marketingCookieStatus = (boolean) $optoutCookieValueArray['dp--cookie-marketing'];
} else {
$this->statisticsCookieStatus = true;
$this->marketingCookieStatus = true;
$versionCompatibility = GeneralUtility::makeInstance(Version::class);
if ($versionCompatibility->isTheNewVersion()) { // dp_cookieconsent new version
if (isset($_COOKIE[self::COOKIECONSENTSTATUS_DP_NAME])) {
$this->cookieValue = '';
$this->cookieStatus = false;
$this->dpCookieValue = json_decode($_COOKIE[self::COOKIECONSENTSTATUS_DP_NAME], true);
$this->dpCookieStatus = $this->dpCookieValue['status'];
if ($this->dpCookieStatus == self::COOKIEDIALOGSTATUSAPPROVED) {
if (is_array($this->dpCookieValue['checkboxes'])) {
foreach ($this->dpCookieValue['checkboxes'] as $key => $value) {
switch ($value['name']) {
case 'statistics':
$this->statisticsCookieStatus = (boolean) $value['checked'];
break;

case 'marketing':
$this->marketingCookieStatus = (boolean) $value['checked'];
break;
}
}
}
} else {
$this->statisticsCookieStatus = false;
$this->marketingCookieStatus = false;
}
}
} else { // dp_cookieconsent old version
if (isset($_COOKIE[self::COOKIECONSENTSTATUS_NAME])) {
$this->cookieValue = $_COOKIE[self::COOKIECONSENTSTATUS_NAME];
if ($this->cookieValue) {
$this->cookieStatus = $_COOKIE[self::COOKIECONSENTSTATUS_NAME] == self::COOKIEDENYVALUE ? false : true;
if ($this->cookieStatus) {
if ($this->cookieValue != self::COOKIEDISMISSVALUE && isset($_COOKIE[self::COOKIECONSENTSTATUS_DP_NAME])) {
$this->dpCookieValue = json_decode($_COOKIE[self::COOKIECONSENTSTATUS_DP_NAME], true);
$this->statisticsCookieStatus = (boolean) $this->dpCookieValue['dp--cookie-statistics'];
$this->marketingCookieStatus = (boolean) $this->dpCookieValue['dp--cookie-marketing'];
} else {
$this->statisticsCookieStatus = true;
$this->marketingCookieStatus = true;
}
}
}
}
Expand All @@ -74,7 +106,7 @@ public function __construct()

/**
* Returns cookieconsent_status cookie value
*
*
* @param void
* @return string
*/
Expand All @@ -85,19 +117,18 @@ public function getCookieValue(): string

/**
* Returns dp_cookieconsent_status cookie value
* in array format
*
*
* @param void
* @return array
*/
public function getOptoutCookieValue(): array
public function getDpCookieValue(): array
{
return json_decode($this->optoutCookieValue, true);
return $this->dpCookieValue;
}

/**
* Returns cookies status
*
*
* @param void
* @return bool
*/
Expand All @@ -107,21 +138,20 @@ public function getCookieStatus(): bool
}

/**
* Returns mandatory cookies status
* accepted: true, denied: false
*
* Returns dp cookies status
*
* @param void
* @return bool
* @return string
*/
public function isMandatoryOn(): bool
public function getDpCookieStatus(): string
{
return $this->mandatoryCookieStatus;
return $this->dpCookieStatus;
}

/**
* Returns statistics cookies status
* accepted: true, denied: false
*
*
* @param void
* @return bool
*/
Expand All @@ -133,7 +163,7 @@ public function isStatisticsOn(): bool
/**
* Returns marketing cookies status
* accepted: true, denied: false
*
*
* @param void
* @return bool
*/
Expand All @@ -145,9 +175,9 @@ public function isMarketingOn(): bool
/**
* Removes cookie with defined
* name, path and domain
*
*
* @param string $name - the cookie name
* @param string $path - the path for which the cookie is valid
* @param string $path - the path for which the cookie is valid
* @param string $domain - the domain from which the cookie comes
* @return void
*/
Expand Down
29 changes: 7 additions & 22 deletions Classes/Xclass/PageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* along with Cookie Consent Plus. If not, see https://www.gnu.org/licenses/gpl-3.0.en.html.
* See the file LICENSE.md for copying conditions.
* Website: https://www.penguinable.it
*
*
* @category TYPO3
* @copyright 2021 Davide Alghi
* @author Davide Alghi <[email protected]>
Expand All @@ -31,10 +31,10 @@

namespace PAD\CookieconsentPlus\Xclass;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Domain\Repository\PageRepository as CmsPageRepository;
use PAD\CookieconsentPlus\Cookie\CookieManager;
use \PAD\CookieconsentPlus\Cookie\CookieManager;
use \TYPO3\CMS\Core\Database\ConnectionPool;
use \TYPO3\CMS\Core\Utility\GeneralUtility;
use \TYPO3\CMS\Core\Domain\Repository\PageRepository as CmsPageRepository;

class PageRepository extends CmsPageRepository
{
Expand All @@ -46,7 +46,6 @@ class PageRepository extends CmsPageRepository
const CONDITIONTYPE_VALUE_SHOWOR = 'showor';
const CONDITIONTYPE_VALUE_HIDEAND = 'hideand';
const CONDITIONTYPE_VALUE_HIDEOR = 'hideor';
const MANDATORYCONDITION_FIELD = 'tx_cookieconsentplus_mandatorycondition';
const STATISTICSCONDITION_FIELD = 'tx_cookieconsentplus_statisticscondition';
const MARKETINGCONDITION_FIELD = 'tx_cookieconsentplus_marketingcondition';
const CONDITION_VALUE_ANYVALUE = 'anyvalue';
Expand All @@ -60,7 +59,7 @@ class PageRepository extends CmsPageRepository

/**
* Returns enable fields (constraints) for cookies dependency
*
*
* @param string $table - table on which to create the query
* @return string
*/
Expand All @@ -69,13 +68,8 @@ protected function getCookiesEnableFields($table): string
$constraints = [];
if (in_array($table, $this->allowedTables)) {
$cookieManager = GeneralUtility::makeInstance(CookieManager::class);
$isMandatoryOn = $cookieManager->isMandatoryOn();
$isStatisticsOn = $cookieManager->isStatisticsOn();
$isMarketingOn = $cookieManager->isMarketingOn();
$mandatoryValues = [
self::CONDITION_VALUE_ANYVALUE,
$isMandatoryOn ? self::CONDITION_VALUE_ACCEPTED : self::CONDITION_VALUE_DENIED,
];
$statisticsValues = [
self::CONDITION_VALUE_ANYVALUE,
$isStatisticsOn ? self::CONDITION_VALUE_ACCEPTED : self::CONDITION_VALUE_DENIED,
Expand All @@ -86,7 +80,6 @@ protected function getCookiesEnableFields($table): string
];
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$expressionBuilder = $queryBuilder->expr();
$mandatoryValues = array_map([$expressionBuilder, 'literal'], $mandatoryValues);
$statisticsValues = array_map([$expressionBuilder, 'literal'], $statisticsValues);
$marketingValues = array_map([$expressionBuilder, 'literal'], $marketingValues);
$constraints[] = $expressionBuilder->eq(
Expand All @@ -103,10 +96,6 @@ protected function getCookiesEnableFields($table): string
$expressionBuilder->literal(self::CONDITIONTYPE_VALUE_SHOWAND)
),
$expressionBuilder->andX(
$expressionBuilder->in(
self::MANDATORYCONDITION_FIELD,
$mandatoryValues
),
$expressionBuilder->in(
self::STATISTICSCONDITION_FIELD,
$statisticsValues
Expand All @@ -127,10 +116,6 @@ protected function getCookiesEnableFields($table): string
$expressionBuilder->literal(self::CONDITIONTYPE_VALUE_SHOWOR)
),
$expressionBuilder->orX(
$expressionBuilder->in(
self::MANDATORYCONDITION_FIELD,
$mandatoryValues
),
$expressionBuilder->in(
self::STATISTICSCONDITION_FIELD,
$statisticsValues
Expand All @@ -147,7 +132,7 @@ protected function getCookiesEnableFields($table): string

/**
* {@inheritdoc}
*
*
* In addition cookies sql constraints
* are added to enableFields query
*/
Expand Down
33 changes: 3 additions & 30 deletions Configuration/TCA/Overrides/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

defined('TYPO3_MODE') or die();

use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

// add tx_cookieconsentplus_visibility field to 'page' model TCA and insert in 'access' sheet
$ll = 'LLL:EXT:cookieconsent_plus/Resources/Private/Language/locallang_db.xlf:pages.';
$newFields = [
Expand Down Expand Up @@ -38,31 +36,6 @@
],
],
],
'tx_cookieconsentplus_mandatorycondition' => [
'label' => $ll . 'tx_cookieconsentplus_mandatorycondition',
'exclude' => 0,
'l10n_mode' => 'exclude',
'l10n_display' => 'defaultAsReadonly',
'displayCond' => 'FIELD:tx_cookieconsentplus_iscookiesdependent:=:1',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'items' => [
[
$ll . 'tx_cookieconsentplus_conditionvalue.anyvalue',
'anyvalue'
],
[
$ll . 'tx_cookieconsentplus_conditionvalue.denied',
'denied'
],
[
$ll . 'tx_cookieconsentplus_conditionvalue.accepted',
'accepted'
],
],
],
],
'tx_cookieconsentplus_statisticscondition' => [
'label' => $ll . 'tx_cookieconsentplus_statisticscondition',
'exclude' => 0,
Expand Down Expand Up @@ -114,14 +87,14 @@
],
],
];
ExtensionManagementUtility::addTCAcolumns('pages', $newFields);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('pages', $newFields);
$GLOBALS['TCA']['pages']['palettes']['cookiesdependent'] = [
'label' => $ll . 'tx_cookieconsentplus_iscookiesdependent.label',
'showitem' => 'tx_cookieconsentplus_iscookiesdependent,
--linebreak--, tx_cookieconsentplus_conditiontype,
--linebreak--, tx_cookieconsentplus_mandatorycondition, tx_cookieconsentplus_statisticscondition, tx_cookieconsentplus_marketingcondition',
--linebreak--, tx_cookieconsentplus_statisticscondition, tx_cookieconsentplus_marketingcondition',
];
ExtensionManagementUtility::addToAllTCAtypes(
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'pages',
'--palette--;;cookiesdependent',
'',
Expand Down
Loading

0 comments on commit 5cb8b6d

Please sign in to comment.