|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Form\Renderer\Elements; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Form\Elements\AbstractElement; |
|
6
|
|
|
use Nip\Form\Renderer\AbstractRenderer; |
|
7
|
|
|
use Nip\Form\Renderer\Elements\Traits\CanRenderErrors; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractElementRenderer |
|
11
|
|
|
* @package Nip\Form\Renderer\Elements |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractElementRenderer |
|
14
|
|
|
{ |
|
15
|
|
|
use CanRenderErrors; |
|
16
|
|
|
|
|
17
|
|
|
protected $_renderer; |
|
18
|
|
|
protected $_element; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return AbstractRenderer |
|
22
|
|
|
*/ |
|
23
|
3 |
|
public function getRenderer() |
|
24
|
|
|
{ |
|
25
|
3 |
|
return $this->_renderer; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param AbstractRenderer $renderer |
|
30
|
|
|
* |
|
31
|
|
|
* @return $this |
|
32
|
|
|
*/ |
|
33
|
6 |
|
public function setRenderer(AbstractRenderer $renderer) |
|
34
|
|
|
{ |
|
35
|
6 |
|
$this->_renderer = $renderer; |
|
36
|
|
|
|
|
37
|
6 |
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
7 |
|
public function render() |
|
44
|
|
|
{ |
|
45
|
7 |
|
$return = ''; |
|
46
|
7 |
|
$return .= $this->renderElement(); |
|
47
|
7 |
|
$return .= $this->renderErrors(); |
|
48
|
|
|
|
|
49
|
7 |
|
return $return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
*/ |
|
55
|
8 |
|
public function renderElement() |
|
56
|
|
|
{ |
|
57
|
8 |
|
$this->renderElementBefore(); |
|
58
|
8 |
|
$return = $this->renderDecorators($this->generateElement(), 'element'); |
|
|
|
|
|
|
59
|
8 |
|
$this->getElement()->setRendered(true); |
|
60
|
|
|
|
|
61
|
8 |
|
return $return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
8 |
|
protected function renderElementBefore() |
|
65
|
|
|
{ |
|
66
|
8 |
|
if ($this->getElement()->isError()) { |
|
67
|
|
|
$formRenderer = $this->getElement()->getForm()->getRenderer(); |
|
68
|
|
|
$this->getElement()->addClass($formRenderer->classForElementHasError()); |
|
69
|
|
|
} |
|
70
|
8 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string|array $classes |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function renderLabel($classes = null) |
|
77
|
|
|
{ |
|
78
|
2 |
|
$label = $this->getElement()->getLabel(); |
|
79
|
2 |
|
$required = $this->getElement()->isRequired(); |
|
80
|
2 |
|
$classes = is_array($classes) ? implode(' ', $classes) : $classes; |
|
81
|
|
|
|
|
82
|
2 |
|
$return = '<label class="' . $classes . '">'; |
|
83
|
2 |
|
$return .= $label . ':'; |
|
84
|
|
|
|
|
85
|
2 |
|
if ($required) { |
|
86
|
|
|
$return .= '<span class="required">*</span>'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
$return .= "</label>"; |
|
90
|
|
|
|
|
91
|
2 |
|
return $return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $return |
|
96
|
|
|
* @param bool $position |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
8 |
|
public function renderDecorators($return, $position = false) |
|
101
|
|
|
{ |
|
102
|
8 |
|
if ($position) { |
|
103
|
8 |
|
$decorators = $this->getElement()->getDecoratorsByPosition($position); |
|
104
|
8 |
|
if (is_array($decorators)) { |
|
105
|
|
|
foreach ($decorators as $decorator) { |
|
106
|
|
|
$return = $decorator->render($return); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
8 |
|
return $return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return AbstractElement |
|
116
|
|
|
*/ |
|
117
|
8 |
|
public function getElement() |
|
118
|
|
|
{ |
|
119
|
8 |
|
return $this->_element; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param AbstractElement $element |
|
124
|
|
|
* |
|
125
|
|
|
* @return $this |
|
126
|
|
|
*/ |
|
127
|
8 |
|
public function setElement(AbstractElement $element) |
|
128
|
|
|
{ |
|
129
|
8 |
|
$this->_element = $element; |
|
130
|
|
|
|
|
131
|
8 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
abstract public function generateElement(); |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param array $overrides |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
5 |
|
public function renderAttributes($overrides = []) |
|
143
|
|
|
{ |
|
144
|
5 |
|
$attribs = $this->getElement()->getAttribs(); |
|
145
|
5 |
|
if (!isset($attribs['title'])) { |
|
146
|
5 |
|
$attribs['title'] = strip_tags($this->getElement()->getLabel()); |
|
147
|
|
|
} |
|
148
|
5 |
|
$elementAttribs = $this->getElementAttribs(); |
|
149
|
5 |
|
$return = ''; |
|
150
|
5 |
|
foreach ($attribs as $name => $value) { |
|
151
|
5 |
|
if (in_array($name, $elementAttribs)) { |
|
152
|
5 |
|
if (in_array($name, array_keys($overrides))) { |
|
153
|
|
|
$value = $overrides[$name]; |
|
154
|
|
|
} |
|
155
|
5 |
|
if ($name == "name" && $this->getElement()->isGroup()) { |
|
156
|
|
|
$value = $value . "[]"; |
|
157
|
|
|
} |
|
158
|
5 |
|
$return .= ' ' . $name . '="' . $value . '"'; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
5 |
|
return $return; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
5 |
|
public function getElementAttribs() |
|
169
|
|
|
{ |
|
170
|
5 |
|
return ['id', 'name', 'style', 'class', 'title', 'readonly', 'disabled']; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|