From 3698f739c8155069451c61085ab925250c592e42 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Sun, 10 Dec 2006 08:32:51 -0500 Subject: [PATCH] metaform working. added some encodings and formatters. --- encode.php | 9 +++ format.php | 22 ++++++ metaform.php | 163 +++++++++++++++++++++++++++++++++++++++++++ metaform/main.html | 24 +++++++ metaform/template.email.txt | 3 + metaform/template.html | 20 ++++++ metaform/template.php | 30 ++++++++ metaform/template.sql | 5 ++ 8 files changed, 276 insertions(+) create mode 100644 metaform.php create mode 100644 metaform/main.html create mode 100644 metaform/template.email.txt create mode 100644 metaform/template.html create mode 100644 metaform/template.php create mode 100644 metaform/template.sql diff --git a/encode.php b/encode.php index 90ec6c8..c3c71a7 100644 --- a/encode.php +++ b/encode.php @@ -37,4 +37,13 @@ function enc_attr($str) { return $str; } +# this is a stupid hack to work around html's stupid syntax for checkboxes +function enc_checked($str) { + if($str == 'Yes') { + return '" checked="checked'; + } else { + return ''; + } +} + diff --git a/format.php b/format.php index 411a09a..b9270bd 100644 --- a/format.php +++ b/format.php @@ -22,6 +22,28 @@ # This file contains basic encodings +function format_oneline($str) { + $str = str_replace("\r", '', $str); + return str_replace("\n", '', $str); +} + +function format_unix($str) { + return unix_newlines($str); +} + +function format_yesno($str) { + if($str) { + return "Yes"; + } else { + return "No"; + } +} + +function format_email($str) { + # FIXME + return format_oneline($str); +} + function format_money($str, $display_cents = true) { $str = ereg_replace('[^0-9.]', '', $str); if($display_cents) { diff --git a/metaform.php b/metaform.php new file mode 100644 index 0000000..c20515e --- /dev/null +++ b/metaform.php @@ -0,0 +1,163 @@ + array('textbox', 'oneline', 'varchar(200)'), + 'textbox' => array('textbox', 'oneline', 'varchar(200)'), + 'email' => array('textbox', 'email', 'varchar(100)'), + 'phone' => array('textbox', 'phone', 'varchar(32)'), + 'money' => array('textbox', 'money', 'varchar(32)'), + 'dollars' => array('textbox', 'dollars', 'varchar(32)'), + 'url' => array('textbox', 'url', 'varchar(200)'), + 'textarea' => array('textarea', 'unix', 'text'), + 'pulldown' => array('pulldown', 'options', 'int'), + 'checkbox' => array('checkbox', 'yesno', 'int'), + 'yesno' => array('checkbox', 'yesno', 'int'), + 'submit' => array('submit', 'oneline', 'n/a') +); + +if(isset($_REQUEST['form_name'])) { + $GLOBALS['form_name'] = $_REQUEST['form_name']; +} else { + $GLOBALS['form_name'] = 'some_form'; +} + +if(isset($_REQUEST['fields'])) { + if(isset($_REQUEST['download_sql'])) { + download_sql(); + exit(); + } elseif(isset($_REQUEST['download_php'])) { + download_php(); + exit(); + } elseif(isset($_REQUEST['download_template'])) { + download_template(); + exit(); + } elseif(isset($_REQUEST['download_email'])) { + download_email(); + exit(); + } else { + tem_set('message', "Sorry... couldn't tell which button you pressed"); + # fall through + } +} else { + tem_output('code/wfpl/metaform/main.html'); +} + +function field_input($type) { return $GLOBALS['types'][$type][0]; } +function field_format($type) { return $GLOBALS['types'][$type][1]; } +function field_sql($type) { return $GLOBALS['types'][$type][2]; } + +function get_fields() { + $fields_str = unix_newlines($_REQUEST['fields']); + $ret = array(); + $fields_str = rtrim($fields_str); + $fields = split("\n", $fields_str); + foreach($fields as $field) { + list($name, $type, $options) = split(' *', $field); + if($options) $options = split(',', $options); + if(!$type) $type = $name; + $input = field_input($type); + $format = field_format($type); + $sql = field_sql($type); + $ret[] = array($name, $type, $input, $format, $sql, $options); + } + return $ret; +} + +function download_headers() { + header('Content-type: application/octet-stream'); + header('Content-disposition: download'); # is this correct? does it do anything? +} + + + + +function download_sql() { + tem_load('code/wfpl/metaform/template.sql'); + tem_set('form_name', $GLOBALS['form_name']); + $fields = get_fields(); + foreach($fields as $field) { + list($name, $type, $input, $format, $sql) = $field; + if($sql != 'n/a') { + tem_set('name', $name); + tem_set('type', $sql); + if($sql == 'int') { + tem_set('default', '0'); + } else { + tem_set('default', '""'); + } + tem_sub('column'); + } + } + download_headers(); + tem_output(); +} + + +function download_template() { + tem_load('code/wfpl/metaform/template.html'); + tem_set('form_name', $GLOBALS['form_name']); + $fields = get_fields(); + foreach($fields as $field) { + list($name, $type, $input, $format, $sql) = $field; + tem_set('name', $name); + tem_set('caption', $name); # fixme + tem_sub($input); + } + tem_set('name', 'save'); + tem_set('caption', 'Save'); + tem_sub('submit'); + download_headers(); + tem_output(); +} + + +function download_php() { + tem_load('code/wfpl/metaform/template.php'); + tem_set('form_name', $GLOBALS['form_name']); + $fields = get_fields(); + $db_fields = ''; + $always_field = false; + foreach($fields as $field) { + list($name, $type, $input, $format, $sql) = $field; + if($input != 'submit') { + tem_set('format', $format); + tem_set('name', $name); + tem_set('db_field', ''); # we don't want to use the value from last time + if($sql != 'n/a') { + tem_sub('db_field'); + if($db_fields != '') $db_fields .= ','; + $db_fields .= $name; + } + tem_sub('formats'); + if(!$always_field and $input != 'checkbox' and $input != 'radio') { + $always_field = $name; + } + } + } + # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not. + tem_set('always_field', $always_field); + tem_set('db_fields', $db_fields); + download_headers(); + tem_output(); +} + + +function download_email() { + tem_load('code/wfpl/metaform/template.email.txt'); + tem_set('form_name', $GLOBALS['form_name']); + $fields = get_fields(); + foreach($fields as $field) { + list($name, $type, $input, $format, $sql) = $field; + tem_set('name', $name); + tem_set('caption', $name); # fixme + tem_sub('fields'); + } + download_headers(); + tem_output(); +} + +?> diff --git a/metaform/main.html b/metaform/main.html new file mode 100644 index 0000000..64c09d4 --- /dev/null +++ b/metaform/main.html @@ -0,0 +1,24 @@ + + + + + Meta Form + + + +
+

