1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
* @author [email protected] |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Core\Form\View\Helper; |
13
|
|
|
|
14
|
|
|
use Laminas\Form\View\Helper\FormRow as LaminasFormRow; |
15
|
|
|
use Laminas\Form\ElementInterface; |
16
|
|
|
use Core\Form\ViewPartialProviderInterface; |
17
|
|
|
use Core\Form\Element\ViewhelperProviderInterface; |
18
|
|
|
use Laminas\Form\Element\Button; |
19
|
|
|
use Core\Form\Element\Select; |
20
|
|
|
|
21
|
|
|
class FormSimpleRow extends LaminasFormRow |
22
|
|
|
{ |
23
|
|
|
protected $renderStrategies; |
24
|
|
|
|
25
|
|
|
public function render(ElementInterface $element, $labelPosition = null) |
26
|
|
|
{ |
27
|
|
|
$escapeHtmlHelper = $this->getEscapeHtmlHelper(); |
28
|
|
|
$labelHelper = $this->getLabelHelper(); |
29
|
|
|
$elementHelper = $this->getElementHelper(); |
30
|
|
|
$elementErrorsHelper = $this->getElementErrorsHelper(); |
31
|
|
|
|
32
|
|
|
$label = $element->getLabel(); |
33
|
|
|
$inputErrorClass = $this->getInputErrorClass(); |
34
|
|
|
|
35
|
|
|
if (isset($label) && '' !== $label) { |
36
|
|
|
// Translate the label |
37
|
|
|
if (null !== ($translator = $this->getTranslator())) { |
38
|
|
|
$label = $translator->translate( |
39
|
|
|
$label, |
40
|
|
|
$this->getTranslatorTextDomain() |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Does this element have errors ? |
46
|
|
|
if (count($element->getMessages()) > 0 && !empty($inputErrorClass)) { |
|
|
|
|
47
|
|
|
$classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class') . ' ' : ''); |
48
|
|
|
$classAttributes = $classAttributes . $inputErrorClass; |
49
|
|
|
|
50
|
|
|
$element->setAttribute('class', $classAttributes); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($this->partial) { |
54
|
|
|
$vars = array( |
55
|
|
|
'element' => $element, |
56
|
|
|
'label' => $label, |
57
|
|
|
'labelAttributes' => $this->labelAttributes, |
58
|
|
|
'labelPosition' => $this->labelPosition, |
59
|
|
|
'renderErrors' => $this->renderErrors, |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return $this->view->render($this->partial, $vars); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->renderErrors) { |
66
|
|
|
$elementErrors = $elementErrorsHelper->render($element); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$elementString = $elementHelper->render($element); |
70
|
|
|
|
71
|
|
|
if (isset($label) && '' !== $label) { |
72
|
|
|
$label = $escapeHtmlHelper($label); |
73
|
|
|
$labelAttributes = $element->getLabelAttributes(); |
|
|
|
|
74
|
|
|
|
75
|
|
|
if (empty($labelAttributes)) { |
76
|
|
|
$labelAttributes = $this->labelAttributes; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested |
80
|
|
|
// labels. The semantic way is to group them inside a fieldset |
81
|
|
|
$type = $element->getAttribute('type'); |
82
|
|
|
if ($type === 'multi_checkbox' || $type === 'radio') { |
83
|
|
|
$markup = sprintf( |
84
|
|
|
'<fieldset><legend>%s</legend>%s</fieldset>', |
85
|
|
|
$label, |
86
|
|
|
$elementString |
87
|
|
|
); |
88
|
|
|
} else { |
89
|
|
|
if ($element->hasAttribute('id')) { |
90
|
|
|
$labelOpen = ''; |
91
|
|
|
$labelClose = ''; |
92
|
|
|
$label = $labelHelper($element); |
93
|
|
|
} else { |
94
|
|
|
$labelOpen = $labelHelper->openTag($labelAttributes); |
95
|
|
|
$labelClose = $labelHelper->closeTag(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($label !== '' && !$element->hasAttribute('id')) { |
99
|
|
|
$label = '<span>' . $label . '</span>'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Button element is a special case, because label is always rendered inside it |
103
|
|
|
if ($element instanceof Button) { |
104
|
|
|
$labelOpen = $labelClose = $label = ''; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
switch ($this->labelPosition) { |
108
|
|
|
case self::LABEL_PREPEND: |
109
|
|
|
$markup = $labelOpen . $label . $elementString . $labelClose; |
110
|
|
|
break; |
111
|
|
|
case self::LABEL_APPEND: |
112
|
|
|
default: |
113
|
|
|
$markup = $labelOpen . $elementString . $label . $labelClose; |
114
|
|
|
break; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($this->renderErrors) { |
119
|
|
|
$markup .= $elementErrors; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} else { |
122
|
|
|
if (!empty($this->renderErrors)) { |
123
|
|
|
$markup = $elementString . $elementErrors; |
124
|
|
|
} else { |
125
|
|
|
$markup = $elementString; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $markup; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function addStrategy($key, $method, $strategy) |
133
|
|
|
{ |
134
|
|
|
if (array_key_exists($key, $this->renderStrategies)) { |
135
|
|
|
$this->renderStrategies['key'] = array(); |
136
|
|
|
} |
137
|
|
|
$this->renderStrategies['key'][$method] = $strategy; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function applyStrategy($method, $params) |
141
|
|
|
{ |
142
|
|
|
$result = ''; |
143
|
|
|
foreach ($this->renderStrategies as $strategy) { |
144
|
|
|
if (array_key_exists($method, $strategy)) { |
145
|
|
|
$result = vsprintf($strategy[$method], $params); |
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
return $result; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|