1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Component.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:FlashMessages! |
9
|
|
|
* @subpackage Components |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 12.03.14 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\FlashMessages\Components; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\Application; |
21
|
|
|
use Nette\Localization; |
22
|
|
|
use Nette\Utils; |
23
|
|
|
|
24
|
|
|
use IPub; |
25
|
|
|
use IPub\FlashMessages; |
26
|
|
|
use IPub\FlashMessages\Entities; |
27
|
|
|
use IPub\FlashMessages\Exceptions; |
28
|
|
|
use IPub\FlashMessages\Storage; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Flash messages control |
32
|
|
|
* |
33
|
|
|
* @package iPublikuj:FlashMessages! |
34
|
|
|
* @subpackage Components |
35
|
|
|
* |
36
|
|
|
* @property Application\UI\ITemplate $template |
37
|
|
|
*/ |
38
|
1 |
|
class Control extends Application\UI\Control |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $templateFile; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Storage\IStorage |
47
|
|
|
*/ |
48
|
|
|
private $storage; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Localization\ITranslator |
52
|
|
|
*/ |
53
|
|
|
private $translator; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
private $useTitle = FALSE; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
private $useOverlay = FALSE; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Localization\ITranslator $translator |
67
|
|
|
*/ |
68
|
|
|
public function injectTranslator(Localization\ITranslator $translator = NULL) |
69
|
|
|
{ |
70
|
1 |
|
$this->translator = $translator; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param NULL|string $templateFile |
75
|
|
|
* @param Storage\IStorage $storage |
76
|
|
|
* |
77
|
|
|
* @throws Exceptions\FileNotFoundException |
78
|
|
|
*/ |
79
|
|
|
public function __construct( |
80
|
|
|
$templateFile = NULL, |
81
|
|
|
Storage\IStorage $storage |
82
|
|
|
) { |
83
|
1 |
|
parent::__construct(); |
84
|
|
|
|
85
|
1 |
|
if ($templateFile !== NULL) { |
86
|
|
|
$this->setTemplateFile($templateFile); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$this->storage = $storage; |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \Nette\ComponentModel\IComponent |
94
|
|
|
*/ |
95
|
|
|
public function attached($presenter) |
96
|
|
|
{ |
97
|
1 |
|
parent::attached($presenter); |
98
|
|
|
|
99
|
1 |
|
$this->redrawControl(); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function enableTitle() |
106
|
|
|
{ |
107
|
1 |
|
$this->useTitle = TRUE; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function disableTitle() |
114
|
|
|
{ |
115
|
1 |
|
$this->useTitle = FALSE; |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
public function enableOverlay() |
122
|
|
|
{ |
123
|
1 |
|
$this->useOverlay = TRUE; |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function disableOverlay() |
130
|
|
|
{ |
131
|
1 |
|
$this->useOverlay = FALSE; |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Prepare component for rendering |
136
|
|
|
*/ |
137
|
|
|
public function beforeRender() |
138
|
|
|
{ |
139
|
|
|
// Load messages from session |
140
|
|
|
/** @var Entities\IMessage[] $messages */ |
141
|
1 |
|
$messages = $this->storage->get(Storage\IStorage::KEY_MESSAGES, []); |
142
|
|
|
|
143
|
|
|
// Assign vars to template |
144
|
1 |
|
$this->template->flashes = $messages ? $messages : []; |
|
|
|
|
145
|
1 |
|
$this->template->useTitle = $this->useTitle; |
|
|
|
|
146
|
1 |
|
$this->template->useOverlay = $this->useOverlay; |
|
|
|
|
147
|
|
|
|
148
|
|
|
// Check if translator is available |
149
|
1 |
|
if ($this->getTranslator() instanceof Localization\ITranslator) { |
150
|
|
|
$this->template->setTranslator($this->getTranslator()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// If template was not defined before... |
154
|
1 |
|
if ($this->template->getFile() === NULL) { |
155
|
|
|
// ...try to get base component template file |
156
|
1 |
|
$templateFile = !empty($this->templateFile) ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'default.latte'; |
157
|
1 |
|
$this->template->setFile($templateFile); |
158
|
|
|
} |
159
|
1 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Render control |
163
|
|
|
*/ |
164
|
|
|
public function render() |
165
|
|
|
{ |
166
|
|
|
// Check if control has template |
167
|
1 |
|
if ($this->template instanceof Nette\Bridges\ApplicationLatte\Template) { |
168
|
1 |
|
$this->beforeRender(); |
169
|
|
|
|
170
|
|
|
// Render component template |
171
|
1 |
|
$this->template->render(); |
172
|
|
|
|
173
|
|
|
} else { |
174
|
|
|
throw new Exceptions\InvalidStateException('Flash messages control is without template.'); |
175
|
|
|
} |
176
|
1 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Change default control template path |
180
|
|
|
* |
181
|
|
|
* @param string $templateFile |
182
|
|
|
* |
183
|
|
|
* @return void |
184
|
|
|
* |
185
|
|
|
* @throws Exceptions\FileNotFoundException |
186
|
|
|
*/ |
187
|
|
|
public function setTemplateFile($templateFile) |
188
|
|
|
{ |
189
|
|
|
// Check if template file exists... |
190
|
1 |
|
if (!is_file($templateFile)) { |
191
|
|
|
// Remove extension |
192
|
1 |
|
$template = basename($templateFile, '.latte'); |
193
|
|
|
|
194
|
|
|
// ...check if extension template is used |
195
|
1 |
|
if (is_file(__DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $template . DIRECTORY_SEPARATOR . 'default.latte')) { |
196
|
1 |
|
$templateFile = __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $template . DIRECTORY_SEPARATOR . 'default.latte'; |
197
|
|
|
|
198
|
|
|
} else { |
199
|
|
|
// ...if not throw exception |
200
|
1 |
|
throw new Exceptions\FileNotFoundException('Template file "' . $templateFile . '" was not found.'); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
$this->templateFile = $templateFile; |
205
|
1 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param Localization\ITranslator $translator |
209
|
|
|
* |
210
|
|
|
* @return void |
211
|
|
|
*/ |
212
|
|
|
public function setTranslator(Localization\ITranslator $translator) |
213
|
|
|
{ |
214
|
|
|
$this->translator = $translator; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return Localization\ITranslator|null |
219
|
|
|
*/ |
220
|
|
|
public function getTranslator() |
221
|
|
|
{ |
222
|
1 |
|
if ($this->translator instanceof Localization\ITranslator) { |
223
|
|
|
return $this->translator; |
224
|
|
|
} |
225
|
|
|
|
226
|
1 |
|
return NULL; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: