This repository was archived by the owner on Jul 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathTwbBundleForm.php
More file actions
123 lines (112 loc) · 3.68 KB
/
Copy pathTwbBundleForm.php
File metadata and controls
123 lines (112 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace TwbBundle\Form\View\Helper;
use Zend\Form\View\Helper\Form;
use Zend\Form\FormInterface;
use Zend\Form\FieldsetInterface;
class TwbBundleForm extends Form
{
const LAYOUT_HORIZONTAL = 'horizontal';
const LAYOUT_INLINE = 'inline';
/**
* @var string
*/
private static $formRowFormat = '<div class="row">%s</div>';
/**
* Form layout (see LAYOUT_* consts)
*
* @var string
*/
protected $formLayout = null;
/**
* @see Form::__invoke()
* @param FormInterface $oForm
* @param string $sFormLayout
* @return TwbBundleForm|string
*/
public function __invoke(FormInterface $oForm = null, $sFormLayout = self::LAYOUT_HORIZONTAL)
{
if ($oForm) {
return $this->render($oForm, $sFormLayout);
}
$this->formLayout = $sFormLayout;
return $this;
}
/**
* Render a form from the provided $oForm,
* @see Form::render()
* @param FormInterface $oForm
* @param string $sFormLayout
* @return string
*/
public function render(FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL)
{
//Prepare form if needed
if (method_exists($oForm, 'prepare')) {
$oForm->prepare();
}
$this->setFormClass($oForm, $sFormLayout);
//Set form role
if (!$oForm->getAttribute('role')) {
$oForm->setAttribute('role', 'form');
}
$bHasColumnSizes = false;
$sFormContent = '';
$oRenderer = $this->getView();
$rowsOpened = 0;
foreach ($oForm as $oElement) {
$aOptions = $oElement->getOptions();
if (isset($aOptions['twb-row-open']) && $aOptions['twb-row-open']) {
$sFormContent .= '<div class="row">';
$rowsOpened++;
}
if (!$bHasColumnSizes && !empty($aOptions['column-size']) && $rowsOpened === 0) {
$bHasColumnSizes = true;
}
//Define layout option to form elements if not already defined
if ($sFormLayout && empty($aOptions['twb-layout'])) {
$aOptions['twb-layout'] = $sFormLayout;
$oElement->setOptions($aOptions);
}
$sFormContent .= $oElement instanceof FieldsetInterface ? $oRenderer->formCollection($oElement) : $oRenderer->formRow($oElement);
if (isset($aOptions['twb-row-close']) && $aOptions['twb-row-close']) {
$sFormContent .= '</div>';
$rowsOpened--;
}
}
if ($bHasColumnSizes && $sFormLayout !== self::LAYOUT_HORIZONTAL) {
$sFormContent = sprintf(self::$formRowFormat, $sFormContent);
}
return $this->openTag($oForm) . $sFormContent . $this->closeTag();
}
/**
* Sets form layout class
*
* @param FormInterface $oForm
* @param string $sFormLayout
* @return void
*/
protected function setFormClass(FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL)
{
if (is_string($sFormLayout)) {
$sLayoutClass = 'form-'.$sFormLayout;
if ($sFormClass = $oForm->getAttribute('class')) {
if (!preg_match('/(\s|^)' . preg_quote($sLayoutClass, '/') . '(\s|$)/', $sFormClass)) {
$oForm->setAttribute('class', trim($sFormClass . ' ' . $sLayoutClass));
}
} else {
$oForm->setAttribute('class', $sLayoutClass);
}
}
}
/**
* Generate an opening form tag
*
* @param null|FormInterface $form
* @return string
*/
public function openTag(FormInterface $form = null)
{
$this->setFormClass($form, $this->formLayout);
return parent::openTag($form);
}
}