1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\GDPRBundle\Block\Service; |
13
|
|
|
|
14
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
15
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractBlockService; |
16
|
|
|
use Sonata\BlockBundle\Block\Service\EditableBlockService; |
17
|
|
|
use Sonata\BlockBundle\Form\Mapper\FormMapper; |
18
|
|
|
use Sonata\BlockBundle\Meta\Metadata; |
19
|
|
|
use Sonata\BlockBundle\Meta\MetadataInterface; |
20
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
21
|
|
|
use Sonata\Form\Type\ImmutableArrayType; |
22
|
|
|
use Sonata\Form\Validator\ErrorElement; |
23
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
24
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
25
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
26
|
|
|
use Symfony\Component\Form\Extension\Core\Type\UrlType; |
27
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
28
|
|
|
use Symfony\Component\HttpFoundation\Response; |
29
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
30
|
|
|
|
31
|
|
|
final class GDPRInformationBlockService extends AbstractBlockService implements EditableBlockService |
32
|
|
|
{ |
33
|
|
|
public const COOKIE_NAME = 'GDPR_COOKIE_LAW_CONSENT'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RequestStack |
37
|
|
|
*/ |
38
|
|
|
private $request; |
39
|
|
|
|
40
|
|
|
public function __construct(string $name, EngineInterface $templating, RequestStack $request) |
41
|
|
|
{ |
42
|
|
|
parent::__construct($name, $templating); |
43
|
|
|
|
44
|
|
|
$this->request = $request; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function execute(BlockContextInterface $blockContext, Response $response = null) |
48
|
|
|
{ |
49
|
|
|
if ($this->hasGdprCookie()) { |
50
|
|
|
return new Response('', Response::HTTP_NO_CONTENT); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$parameters = [ |
54
|
|
|
'context' => $blockContext, |
55
|
|
|
'settings' => $blockContext->getSettings(), |
56
|
|
|
'block' => $blockContext->getBlock(), |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
return $this->renderPrivateResponse($blockContext->getTemplate(), $parameters, $response); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function configureCreateForm(FormMapper $form, BlockInterface $block): void |
63
|
|
|
{ |
64
|
|
|
$this->configureEditForm($form, $block); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function configureEditForm(FormMapper $formMapper, BlockInterface $block): void |
68
|
|
|
{ |
69
|
|
|
$formMapper->add('settings', ImmutableArrayType::class, [ |
70
|
|
|
'keys' => [ |
71
|
|
|
['text', TextType::class, [ |
72
|
|
|
'label' => 'form.label_text', |
73
|
|
|
'required' => false, |
74
|
|
|
]], |
75
|
|
|
['url', UrlType::class, [ |
76
|
|
|
'label' => 'form.label_url', |
77
|
|
|
'required' => false, |
78
|
|
|
]], |
79
|
|
|
['position', ChoiceType::class, [ |
80
|
|
|
'label' => 'form.label_position', |
81
|
|
|
'choices' => [ |
82
|
|
|
'form.choice_top' => 'top', |
83
|
|
|
'form.choice_fixedtop' => 'fixedtop', |
84
|
|
|
'form.choice_bottom' => 'bottom', |
85
|
|
|
'form.choice_block' => 'block', |
86
|
|
|
], |
87
|
|
|
]], |
88
|
|
|
], |
89
|
|
|
'translation_domain' => 'Core23GDPRBundle', |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function configureSettings(OptionsResolver $resolver): void |
94
|
|
|
{ |
95
|
|
|
$resolver->setDefaults([ |
96
|
|
|
'text' => null, |
97
|
|
|
'url' => 'https://gdpr-info.eu/', |
98
|
|
|
'template' => '@Core23GDPR/Block/block_gdpr.html.twig', |
99
|
|
|
'position' => 'block', |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function validate(ErrorElement $errorElement, BlockInterface $block): void |
104
|
|
|
{ |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getMetadata(): MetadataInterface |
108
|
|
|
{ |
109
|
|
|
return new Metadata($this->getName(), null, null, 'Core23GDPRBundle', [ |
110
|
|
|
'class' => 'fa fa-balance-scale', |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function hasGdprCookie(): bool |
115
|
|
|
{ |
116
|
|
|
$request = $this->request->getMasterRequest(); |
117
|
|
|
|
118
|
|
|
return null !== $request && $request->cookies->getBoolean(self::COOKIE_NAME); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|