diff --git a/Formbuilder/Formbuilder.php b/Formbuilder/Formbuilder.php index c7202b0..c9634e2 100644 --- a/Formbuilder/Formbuilder.php +++ b/Formbuilder/Formbuilder.php @@ -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,67 +46,27 @@ 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'])); } @@ -132,21 +74,10 @@ public function retrieve(){ * 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 .= '