|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Form\Renderer; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Form\AbstractForm; |
|
6
|
|
|
use Nip\Form\Renderer\Traits\HasButtonRendererTrait; |
|
7
|
|
|
use Nip\Helpers\View\Errors as ErrorsHelper; |
|
8
|
|
|
use Nip\Helpers\View\Messages as MessagesHelper; |
|
9
|
|
|
use Nip\Form\Elements\AbstractElement; |
|
10
|
|
|
use Nip_Form_Renderer_Elements_Abstract as AbstractElementRenderer; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class AbstractRenderer |
|
14
|
|
|
* @package Nip\Form\Renderer |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractRenderer |
|
17
|
|
|
{ |
|
18
|
|
|
use HasButtonRendererTrait; |
|
19
|
|
|
|
|
20
|
|
|
protected $form; |
|
21
|
|
|
|
|
22
|
|
|
protected $elements; |
|
23
|
|
|
protected $elementsRenderer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* AbstractRenderer constructor. |
|
27
|
|
|
*/ |
|
28
|
8 |
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
8 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getElements() |
|
36
|
|
|
{ |
|
37
|
|
|
if (!$this->elements) { |
|
38
|
|
|
$this->elements = $this->getForm()->getElements(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->elements; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param $elements |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setElements($elements) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->elements = $elements; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return AbstractForm |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function getForm() |
|
56
|
|
|
{ |
|
57
|
1 |
|
return $this->form; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param AbstractForm $form |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
8 |
|
public function setForm(AbstractForm $form) |
|
65
|
|
|
{ |
|
66
|
8 |
|
$this->form = $form; |
|
67
|
|
|
|
|
68
|
8 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function render() |
|
75
|
|
|
{ |
|
76
|
|
|
$return = $this->openTag(); |
|
77
|
|
|
$return .= $this->renderHidden(); |
|
78
|
|
|
|
|
79
|
|
|
$renderErrors = $this->getForm()->getOption('render_messages'); |
|
80
|
|
|
if ($renderErrors !== false) { |
|
81
|
|
|
$return .= $this->renderMessages(); |
|
82
|
|
|
} |
|
83
|
|
|
$return .= $this->renderGroups(); |
|
84
|
|
|
$return .= $this->renderElements(); |
|
85
|
|
|
$return .= $this->renderButtons(); |
|
86
|
|
|
|
|
87
|
|
|
$return .= $this->closeTag(); |
|
88
|
|
|
|
|
89
|
|
|
return $return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function openTag() |
|
96
|
|
|
{ |
|
97
|
|
|
$return = '<form '; |
|
98
|
|
|
$atributes = $this->getForm()->getAttribs(); |
|
99
|
|
|
foreach ($atributes as $name => $value) { |
|
100
|
|
|
$return .= $name . '="' . $value . '" '; |
|
101
|
|
|
} |
|
102
|
|
|
$return .= '>'; |
|
103
|
|
|
|
|
104
|
|
|
return $return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function renderHidden() |
|
111
|
|
|
{ |
|
112
|
|
|
$hiddenElements = $this->getForm()->findElements(['type' => 'hidden']); |
|
|
|
|
|
|
113
|
|
|
$return = ''; |
|
114
|
|
|
if ($hiddenElements) { |
|
|
|
|
|
|
115
|
|
|
foreach ($hiddenElements as $element) { |
|
116
|
|
|
$return .= $this->renderElement($element); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param AbstractElement $element |
|
125
|
|
|
* @return mixed |
|
126
|
|
|
*/ |
|
127
|
|
|
public function renderElement(AbstractElement $element) |
|
128
|
|
|
{ |
|
129
|
|
|
return $element->render(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* The errors are rendered using the Errors View Helper |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function renderMessages() |
|
137
|
|
|
{ |
|
138
|
|
|
$return = ''; |
|
139
|
|
|
$messages = $this->getForm()->getMessages(); |
|
140
|
|
|
foreach ($messages as $type => $lines) { |
|
141
|
|
|
if ($type == "error") { |
|
142
|
|
|
$return .= ErrorsHelper::render($lines); |
|
143
|
|
|
} else { |
|
144
|
|
|
$return .= MessagesHelper::render($lines, $type); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
|
|
public function renderGroups() |
|
155
|
|
|
{ |
|
156
|
|
|
$groups = $this->getForm()->getDisplayGroups(); |
|
157
|
|
|
$return = ''; |
|
158
|
|
|
foreach ($groups as $group) { |
|
159
|
|
|
$return .= $group->render(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $return; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return string |
|
167
|
|
|
*/ |
|
168
|
|
|
public function renderElements() |
|
169
|
|
|
{ |
|
170
|
|
|
return ''; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
public function closeTag() |
|
177
|
|
|
{ |
|
178
|
|
|
$return = '</form>'; |
|
179
|
|
|
|
|
180
|
|
|
return $return; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param string|AbstractElement $label |
|
185
|
|
|
* @param bool $required |
|
186
|
|
|
* @param bool $error |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
|
|
public function renderLabel($label, $required = false, $error = false) |
|
190
|
|
|
{ |
|
191
|
|
|
if (is_object($label)) { |
|
192
|
|
|
$element = $label; |
|
193
|
|
|
$label = $element->getLabel(); |
|
194
|
|
|
$required = $element->isRequired(); |
|
195
|
|
|
$error = $element->isError(); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$return = '<label class="col-sm-3 ' . ($error ? ' error' : '') . '">'; |
|
199
|
|
|
$return .= $label . ':'; |
|
200
|
|
|
|
|
201
|
|
|
if ($required) { |
|
202
|
|
|
$return .= '<span class="required">*</span>'; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$return .= "</label>"; |
|
206
|
|
|
|
|
207
|
|
|
return $return; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param AbstractElement $element |
|
212
|
|
|
* @return mixed |
|
213
|
|
|
*/ |
|
214
|
5 |
|
public function getElementRenderer(AbstractElement $element) |
|
215
|
|
|
{ |
|
216
|
5 |
|
$name = $element->getUniqueId(); |
|
217
|
5 |
|
if (!isset($this->elementsRenderer[$name])) { |
|
218
|
5 |
|
$this->elementsRenderer[$name] = $this->getNewElementRenderer($element); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
5 |
|
return $this->elementsRenderer[$name]; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param AbstractElement $element |
|
226
|
|
|
* @return mixed |
|
227
|
|
|
*/ |
|
228
|
5 |
|
protected function getNewElementRenderer(AbstractElement $element) |
|
229
|
|
|
{ |
|
230
|
5 |
|
$type = $element->getType(); |
|
231
|
5 |
|
$name = 'Nip_Form_Renderer_Elements_' . ucfirst($type); |
|
232
|
|
|
/** @var AbstractElementRenderer $renderer */ |
|
233
|
5 |
|
$renderer = new $name(); |
|
234
|
5 |
|
$renderer->setRenderer($this); |
|
235
|
5 |
|
$renderer->setElement($element); |
|
236
|
|
|
|
|
237
|
5 |
|
return $renderer; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|