|
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
|
|
|
* Active template |
|
38
|
|
|
* |
|
39
|
|
|
* @var HtmlTemplate |
|
40
|
|
|
*/ |
|
41
|
|
|
private $template = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Error code |
|
45
|
|
|
* |
|
46
|
|
|
* @var integer |
|
47
|
|
|
*/ |
|
48
|
|
|
private $errorCode = 0; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Error message |
|
52
|
|
|
* |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
private $errorMessage = ''; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* View variables |
|
59
|
|
|
* |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
private $variables = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Constructor |
|
66
|
|
|
* |
|
67
|
|
|
* @param HtmlTemplate $template |
|
68
|
|
|
* template |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __construct(HtmlTemplate $template = null) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->template = $template; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Method returns template |
|
77
|
|
|
* |
|
78
|
|
|
* @return HtmlTemplate template |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getTemplate(): HtmlTemplate |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->template === null) { |
|
83
|
|
|
throw (new \Exception('Template was not set for the view', - 1)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->template; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Method returns code of the last error |
|
91
|
|
|
* |
|
92
|
|
|
* @return int code of the last error |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getErrorCode(): int |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->errorCode; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Method checks if the template was setup |
|
101
|
|
|
* |
|
102
|
|
|
* @return bool true if the method was setup, false otherwise |
|
103
|
|
|
*/ |
|
104
|
|
|
public function templateWasSetup(): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->template !== null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Method sets code of the last error |
|
111
|
|
|
* |
|
112
|
|
|
* @param int $code |
|
113
|
|
|
* code of the last error |
|
114
|
|
|
*/ |
|
115
|
|
|
public function setErrorCode(int $errorCode): void |
|
116
|
|
|
{ |
|
117
|
|
|
$this->errorCode = $errorCode; |
|
118
|
|
|
|
|
119
|
|
|
if ($this->templateWasSetup()) { |
|
120
|
|
|
$this->getTemplate()->setPageVar(ViewBase::ERROR_CODE, $errorCode); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Method return last error description |
|
126
|
|
|
* |
|
127
|
|
|
* @return string last error description |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getErrorMessage(): string |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->errorMessage; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Method sets last error description |
|
136
|
|
|
* |
|
137
|
|
|
* @param |
|
138
|
|
|
* string last error description |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setErrorMessage(string $errorMessage): void |
|
141
|
|
|
{ |
|
142
|
|
|
$this->errorMessage = $errorMessage; |
|
143
|
|
|
|
|
144
|
|
|
if ($this->templateWasSetup()) { |
|
145
|
|
|
$this->getTemplate()->setPageVar(ViewBase::ERROR_MESSAGE, $errorMessage); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Method sets view's var |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $name |
|
153
|
|
|
* var name |
|
154
|
|
|
* @param mixed $value |
|
155
|
|
|
* var value |
|
156
|
|
|
* @param bool $setTemplateVar |
|
157
|
|
|
* do we need to set template parameter |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setViewParameter(string $name, $value, bool $setTemplateVar): void |
|
160
|
|
|
{ |
|
161
|
|
|
if ($this->template !== null && $setTemplateVar) { |
|
162
|
|
|
$this->template->setPageVar($name, $value); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$this->variables[$name] = $value; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Method sets view's var |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $name |
|
172
|
|
|
* var name |
|
173
|
|
|
* @return mixed view's variable value |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getViewParameter(string $name) |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->variables[$name] ?? null; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|