|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Nip\Form\Renderer\AbstractRenderer; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Form\Elements\AbstractElement; |
|
6
|
|
|
|
|
7
|
|
|
class Nip_Form_Renderer_Bootstrap extends AbstractRenderer |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @return string |
|
12
|
|
|
*/ |
|
13
|
|
|
public function renderElements() |
|
14
|
|
|
{ |
|
15
|
|
|
$return = ''; |
|
16
|
|
|
|
|
17
|
|
|
$renderRows = $this->renderRows(); |
|
18
|
|
|
if ($renderRows) { |
|
19
|
|
|
$return .= $renderRows; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
return $return; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
public function renderRows() |
|
29
|
|
|
{ |
|
30
|
|
|
$elements = $this->getElements(); |
|
31
|
|
|
$return = ''; |
|
32
|
|
|
foreach ($elements as $element) { |
|
33
|
|
|
$return .= $this->renderRow($element); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Nip_Form_Element_Abstract $element |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function renderRow($element) |
|
44
|
|
|
{ |
|
45
|
|
|
$return = ''; |
|
46
|
|
|
if (!$element->isRendered()) { |
|
47
|
|
|
if ($element->hasCustomRenderer()) { |
|
48
|
|
|
return $element->render(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$return .= '<div class="form-group row-'.$element->getUniqueId().($element->isError() ? ' has-error' : '').'">'; |
|
52
|
|
|
|
|
53
|
|
|
$renderLabel = $element->getOption('render_label'); |
|
|
|
|
|
|
54
|
|
|
if ($renderLabel !== false) { |
|
|
|
|
|
|
55
|
|
|
$return .= $this->renderLabel($element); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$class = ''; |
|
59
|
|
|
if ($this->getForm()->hasClass('form-horizontal')) { |
|
60
|
|
|
$class = $element->getType() == 'checkbox' ? 'col-sm-offset-3 col-sm-9' : 'col-sm-9'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$return .= '<div class="'.$class.'">'; |
|
64
|
|
|
$return .= $this->renderElement($element); |
|
65
|
|
|
|
|
66
|
|
|
$helpBlock = $element->getOption('form-help'); |
|
67
|
|
|
if ($helpBlock) { |
|
68
|
|
|
$return .= '<span class="help-block">'.$helpBlock.'</span>'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$return .= $element->renderErrors(); |
|
72
|
|
|
$return .= '</div>'; |
|
73
|
|
|
$return .= '</div>'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $label |
|
81
|
|
|
* @param bool $required |
|
82
|
|
|
* @param bool $error |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function renderLabel($label, $required = false, $error = false) |
|
86
|
|
|
{ |
|
87
|
|
|
if (is_object($label)) { |
|
88
|
|
|
$element = $label; |
|
89
|
|
|
$label = $element->getLabel(); |
|
90
|
|
|
$required = $element->isRequired(); |
|
91
|
|
|
$error = $element->isError(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$return = '<label class="control-label'.($this->getForm()->hasClass('form-horizontal') ? ' col-sm-3' : '').($error ? ' error' : '').'">'; |
|
95
|
|
|
$return .= $label.':'; |
|
96
|
|
|
|
|
97
|
|
|
if ($required) { |
|
98
|
|
|
$return .= '<span class="required">*</span>'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$return .= "</label>"; |
|
102
|
|
|
|
|
103
|
|
|
return $return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param AbstractElement $element |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
|
|
public function renderElement(AbstractElement $element) |
|
111
|
|
|
{ |
|
112
|
|
|
$element->addClass('form-control'); |
|
113
|
|
|
|
|
114
|
|
|
return $element->renderElement(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function renderButtons() |
|
121
|
|
|
{ |
|
122
|
|
|
$return = ''; |
|
123
|
|
|
$buttons = $this->getForm()->getButtons(); |
|
124
|
|
|
|
|
125
|
|
|
if ($buttons) { |
|
|
|
|
|
|
126
|
|
|
$return .= '<div class="form-group"> |
|
127
|
|
|
<div class="'.($this->getForm()->hasClass('form-horizontal') ? 'col-sm-offset-3 col-sm-9' : '').'">'; |
|
128
|
|
|
foreach ($buttons as $button) { |
|
129
|
|
|
$return .= $button->render()."\n"; |
|
130
|
|
|
} |
|
131
|
|
|
$return .= '</div>'; |
|
132
|
|
|
$return .= '</div>'; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $return; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
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.