1
+ <?php
2
+
3
+ class MvcFormTagsHelper extends MvcHelper {
4
+
5
+ // Generalized method that chooses the appropriate input type based on the SQL type of the field
6
+ public function input ($ field_name , $ options =array ()) {
7
+ $ defaults = array (
8
+ 'id ' => self ::input_id ($ field_name ),
9
+ 'name ' => self ::input_name ($ field_name ),
10
+ 'type ' => 'text ' ,
11
+ 'label ' => null ,
12
+ 'value ' => null
13
+ );
14
+ $ options = array_merge ($ defaults , $ options );
15
+ $ method = $ options ['type ' ].'_input ' ;
16
+ $ html = self ::$ method ($ field_name , $ options );
17
+ return $ html ;
18
+ }
19
+
20
+ public function text_input ($ field_name , $ options =array ()) {
21
+ $ defaults = array (
22
+ 'id ' => self ::input_id ($ field_name ),
23
+ 'name ' => self ::input_name ($ field_name ),
24
+ 'type ' => 'text '
25
+ );
26
+ $ options = array_merge ($ defaults , $ options );
27
+ $ attributes_html = self ::attributes_html ($ options , 'input ' );
28
+ $ html = self ::before_input ($ field_name , $ options );
29
+ $ html .= '<input ' .$ attributes_html .' /> ' ;
30
+ $ html .= self ::after_input ($ field_name , $ options );
31
+ return $ html ;
32
+ }
33
+
34
+ public function textarea_input ($ field_name , $ options =array ()) {
35
+ $ defaults = array (
36
+ 'id ' => self ::input_id ($ field_name ),
37
+ 'name ' => self ::input_name ($ field_name )
38
+ );
39
+ $ options = array_merge ($ defaults , $ options );
40
+ $ attributes_html = self ::attributes_html ($ options , 'textarea ' );
41
+ $ html = self ::before_input ($ field_name , $ options );
42
+ $ html .= '<textarea ' .$ attributes_html .'> ' .$ options ['value ' ].'</textarea> ' ;
43
+ $ html .= self ::after_input ($ field_name , $ options );
44
+ return $ html ;
45
+ }
46
+
47
+ public function checkbox_input ($ field_name , $ options =array ()) {
48
+ $ defaults = array (
49
+ 'id ' => self ::input_id ($ field_name ),
50
+ 'name ' => self ::input_name ($ field_name ),
51
+ 'type ' => 'checkbox ' ,
52
+ 'checked ' => false ,
53
+ 'value ' => '1 ' ,
54
+ 'include_hidden_input ' => true
55
+ );
56
+ $ options = array_merge ($ defaults , $ options );
57
+ if (!$ options ['checked ' ]) {
58
+ unset($ options ['checked ' ]);
59
+ } else {
60
+ $ options ['checked ' ] = 'checked ' ;
61
+ }
62
+ $ attributes_html = self ::attributes_html ($ options , 'input ' );
63
+ $ html = self ::before_input ($ field_name , $ options );
64
+ if ($ options ['include_hidden_input ' ]) {
65
+ // Included to allow for a workaround to the issue of unchecked checkbox fields not being sent by clients
66
+ $ html .= '<input type="hidden" name=" ' .self ::esc_attr ($ options ['name ' ]).'" value="0" /> ' ;
67
+ }
68
+ $ html .= '<input ' .$ attributes_html .' /> ' ;
69
+ $ html .= self ::after_input ($ field_name , $ options );
70
+ return $ html ;
71
+ }
72
+
73
+ public function hidden_input ($ field_name , $ options =array ()) {
74
+ $ defaults = array (
75
+ 'id ' => self ::input_id ($ field_name ),
76
+ 'name ' => self ::input_name ($ field_name ),
77
+ 'type ' => 'hidden '
78
+ );
79
+ $ options = array_merge ($ defaults , $ options );
80
+ $ attributes_html = self ::attributes_html ($ options , 'input ' );
81
+ $ html = '<input ' .$ attributes_html .' /> ' ;
82
+ return $ html ;
83
+ }
84
+
85
+ public function select_input ($ field_name , $ options =array ()) {
86
+ $ html = self ::before_input ($ field_name , $ options );
87
+ $ html .= self ::select_tag ($ field_name , $ options );
88
+ $ html .= self ::after_input ($ field_name , $ options );
89
+ return $ html ;
90
+ }
91
+
92
+ public function select_tag ($ field_name , $ options =array ()) {
93
+ $ defaults = array (
94
+ 'id ' => self ::input_id ($ field_name ),
95
+ 'name ' => self ::input_name ($ field_name ),
96
+ 'empty ' => false ,
97
+ 'value ' => null
98
+ );
99
+
100
+ $ options = array_merge ($ defaults , $ options );
101
+ $ options ['options ' ] = empty ($ options ['options ' ]) ? array () : $ options ['options ' ];
102
+ $ attributes_html = self ::attributes_html ($ options , 'select ' );
103
+ $ html = '<select ' .$ attributes_html .'> ' ;
104
+ if ($ options ['empty ' ]) {
105
+ $ empty_name = is_string ($ options ['empty ' ]) ? $ options ['empty ' ] : '' ;
106
+ $ html .= '<option value=""> ' .$ empty_name .'</option> ' ;
107
+ }
108
+ foreach ($ options ['options ' ] as $ key => $ value ) {
109
+ if (is_object ($ value )) {
110
+ $ key = $ value ->__id ;
111
+ $ value = $ value ->__name ;
112
+ }
113
+ $ selected_attribute = $ options ['value ' ] == $ key ? ' selected="selected" ' : '' ;
114
+ $ html .= '<option value=" ' .self ::esc_attr ($ key ).'" ' .$ selected_attribute .'> ' .$ value .'</option> ' ;
115
+ }
116
+ $ html .= '</select> ' ;
117
+ return $ html ;
118
+ }
119
+
120
+ public function button ($ text , $ options =array ()) {
121
+ $ defaults = array (
122
+ 'id ' => self ::input_id ($ text ),
123
+ 'type ' => 'button ' ,
124
+ 'class ' => 'button '
125
+ );
126
+ $ options = array_merge ($ defaults , $ options );
127
+ $ attributes_html = self ::attributes_html ($ options , 'input ' );
128
+ $ html = '<button ' .$ attributes_html .'> ' .$ text .'</button> ' ;
129
+ return $ html ;
130
+ }
131
+
132
+ private function before_input ($ field_name , $ options ) {
133
+ $ defaults = array (
134
+ 'before ' => ''
135
+ );
136
+ $ options = array_merge ($ defaults , $ options );
137
+ $ html = $ options ['before ' ];
138
+ if (!empty ($ options ['label ' ])) {
139
+ $ html .= '<label for=" ' .$ options ['id ' ].'"> ' .$ options ['label ' ].'</label> ' ;
140
+ }
141
+ return $ html ;
142
+ }
143
+
144
+ private function after_input ($ field_name , $ options ) {
145
+ $ defaults = array (
146
+ 'after ' => ''
147
+ );
148
+ $ options = array_merge ($ defaults , $ options );
149
+ $ html = $ options ['after ' ];
150
+ return $ html ;
151
+ }
152
+
153
+ private function input_id ($ field_name ) {
154
+ return $ field_name ;
155
+ }
156
+
157
+ private function input_name ($ field_name ) {
158
+ return $ field_name ;
159
+ }
160
+
161
+ }
162
+
163
+ ?>
0 commit comments