Skip to content

Commit

Permalink
Drop XCLASS. EnableFields, QueryBuilder constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
davide-alghi committed Nov 1, 2023
1 parent 4cddbf2 commit ee339db
Show file tree
Hide file tree
Showing 33 changed files with 698 additions and 895 deletions.
58 changes: 0 additions & 58 deletions Classes/Compatibility/Version.php

This file was deleted.

119 changes: 119 additions & 0 deletions Classes/Cookie/CookieConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?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 <davide@penguinable.it>
* @license GPLv3
*/

namespace PAD\CookieconsentPlus\Cookie;

use \TYPO3\CMS\Core\Database\ConnectionPool;
use \TYPO3\CMS\Core\Utility\GeneralUtility;
use \PAD\CookieconsentPlus\Cookie\CookieManager;

class CookieConstraint
{
const ISCOOKIESDEPENDENT_OFF = 0;
const ISCOOKIESDEPENDENT_ON = 1;
const CONDITIONTYPE_VALUE_SHOWAND = 'showand';
const CONDITIONTYPE_VALUE_SHOWOR = 'showor';
const CONDITION_VALUE_ANYVALUE = 'anyvalue';
const CONDITION_VALUE_DENIED = 'denied';
const CONDITION_VALUE_ACCEPTED = 'accepted';

/**
* Returns enable fields (constraints) for cookies dependency
*
* @param string $table
* @param array $enableFields - cookieconsent enable fields
* @return string
*/
public static function getCookiesConstraints(string $table, array $enableFields): string
{
$constraints = [];
$cookieManager = GeneralUtility::makeInstance(CookieManager::class);
$isStatisticsOn = $cookieManager->isStatisticsOn();
$isMarketingOn = $cookieManager->isMarketingOn();
$statisticsValues = [
self::CONDITION_VALUE_ANYVALUE,
$isStatisticsOn ? self::CONDITION_VALUE_ACCEPTED : self::CONDITION_VALUE_DENIED,
];
$marketingValues = [
self::CONDITION_VALUE_ANYVALUE,
$isMarketingOn ? self::CONDITION_VALUE_ACCEPTED : self::CONDITION_VALUE_DENIED,
];
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$expressionBuilder = $queryBuilder->expr();
$statisticsValues = array_map([$expressionBuilder, 'literal'], $statisticsValues);
$marketingValues = array_map([$expressionBuilder, 'literal'], $marketingValues);
$constraints[] = $expressionBuilder->eq(
$enableFields['iscookiesdependent'],
self::ISCOOKIESDEPENDENT_OFF
);
$constraints[] = $expressionBuilder->and(
$expressionBuilder->eq(
$enableFields['iscookiesdependent'],
self::ISCOOKIESDEPENDENT_ON
),
$expressionBuilder->eq(
$enableFields['conditiontype'],
$expressionBuilder->literal(self::CONDITIONTYPE_VALUE_SHOWAND)
),
$expressionBuilder->and(
$expressionBuilder->in(
$enableFields['statisticscondition'],
$statisticsValues
),
$expressionBuilder->in(
$enableFields['marketingcondition'],
$marketingValues
),
)
);
$constraints[] = $expressionBuilder->and(
$expressionBuilder->eq(
$enableFields['iscookiesdependent'],
self::ISCOOKIESDEPENDENT_ON
),
$expressionBuilder->eq(
$enableFields['conditiontype'],
$expressionBuilder->literal(self::CONDITIONTYPE_VALUE_SHOWOR)
),
$expressionBuilder->or(
$expressionBuilder->in(
$enableFields['statisticscondition'],
$statisticsValues
),
$expressionBuilder->in(
$enableFields['marketingcondition'],
$marketingValues
),
)
);
return strval($expressionBuilder->or(...$constraints));
}
}
70 changes: 22 additions & 48 deletions Classes/Cookie/CookieManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,12 @@

namespace PAD\CookieconsentPlus\Cookie;

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

