44 * ArrayForm Class
55 *
66 * Responsible for building forms
7+ * Usage
78 *
89 * @param array $elements renderable array containing form elements
910 *
1314
1415class ArrayForm {
1516
16- public $ elements ;
17- public $ form_number = 1 ;
17+ protected $ elements ;
18+ protected $ formData = [];
19+ protected $ formNo = 0 ;
20+ protected $ id ;
1821
19- public function __construct ( $ elements ){
20- $ this ->elements = $ elements ;
22+ public function __construct ( $ formData , $ elements ){
23+ $ this ->formData = $ formData ;
24+ $ this ->elements = $ elements ;
25+ $ this ->id = rand (9 ,1000 );
2126 }
2227
2328 /**
@@ -29,7 +34,7 @@ public function __construct( $elements ){
2934 *
3035 */
3136 public function dumpData () {
32- var_dump ($ this ->elements );
37+ var_dump ( $ this ->elements , $ this -> formData );
3338 }
3439
3540 /**
@@ -39,38 +44,136 @@ public function dumpData() {
3944 * @return string $output contains the form as HTML
4045 *
4146 */
42- function build () {
43- $ output = '' ;
47+ public function build () {
4448
4549 // For multiple forms, create a counter.
46- $ this ->form_number ++;
50+ $ this ->formNo ++;
51+
52+ $ input = $ this ->renderInputs ();
53+ return $ this ->printOutput ( $ input );
54+ }
55+
56+ protected function renderInputs (){
4757
4858 // Loop through each form element and render it.
49- foreach ($ this ->elements as $ name => $ elements ) {
50- $ label = '<label> ' . $ elements ['title ' ] . '</label> ' ;
51- switch ($ elements ['type ' ]) {
52- case 'textarea ' :
53- $ input = '<textarea name=" ' . $ name . '" ></textarea> ' ;
54- break ;
55- case 'submit ' :
56- $ input = '<input type="submit" name=" ' . $ name . '" value=" ' . $ elements ['title ' ] . '"> ' ;
57- $ label = '' ;
58- break ;
59- default :
60- $ input = '<input type=" ' . $ elements ['type ' ] . '" name=" ' . $ name . '" /> ' ;
61- break ;
62- }
63- $ output .= $ label . '<p> ' . $ input . '</p> ' ;
64- }
59+ foreach ($ this ->elements as $ element ) {
60+
61+ //Values to be reset for each input
62+ $ opts = $ input = $ label = $ attributes = "" ;
63+
64+
65+ //Extract key data from attributes
66+ $ id = htmlspecialchars ( $ element ["id " ] );
67+ $ name = htmlspecialchars ( $ element ["name " ] );
68+ $ options = $ element ["options " ] ;
69+ $ default = htmlspecialchars ( $ element ["default " ] );
70+ $ type = htmlspecialchars ( $ element ["type " ] );
71+
72+
73+
74+ unset(
75+ $ element ["id " ],
76+ $ element ["name " ],
77+ $ element ["options " ],
78+ $ element ["type " ],
79+ $ element ["default " ]
80+ );
81+
82+ //Input ID
83+ $ inputID = "DFF $ this ->id - $ this ->formNo - $ id " ;
84+
85+ //Create Attributes
86+ foreach ( $ element as $ attr => $ val ){
87+
88+ $ val = htmlspecialchars ( $ val );
6589
66- // Wrap a form around the inputs.
67- $ output = '
68- <form action=" ' . $ _SERVER ['PHP_SELF ' ] . '" method="post">
69- <input type="hidden" name="action" value="submit_ ' . $ this ->form_number . '" />
70- ' . $ output . '
71- </form> ' ;
90+ if ( gettype ( $ val ) === true ){
91+ $ attributes .= "$ attr=' $ attr' " ;
92+ }
93+ if ( gettype ( $ val ) === false ){
94+
95+ }
96+ else {
97+ $ attributes .= "$ attr=' $ val' " ;
98+ }
99+ }
100+
101+ //Create options
102+ switch ( $ type ) {
103+ case 'textarea ' :
104+ $ input = "<textarea id=' $ inputID' name=' $ id' value=' $ default' $ attributes></textarea> " ;
105+ break ;
106+ case "select " :
107+ foreach ( $ options as $ key => $ val ){
108+ if ( is_numeric ($ key ) ){
109+ $ value = "" ;
110+ }
111+ else {
112+ $ key = htmlspecialchars ( $ key );
113+ $ value = "value=' $ key' " ;
114+ }
115+ $ opts .= "\n <option $ value> $ val</option> " ;
116+ }
117+ $ input = "<select id=' $ inputID' name=' $ id' $ attributes value=' $ default'> $ opts \n </select> " ;
118+
119+ break ;
120+ case "radio " :
121+ case "checkbox " :
122+ foreach ( $ options as $ key =>$ val ){
123+ if ( is_numeric ( $ key ) ){
124+ $ val = htmlspecialchars ( $ val );
125+ $ value = "value=' $ val' " ;
126+ }
127+ else {
128+ $ key = htmlspecialchars ( $ key );
129+ $ value = "value=' $ key' " ;
130+ }
131+ $ input .= "\n<label><input type=' $ type' name=' $ id' $ attributes $ value/> $ val</label> " ;
132+ }
133+
134+ break ;
135+ case 'submit ' :
136+ $ input = '<input type="submit" name=" ' . $ id . '" value=" ' . $ name . '"> ' ;
137+ $ label = '' ;
138+ break ;
139+ default :
140+ $ input = "<input id=' $ inputID' type=' $ type' name=' $ id' $ attributes /> " ;
141+ break ;
142+ }
143+
144+ switch ( $ this ->formData ["display " ] ){
145+
146+ case "table " :
147+ $ output .=<<<INPUT
148+ <tr>
149+ <td class="DFForm-input-title"><label for=" $ inputID"> $ name</label></td>
150+ <td class="DFForm-input-content"> $ input</td>
151+ </tr>
152+ INPUT ;
153+ break ;
154+
155+ default :
156+ $ output .=<<<INPUT
157+ <div class="DFForm-input">
158+ <div><label for=" $ inputID"> $ name</label></div>
159+ <div> $ input</div>
160+ </div>
161+ INPUT ;
162+
163+ }
164+
165+ }
72166
73- // Return the form.
74167 return $ output ;
75168 }
169+
170+ protected function printOutput ( $ output ){
171+ //HTML wrapper for all inputs
172+ $ wrapper = ( $ this ->formData ["display " ] == "table " ) ? ["<table class='DFForm-table'> " ,"</table> " ] : ["" ,"" ];
173+
174+ return "
175+ <form id='DFF $ this ->id - $ this ->formNo ' class='DFForm'>
176+ $ wrapper [0 ] $ output $ wrapper [1 ]
177+ </form> " ;
178+ }
76179 }
0 commit comments