JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform working. added some encodings and formatters.
authorJason Woofenden <jason@vorticies.(none)>
Sun, 10 Dec 2006 13:32:51 +0000 (08:32 -0500)
committerJason Woofenden <jason@vorticies.(none)>
Sun, 10 Dec 2006 13:32:51 +0000 (08:32 -0500)
encode.php
format.php
metaform.php [new file with mode: 0644]
metaform/main.html [new file with mode: 0644]
metaform/template.email.txt [new file with mode: 0644]
metaform/template.html [new file with mode: 0644]
metaform/template.php [new file with mode: 0644]
metaform/template.sql [new file with mode: 0644]

index 90ec6c8..c3c71a7 100644 (file)
@@ -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 '';
+       }
+}
+       
 
index 411a09a..b9270bd 100644 (file)
 
 # 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 (file)
index 0000000..c20515e
--- /dev/null
@@ -0,0 +1,163 @@
+<?php
+
+require_once('code/wfpl/template.php');
+
+# see code/wfpl/metaform/template.html for the html templates for these elements
+$GLOBALS['types'] = array(
+#    type                  input          format        sql     
+       'name' =>       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 (file)
index 0000000..64c09d4
--- /dev/null
@@ -0,0 +1,24 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+  <title>Meta Form</title>
+</head>
+
+<body>
+  <form action="metaform.php" method="get">
+    <p>Form name: <input type="text" name="form_name" value="~form_name~" /></p>
+
+    <p>Fields: 
+    <textarea name="fields">~fields.html~</textarea></p>
+
+    <p><input type="submit" name="download_sql" value="Download SQL" /></p>
+
+    <p><input type="submit" name="download_php" value="Download PHP" /></p>
+
+    <p><input type="submit" name="download_template" value="Download template" /></p>
+
+    <p><input type="submit" name="download_email" value="Download e-mail" /></p>
+  </form>
+</body>
+</html>
diff --git a/metaform/template.email.txt b/metaform/template.email.txt
new file mode 100644 (file)
index 0000000..6e9ecc6
--- /dev/null
@@ -0,0 +1,3 @@
+~form_name~ form submitted with the following:
+<!--~fields start~-->
+~caption~: ~~~name~~~<!--~end~-->
diff --git a/metaform/template.html b/metaform/template.html
new file mode 100644 (file)
index 0000000..ca0d271
--- /dev/null
@@ -0,0 +1,20 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+  <title>~form_name~ entry</title>
+</head>
+
+<body>
+  <h2>~form_name~ entry form</h2>
+
+  <form action="~form_name~.php" method="post">
+    <table cellspacing="0" cellpadding="4" border="0" summary="">
+<!--~textbox start~-->      <tr><td class="caption">~caption.html~: </td><td><input type="text" name="~name~" value="~~~name~.attr~~" /></td></tr>
+<!--~end~--><!--~textarea start~-->      <tr><td class="caption">~caption.html~: </td><td><textarea name="~name~">~~~name~.html~~</textarea></td></tr>
+<!--~end~--><!--~checkbox start~-->      <tr><td class="caption">~caption.html~: </td><td><input type="checkbox" name="~name~~~~name~.checked~~" /></td></tr>
+<!--~end~--><!--~submit start~-->      <tr><td class="submit_row" colspan="2"><input type="submit" name="~name~" value="~caption.attr~" /></td></tr>
+<!--~end~-->    </table>
+  </form>
+</body>
+</html>
diff --git a/metaform/template.php b/metaform/template.php
new file mode 100644 (file)
index 0000000..a3266de
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+require_once('code/wfpl/template.php');
+require_once('code/wfpl/format.php');
+#require_once('code/wfpl/db.php'); # fixme
+
+function ~form_name~_get_fields() {
+       $GLOBALS['~form_name~_fields'] = array();<!--~formats start~-->
+
+       $value = format_~format~($_REQUEST['~name~']);
+       tem_set('~name~', $value);<!--~db_field start~-->
+       $GLOBALS['~form_name~_fields'][] = $value;<!--~end~--><!--~end~-->
+}
+       
+
+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 (file)
index 0000000..eee20b3
--- /dev/null
@@ -0,0 +1,5 @@
+drop table if exists ~form_name~;
+create table ~form_name~ (
+    id int unique auto_increment<!--~column start~-->,
+    ~name~ ~type~ not null default ~default~<!--~end~-->
+);