|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula Foundation - 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\BlocksModule; |
|
15
|
|
|
|
|
16
|
|
|
use LogicException; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
18
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
19
|
|
|
use Twig\Environment; |
|
20
|
|
|
use Zikula\Bundle\CoreBundle\AbstractBundle; |
|
21
|
|
|
use Zikula\Bundle\CoreBundle\Translation\TranslatorTrait; |
|
22
|
|
|
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface; |
|
23
|
|
|
use Zikula\ExtensionsModule\ExtensionVariablesTrait; |
|
24
|
|
|
use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface; |
|
25
|
|
|
|
|
26
|
|
|
abstract class AbstractBlockHandler implements BlockHandlerInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use TranslatorTrait; |
|
29
|
|
|
use ExtensionVariablesTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var AbstractBundle |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $bundle; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var RequestStack |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $requestStack; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var PermissionApiInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $permissionApi; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Environment |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $twig; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct( |
|
52
|
|
|
AbstractBundle $bundle, |
|
53
|
|
|
RequestStack $requestStack, |
|
54
|
|
|
TranslatorInterface $translator, |
|
55
|
|
|
VariableApiInterface $variableApi, |
|
56
|
|
|
PermissionApiInterface $permissionApi, |
|
57
|
|
|
Environment $twig |
|
58
|
|
|
) { |
|
59
|
|
|
$this->bundle = $bundle; |
|
60
|
|
|
$this->extensionName = $bundle->getName(); // for ExtensionVariablesTrait |
|
61
|
|
|
$this->requestStack = $requestStack; |
|
62
|
|
|
$this->setTranslator($translator); // for TranslatorTrait |
|
63
|
|
|
$this->variableApi = $variableApi; // for ExtensionVariablesTrait |
|
|
|
|
|
|
64
|
|
|
$this->permissionApi = $permissionApi; |
|
65
|
|
|
$this->twig = $twig; |
|
66
|
|
|
$this->boot($bundle); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Boot the handler. |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function boot(AbstractBundle $bundle): void |
|
73
|
|
|
{ |
|
74
|
|
|
// load optional bootstrap |
|
75
|
|
|
$bootstrap = $bundle->getPath() . '/bootstrap.php'; |
|
76
|
|
|
if (file_exists($bootstrap)) { |
|
77
|
|
|
include_once $bootstrap; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getFormClassName(): string |
|
82
|
|
|
{ |
|
83
|
|
|
return ''; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getFormOptions(): array |
|
87
|
|
|
{ |
|
88
|
|
|
return []; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getFormTemplate(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return '@ZikulaBlocksModule/Block/default_modify.html.twig'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getPropertyDefaults(): array |
|
97
|
|
|
{ |
|
98
|
|
|
return []; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function display(array $properties): string |
|
102
|
|
|
{ |
|
103
|
|
|
return nl2br(implode("\n", $properties)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function getType(): string |
|
107
|
|
|
{ |
|
108
|
|
|
// default to the ClassName without the `Block` suffix |
|
109
|
|
|
// note: This string is intentionally left untranslated. |
|
110
|
|
|
$fqCn = get_class($this); |
|
111
|
|
|
$pos = mb_strrpos($fqCn, '\\'); |
|
112
|
|
|
|
|
113
|
|
|
return mb_substr($fqCn, $pos + 1, -5); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Adds a flash message to the current session for type. |
|
118
|
|
|
* |
|
119
|
|
|
* @throws LogicException |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function addFlash(string $type, string $message): void |
|
122
|
|
|
{ |
|
123
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
|
124
|
|
|
if (null === $request) { |
|
125
|
|
|
return; |
|
126
|
|
|
} |
|
127
|
|
|
if (!$request->hasSession()) { |
|
128
|
|
|
throw new LogicException('You can not use the addFlash method if sessions are disabled.'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$request->getSession()->getFlashBag()->add($type, $message); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Returns a rendered view. |
|
136
|
|
|
*/ |
|
137
|
|
|
public function renderView(string $view, array $parameters = []): string |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->twig->render($view, $parameters); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Convenience shortcut to check if user has requested permissions. |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function hasPermission( |
|
146
|
|
|
string $component = null, |
|
147
|
|
|
string $instance = null, |
|
148
|
|
|
int $level = null, |
|
149
|
|
|
int $user = null |
|
150
|
|
|
): bool { |
|
151
|
|
|
return $this->permissionApi->hasPermission($component, $instance, $level, $user); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getBundle(): AbstractBundle |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->bundle; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.