Skip to content

Commit

Permalink
[FEATURE] Allow To Fetch Levels From Consent Cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWeinert committed Nov 30, 2022
1 parent 02efd38 commit 7f38e99
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/system/Papaya/Session/ConsentCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ public function isRequired() {
);
}

public function getName() {
return $this->papaya()->options->get(
CMSSettings::CONSENT_COOKIE_NAME, Session\ConsentCookie::DEFAULT_NAME
);
}

public function getLevels($minimumLevel = 1) {
$levels = array_filter(
array_map(
static function($value) {
return trim($value);
},
explode(
',',
$this->papaya()->options->get(CMSSettings::CONSENT_COOKIE_LEVELS, self::DEFAULT_LEVELS)
)
),
static function($value) {
return !empty($value);
}
);
$offset = (is_string($minimumLevel))
? array_search($levels, $minimumLevel, TRUE)
: $minimumLevel;
return $offset > 0 ? array_slice($levels, $offset) : $levels;
}

/**
* @return int
*/
Expand All @@ -49,15 +76,7 @@ public function getLevel() {
return self::$_level;
}
$cookieName = $this->papaya()->options->get(CMSSettings::CONSENT_COOKIE_NAME, self::DEFAULT_NAME);
$levels = array_map(
static function($value) {
return trim($value);
},
explode(
',',
$this->papaya()->options->get(CMSSettings::CONSENT_COOKIE_LEVELS, self::DEFAULT_LEVELS)
)
);
$levels = $this->getLevels(0);
if (
isset($_COOKIE[$cookieName]) &&
FALSE !== ($current = array_search($_COOKIE[$cookieName], $levels))
Expand All @@ -67,8 +86,15 @@ static function($value) {
return self::$_level = -1;
}

public function getLevelIndex($levelName) {
$levels = $this->getLevels();
$index = array_search($level, $levels, true);
return (FALSE !== $index) ? $index : -1;
}

public function hasLevel($level) {
return $level <= $this->getLevel();
$minimumLevel = is_string($level) ? $this->getLevelIndex($level) : $level;
return $minimumLevel <= $this->getLevel();
}

public static function reset(){
Expand Down

0 comments on commit 7f38e99

Please sign in to comment.