<?php
/**
* @package jquery.Formbuilder
* @author Michael Botsko
* @copyright 2009 Trellis Development, LLC
*
* This PHP object is the server-side component of the jquery formbuilder
* plugin. The Formbuilder allows you to provide users with a way of
* creating a formand saving that structure to the database.
*
* Using this class you can easily prepare the structure for storage,
* rendering the xml file needed for the builder, or render the html of the form.
*
* This package is licensed using the Mozilla Public License 1.1
*
* We encourage comments and suggestion to be sent to mbotsko@trellisdev.com.
* Please feel free to file issues at http://github.com/botskonet/jquery.formbuilder/issues
* Please feel free to fork the project and provide patches back.
*/
// Uncomment these for debug
//error_reporting(E_ALL);
//ini_set('display_errors', true);
/**
* @abstract This class is the server-side component that handles interaction with
* the jquery formbuilder plugin.
* @package jquery.Formbuilder
*/
class Formbuilder {
/**
* @var array Contains the form_hash and serialized form_structure from an external source (db)
* @access protected
*/
protected $_container;
/**
* @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;
/**
* @var array Holds the hash of the serialized form
* @access protected
*/
protected $_hash;
/**
* Constructor, loads either a pre-serialized form structure or an incoming POST form
* @param array $containing_form_array
* @access public
*/
public function __construct($form = false){
$form = is_array($form) ? $form : array();
// 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 serialized form
$this->_hash = $this->hash(); // hash the current structure
$this->_structure = $this->retrieve(); // unserialize the form as the raw structure
} else {
$this->_structure = $form; // since the form is from POST, set it as the raw array
$this->_structure_ser = $this->store(); // serialize it
$this->rebuild_container(); // rebuild a new container
}
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_hash'] = $this->_hash;
$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
* @return array
*/
public function store(){
$this->_structure_ser = serialize($this->_structure);
$this->_hash = $this->hash($this->_structure_ser);
return array('form_structure'=>$this->_structure_ser,'form_hash'=>$this->_hash);
}
/**
* Creates a hash that's used to check the contents
* have not changed from what was saved.
*
* @access public
* @return string
*/
public function hash(){
return sha1($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_hash', $this->_container)){
if($this->_container['form_hash'] == $this->hash($this->_container['form_structure'])){
return unserialize($this->_container['form_structure']);
}
}
return false;
}
/**
* Prints out the generated xml file with a content-type of text/xml
*
* @access public
* @uses generate_xml
*/
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 generate_xml(){
// begin forming the xml
$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n";
$xml .= '<form>'."\n";
if(is_array($this->_structure)){
foreach($this->_structure as $field){
// input type="text"
if($field['class'] == "input_text"){
$xml .= sprintf('<field type="input_text" required="%s">%s</field>'."\n", $field['required'], $this->encode_for_xml($field['values']));
}
// textarea
if($field['class'] == "textarea"){
$xml .= sprintf('<field type="textarea" required="%s">%s</field>'."\n", $field['required'], $this->encode_for_xml($field['values']));
}
// input type="checkbox"
if($field['class'] == "checkbox"){
$xml .= sprintf('<field type="checkbox" required="%s" title="%s">'."\n", $field['required'], (isset($field['title']) ? $this->encode_for_xml($field['title']) : ''));
if(is_array($field['values'])){
foreach($field['values'] as $input){
$xml .= sprintf('<checkbox checked="%s">%s</checkbox>'."\n", $input['default'], $this->encode_for_xml($input['value']));
}
}
$xml .= '</field>'."\n";
}
// input type="radio"
if($field['class'] == "radio"){
$xml .= sprintf('<field type="radio" required="%s" title="%s">'."\n", $field['required'], (isset($field['title']) ? $this->encode_for_xml($field['title']) : ''));
if(is_array($field['values'])){
foreach($field['values'] as $input){
$xml .= sprintf('<radio checked="%s">%s</radio>'."\n", $input['default'], $this->encode_for_xml($input['value']));
}
}
$xml .= '</field>'."\n";
}
// select
if($field['class'] == "select"){
$xml .= sprintf('<field type="select" required="%s" multiple="%s" title="%s">'."\n", $field['required'], $field['multiple'], (isset($field['title']) ? $this->encode_for_xml($field['title']) : ''));
if(is_array($field['values'])){
foreach($field['values'] as $input){
$xml .= sprintf('<option checked="%s">%s</option>'."\n", $input['default'], $this->encode_for_xml($input['value']));
}
}
$xml .= '</field>'."\n";
}
}
}
$xml .= '</form>'."\n";
return $xml;
}
/**
* @abstract Encodes strings for xml.
* @param string $string
* @access private
* @return string
*/
protected function encode_for_xml($string){
$string = html_entity_decode($string, ENT_NOQUOTES, 'UTF-8');
$string = htmlentities($string, ENT_NOQUOTES, 'UTF-8');
// manually add back in html
$string = str_replace("<", "<", $string);
$string = str_replace(">", ">", $string);
return $string;
}
/**
* Renders the generated html of the form.
*
* @param string $form_action Action attribute of the form element.
* @access public
* @uses generate_html
*/
public function render_html($form_action = false){
print $this->generate_html($form_action);
}
/**
* Generates the form structure in html.
*
* @param string $form_action Action attribute of the form element.
* @return string
* @access public
*/
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=
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
botskonet-jquery.formbuilder-e9f8910.zip (17个子文件)
botskonet-jquery.formbuilder-e9f8910
.gitignore 46B
example-load.php 334B
Formbuilder
Formbuilder.php 17KB
example-save.php 2KB
tests
testsSuite.php 626B
FormbuilderTest.php 6KB
docs
DOCUMENTATION 4KB
LICENSE 25KB
example-html.php 707B
fake-form-db-vals.php 1KB
README 1KB
js
jquery-ui-1.8.2.custom.min.js 35KB
jquery.formbuilder.js 15KB
jquery.scrollTo-min.js 2KB
jquery-1.3.2.min.js 56KB
index.html 4KB
example-submit.php 750B
共 17 条
- 1
资源评论
- zhangyuhao_9202013-07-18很好 用了下 感觉不错!
- ytliulin2014-12-12可以用 不错
- skdi2013-06-13很好 用了下 感觉不错!
xiongmaoguan
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功