Form name:

+ +

Fields: +

+ +

+ +

+ +

+ +

+
+ + diff --git a/metaform/template.email.txt b/metaform/template.email.txt new file mode 100644 index 0000000..6e9ecc6 --- /dev/null +++ b/metaform/template.email.txt @@ -0,0 +1,3 @@ +~form_name~ form submitted with the following: + +~caption~: ~~~name~~~ diff --git a/metaform/template.html b/metaform/template.html new file mode 100644 index 0000000..ca0d271 --- /dev/null +++ b/metaform/template.html @@ -0,0 +1,20 @@ + + + + + ~form_name~ entry + + + +

~form_name~ entry form

+ +
+ + + + + +
~caption.html~:
~caption.html~:
~caption.html~:
+
+ + diff --git a/metaform/template.php b/metaform/template.php new file mode 100644 index 0000000..a3266de --- /dev/null +++ b/metaform/template.php @@ -0,0 +1,30 @@ + + + $value = format_~format~($_REQUEST['~name~']); + tem_set('~name~', $value); + $GLOBALS['~form_name~_fields'][] = $value; +} + + +if(isset($_REQUEST['~always_field~'])) { + ~form_name~_get_fields(); + + if("you're happy with the values") { + #db_insert('~form_name~', '~db_fields~', $GLOBALS['~form_name~_fields']); # fixme + header('Content-type: text/plain'); + print "e-mailing this: \n\n"; + tem_output('~form_name~.email.txt'); + exit(); + } +} + +tem_output('~form_name~.html'); + +?> diff --git a/metaform/template.sql b/metaform/template.sql new file mode 100644 index 0000000..eee20b3 --- /dev/null +++ b/metaform/template.sql @@ -0,0 +1,5 @@ +drop table if exists ~form_name~; +create table ~form_name~ ( + id int unique auto_increment, + ~name~ ~type~ not null default ~default~ +); -- 1.7.10.4