1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\LegalBundle\Twig; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Routing\RouterInterface; |
17
|
|
|
use Twig\Environment; |
18
|
|
|
use Twig\Extension\AbstractExtension; |
19
|
|
|
use Twig\Loader\LoaderInterface; |
20
|
|
|
use Twig\TwigFunction; |
21
|
|
|
|
22
|
|
|
class TwigExtension extends AbstractExtension |
23
|
|
|
{ |
24
|
|
|
public function __construct( |
25
|
|
|
private readonly RouterInterface $router, |
26
|
|
|
private readonly Environment $twig, |
27
|
|
|
private readonly LoaderInterface $twigLoader, |
28
|
|
|
private readonly array $legalConfig |
29
|
|
|
) { |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getFunctions(): array |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
new TwigFunction('zikulalegalbundle_getUrl', [$this, 'getUrl']), |
36
|
|
|
new TwigFunction('zikulalegalbundle_inlineLink', [$this, 'inlineLink'], ['is_safe' => ['html']]), |
37
|
|
|
new TwigFunction('zikulalegalbundle_minimumAge', [$this, 'getMinimumAge']), |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the link to a specific policy for the Legal bundle. |
43
|
|
|
* |
44
|
|
|
* Example |
45
|
|
|
* {{ zikulalegalbundle_getUrl('termsOfUse') }} |
46
|
|
|
*/ |
47
|
|
|
public function getUrl(string $policy = ''): string |
48
|
|
|
{ |
49
|
|
|
// see https://stackoverflow.com/questions/1993721/how-to-convert-pascalcase-to-snake-case |
50
|
|
|
$policyConfigName = mb_strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $policy)); |
51
|
|
|
if ('accessibility_statement' === $policyConfigName) { |
52
|
|
|
$policyConfigName = 'accessibility'; |
53
|
|
|
} |
54
|
|
|
$policyConfig = $this->legalConfig['policies'][$policyConfigName]; |
55
|
|
|
|
56
|
|
|
return $policyConfig['custom_url'] ?: $this->router->generate('zikulalegalbundle_user_' . mb_strtolower($policy)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Displays a single inline user link of a specific policy for the Legal bundle. |
61
|
|
|
* |
62
|
|
|
* Example |
63
|
|
|
* {{ zikulalegalbundle_inlineLink('termsOfUse') }} |
64
|
|
|
* |
65
|
|
|
* Templates used: |
66
|
|
|
* User/Policy/InlineLink/* |
67
|
|
|
*/ |
68
|
|
|
public function inlineLink(string $policy = '', string $target = ''): string |
69
|
|
|
{ |
70
|
|
|
$templatePath = '@ZikulaLegal/User/Policy/InlineLink/'; |
71
|
|
|
$templateParameters = [ |
72
|
|
|
'policyUrl' => $this->getUrl($policy), |
73
|
|
|
'target' => $target, |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
if (!empty($policy)) { |
77
|
|
|
$template = $templatePath . $policy . '.html.twig'; |
78
|
|
|
if ($this->twigLoader->exists($template)) { |
79
|
|
|
return $this->twig->render($template, $templateParameters); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->twig->render($templatePath . 'notFound.html.twig', $templateParameters); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the minimum age for the age policy. |
88
|
|
|
* |
89
|
|
|
* Example |
90
|
|
|
* {{ zikulalegalbundle_minimumAge() }} |
91
|
|
|
*/ |
92
|
|
|
public function getMinimumAge(): int |
93
|
|
|
{ |
94
|
|
|
return $this->legalConfig['minimum_age']; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|