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
|
1 |
|
public function renderLabel($classes = null) |
77
|
|
|
{ |
78
|
1 |
|
$label = $this->getElement()->getLabel(); |
79
|
1 |
|
$required = $this->getElement()->isRequired(); |
80
|
1 |
|
$classes = is_array($classes) ? implode(' ', $classes) : $classes; |
81
|
|
|
|
82
|
1 |
|
$return = '<label class="' . $classes . '">'; |
83
|
1 |
|
$return .= $label . ':'; |
84
|
|
|
|
85
|
1 |
|
if ($required) { |
86
|
|
|
$return .= '<span class="required">*</span>'; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$return .= "</label>"; |
90
|
|
|
|
91
|
1 |
|
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
|
8 |
|
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 |
|
$label = $this->getElement()->getLabel(); |
147
|
|
|
$attribs['title'] = $label ? strip_tags($label) : ''; |
148
|
5 |
|
} |
149
|
5 |
|
$elementAttribs = $this->getElementAttribs(); |
150
|
5 |
|
$return = ''; |
151
|
5 |
|
foreach ($attribs as $name => $value) { |
152
|
5 |
|
if (strpos($name, 'data-') === 0 || in_array($name, $elementAttribs)) { |
153
|
|
|
if (in_array($name, array_keys($overrides))) { |
154
|
|
|
$value = $overrides[$name]; |
155
|
5 |
|
} |
156
|
|
|
if ($name == "name" && $this->getElement()->isGroup()) { |
157
|
|
|
$value = $value . "[]"; |
158
|
5 |
|
} |
159
|
|
|
$return .= ' ' . $name . '="' . $value . '"'; |
160
|
|
|
} |
161
|
|
|
} |
162
|
5 |
|
|
163
|
|
|
return $return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return array |
168
|
5 |
|
*/ |
169
|
|
|
public function getElementAttribs() |
170
|
5 |
|
{ |
171
|
|
|
return ['id', 'name', 'style', 'class', 'title', 'readonly', 'disabled']; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|