<?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>