Permalink
Browse files

always hated the retreive/store encoding/decoding complexity, so I du…

…mped it
  • Loading branch information...
1 parent b992b4d commit 941e6d4b217807a302c441516e9a15fc447185cb @viveleroi committed Dec 15, 2011
Showing with 34 additions and 101 deletions.
  1. +16 −85 Formbuilder/Formbuilder.php
  2. +1 −1 Formbuilder/Formbuilder_pdo.php
  3. +2 −0 README
  4. +1 −1 docs/DOCUMENTATION
  5. +1 −1 example-html.php
  6. +1 −1 example-json.php
  7. +10 −8 example-save.php
  8. +1 −1 example-submit.php
  9. +1 −3 fake-form-db-vals.php
View
@@ -28,28 +28,10 @@
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
*/
- protected $_container;
-
- /**
- * 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;
+ protected $_form_array;
/**
@@ -64,89 +46,38 @@ public function __construct($form = false){
// Set the serialized structure if it's provided
// otherwise, store the source
if(array_key_exists('form_structure', $form)){
-
- $this->_container = $form; // set the form as the container
- $this->_structure_ser = $form['form_structure']; // pull the encoded form
- $this->_structure = $this->retrieve(); // unserialize the form as the raw structure
-
+ $form['form_structure'] = json_decode($form['form_structure']);
+ $this->_form_array = $form;
}
else if(array_key_exists('frmb', $form)){
-
- $this->_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
- $this->_structure_ser = $this->store(); // encode it
- $this->rebuild_container(); // rebuild a new container
-
+ $_form = array();
+ $_form['form_id'] = ($form['form_id'] == "undefined" ? false : $form['form_id']);
+ $_form['form_structure'] = $form['frmb']; // since the form is from POST, set it as the raw array
+ $this->_form_array = $_form;
}
return true;
}
/**
- * Wipes and re-saves the structure and hash to the containing array.
- *
- * @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.
+ * Returns the form array with the structure encoded, for saving to a database or other store
*
* @access public
* @return array
*/
- public function store(){
- $this->_structure_ser = json_encode($this->_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;
+ public function get_encoded_form_array(){
+ return array('form_id'=>$this->_form_array['form_id'],'form_structure'=>json_encode($this->_form_array['form_structure']));
}
/**
* Prints out the generated json file with a content-type of application/json
*
* @access public
- * @uses generate_json
*/
public function render_json(){
header("Content-Type: application/json");
- print $this->_structure_ser;
- }
-
-
- /**
- * Builds a json object that the jquery plugin will parse
- *
- * @access public
- */
- public function generate_json(){
- return json_encode( $this->_structure );
+ print json_encode( $this->_form_array );
}
@@ -175,12 +106,12 @@ public function generate_html($form_action = false){
$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 .= '<ol>'."\n";
- foreach($this->_structure as $field){
+ foreach($this->_form_array['form_structure'] as $field){
$html .= $this->loadField($field);
}
@@ -208,8 +139,8 @@ public function process(){
$results = array();
// Put together an array of all expected indices
- if(is_array($this->_structure)){
- foreach($this->_structure as $field){
+ if(is_array($this->_form_array['form_structure'])){
+ foreach($this->_form_array['form_structure'] as $field){
$field['required'] = $field['required'] == 'checked' ? ' required' : false;
@@ -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.
*/
public function save_form(){
- $for_db = parent::store();
+ $for_db = parent::get_encoded_form_array();
if($for_db['form_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);
View
2 README
@@ -23,6 +23,8 @@ Version 0.4 - 20111215
- Removing old XML-based form loading system, moving to pure JSON
- 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)
+- 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
- Corrected issue with reserved words breaking support for Safari, Opera, IE
View
@@ -60,7 +60,7 @@ To save the POST request from the jquery object, just invoke the following:
$form = new Formbuilder($form_data);
// 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
serialized form value. Save these two values to your database. They both need
View
@@ -13,7 +13,7 @@
// At this stage, we simulate getting an array of the
// form_structure and hash from our database. This is
// 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');
$form = new Formbuilder($fake_db_vals);
View
@@ -5,7 +5,7 @@
// At this stage, we simulate getting an array of the
// form_structure and hash from our database. This is
// 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');
//$form = new Formbuilder($fake_db_vals);
View
@@ -7,14 +7,14 @@
// pass POST directly, but DO NOT use POST without
// 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
// is also what's passed to the class when rendering
// or editing the form.
-//$form_data = isset($_POST['frmb']) ? $_POST : false;
-//$form = new Formbuilder($form_data);
-//$for_db = $form->store();
+$form_data = isset($_POST['frmb']) ? $_POST : false;
+$form = new Formbuilder($form_data);
+$for_db = $form->get_encoded_form_array();
//------------------------------------------------------------------------------
@@ -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);
-//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"}}}]
+print_r($for_db);
+//Array
+//(
+// [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"}}}]
//)
?>
View
@@ -13,7 +13,7 @@
// At this stage, we simulate getting an array of the
// form_structure and hash from our database. This is
// 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');
$form = new Formbuilder($fake_db_vals);
View
@@ -3,8 +3,6 @@
// 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
// example purposes only.
-$fake_db_vals = 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"}}}]');
-
+$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"}}}]');
?>

0 comments on commit 941e6d4

Please sign in to comment.