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