This will convert the jquery.FormBuilder version 1.0 form structure (serialized or json forms) to the new jquery.formbuilder version 2.0 structure.
Project link: https://github.com/viveleroi/jquery.formbuilder
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
<?php function form_converter($form_id, $form_structure) { $format_test = @unserialize(preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $form_structure)); if ($format_test !== false) { //this is a serialized form $form_structure = str_replace("'", "'", $form_structure); $form_structure = unserialize(preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $form_structure)); } else { //must be json $form_structure = json_decode($form_structure, true); } $out = array(); $i = 1; foreach($form_structure as $k => $v){ // // Changes applicable to all fields // //Setup the fbid rand num (Uses rand() because server is too fast for time() to stay unique) $fbid_num = rand(1111111111111,9999999999999); //Add "name" and "sortOrder" fields $v['name'] = false; $v['sortOrder'] = false; //Create the sortOrder index $v['sortOrder'] = $i; //Rename "cssClass" field to "type" $v['type'] = $v['cssClass']; unset($v['cssClass']); //Change required=>checked to required=>1 and "undefined" to false $v['required'] = ($v['required'] == 'checked' ? '1' : false); // // Text Fields Changes // if($v['type'] == 'input_text'){ //create the full fbid $v['fbid'] = 'text_' . $fbid_num; //update type=>input_text to type=>text $v['type'] = 'text'; //rename "values" to "label" $v['label'] = $v['values']; unset($v['values']); } // // Textarea Fields Changes // if($v['type'] == 'textarea'){ //create the full fbid $v['fbid'] = 'textarea_' . $fbid_num; //rename "values" to "label" $v['label'] = $v['values']; unset($v['values']); } // // Checkbox Field Changes // if($v['type'] == 'checkbox'){ //create the full fbid $v['fbid'] = 'checkbox_' . $fbid_num; //rename "title" to "label" $v['label'] = $v['title']; unset($v['title']); //rename "values" to "choices" $v['choices'] = $v['values']; unset($v['values']); //format the checkbox choices $cb_out = array(); $cb_i = 0; if($v['choices']) { foreach($v['choices'] as $cb_k => $cb_v){ //rename "baseline" to "selected" $cb_v['selected'] = $cb_v['baseline']; unset($cb_v['baseline']); //Change selected=>checked to selected=>1 and "undefined" to false $cb_v['selected'] = ($cb_v['selected'] == 'checked' ? '1' : false); //rename "value" to "label" $cb_v['label'] = $cb_v['value']; unset($cb_v['value']); $new_cb_keys[] = $cb_i; $cb_i++; array_push($cb_out,$cb_v); } $v['choices'] = array_combine($new_cb_keys, $cb_out); unset($new_cb_keys); } } // // Radio Field Changes // if($v['type'] == 'radio'){ //create the full fbid $v['fbid'] = 'radio_' . $fbid_num; //rename "title" to "label" $v['label'] = $v['title']; unset($v['title']); //rename "values" to "choices" $v['choices'] = $v['values']; unset($v['values']); //format the radio box choices $rb_out = array(); $rb_i = 0; if($v['choices']) { foreach ($v['choices'] as $rb_k => $rb_v) { //rename "baseline" to "selected" $rb_v['selected'] = $rb_v['baseline']; unset($rb_v['baseline']); //Change selected=>checked to selected=>1 and "undefined" to false $rb_v['selected'] = ($rb_v['selected'] == 'checked' ? '1' : false); //rename "value" to "label" $rb_v['label'] = $rb_v['value']; unset($rb_v['value']); $new_rb_keys[] = $rb_i; $rb_i++; array_push($rb_out, $rb_v); } $v['choices'] = array_combine($new_rb_keys, $rb_out); unset($new_rb_keys); } } // // Select Field Changes // if($v['type'] == 'select'){ //create the full fbid $v['fbid'] = 'select_' . $fbid_num; //rename "title" to "label" $v['label'] = $v['title']; unset($v['title']); //rename "values" to "choices" $v['choices'] = $v['values']; unset($v['values']); //remove "multiple" key unset($v['multiple']); //format the select choices $sc_out = array(); $sc_i = 0; if($v['choices']) { foreach ($v['choices'] as $sc_k => $sc_v) { //rename "baseline" to "selected" $sc_v['selected'] = $sc_v['baseline']; unset($sc_v['baseline']); //Change selected=>checked to selected=>1 and "undefined" to false $sc_v['selected'] = ($sc_v['selected'] == 'checked' ? '1' : false); //rename "value" to "label" $sc_v['label'] = $sc_v['value']; unset($sc_v['value']); $new_sc_keys[] = $sc_i; $sc_i++; array_push($sc_out, $sc_v); } $v['choices'] = array_combine($new_sc_keys, $sc_out); unset($new_sc_keys); } } $i++; $new_keys[] = $v['fbid']; array_push($out,$v); } $converted_form_model = array_combine($new_keys, $out); $form = array( "form_id" => $form_id, "model" => $converted_form_model ); $form = json_encode($form); return $form; } //Two possible form structure types: //json form example (from 1.0's fake-form-db-vals.php) $json_form_structure = Array( 'form_structure' => '[{"cssClass":"input_text","required":"undefined","values":"First Name"},{"cssClass":"input_text","required":"undefined","values":"Last Name"},{"cssClass":"input_text","required":"undefined","values":"Email","type":"email"},{"cssClass":"textarea","required":"undefined","values":"Bio"},{"cssClass":"checkbox","required":"undefined","title":"What\'s on your pizza?","values":{"2":{"value":"Extra Cheese","baseline":"undefined"},"3":{"value":"Pepperoni","baseline":"undefined"},"4":{"value":"Beef","baseline":"undefined"}}}]'); //serialized form example (created with old version of 1.0) $serialized_form_structure = 'a:5:{i:0;a:3:{s:8:"cssClass";s:10:"input_text";s:8:"required";s:9:"undefined";s:6:"values";s:10:"First Name";}i:1;a:3:{s:8:"cssClass";s:10:"input_text";s:8:"required";s:9:"undefined";s:6:"values";s:9:"Last Name";}i:2;a:3:{s:8:"cssClass";s:10:"input_text";s:8:"required";s:9:"undefined";s:6:"values";s:5:"Email";}i:3;a:3:{s:8:"cssClass";s:8:"textarea";s:8:"required";s:9:"undefined";s:6:"values";s:3:"Bio";}i:4;a:4:{s:8:"cssClass";s:8:"checkbox";s:8:"required";s:9:"undefined";s:5:"title";s:26:"What's on your pizza?";s:6:"values";a:3:{i:2;a:2:{s:5:"value";s:12:"Extra Cheese";s:8:"baseline";s:9:"undefined";}i:3;a:2:{s:5:"value";s:9:"Pepperoni";s:8:"baseline";s:9:"undefined";}i:4;a:2:{s:5:"value";s:4:"Beef";s:8:"baseline";s:9:"undefined";}}}}'; //form created with 2.0 (for comparison) $version_2_form = '{"form_id":"1","model":{"text_1428119398207":{"name":false,"fbid":"text_1428119398207","label":"First Name","required":false,"type":"text","sortOrder":1},"text_1428119399325":{"name":false,"fbid":"text_1428119399325","label":"Last Name","required":false,"type":"text","sortOrder":2},"text_1428119400691":{"name":false,"fbid":"text_1428119400691","label":"Email","required":false,"type":"text","sortOrder":3},"textarea_1428119402013":{"name":false,"fbid":"textarea_1428119402013","label":"Bio","required":false,"type":"textarea","sortOrder":4},"checkbox_1428119403153":{"name":false,"fbid":"checkbox_1428119403153","label":"What\'s on your pizza?","required":false,"choices":[{"selected":false,"label":"Extra Cheese"},{"selected":false,"label":"Pepperoni"},{"selected":false,"label":"Beef"}],"type":"checkbox","sortOrder":5}}}'; $form_id = '1'; // 1.0 to 2.0 - json example //Run the json form through the function (the "cssClass" is a FormBuilder 1.0 form check) $form_structure = $json_form_structure[form_structure]; if (strpos($form_structure,'cssClass') !== false) { $updated_json_form_code = form_converter($form_id, $form_structure); } // 1.0 to 2.0 - serialized example //Run the serialized form through the same function $form_structure = $serialized_form_structure; if (strpos($form_structure,'cssClass') !== false) { $updated_serialized_form_code = form_converter($form_id, $form_structure); } ?> <!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> </head> <body> <div style="font-family: monospace; white-space:pre;"> <p>1.0 json form conversion output:</p> <?php var_dump($updated_json_form_code);?> <p>1.0 serialized form conversion output:</p> <?php var_dump($updated_serialized_form_code);?> <p>Form created with 2.0 (for comparison)</p> <?php var_dump($version_2_form);?> </div> </body> </html> |
The output when the above code is run (I’m using PHP 5.5):
1 2 3 4 5 6 7 8 9 10 11 |
1.0 json form conversion output: string(824) "{"form_id":"1","model":{"text_5955102911425":{"required":false,"name":false,"sortOrder":1,"type":"text","fbid":"text_5955102911425","label":"First Name"},"text_6115533755057":{"required":false,"name":false,"sortOrder":2,"type":"text","fbid":"text_6115533755057","label":"Last Name"},"text_8350398921304":{"required":false,"type":"text","name":false,"sortOrder":3,"fbid":"text_8350398921304","label":"Email"},"textarea_1981103900405":{"required":false,"name":false,"sortOrder":4,"type":"textarea","fbid":"textarea_1981103900405","label":"Bio"},"checkbox_8567867688834":{"required":false,"name":false,"sortOrder":5,"type":"checkbox","fbid":"checkbox_8567867688834","label":"Whats on your pizza?","choices":[{"selected":false,"label":"Extra Cheese"},{"selected":false,"label":"Pepperoni"},{"selected":false,"label":"Beef"}]}}}" 1.0 serialized form conversion output: string(824) "{"form_id":"1","model":{"text_3023350267774":{"required":false,"name":false,"sortOrder":1,"type":"text","fbid":"text_3023350267774","label":"First Name"},"text_9516621749434":{"required":false,"name":false,"sortOrder":2,"type":"text","fbid":"text_9516621749434","label":"Last Name"},"text_5590182311005":{"required":false,"name":false,"sortOrder":3,"type":"text","fbid":"text_5590182311005","label":"Email"},"textarea_2501362785696":{"required":false,"name":false,"sortOrder":4,"type":"textarea","fbid":"textarea_2501362785696","label":"Bio"},"checkbox_5132304839789":{"required":false,"name":false,"sortOrder":5,"type":"checkbox","fbid":"checkbox_5132304839789","label":"Whats on your pizza?","choices":[{"selected":false,"label":"Extra Cheese"},{"selected":false,"label":"Pepperoni"},{"selected":false,"label":"Beef"}]}}}" Form created with 2.0 (for comparison) string(824) "{"form_id":"1","model":{"text_1428119398207":{"name":false,"fbid":"text_1428119398207","label":"First Name","required":false,"type":"text","sortOrder":1},"text_1428119399325":{"name":false,"fbid":"text_1428119399325","label":"Last Name","required":false,"type":"text","sortOrder":2},"text_1428119400691":{"name":false,"fbid":"text_1428119400691","label":"Email","required":false,"type":"text","sortOrder":3},"textarea_1428119402013":{"name":false,"fbid":"textarea_1428119402013","label":"Bio","required":false,"type":"textarea","sortOrder":4},"checkbox_1428119403153":{"name":false,"fbid":"checkbox_1428119403153","label":"Whats on your pizza?","required":false,"choices":[{"selected":false,"label":"Extra Cheese"},{"selected":false,"label":"Pepperoni"},{"selected":false,"label":"Beef"}],"type":"checkbox","sortOrder":5}}}" |