1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Form View Helper. |
5
|
|
|
* |
6
|
|
|
* This view helper overrides the default ZF2 helper to use the LosFormRow |
7
|
|
|
* |
8
|
|
|
* @author Leandro Silva <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @category LosUi |
11
|
|
|
* |
12
|
|
|
* @license https://github.com/Lansoweb/LosUi/blob/master/LICENSE MIT License |
13
|
|
|
* |
14
|
|
|
* @link http://github.com/LansoWeb/LosUi |
15
|
|
|
* @link http://getbootstrap.com/css/#forms |
16
|
|
|
*/ |
17
|
|
|
namespace LosUi\Form\View\Helper; |
18
|
|
|
|
19
|
|
|
use Zend\Form\FormInterface; |
20
|
|
|
use Zend\Form\FieldsetInterface; |
21
|
|
|
use Zend\Form\View\Helper\Form as ZfFormHelper; |
22
|
|
|
use Zend\Form\Element\Button; |
23
|
|
|
use Zend\Form\Element\Submit; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Form View Helper. |
27
|
|
|
* |
28
|
|
|
* This view helper overrides the default ZF2 helper to use the LosFormRow |
29
|
|
|
* |
30
|
|
|
* @author Leandro Silva <[email protected]> |
31
|
|
|
* |
32
|
|
|
* @category LosUi |
33
|
|
|
* |
34
|
|
|
* @license https://github.com/Lansoweb/LosUi/blob/master/LICENSE MIT License |
35
|
|
|
* |
36
|
|
|
* @link http://github.com/LansoWeb/LosUi |
37
|
|
|
* @link http://getbootstrap.com/css/#forms |
38
|
|
|
*/ |
39
|
|
|
class Form extends ZfFormHelper |
40
|
|
|
{ |
41
|
|
|
protected $isHorizontal = false; |
42
|
|
|
|
43
|
|
|
protected $labelColumns = 2; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* (non-PHPdoc). |
47
|
|
|
* |
48
|
|
|
* @see \Zend\Form\View\Helper\Form::render() |
49
|
|
|
*/ |
50
|
|
|
public function render(FormInterface $form, $isHorizontal = false, $labelColumns = 2) |
51
|
|
|
{ |
52
|
|
|
$this->isHorizontal = (bool) $isHorizontal; |
53
|
|
|
$this->labelColumns = (int) $labelColumns; |
54
|
|
|
|
55
|
|
|
$this->setHorizontal($form, $this->isHorizontal); |
56
|
|
|
|
57
|
|
|
if (method_exists($form, 'prepare')) { |
58
|
|
|
$form->prepare(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$formContent = ''; |
62
|
|
|
|
63
|
|
|
$buttons = []; |
64
|
|
|
|
65
|
|
|
foreach ($form as $element) { |
66
|
|
|
if ($element instanceof Button || $element instanceof Submit) { |
67
|
|
|
$buttons[] = $element; |
68
|
|
|
continue; |
69
|
|
|
} elseif ($element instanceof FieldsetInterface) { |
70
|
|
|
$formContent .= $this->view->plugin('losFormCollection')->render($element); |
71
|
|
|
} else { |
72
|
|
|
$formContent .= $this->view->plugin('losFormRow')->render( |
73
|
|
|
$element, |
74
|
|
|
$this->isHorizontal, |
75
|
|
|
$this->labelColumns |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (count($buttons) > 0) { |
81
|
|
|
$formContent .= $this->view->plugin('losFormRow')->renderButtons( |
82
|
|
|
$buttons, |
83
|
|
|
$this->isHorizontal, |
84
|
|
|
$this->labelColumns |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->openTag($form).$formContent.$this->closeTag(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* (non-PHPdoc). |
93
|
|
|
* |
94
|
|
|
* @see \Zend\Form\View\Helper\Form::__invoke() |
95
|
|
|
*/ |
96
|
|
|
public function __invoke(FormInterface $form = null, $isHorizontal = false, $labelColumns = 2) |
97
|
|
|
{ |
98
|
|
|
$this->isHorizontal = (bool) $isHorizontal; |
99
|
|
|
$this->labelColumns = (int) $labelColumns; |
100
|
|
|
|
101
|
|
|
if (! $form) { |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->render($form, $this->isHorizontal, $this->labelColumns); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param FormInterface|null $form |
110
|
|
|
*/ |
111
|
|
|
private function setHorizontal($form, $isHorizontal = false) |
112
|
|
|
{ |
113
|
|
|
$this->isHorizontal = (bool) $isHorizontal; |
114
|
|
|
|
115
|
|
|
if ($this->isHorizontal && $form !== null) { |
116
|
|
|
if ($form->hasAttribute('class')) { |
117
|
|
|
$form->setAttribute('class', 'form-horizontal '.$form->getAttribute('class')); |
118
|
|
|
} else { |
119
|
|
|
$form->setAttribute('class', 'form-horizontal'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function openTag(FormInterface $form = null, $isHorizontal = false) |
125
|
|
|
{ |
126
|
|
|
$this->setHorizontal($form, $isHorizontal); |
127
|
|
|
|
128
|
|
|
return parent::openTag($form); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|