Skip to content

Commit

Permalink
always hated the retreive/store encoding/decoding complexity, so I du…
Browse files Browse the repository at this point in the history
…mped it
  • Loading branch information
viveleroi committed Dec 15, 2011
1 parent b992b4d commit 941e6d4
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 101 deletions.
101 changes: 16 additions & 85 deletions Formbuilder/Formbuilder.php
Expand Up @@ -28,28 +28,10 @@
class Formbuilder { class Formbuilder {


/** /**
* @var array Contains the form_hash and serialized form_structure from an external source (db) * @var array Holds the form id and array form structure
* @access protected * @access protected
*/ */
protected $_container; protected $_form_array;

/**
* Form ID, useful for storing in a database, etc
* @var type
*/
protected $_form_id;

/**
* @var array Holds the form source in raw array form
* @access protected
*/
protected $_structure;

/**
* @var array Holds the form source in serialized form
* @access protected
*/
protected $_structure_ser;




/** /**
Expand All @@ -64,89 +46,38 @@ public function __construct($form = false){
// Set the serialized structure if it's provided // Set the serialized structure if it's provided
// otherwise, store the source // otherwise, store the source
if(array_key_exists('form_structure', $form)){ if(array_key_exists('form_structure', $form)){

$form['form_structure'] = json_decode($form['form_structure']);
$this->_container = $form; // set the form as the container $this->_form_array = $form;
$this->_structure_ser = $form['form_structure']; // pull the encoded form
$this->_structure = $this->retrieve(); // unserialize the form as the raw structure

} }
else if(array_key_exists('frmb', $form)){ else if(array_key_exists('frmb', $form)){

$_form = array();
$this->_form_id = ($form['form_id'] == "undefined" ? false : $form['form_id']); $_form['form_id'] = ($form['form_id'] == "undefined" ? false : $form['form_id']);
$this->_structure = $form['frmb']; // since the form is from POST, set it as the raw array $_form['form_structure'] = $form['frmb']; // since the form is from POST, set it as the raw array
$this->_structure_ser = $this->store(); // encode it $this->_form_array = $_form;
$this->rebuild_container(); // rebuild a new container

} }
return true; return true;
} }




/** /**
* Wipes and re-saves the structure and hash to the containing array. * Returns the form array with the structure encoded, for saving to a database or other store
*
* @access protected
* @return boolean
*/
protected function rebuild_container(){
$this->_container = array();
$this->_container['form_structure'] = $this->_structure_ser;
return true;
}


/**
* Takes an array containing the form admin information
* and serializes it for storage in the database. Provides a hash
* that can will be used later during rendering.
*
* The array provided is typically from $_POST generated by the jquery
* plugin.
* *
* @access public * @access public
* @return array * @return array
*/ */
public function store(){ public function get_encoded_form_array(){
$this->_structure_ser = json_encode($this->_structure); return array('form_id'=>$this->_form_array['form_id'],'form_structure'=>json_encode($this->_form_array['form_structure']));
return array('form_id'=>$this->_form_id,'form_structure'=>$this->_structure_ser);
}


/**
* Returns a serialized form back into it's original array, for use
* with rendering.
*
* @param string $form_array
* @access public
* @return boolean
*/
public function retrieve(){
if(is_array($this->_container) && array_key_exists('form_structure', $this->_container)){
return json_decode($this->_container['form_structure']);
}
return false;
} }




/** /**
* Prints out the generated json file with a content-type of application/json * Prints out the generated json file with a content-type of application/json
* *
* @access public * @access public
* @uses generate_json
*/ */
public function render_json(){ public function render_json(){
header("Content-Type: application/json"); header("Content-Type: application/json");
print $this->_structure_ser; print json_encode( $this->_form_array );
}


/**
* Builds a json object that the jquery plugin will parse
*
* @access public
*/
public function generate_json(){
return json_encode( $this->_structure );
} }




Expand Down Expand Up @@ -175,12 +106,12 @@ public function generate_html($form_action = false){


$form_action = $form_action ? $form_action : $_SERVER['PHP_SELF']; $form_action = $form_action ? $form_action : $_SERVER['PHP_SELF'];


if(is_array($this->_structure)){ if(is_array($this->_form_array['form_structure'])){


$html .= '<form class="frm-bldr" method="post" action="'.$form_action.'">' . "\n"; $html .= '<form class="frm-bldr" method="post" action="'.$form_action.'">' . "\n";
$html .= '<ol>'."\n"; $html .= '<ol>'."\n";


foreach($this->_structure as $field){ foreach($this->_form_array['form_structure'] as $field){
$html .= $this->loadField($field); $html .= $this->loadField($field);
} }


Expand Down Expand Up @@ -208,8 +139,8 @@ public function process(){
$results = array(); $results = array();


// Put together an array of all expected indices // Put together an array of all expected indices
if(is_array($this->_structure)){ if(is_array($this->_form_array['form_structure'])){
foreach($this->_structure as $field){ foreach($this->_form_array['form_structure'] as $field){


$field['required'] = $field['required'] == 'checked' ? ' required' : false; $field['required'] = $field['required'] == 'checked' ? ' required' : false;


Expand Down
2 changes: 1 addition & 1 deletion Formbuilder/Formbuilder_pdo.php
Expand Up @@ -57,7 +57,7 @@ public function connect($url = "mysql:host=127.0.0.1;dbname=formbuilder", $user
* Save the data to the database, but still returns the $for_db array. * Save the data to the database, but still returns the $for_db array.
*/ */
public function save_form(){ public function save_form(){
$for_db = parent::store(); $for_db = parent::get_encoded_form_array();
if($for_db['form_id']){ if($for_db['form_id']){
$stmt = $this->_db->prepare("UPDATE fb_savedforms SET form_structure = :struct WHERE id = :id"); $stmt = $this->_db->prepare("UPDATE fb_savedforms SET form_structure = :struct WHERE id = :id");
$stmt->bindParam(':id', $for_db['form_id'], PDO::PARAM_INT); $stmt->bindParam(':id', $for_db['form_id'], PDO::PARAM_INT);
Expand Down
2 changes: 2 additions & 0 deletions README
Expand Up @@ -23,6 +23,8 @@ Version 0.4 - 20111215
- Removing old XML-based form loading system, moving to pure JSON - Removing old XML-based form loading system, moving to pure JSON
- Removing internal serialization system in favor of json formatting - Removing internal serialization system in favor of json formatting
- Adding example mysql storage methods to the load, save process (PDO-based so other databases may be used) - Adding example mysql storage methods to the load, save process (PDO-based so other databases may be used)
- Incorporated a required attribute bug fix: https://snowy-evening.com/botsko/jquery-form-builder/8/
- Dumped a ton of unnecessary complexity of encoding/decoding and array vars


Version 0.3.1 - 20110722 Version 0.3.1 - 20110722
- Corrected issue with reserved words breaking support for Safari, Opera, IE - Corrected issue with reserved words breaking support for Safari, Opera, IE
Expand Down
2 changes: 1 addition & 1 deletion docs/DOCUMENTATION
Expand Up @@ -60,7 +60,7 @@ To save the POST request from the jquery object, just invoke the following:
$form = new Formbuilder($form_data); $form = new Formbuilder($form_data);


// Return the array for saving to a database // Return the array for saving to a database
$for_db = $form->store(); $for_db = $form->get_encoded_form_array();


The $for_db variable is an array and will contain a hash value, and the The $for_db variable is an array and will contain a hash value, and the
serialized form value. Save these two values to your database. They both need serialized form value. Save these two values to your database. They both need
Expand Down
2 changes: 1 addition & 1 deletion example-html.php
Expand Up @@ -13,7 +13,7 @@
// At this stage, we simulate getting an array of the // At this stage, we simulate getting an array of the
// form_structure and hash from our database. This is // form_structure and hash from our database. This is
// how the form data would have been saved using // how the form data would have been saved using
// the $form->store() method. // the $form->get_encoded_form_array() method.
include('fake-form-db-vals.php'); include('fake-form-db-vals.php');


$form = new Formbuilder($fake_db_vals); $form = new Formbuilder($fake_db_vals);
Expand Down
2 changes: 1 addition & 1 deletion example-json.php
Expand Up @@ -5,7 +5,7 @@
// At this stage, we simulate getting an array of the // At this stage, we simulate getting an array of the
// form_structure and hash from our database. This is // form_structure and hash from our database. This is
// how the form data would have been saved using // how the form data would have been saved using
// the $form->store() method. // the $form->get_encoded_form_array() method.
include('fake-form-db-vals.php'); include('fake-form-db-vals.php');


//$form = new Formbuilder($fake_db_vals); //$form = new Formbuilder($fake_db_vals);
Expand Down
18 changes: 10 additions & 8 deletions example-save.php
Expand Up @@ -7,14 +7,14 @@
// pass POST directly, but DO NOT use POST without // pass POST directly, but DO NOT use POST without
// proper security in place. // proper security in place.


// The store() method returns an array that should be // The get_encoded_form_array() method returns an array that should be
// used to store the values in the database. This array // used to store the values in the database. This array
// is also what's passed to the class when rendering // is also what's passed to the class when rendering
// or editing the form. // or editing the form.


//$form_data = isset($_POST['frmb']) ? $_POST : false; $form_data = isset($_POST['frmb']) ? $_POST : false;
//$form = new Formbuilder($form_data); $form = new Formbuilder($form_data);
//$for_db = $form->store(); $for_db = $form->get_encoded_form_array();




//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
Expand All @@ -32,11 +32,13 @@


//------------------------------------------------------------------------------ //------------------------------------------------------------------------------


// Here's the example output of store() // Here's the example output of get_encoded_form_array()


//print_r($for_db); print_r($for_db);
//Array( //Array
// [form_structure] => [{"cssClass":"input_text","required":"undefined","values":"First Name"},{"cssClass":"checkbox","required":"undefined","title":"Favorite programming language?","values":{"2":{"value":"PHP","baseline":"checked"},"3":{"value":"Ruby","baseline":"undefined"},"4":{"value":"Java","baseline":"undefined"}}}] //(
// [form_id] => 1
// [form_structure] => [{"cssClass":"input_text","required":"undefined","values":"First Name"},{"cssClass":"input_text","required":"undefined","values":"Last Name"},{"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"}}}]
//) //)


?> ?>
2 changes: 1 addition & 1 deletion example-submit.php
Expand Up @@ -13,7 +13,7 @@
// At this stage, we simulate getting an array of the // At this stage, we simulate getting an array of the
// form_structure and hash from our database. This is // form_structure and hash from our database. This is
// how the form data would have been saved using // how the form data would have been saved using
// the $form->store() method. // the $form->get_encoded_form_array() method.
include('fake-form-db-vals.php'); include('fake-form-db-vals.php');


$form = new Formbuilder($fake_db_vals); $form = new Formbuilder($fake_db_vals);
Expand Down
4 changes: 1 addition & 3 deletions fake-form-db-vals.php
Expand Up @@ -3,8 +3,6 @@
// This is an arry of fake form values that should be coming from your // This is an arry of fake form values that should be coming from your
// database, or data storage system. This is simply defined here for // database, or data storage system. This is simply defined here for
// example purposes only. // example purposes only.
$fake_db_vals = array( $fake_db_vals = Array( 'form_structure' => '[{"cssClass":"input_text","required":"undefined","values":"First Name"},{"cssClass":"input_text","required":"undefined","values":"Last Name"},{"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"}}}]');
'form_structure'=>'[{"cssClass":"input_text","required":"undefined","values":"First Name"},{"cssClass":"checkbox","required":"undefined","title":"Favorite programming language?","values":{"2":{"value":"PHP","baseline":"checked"},"3":{"value":"Ruby","baseline":"undefined"},"4":{"value":"Java","baseline":"undefined"}}}]');



?> ?>

0 comments on commit 941e6d4

Please sign in to comment.