1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Application; |
3
|
|
|
|
4
|
|
|
use Mezon\HtmlTemplate\HtmlTemplate; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class ViewBase |
8
|
|
|
* |
9
|
|
|
* @package Mezon |
10
|
|
|
* @subpackage View |
11
|
|
|
* @author Dodonov A.A. |
12
|
|
|
* @version v.1.0 (2019/08/06) |
13
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base class for all views |
18
|
|
|
*/ |
19
|
|
|
abstract class ViewBase implements \Mezon\Application\ViewInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Template variable |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const ERROR_CODE = 'error-code'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Template variable |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const ERROR_MESSAGE = 'error-message'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Template variable |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
const SUCCESS_MESSAGE = 'success-message'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Active template |
45
|
|
|
* |
46
|
|
|
* @var HtmlTemplate |
47
|
|
|
*/ |
48
|
|
|
private $template = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Error code |
52
|
|
|
* |
53
|
|
|
* @var integer |
54
|
|
|
*/ |
55
|
|
|
private $errorCode = 0; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Error message |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $errorMessage = ''; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Success message |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $successMessage = ''; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* View variables |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
private $variables = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Constructor |
80
|
|
|
* |
81
|
|
|
* @param HtmlTemplate $template |
82
|
|
|
* template |
83
|
|
|
*/ |
84
|
|
|
public function __construct(HtmlTemplate $template = null) |
85
|
|
|
{ |
86
|
|
|
$this->template = $template; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Method returns template |
91
|
|
|
* |
92
|
|
|
* @return HtmlTemplate template |
93
|
|
|
*/ |
94
|
|
|
public function getTemplate(): HtmlTemplate |
95
|
|
|
{ |
96
|
|
|
if ($this->template === null) { |
97
|
|
|
throw (new \Exception('Template was not set for the view', - 1)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->template; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Method returns code of the last error |
105
|
|
|
* |
106
|
|
|
* @return int code of the last error |
107
|
|
|
*/ |
108
|
|
|
public function getErrorCode(): int |
109
|
|
|
{ |
110
|
|
|
return $this->errorCode; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Method checks if the template was setup |
115
|
|
|
* |
116
|
|
|
* @return bool true if the method was setup, false otherwise |
117
|
|
|
*/ |
118
|
|
|
public function templateWasSetup(): bool |
119
|
|
|
{ |
120
|
|
|
return $this->template !== null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Method sets code of the last error |
125
|
|
|
* |
126
|
|
|
* @param int $code |
127
|
|
|
* code of the last error |
128
|
|
|
*/ |
129
|
|
|
public function setErrorCode(int $errorCode): void |
130
|
|
|
{ |
131
|
|
|
$this->errorCode = $errorCode; |
132
|
|
|
|
133
|
|
|
if ($this->templateWasSetup()) { |
134
|
|
|
$this->getTemplate()->setPageVar(ViewBase::ERROR_CODE, $errorCode); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Method return last error description |
140
|
|
|
* |
141
|
|
|
* @return string last error description |
142
|
|
|
*/ |
143
|
|
|
public function getErrorMessage(): string |
144
|
|
|
{ |
145
|
|
|
return $this->errorMessage; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Method sets last error description |
150
|
|
|
* |
151
|
|
|
* @param |
152
|
|
|
* string last error description |
153
|
|
|
*/ |
154
|
|
|
public function setErrorMessage(string $errorMessage): void |
155
|
|
|
{ |
156
|
|
|
$this->errorMessage = $errorMessage; |
157
|
|
|
|
158
|
|
|
if ($this->templateWasSetup()) { |
159
|
|
|
$this->getTemplate()->setPageVar(ViewBase::ERROR_MESSAGE, $errorMessage); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Method return success message |
165
|
|
|
* |
166
|
|
|
* @return string success message |
167
|
|
|
*/ |
168
|
|
|
public function getSuccessMessage(): string |
169
|
|
|
{ |
170
|
|
|
return $this->successMessage; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Method sets success message |
175
|
|
|
* |
176
|
|
|
* @param string $successMessage |
177
|
|
|
* success message |
178
|
|
|
*/ |
179
|
|
|
public function setSuccessMessage(string $successMessage): void |
180
|
|
|
{ |
181
|
|
|
$this->successMessage = $successMessage; |
182
|
|
|
|
183
|
|
|
if ($this->templateWasSetup()) { |
184
|
|
|
$this->getTemplate()->setPageVar(ViewBase::SUCCESS_MESSAGE, $successMessage); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Method sets view's var |
190
|
|
|
* |
191
|
|
|
* @param string $name |
192
|
|
|
* var name |
193
|
|
|
* @param mixed $value |
194
|
|
|
* var value |
195
|
|
|
* @param bool $setTemplateVar |
196
|
|
|
* do we need to set template parameter |
197
|
|
|
*/ |
198
|
|
|
public function setViewParameter(string $name, $value, bool $setTemplateVar): void |
199
|
|
|
{ |
200
|
|
|
if ($this->template !== null && $setTemplateVar) { |
201
|
|
|
$this->template->setPageVar($name, $value); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$this->variables[$name] = $value; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Method sets view's var |
209
|
|
|
* |
210
|
|
|
* @param string $name |
211
|
|
|
* var name |
212
|
|
|
* @return mixed view's variable value |
213
|
|
|
*/ |
214
|
|
|
public function getViewParameter(string $name) |
215
|
|
|
{ |
216
|
|
|
return $this->variables[$name] ?? null; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|