Skip to content

Commit 9c41d2e

Browse files
committed
Add @cookieconsentinfo blade directive
1 parent e8a363b commit 9c41d2e

File tree

6 files changed

+86
-3
lines changed

6 files changed

+86
-3
lines changed

resources/lang/en/cookies.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
'less' => 'Less details',
1414
],
1515
'save' => 'Save settings',
16+
'cookie' => 'Cookie',
17+
'purpose' => 'Purpose',
18+
'duration' => 'Duration',
19+
'year' => 'Year|Years',
20+
'day' => 'Day|Days',
21+
'hour' => 'Hour|Hours',
22+
'minute' => 'Minute|Minutes',
1623

1724
'categories' => [
1825
'essentials' => [
@@ -38,4 +45,4 @@
3845
'_gid' => 'Used by Google Analytics to identify the user.',
3946
'_gat' => 'Used by Google Analytics to throttle the request rate.',
4047
],
41-
];
48+
];

resources/lang/fr/cookies.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
'less' => 'Moins d\'informations',
1414
],
1515
'save' => 'Enregistrer',
16+
'cookie' => 'Cookie',
17+
'purpose' => 'But',
18+
'duration' => 'Durée',
19+
'year' => 'Année|Années',
20+
'day' => 'Jour|Jours',
21+
'hour' => 'Heure|Heures',
22+
'minute' => 'Minute|Minutes',
1623

1724
'categories' => [
1825
'essentials' => [
@@ -38,4 +45,4 @@
3845
'_gid' => 'Utilisé par Google Analytics pour identifier un visiteur.',
3946
'_gat' => 'Utilisé par Google Analytics pour limiter le taux de demande.',
4047
],
41-
];
48+
];

resources/views/info.blade.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@foreach($cookies->getCategories() as $category)
2+
<h3>{{ $category->title }}</h3>
3+
<table>
4+
<thead>
5+
<th>@lang('cookieConsent::cookies.cookie')</th>
6+
<th>@lang('cookieConsent::cookies.purpose')</th>
7+
<th>@lang('cookieConsent::cookies.duration')</th>
8+
</thead>
9+
<tbody>
10+
@foreach($category->getCookies() as $cookie)
11+
<tr>
12+
<td>{{ $cookie->name }}</td>
13+
<td>{{ $cookie->description }}</td>
14+
<td>{{ $cookie->durationInHumanReadableFormat() }}</td>
15+
</tr>
16+
@endforeach
17+
</tbody>
18+
</table>
19+
@endforeach

src/Cookie.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,42 @@ public function duration(int $minutes): static
3737
return $this;
3838
}
3939

40+
41+
public function durationInHumanReadableFormat(): string
42+
{
43+
$duration = $this->duration;
44+
45+
$years = floor($duration / 525600); // 1 year = 525600 minutes
46+
$duration %= 525600;
47+
48+
$days = floor($duration / 1440); // 1 day = 1440 minutes
49+
$duration %= 1440;
50+
51+
$hours = floor($duration / 60); // 1 hour = 60 minutes
52+
53+
$minutes = $duration % 60;
54+
55+
$result = [];
56+
57+
if ($years > 0) {
58+
$result[] = "$years ". trans_choice('cookieConsent::cookies.year', $years);
59+
}
60+
61+
if ($days > 0) {
62+
$result[] = "$days ". trans_choice('cookieConsent::cookies.day', $days);
63+
}
64+
65+
if ($hours > 0) {
66+
$result[] = "$hours ". trans_choice('cookieConsent::cookies.hour', $hours);
67+
}
68+
69+
if ($minutes > 0 || empty($result)) {
70+
$result[] = "$minutes ". trans_choice('cookieConsent::cookies.minute', $minutes);
71+
}
72+
73+
return implode(", ", $result);
74+
}
75+
4076
/**
4177
* Set an attribute dynamically.
4278
*/

src/CookiesManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,14 @@ public function renderButton(string $action, ?string $label = null, array $attri
266266
'basename' => $basename,
267267
])->render();
268268
}
269+
270+
/**
271+
* Output a table with all the cookies infos.
272+
*/
273+
public function renderInfo(): string
274+
{
275+
return view('cookie-consent::info', [
276+
'cookies' => $this->registrar,
277+
])->render();
278+
}
269279
}

src/ServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,9 @@ protected function registerBladeDirectives()
7474
Blade::directive('cookieconsentbutton', function (string $expression) {
7575
return '<?php echo ' . Facades\Cookies::class . '::renderButton(' . $expression . '); ?>';
7676
});
77+
78+
Blade::directive('cookieconsentinfo', function () {
79+
return '<?php echo ' . Facades\Cookies::class . '::renderInfo(); ?>';
80+
});
7781
}
78-
}
82+
}

0 commit comments

Comments
 (0)