Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie injection text #53

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion resources/lang/en/cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
'less' => 'Less details',
],
'save' => 'Save settings',
'cookie' => 'Cookie',
'purpose' => 'Purpose',
'duration' => 'Duration',
'year' => 'Year|Years',
'day' => 'Day|Days',
'hour' => 'Hour|Hours',
'minute' => 'Minute|Minutes',

'categories' => [
'essentials' => [
Expand All @@ -38,4 +45,4 @@
'_gid' => 'Used by Google Analytics to identify the user.',
'_gat' => 'Used by Google Analytics to throttle the request rate.',
],
];
];
9 changes: 8 additions & 1 deletion resources/lang/fr/cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
'less' => 'Moins d\'informations',
],
'save' => 'Enregistrer',
'cookie' => 'Cookie',
'purpose' => 'But',
'duration' => 'Durée',
'year' => 'Année|Années',
'day' => 'Jour|Jours',
'hour' => 'Heure|Heures',
'minute' => 'Minute|Minutes',

'categories' => [
'essentials' => [
Expand All @@ -38,4 +45,4 @@
'_gid' => 'Utilisé par Google Analytics pour identifier un visiteur.',
'_gat' => 'Utilisé par Google Analytics pour limiter le taux de demande.',
],
];
];
19 changes: 19 additions & 0 deletions resources/views/info.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@foreach($cookies->getCategories() as $category)
<h3>{{ $category->title }}</h3>
<table>
<thead>
<th>@lang('cookieConsent::cookies.cookie')</th>
<th>@lang('cookieConsent::cookies.purpose')</th>
<th>@lang('cookieConsent::cookies.duration')</th>
</thead>
<tbody>
@foreach($category->getCookies() as $cookie)
<tr>
<td>{{ $cookie->name }}</td>
<td>{{ $cookie->description }}</td>
<td>{{ $cookie->durationInHumanReadableFormat() }}</td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<td>{{ $cookie->durationInHumanReadableFormat() }}</td>
<td>{{ \Carbon\CarbonInterval::minutes($cookie->duration)->cascade() }}</td>

</tr>
@endforeach
</tbody>
</table>
@endforeach
36 changes: 36 additions & 0 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,42 @@ public function duration(int $minutes): static
return $this;
}


public function durationInHumanReadableFormat(): string
{
$duration = $this->duration;

$years = floor($duration / 525600); // 1 year = 525600 minutes
$duration %= 525600;

$days = floor($duration / 1440); // 1 day = 1440 minutes
$duration %= 1440;

$hours = floor($duration / 60); // 1 hour = 60 minutes

$minutes = $duration % 60;

$result = [];

if ($years > 0) {
$result[] = "$years ". trans_choice('cookieConsent::cookies.year', $years);
}

if ($days > 0) {
$result[] = "$days ". trans_choice('cookieConsent::cookies.day', $days);
}

if ($hours > 0) {
$result[] = "$hours ". trans_choice('cookieConsent::cookies.hour', $hours);
}

if ($minutes > 0 || empty($result)) {
$result[] = "$minutes ". trans_choice('cookieConsent::cookies.minute', $minutes);
}

return implode(", ", $result);
}

/**
* Set an attribute dynamically.
*/
Expand Down
28 changes: 28 additions & 0 deletions src/CookiesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,32 @@ public function renderButton(string $action, ?string $label = null, array $attri
'basename' => $basename,
])->render();
}

/**
* Output a table with all the cookies infos.
*/
public function renderInfo(): string
{
return view('cookie-consent::info', [
'cookies' => $this->registrar,
])->render();
}

public function replaceInfoTag(string $wysiwyg): string
{
$cookieConsentInfo = view('cookie-consent::info', [
'cookies' => $this->registrar,
])->render();

$formattedString = preg_replace(
[
'/\<(\w)[^\>]+\>\@cookieconsentinfo\<\/\1\>/',
'/\@cookieconsentinfo/',
],
$cookieConsentInfo,
$wysiwyg,
);

return $formattedString;
}
}
6 changes: 5 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,9 @@ protected function registerBladeDirectives()
Blade::directive('cookieconsentbutton', function (string $expression) {
return '<?php echo ' . Facades\Cookies::class . '::renderButton(' . $expression . '); ?>';
});

Blade::directive('cookieconsentinfo', function () {
return '<?php echo ' . Facades\Cookies::class . '::renderInfo(); ?>';
});
}
}
}