|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Nip\Form\Renderer\AbstractRenderer; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Form\Elements\AbstractElement; |
|
6
|
|
|
|
|
7
|
|
|
abstract class Nip_Form_Renderer_Elements_Abstract |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
protected $_renderer; |
|
10
|
|
|
protected $_element; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @return AbstractRenderer |
|
14
|
|
|
*/ |
|
15
|
3 |
|
public function getRenderer() |
|
16
|
|
|
{ |
|
17
|
3 |
|
return $this->_renderer; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param AbstractRenderer $renderer |
|
22
|
|
|
* @return $this |
|
23
|
|
|
*/ |
|
24
|
4 |
|
public function setRenderer(AbstractRenderer $renderer) |
|
25
|
|
|
{ |
|
26
|
4 |
|
$this->_renderer = $renderer; |
|
27
|
|
|
|
|
28
|
4 |
|
return $this; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
5 |
|
public function render() |
|
35
|
|
|
{ |
|
36
|
5 |
|
$return = ''; |
|
37
|
5 |
|
$return .= $this->renderElement(); |
|
38
|
|
|
|
|
39
|
5 |
|
$renderErrors = $this->getElement()->getForm()->getOption('render_input_errors'); |
|
40
|
5 |
|
if ($renderErrors !== false) { |
|
41
|
5 |
|
$return .= $this->renderErrors(); |
|
42
|
|
|
} |
|
43
|
5 |
|
$this->getElement()->setRendered(true); |
|
44
|
|
|
|
|
45
|
5 |
|
return $return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
5 |
|
public function renderElement() |
|
52
|
|
|
{ |
|
53
|
5 |
|
$return = $this->renderDecorators($this->generateElement(), 'element'); |
|
|
|
|
|
|
54
|
5 |
|
$this->getElement()->setRendered(true); |
|
55
|
|
|
|
|
56
|
5 |
|
return $return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $return |
|
61
|
|
|
* @param bool $position |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
5 |
|
public function renderDecorators($return, $position = false) |
|
65
|
|
|
{ |
|
66
|
5 |
|
if ($position) { |
|
67
|
5 |
|
$decorators = $this->getElement()->getDecoratorsByPosition($position); |
|
68
|
5 |
|
if (is_array($decorators)) { |
|
69
|
|
|
foreach ($decorators as $decorator) { |
|
70
|
|
|
$return = $decorator->render($return); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
5 |
|
return $return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Nip_Form_Element_Abstract |
|
80
|
|
|
*/ |
|
81
|
5 |
|
public function getElement() |
|
82
|
|
|
{ |
|
83
|
5 |
|
return $this->_element; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
5 |
|
public function setElement(AbstractElement $element) |
|
87
|
|
|
{ |
|
88
|
5 |
|
$this->_element = $element; |
|
89
|
|
|
|
|
90
|
5 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function generateElement() |
|
94
|
|
|
{ |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
5 |
|
public function renderErrors() |
|
99
|
|
|
{ |
|
100
|
5 |
|
$return = ''; |
|
101
|
5 |
|
if ($this->getElement()->isError() && $this->getElement()->getForm()->getOption('renderElementErrors') !== false) { |
|
102
|
|
|
$errors = $this->getElement()->getErrors(); |
|
103
|
|
|
$errors_string = implode('<br />', $errors); |
|
104
|
|
|
$return .= '<span class="help-inline">'.$errors_string.'</span>'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
5 |
|
return $return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param array $overrides |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
3 |
|
public function renderAttributes($overrides = []) |
|
115
|
|
|
{ |
|
116
|
3 |
|
$attribs = $this->getElement()->getAttribs(); |
|
117
|
3 |
|
if (!isset($attribs['title'])) { |
|
118
|
3 |
|
$attribs['title'] = $this->getElement()->getLabel(); |
|
119
|
|
|
} |
|
120
|
3 |
|
$elementAttribs = $this->getElementAttribs(); |
|
121
|
3 |
|
$return = ''; |
|
122
|
3 |
|
foreach ($attribs as $name => $value) { |
|
123
|
3 |
|
if (in_array($name, $elementAttribs)) { |
|
124
|
3 |
|
if (in_array($name, array_keys($overrides))) { |
|
125
|
|
|
$value = $overrides[$name]; |
|
126
|
|
|
} |
|
127
|
3 |
|
if ($name == "name" && $this->getElement()->isGroup()) { |
|
128
|
|
|
$value = $value."[]"; |
|
129
|
|
|
} |
|
130
|
3 |
|
$return .= ' '.$name.'="'.$value.'"'; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
3 |
|
return $return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
3 |
|
public function getElementAttribs() |
|
141
|
|
|
{ |
|
142
|
3 |
|
return ['id', 'name', 'style', 'class', 'title', 'readonly', 'disabled']; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.