Skip to content
Browse files

more work on samples cleanup, html rendering

  • Loading branch information...
1 parent 5c45753 commit e6f87a966a0047edd04dc2cf734898a491dfa031 Michael Botsko committed Sep 8, 2009
Showing with 329 additions and 308 deletions.
  1. +294 −290 Formbuilder/Formbuilder.php
  2. +0 −15 example-form.xml
  3. +31 −1 example-save.php
  4. +3 −1 index.html
  5. +1 −1 tests/FormbuilderTest.php
View
584 Formbuilder/Formbuilder.php
@@ -145,13 +145,22 @@ public function retrieve(){
/**
- * Builds an xml file that the jquery plugin will parse for form admin
+ *
+ */
+ public function render_xml(){
+ header("Content-Type: text/xml");
+ print $this->generate_xml();
+ }
+
+
+ /**
+ * Builds an xml structure that the jquery plugin will parse for form admin
* structure. Right now we're just building the xml the old fashioned way
* so that we're not dependant on DOMDocument or something.
*
* @access public
*/
- public function render_xml(){
+ public function generate_xml(){
// begin forming the xml
$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n";
@@ -207,12 +216,8 @@ public function render_xml(){
$xml .= '</form>'."\n";
-// header("Content-Type: text/xml");
-// print $xml;
-
return $xml;
-
}
@@ -325,289 +330,288 @@ private function encode_for_xml($string){
// }
// }
-//
-//public function displaySection($section){
-//
-// if(!empty($section['title']) && $section['show_title']){
-// print "\n" . '<h3>' . htmlentities($section['title'], ENT_QUOTES, 'UTF-8') . '</h3>' . "\n";
-// }
-//
-// if(is_array($section['form'])){
-// if(sha1($section['form']['structure']) == $section['form']['hash']){
-//
-// if($this->validation_errors){
-// print '<div class="frm-warning">' . "\n";
-// print '<ol>' . "\n";
-// print $this->validation_errors;
-// print '</ol>' . "\n";
-// print '</div>' . "\n";
-// }
-//
-// print '<form class="frm-bldr" method="post" action="'.$this->APP->cms_lib->url().'">' . "\n";
-// printf('<input type="hidden" name="form_id" id="form_%s" value="%1$s" />'."\n", $section['form']['id']);
-// printf('<ol id="%s">'."\n", $this->APP->router->encodeForRewriteUrl(strtolower($section['form']['title'])));
-//
-// $form = unserialize($section['form']['structure']);
-// if(is_array($form)){
-// foreach($form as $field){
-//
-// print $this->loadField($field);
-//
-// }
-// }
-//
-// printf('<li class="btn-submit"><input type="submit" name="submit" value="%s" /></li>' . "\n", 'Submit');
-// print '</ol>' . "\n";
-// print '</form>' . "\n";
-//
-// }
-// }
-// }
-//
-///**
-// * @abstract Loads a new field based on its type
-// * @param array $field
-// * @return string
-// * @access private
-// */
-// private function loadField($field){
-//
-// if(is_array($field) && isset($field['class'])){
-//
-// switch($field['class']){
-//
-// case 'input_text':
-// return $this->loadInputText($field);
-// break;
-// case 'textarea':
-// return $this->loadTextarea($field);
-// break;
-// case 'checkbox':
-// return $this->loadCheckboxGroup($field);
-// break;
-// case 'radio':
-// return $this->loadRadioGroup($field);
-// break;
-// case 'select':
-// return $this->loadSelectBox($field);
-// break;
-// }
-// }
-//
-// return false;
-//
-// }
-//
-//
-// /**
-// *
-// * @param <type> $key
-// * @return <type>
-// */
-// private function getPostValue($key){
-// return $this->APP->params->post->getRaw($key);
-// }
-//
-//
-// /**
-// * @abstract Returns html for a textarea
-// * @param array $field Field values from database
-// * @return string
-// * @access private
-// */
-// private function loadTextarea($field){
-//
-// $field['required'] = $field['required'] == 'true' ? ' required' : false;
-//
-// $html = '';
-// $html .= sprintf('<li class="%s%s" id="fld-%s">' . "\n", $this->elemId($field['class']), $field['required'], $this->elemId($field['values']));
-// $html .= sprintf('<label for="%s">%s</label>' . "\n", $this->elemId($field['values']), $field['values']);
-// $html .= sprintf('<textarea id="%s" name="%s" rows="5" cols="50">%s</textarea>' . "\n",
-// $this->elemId($field['values']),
-// $this->elemId($field['values']),
-// $this->getPostValue($this->elemId($field['values'])));
-// $html .= '</li>' . "\n";
-//
-// return $html;
-//
-// }
-//
-//
-// /**
-// * @abstract Returns html for an input type="text"
-// * @param array $field Field values from database
-// * @return string
-// * @access private
-// */
-// private function loadInputText($field){
-//
-// $field['required'] = $field['required'] == 'true' ? ' required' : false;
-//
-// $html = '';
-// $html .= sprintf('<li class="%s%s" id="fld-%s">' . "\n", $this->elemId($field['class']), $field['required'], $this->elemId($field['values']));
-// $html .= sprintf('<label for="%s">%s</label>' . "\n", $this->elemId($field['values']), $field['values']);
-// $html .= sprintf('<input type="text" id="%s" name="%s" value="%s" />' . "\n",
-// $this->elemId($field['values']),
-// $this->elemId($field['values']),
-// $this->getPostValue($this->elemId($field['values'])));
-// $html .= '</li>' . "\n";
-//
-// return $html;
-//
-// }
-//
-//
-// /**
-// * @abstract Returns html for an input type="text"
-// * @param array $field Field values from database
-// * @return string
-// * @access private
-// */
-// private function loadCheckboxGroup($field){
-//
-// $field['required'] = $field['required'] == 'true' ? ' required' : false;
-//
-// $html = '';
-// $html .= sprintf('<li class="%s%s" id="fld-%s">' . "\n", $this->elemId($field['class']), $field['required'], $this->elemId($field['title']));
-//
-// if(isset($field['title']) && !empty($field['title'])){
-// $html .= sprintf('<span class="false_label">%s</span>' . "\n", $field['title']);
-// }
-//
-// if(isset($field['values']) && is_array($field['values'])){
-// $html .= sprintf('<span class="multi-row clearfix">') . "\n";
-// foreach($field['values'] as $item){
-//
-// // set the default checked value
-// $checked = $item['default'] == 'true' ? true : false;
-//
-// // load post value
-// $val = $this->getPostValue($this->elemId($item['value']));
-// $checked = !empty($val);
-//
-// // if checked, set html
-// $checked = $checked ? ' checked="checked"' : '';
-//
-// $checkbox = '<span class="row clearfix"><input type="checkbox" id="%s-%s" name="%s-%s" value="%s"%s /><label for="%s-%s">%s</label></span>' . "\n";
-// $html .= sprintf($checkbox, $this->elemId($field['title']), $this->elemId($item['value']), $this->elemId($field['title']), $this->elemId($item['value']), $item['value'], $checked, $this->elemId($field['title']), $this->elemId($item['value']), $item['value']);
-// }
-// $html .= sprintf('</span>') . "\n";
-// }
-//
-// $html .= '</li>' . "\n";
-//
-// return $html;
-//
-// }
-//
-//
-// /**
-// * @abstract Returns html for an input type="text"
-// * @param array $field Field values from database
-// * @return string
-// * @access private
-// */
-// private function loadRadioGroup($field){
-//
-// $field['required'] = $field['required'] == 'true' ? ' required' : false;
-//
-// $html = '';
-//
-// $html .= sprintf('<li class="%s%s" id="fld-%s">' . "\n", $this->elemId($field['class']), $field['required'], $this->elemId($field['title']));
-//
-// if(isset($field['title']) && !empty($field['title'])){
-// $html .= sprintf('<span class="false_label">%s</span>' . "\n", $field['title']);
-// }
-//
-// if(isset($field['values']) && is_array($field['values'])){
-// $html .= sprintf('<span class="multi-row">') . "\n";
-// foreach($field['values'] as $item){
-//
-// // set the default checked value
-// $checked = $item['default'] == 'true' ? true : false;
-//
-// // load post value
-// $val = $this->getPostValue($this->elemId($field['title']));
-// $checked = !empty($val);
-//
-// // if checked, set html
-// $checked = $checked ? ' checked="checked"' : '';
-//
-// $radio = '<span class="row clearfix"><input type="radio" id="%s-%s" name="%1$s" value="%s"%s /><label for="%1$s-%2$s">%3$s</label></span>' . "\n";
-// $html .= sprintf($radio,
-// $this->elemId($field['title']),
-// $this->elemId($item['value']),
-// $item['value'],
-// $checked);
-// }
-// $html .= sprintf('</span>') . "\n";
-// }
-//
-// $html .= '</li>' . "\n";
-//
-// return $html;
-//
-// }
-//
-//
-// /**
-// * @abstract Returns html for an input type="text"
-// * @param array $field Field values from database
-// * @return string
-// * @access private
-// */
-// private function loadSelectBox($field){
-//
-// $field['required'] = $field['required'] == 'true' ? ' required' : false;
-//
-// $html = '';
-//
-// $html .= sprintf('<li class="%s%s" id="fld-%s">' . "\n", $this->elemId($field['class']), $field['required'], $this->elemId($field['values']));
-//
-// if(isset($field['title']) && !empty($field['title'])){
-// $html .= sprintf('<label for="%s">%s</label>' . "\n", $this->elemId($field['title']), $field['title']);
-// }
-//
-// if(isset($field['values']) && is_array($field['values'])){
-// $multiple = $field['multiple'] == "true" ? ' multiple="multiple"' : '';
-// $html .= sprintf('<select name="%s" id="%s"%s>' . "\n", $this->elemId($field['title']), $this->elemId($field['title']), $multiple);
-//
-// foreach($field['values'] as $item){
-//
-// // set the default checked value
-// $checked = $item['default'] == 'true' ? true : false;
-//
-// // load post value
-// $val = $this->getPostValue($this->elemId($field['title']));
-// $checked = !empty($val);
-//
-// // if checked, set html
-// $checked = $checked ? ' checked="checked"' : '';
-//
-// $option = '<option value="%s"%s>%s</option>' . "\n";
-// $html .= sprintf($option, $item['value'], $checked, $item['value']);
-// }
-//
-// $html .= '</select>' . "\n";
-// $html .= '</li>' . "\n";
-//
-// }
-//
-// return $html;
-//
-// }
-//
-//
-// /**
-// * @abstract Generates an html-safe element id using it's label
-// * @param string $label
-// * @return string
-// * @access private
-// */
-// private function elemId($label){
-// return strtolower( ereg_replace("[^A-Za-z0-9_]", "", str_replace(" ", "_", $label) ) );
-// }
-}
+ /**
+ *
+ */
+ public function render_html(){
+ print $this->generate_html();
+ }
+
+
+ /**
+ *
+ * @param <type> $form_action
+ * @return <type>
+ */
+ public function generate_html($form_action = false){
+
+ $html = '';
+
+ $form_action = $form_action ? $form_action : $_SERVER['PHP_SELF'];
+
+ if(is_array($this->_structure)){
+
+ $html .= '<form class="frm-bldr" method="post" action="'.$form_action.'">' . "\n";
+ $html .= '<ol>'."\n";
-?>
+ foreach($this->_structure as $field){
+ $html .= $this->loadField($field);
+ }
+
+ $html .= '<li class="btn-submit"><input type="submit" name="submit" value="Submit" /></li>' . "\n";
+ $html .= '</ol>' . "\n";
+ $html .= '</form>' . "\n";
+
+ }
+
+ return $html;
+
+ }
+
+
+ /**
+ * @abstract Loads a new field based on its type
+ * @param array $field
+ * @return string
+ * @access private
+ */
+ private function loadField($field){
+
+ if(is_array($field) && isset($field['class'])){
+
+ switch($field['class']){
+
+ case 'input_text':
+ return $this->loadInputText($field);
+ break;
+ case 'textarea':
+ return $this->loadTextarea($field);
+ break;
+ case 'checkbox':
+ return $this->loadCheckboxGroup($field);
+ break;
+ case 'radio':
+ return $this->loadRadioGroup($field);
+ break;
+ case 'select':
+ return $this->loadSelectBox($field);
+ break;
+ }
+ }
+
+ return false;
+
+ }
+
+
+ /**
+ *
+ * @param <type> $key
+ * @return <type>
+ */
+ private function getPostValue($key){
+ return array_key_exists($key, $_POST) ? $_POST[$key] : false;
+ }
+
+
+ /**
+ * @abstract Returns html for a textarea
+ * @param array $field Field values from database
+ * @return string
+ * @access private
+ */
+ private function loadTextarea($field){
+
+ $field['required'] = $field['required'] == 'true' ? ' required' : false;
+
+ $html = '';
+ $html .= sprintf('<li class="%s%s" id="fld-%s">' . "\n", $this->elemId($field['class']), $field['required'], $this->elemId($field['values']));
+ $html .= sprintf('<label for="%s">%s</label>' . "\n", $this->elemId($field['values']), $field['values']);
+ $html .= sprintf('<textarea id="%s" name="%s" rows="5" cols="50">%s</textarea>' . "\n",
+ $this->elemId($field['values']),
+ $this->elemId($field['values']),
+ $this->getPostValue($this->elemId($field['values'])));
+ $html .= '</li>' . "\n";
+
+ return $html;
+
+ }
+
+
+ /**
+ * @abstract Returns html for an input type="text"
+ * @param array $field Field values from database
+ * @return string
+ * @access private
+ */
+ private function loadInputText($field){
+
+ $field['required'] = $field['required'] == 'true' ? ' required' : false;
+
+ $html = '';
+ $html .= sprintf('<li class="%s%s" id="fld-%s">' . "\n", $this->elemId($field['class']), $field['required'], $this->elemId($field['values']));
+ $html .= sprintf('<label for="%s">%s</label>' . "\n", $this->elemId($field['values']), $field['values']);
+ $html .= sprintf('<input type="text" id="%s" name="%s" value="%s" />' . "\n",
+ $this->elemId($field['values']),
+ $this->elemId($field['values']),
+ $this->getPostValue($this->elemId($field['values'])));
+ $html .= '</li>' . "\n";
+
+ return $html;
+
+ }
+
+
+ /**
+ * @abstract Returns html for an input type="text"
+ * @param array $field Field values from database
+ * @return string
+ * @access private
+ */
+ private function loadCheckboxGroup($field){
+
+ $field['required'] = $field['required'] == 'true' ? ' required' : false;
+
+ $html = '';
+ $html .= sprintf('<li class="%s%s" id="fld-%s">' . "\n", $this->elemId($field['class']), $field['required'], $this->elemId($field['title']));
+
+ if(isset($field['title']) && !empty($field['title'])){
+ $html .= sprintf('<span class="false_label">%s</span>' . "\n", $field['title']);
+ }
+
+ if(isset($field['values']) && is_array($field['values'])){
+ $html .= sprintf('<span class="multi-row clearfix">') . "\n";
+ foreach($field['values'] as $item){
+
+ // set the default checked value
+ $checked = $item['default'] == 'true' ? true : false;
+
+ // load post value
+ $val = $this->getPostValue($this->elemId($item['value']));
+ $checked = !empty($val);
+
+ // if checked, set html
+ $checked = $checked ? ' checked="checked"' : '';
+
+ $checkbox = '<span class="row clearfix"><input type="checkbox" id="%s-%s" name="%s-%s" value="%s"%s /><label for="%s-%s">%s</label></span>' . "\n";
+ $html .= sprintf($checkbox, $this->elemId($field['title']), $this->elemId($item['value']), $this->elemId($field['title']), $this->elemId($item['value']), $item['value'], $checked, $this->elemId($field['title']), $this->elemId($item['value']), $item['value']);
+ }
+ $html .= sprintf('</span>') . "\n";
+ }
+
+ $html .= '</li>' . "\n";
+
+ return $html;
+
+ }
+
+
+ /**
+ * @abstract Returns html for an input type="text"
+ * @param array $field Field values from database
+ * @return string
+ * @access private
+ */
+ private function loadRadioGroup($field){
+
+ $field['required'] = $field['required'] == 'true' ? ' required' : false;
+
+ $html = '';
+
+ $html .= sprintf('<li class="%s%s" id="fld-%s">' . "\n", $this->elemId($field['class']), $field['required'], $this->elemId($field['title']));
+
+ if(isset($field['title']) && !empty($field['title'])){
+ $html .= sprintf('<span class="false_label">%s</span>' . "\n", $field['title']);
+ }
+
+ if(isset($field['values']) && is_array($field['values'])){
+ $html .= sprintf('<span class="multi-row">') . "\n";
+ foreach($field['values'] as $item){
+
+ // set the default checked value
+ $checked = $item['default'] == 'true' ? true : false;
+
+ // load post value
+ $val = $this->getPostValue($this->elemId($field['title']));
+ $checked = !empty($val);
+
+ // if checked, set html
+ $checked = $checked ? ' checked="checked"' : '';
+
+ $radio = '<span class="row clearfix"><input type="radio" id="%s-%s" name="%1$s" value="%s"%s /><label for="%1$s-%2$s">%3$s</label></span>' . "\n";
+ $html .= sprintf($radio,
+ $this->elemId($field['title']),
+ $this->elemId($item['value']),
+ $item['value'],
+ $checked);
+ }
+ $html .= sprintf('</span>') . "\n";
+ }
+
+ $html .= '</li>' . "\n";
+
+ return $html;
+
+ }
+
+
+ /**
+ * @abstract Returns html for an input type="text"
+ * @param array $field Field values from database
+ * @return string
+ * @access private
+ */
+ private function loadSelectBox($field){
+
+ $field['required'] = $field['required'] == 'true' ? ' required' : false;
+
+ $html = '';
+
+ $html .= sprintf('<li class="%s%s" id="fld-%s">' . "\n", $this->elemId($field['class']), $field['required'], $this->elemId($field['values']));
+
+ if(isset($field['title']) && !empty($field['title'])){
+ $html .= sprintf('<label for="%s">%s</label>' . "\n", $this->elemId($field['title']), $field['title']);
+ }
+
+ if(isset($field['values']) && is_array($field['values'])){
+ $multiple = $field['multiple'] == "true" ? ' multiple="multiple"' : '';
+ $html .= sprintf('<select name="%s" id="%s"%s>' . "\n", $this->elemId($field['title']), $this->elemId($field['title']), $multiple);
+
+ foreach($field['values'] as $item){
+
+ // set the default checked value
+ $checked = $item['default'] == 'true' ? true : false;
+
+ // load post value
+ $val = $this->getPostValue($this->elemId($field['title']));
+ $checked = !empty($val);
+
+ // if checked, set html
+ $checked = $checked ? ' checked="checked"' : '';
+
+ $option = '<option value="%s"%s>%s</option>' . "\n";
+ $html .= sprintf($option, $item['value'], $checked, $item['value']);
+ }
+
+ $html .= '</select>' . "\n";
+ $html .= '</li>' . "\n";
+
+ }
+
+ return $html;
+
+ }
+
+
+ /**
+ * @abstract Generates an html-safe element id using it's label
+ * @param string $label
+ * @return string
+ * @access private
+ */
+ private function elemId($label){
+ return strtolower( ereg_replace("[^A-Za-z0-9_]", "", str_replace(" ", "_", $label) ) );
+ }
+}
+?>
View
15 example-form.xml
@@ -1,15 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<form>
- <field type="input_text" required="false">Name</field>
- <field type="input_text" required="true">E-mail Address?</field>
- <field type="checkbox" required="false" title="Choose any:">
- <checkbox checked="true">PHP</checkbox>
- <checkbox checked="true">jQuery</checkbox>
- <checkbox checked="true">XML</checkbox>
- <checkbox checked="false">Aspen</checkbox>
- </field>
- <field type="radio" required="true" title="Choose one:">
- <radio checked="true">Yes</radio>
- <radio checked="false">No</radio>
- </field>
-</form>
View
32 example-save.php
@@ -1,5 +1,35 @@
<?php
-print_r(serialize($_POST['ul']));
+require('Formbuilder/Formbuilder.php');
+
+// At this stage, we want to pass in the POST value
+// containing the form. In this example we simply
+// pass POST directly, but DO NOT use POST without
+// proper security in place.
+
+$form_data = isset($_POST['ul']) ? $_POST['ul'] : false;
+
+$form = new Formbuilder($form_data);
+$for_db = $form->store();
+
+// The store() 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.
+
+print_r($for_db);
+
+/*
+ Outputs:
+
+Array
+(
+ [form_structure] => a:4:{i:0;a:3:{s:5:"class";s:10:"input_text";s:8:"required";s:5:"false";s:6:"values";s:4:"Name";}i:1;a:3:{s:5:"class";s:10:"input_text";s:8:"required";s:4:"true";s:6:"values";s:15:"E-mail Address?";}i:2;a:4:{s:5:"class";s:8:"checkbox";s:8:"required";s:5:"false";s:5:"title";s:11:"Choose any:";s:6:"values";a:4:{i:2;a:2:{s:5:"value";s:3:"PHP";s:7:"default";s:4:"true";}i:3;a:2:{s:5:"value";s:6:"jQuery";s:7:"default";s:4:"true";}i:4;a:2:{s:5:"value";s:3:"XML";s:7:"default";s:4:"true";}i:5;a:2:{s:5:"value";s:5:"Aspen";s:7:"default";s:5:"false";}}}i:3;a:4:{s:5:"class";s:5:"radio";s:8:"required";s:4:"true";s:5:"title";s:11:"Choose one:";s:6:"values";a:2:{i:2;a:2:{s:5:"value";s:3:"Yes";s:7:"default";s:4:"true";}i:3;a:2:{s:5:"value";s:2:"No";s:7:"default";s:5:"false";}}}}
+ [form_hash] => bb7d5c1fa425235aa666c1a78b3873e7732709af
+)
+
+
+
+ */
?>
View
4 index.html
@@ -12,7 +12,7 @@
$(document).ready(function(){
$('#form-builder').formbuilder({
'save_url': 'example-save.php',
- 'load_url': 'example-form.xml'
+ 'load_url': 'example-load.php'
});
});
</script>
@@ -53,6 +53,8 @@
<p>This entire form builder is generated for us &ndash; all we needed to do give it the id of UL and a location for where to load/save the data. Just check out the source!</p>
<p>For the nice scrolling, formbuilder uses the jQuery.scrollTo plugin. The demo css we're using here is not compatible with IE.</p>
+
+ <p><a href="example-html.php">View sample rendered html</a>.</p>
<div id="form-wrap">
<ul id="form-builder">
View
2 tests/FormbuilderTest.php
@@ -135,7 +135,7 @@ public function test_Retrieve() {
public function test_RenderXml() {
$form = new Formbuilder($this->_container);
- $this->assertEquals($this->_form_xml, $form->render_xml());
+ $this->assertEquals($this->_form_xml, $form->generate_xml());
}
}

0 comments on commit e6f87a9

Please sign in to comment.
Something went wrong with that request. Please try again.