1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Core\Form\View\Helper; |
4
|
|
|
|
5
|
|
|
use Zend\Form\View\Helper\FormRow as ZendFormRow; |
6
|
|
|
use Zend\Form\View\Helper\AbstractHelper; |
7
|
|
|
use Zend\Form\ElementInterface; |
8
|
|
|
|
9
|
|
|
class FormRowCombined extends AbstractHelper |
10
|
|
|
{ |
11
|
|
|
protected $layout; |
12
|
|
|
|
13
|
1 |
|
public function getFormRowHelper() |
14
|
|
|
{ |
15
|
1 |
|
$helper = clone $this->view->plugin('formRow'); |
|
|
|
|
16
|
1 |
|
$helper->setShouldWrap(false); |
17
|
1 |
|
return $helper; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Utility form helper that renders a label (if it exists), an element and errors |
22
|
|
|
* |
23
|
|
|
* @param ElementInterface $elements |
24
|
|
|
* @return string |
25
|
|
|
* @throws \Zend\Form\Exception\DomainException |
26
|
|
|
*/ |
27
|
1 |
|
public function render(array $elements) |
28
|
|
|
{ |
29
|
1 |
|
$formRowHelper = $this->getFormRowHelper(); |
30
|
1 |
|
$labels = array(); |
31
|
1 |
|
$markups = ''; |
32
|
1 |
|
$totalSpanWidth = 0; |
33
|
1 |
|
foreach ($elements as $spanWidth => $element) { |
34
|
1 |
|
$elementErrors = $element->getMessages(); |
35
|
1 |
|
$markup = $formRowHelper->render($element); |
36
|
1 |
|
if (preg_match('~<label.*</label>~isU', $markup, $match)) { |
37
|
1 |
|
$labels[] = $match[0]; |
38
|
1 |
|
$markup = str_replace($match[0], '', $markup); |
39
|
|
|
} else { |
40
|
|
|
//$labels[] = false; |
41
|
|
|
} |
42
|
1 |
|
$markups .= sprintf( |
43
|
1 |
|
'<div class="col-md-%d%s">%s</div>', |
44
|
1 |
|
$spanWidth, |
45
|
1 |
|
empty($elementErrors) ? '' : ' input-error', |
46
|
1 |
|
$markup |
47
|
|
|
); |
48
|
1 |
|
$totalSpanWidth += $spanWidth; |
49
|
|
|
} |
50
|
1 |
|
$labelSpanWidth = 12 - $totalSpanWidth; |
51
|
|
|
|
52
|
1 |
|
$labelMarkup = sprintf( |
53
|
1 |
|
'<div class="col-md-%d yk-label">%s</div>', |
54
|
1 |
|
$labelSpanWidth, |
55
|
1 |
|
implode(' / ', $labels) |
56
|
|
|
); |
57
|
|
|
|
58
|
1 |
|
$form_row_class = 'row'; |
59
|
1 |
|
if ($this->layout == 'form-horizontal') { |
60
|
1 |
|
$form_row_class = 'form-group'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
1 |
|
return sprintf( |
65
|
1 |
|
'<div class="controls controls-row ' . $form_row_class . '">%s%s</div>', |
66
|
1 |
|
$labelMarkup, |
67
|
1 |
|
$markups |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Invoke helper as function |
73
|
|
|
* |
74
|
|
|
* Proxies to {@link render()}. |
75
|
|
|
* |
76
|
|
|
* @param null|ElementInterface $element |
77
|
|
|
* @param null|string $labelPosition |
78
|
|
|
* @param bool $renderErrors |
79
|
|
|
* @return string|FormRow |
80
|
|
|
*/ |
81
|
1 |
|
public function __invoke(array $elements = array(), $labelPosition = null, $renderErrors = null, $layout = null) |
82
|
|
|
{ |
83
|
1 |
|
if (empty($elements)) { |
84
|
|
|
return $this; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
if (null !== $layout) { |
88
|
1 |
|
$this->layout = $layout; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return $this->render($elements); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|