class CookieManager
{
const COOKIECONSENTSTATUS_NAME = 'cookieconsent_status';
const COOKIECONSENTSTATUS_DP_NAME = 'dp_cookieconsent_status';
const COOKIEDISMISSVALUE = 'dismiss';
const COOKIEALLOWVALUE = 'allow';
const COOKIEDENYVALUE = 'deny';
const COOKIEDIALOGSTATUSOPEN = 'open';
const COOKIEDIALOGSTATUSAPPROVED = 'approved';
const DP_COOKIECONSENT_STATUS = 'dp_cookieconsent_status';
const DP_COOKIECONSENT_STATISTICS = 'statistics';
const DP_COOKIECONSENT_MARKETING = 'marketing';
const DP_COOKIECONSENT_DIALOG_STATUS_APPROVED = 'approved';

protected $cookieValue = '';
protected $dpCookieValue = [];
Expand All @@ -58,48 +52,28 @@ class CookieManager
*/
public function __construct()
{
$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;
if (isset($_COOKIE[self::DP_COOKIECONSENT_STATUS])) {
$this->cookieValue = '';
$this->cookieStatus = false;
$this->dpCookieValue = json_decode($_COOKIE[self::DP_COOKIECONSENT_STATUS], true);
$this->dpCookieStatus = $this->dpCookieValue['status'];
if ($this->dpCookieStatus == self::DP_COOKIECONSENT_DIALOG_STATUS_APPROVED) {
if (is_array($this->dpCookieValue['checkboxes'])) {
foreach ($this->dpCookieValue['checkboxes'] as $key => $value) {
switch ($value['name']) {
case self::DP_COOKIECONSENT_STATISTICS:
$this->statisticsCookieStatus = boolval($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;
case self::DP_COOKIECONSENT_MARKETING:
$this->marketingCookieStatus = boolval($value['checked']);
break;
}
}
}
} else {
$this->statisticsCookieStatus = false;
$this->marketingCookieStatus = false;
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions Classes/Database/Query/Restriction/CookieRestriction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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 <davide@penguinable.it>
* @license GPLv3
*/

namespace PAD\CookieconsentPlus\Database\Query\Restriction;

use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionInterface;
use PAD\CookieconsentPlus\Cookie\CookieConstraint;

class CookieRestriction implements QueryRestrictionInterface
{
/**
* Main method to build expressions for given tables
* Evaluates the ctrl/delete flag of the table and adds the according restriction if set
*
* @param array $queriedTables Array of tables, where array key is table alias and value is a table name
* @param ExpressionBuilder $expressionBuilder Expression builder instance to add restrictions with
* @return CompositeExpression The result of query builder expression(s)
*/
public function buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder): CompositeExpression
{
$constraints = [];
foreach ($queriedTables as $tableAlias => $tableName) {
if (isset($GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'])) {
$enableColumns = $GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'];
if (isset($enableColumns['cookiesdependent_iscookiesdependent']) && $enableColumns['cookiesdependent_iscookiesdependent'] &&
isset($enableColumns['cookiesdependent_conditiontype']) && $enableColumns['cookiesdependent_conditiontype'] &&
isset($enableColumns['cookiesdependent_statisticscondition']) && $enableColumns['cookiesdependent_statisticscondition'] &&
isset($enableColumns['cookiesdependent_marketingcondition']) && $enableColumns['cookiesdependent_marketingcondition'])
{
$enableFields = [
'iscookiesdependent' => $tableAlias . '.' . $enableColumns['cookiesdependent_iscookiesdependent'],
'conditiontype' => $tableAlias . '.' . $enableColumns['cookiesdependent_conditiontype'],
'statisticscondition' => $tableAlias . '.' . $enableColumns['cookiesdependent_statisticscondition'],
'marketingcondition' => $tableAlias . '.' . $enableColumns['cookiesdependent_marketingcondition'],
];
$constraints[] = CookieConstraint::getCookiesConstraints($tableName, $enableFields);
}
}
}
return $expressionBuilder->andX(...$constraints);
}
}
Loading

0 comments on commit ee339db

Please sign in to comment.