JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: added pulldown support, added leftcheck
authorJason Woofenden <jason183@herkamire.com>
Fri, 15 Jun 2007 12:00:34 +0000 (08:00 -0400)
committerJason Woofenden <jason183@herkamire.com>
Fri, 15 Jun 2007 12:00:34 +0000 (08:00 -0400)
metaform now ships a seperate stylesheet.
metaform captions are cleaned up (underscores to spaces and capitalization)
metaform's generated php has less duplication
added enc_upper()
enc_whatever functions can recieve the field name as the second parameter
format_options() forces the value to be something in the options list
metaform's auto-generated submit button says "Send" if it's e-mail only

encode.php
format.php
metaform.php
metaform/htaccess [new file with mode: 0644]
metaform/preview.html
metaform/style.css [new file with mode: 0644]
metaform/template.htaccess [deleted file]
metaform/template.html
metaform/template.php
template.php

index a4c4587..38b09bd 100644 (file)
@@ -89,20 +89,119 @@ function enc_tab($str) {
        return substr($out, 0, -1);
 }
 
+function enc_upper($str) {
+       return strtoupper($str);
+}
+
 
 # display <option>s
 function enc_states($str) {
        $states_assoc = array("AL" => "Alabama", "AK" => "Alaska", "AZ" => "Arizona", "AR" => "Arkansas", "CA" => "California", "CO" => "Colorado", "CT" => "Connecticut", "DE" => "Delaware", "FL" => "Florida", "GA" => "Georgia", "HI" => "Hawaii", "ID" => "Idaho", "IL" => "Illinois", "IN" => "Indiana", "IA" => "Iowa", "KS" => "Kansas", "KY" => "Kentucky", "LA" => "Louisiana", "ME" => "Maine", "MD" => "Maryland", "MA" => "Massachusetts", "MI" => "Michigan", "MN" => "Minnesota", "MS" => "Mississippi", "MO" => "Missouri", "MT" => "Montana", "NE" => "Nebraska", "NV" => "Nevada", "NH" => "New Hampshire", "NJ" => "New Jersey", "NM" => "New Mexico", "NY" => "New York", "NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "PA" => "Pennsylvania", "RI" => "Rhode Island", "SC" => "South Carolina", "SD" => "South Dakota", "TN" => "Tennessee", "TX" => "Texas", "UT" => "Utah", "VT" => "Vermont", "VA" => "Virginia", "WA" => "Washington", "DC" => "Washington, DC", "WV" => "West Virginia", "WI" => "Wisconsin", "WY" => "Wyoming");
        $ret = '';
 
-       foreach($states_assoc as $code => $name) {
-               $ret .= "<option name=\"$code\"";
-               if($code == $str) {
-                       $ret .= " selected";
+       return encode_options($str, $states_assoc, $use_keys = true);
+}
+
+
+define('PULLDOWN_ARRAY', 0); define('PULLDOWN_HASH', 1); define('PULLDOWN_2D', 2);
+
+function pulldown_options_to_hash($options, $keys_from) {
+       # convert other types of input to value=>display hash
+       switch($keys_from) {
+               case PULLDOWN_HASH:
+                       return $options;
+               case PULLDOWN_ARRAY:
+                       $new_options = array();
+                       foreach($options as $opt) {
+                               $new_options[$opt] = $opt;
+                       }
+                       return $new_options;
+               break;
+               case PULLDOWN_2D:
+                       $new_options = array();
+                       foreach($options as $opt) {
+                               $new_options[$opt[0]] = $opt[1];
+                       }
+                       return $new_options;
+               break;
+               default:
+                       die('unknown value: "' . print_r($keys_from) . '" passed in $keys_from parameter');
+       }
+}
+
+
+# call this function before you run() the template so enc_options() knows what
+# to do
+#
+# Parameters:
+#
+#   name: the name of the html control
+#
+#   options: an array of options to display in the pulldown/selectbox
+#
+#   keys_from: Set to one of:
+#        PULLDOWN_ARRAY: (default) values of $options are displayd and posted
+#        PULLDOWN_HASH: values of $options are display, keys are posted
+#        PULLDOWN_2D: $options is a 2 dimensional array.
+#                     $options[0][1] is displayed, $options[0][0] is posted.
+#                     $options[1][1] is displayed, $options[1][0] is posted.
+#
+#   multiple: UNTESTED set to true for multiple-select boxes. 
+
+function pulldown($name, $options, $keys_from = PULLDOWN_ARRAY, $multiple = false) {
+       $options = pulldown_options_to_hash($options, $keys_from);
+       $GLOBALS[$name . '_options'] = array();
+       $GLOBALS[$name . '_options']['options'] = $options;
+       $GLOBALS[$name . '_options']['multiple'] = $multiple;
+}
+
+# output a bunch of <option> tags
+function enc_options($values, $name) {
+       if(!isset($GLOBALS[$name . '_options'])) {
+               die('pulldown() must be called before this template can be run. See code/wfpl/encode.php');
+       }
+       if($GLOBALS[$name . '_options']['multiple']) { # FIXME test this
+               $values = explode(', ', $values);
+       }
+       return encode_options($values, $GLOBALS[$name . '_options']['options'], PULLDOWN_HASH);
+}
+
+# use this function along with a special template to generate the html for pulldowns and multiple select boxes.
+#
+# Parameters:
+#
+#    selected: can be a string or (for multiple-selects) an array
+#
+#    options, keys_from: see documentation for pulldown() above
+function encode_options($selected, $options, $keys_from) {
+       if(!is_array($selected)) {
+               $selected = array($selected);
+       }
+
+       $options = pulldown_options_to_hash($options, $keys_from);
+
+       $out = '';
+       foreach($options as $value => $display) {
+               $out .= '<option';
+
+               if(in_array($value, $selected, $strict = true)) {
+                       $out .= ' selected="selected"';
+               }
+
+               if($value !== $display) {
+                       $out .= ' value="';
+                       $out .= enc_attr($value);
+                       $out .= '"';
                }
-               $ret .= ">$name</option>\n";
+                       
+               $out .= '>';
+
+               $out .= enc_html($display);
+
+               $out .= "</option>\n";
        }
-       return $ret;
+
+       return $out;
 }
 
 ?>
index 8979b26..e158bb3 100644 (file)
 # This file contains basic encodings
 
 function format_caption($str) {
+       $str = str_replace('_', ' ', $str);
        $str = ucwords($str);
        return str_replace('Email', 'E-mail', $str);
 }
 
+# This function makes sure that $str is in the list of options, and returns "" otherwise
+function format_options($str, $name) {
+       if(!isset($GLOBALS[$name . '_options'])) {
+               die("Couldn't find options for \"$name\". Be sure to call pulldown().");
+       }
+
+       if(!in_array($str, array_keys($GLOBALS[$name . '_options']['options']), $strict = true)) {
+               return '';
+       }
+
+       return $str;
+}
+
 function format_int($str) {
        $str = ereg_replace('[^0-9]', '', $str);
        return ereg_replace('^0*([1-9])', '\1', $str);
index c8bb395..a473bbc 100644 (file)
@@ -43,8 +43,9 @@ $GLOBALS['types'] = array(
        'password' =>   array('password',    'oneline',    'varchar(200)'),
        'textarea' =>   array('textarea',    'unix',       'text'),
        'html' =>       array('html',        'unix',       'text'),
-       'pulldown' =>   array('pulldown',    'options',    'int'),
+       'pulldown' =>   array('pulldown',    'options',    'varchar(100)'),
        'radio' =>      array('radio',       'oneline',    'varchar(200)'),
+       'leftcheck' =>  array('leftcheck',   'yesno',      'varchar(3)'),
        'checkbox' =>   array('checkbox',    'yesno',      'varchar(3)'),
        'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
        'delete' =>     array('checkbox',    'yesno',      'n/a'),
@@ -117,8 +118,13 @@ function field_format($type) { return $GLOBALS['types'][$type][1]; }
 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
 
 function get_fields() {
+       # no sense in doing all this so many times
+       if(isset($GLOBALS['gotten_fields'])) {
+               return $GLOBALS['gotten_fields'];
+       }
+
        $fields_str = unix_newlines($_REQUEST['fields']);
-       $ret = array();
+       $GLOBALS['gotten_fields'] = array();
        $fields_str = rtrim($fields_str);
        $fields = split("\n", $fields_str);
        foreach($fields as $field) {
@@ -128,9 +134,9 @@ function get_fields() {
                $input = field_input($type);
                $format = field_format($type);
                $sql = field_sql($type);
-               $ret[] = array($name, $type, $input, $format, $sql, $options);
+               $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
        }
-       return $ret;
+       return $GLOBALS['gotten_fields'];
 }
 
 # this one, that you're using to create forms
@@ -209,10 +215,17 @@ function make_html($whole_file = true) {
        } else {
                $tem->sub('opt_db_1_else');
        }
-       $tem->set('name', 'save');
-       $tem->set('caption', 'Save');
+
+       if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
+               $tem->set('name', 'send');
+               $tem->set('caption', 'Send');
+       } else {
+               $tem->set('name', 'save');
+               $tem->set('caption', 'Save');
+       }
        $tem->sub('submit');
        $tem->sub('row');
+
        $tem->sub('form');
 
        if($has_html_editors) {
@@ -263,6 +276,10 @@ function make_php() {
                                        $image_included_yet = true;
                                }
                        } else {
+                               if($input == 'pulldown') {
+                                       $tem->sub('pulldowns');
+                                       $tem->sub('pulldown_format_extra');
+                               }
                                $tem->sub('formats');
                        }
                        $tem->sub('tem_sets');
@@ -331,7 +348,7 @@ function make_email() {
 function make_htaccess() {
        $tem = new tem();
        $tem->set('form', $GLOBALS['form_name']);
-       return $tem->run('code/wfpl/metaform/template.htaccess');
+       return $tem->run('code/wfpl/metaform/htaccess');
 }
 
 function view_email() {
@@ -349,6 +366,13 @@ function preview() {
        if($GLOBALS['opt_db'] == 'Yes') {
                $preview_tem->sub('new_msg');
        }
+       $fields = get_fields();
+       foreach($fields as $field) {
+               list($name, $type, $input, $format, $sql) = $field;
+               if($type == 'pulldown') {
+                       pulldown($name, array('option 1', 'option 2', 'option 3'));
+               }
+       }
        $preview = $preview_tem->run();
        unset($preview_tem);
        tem_set('preview', $preview);
@@ -361,6 +385,7 @@ function download_tar() {
        $data = array(
                ".htaccess" => make_htaccess(),
                "run.php ->" => 'code/wfpl/run.php',
+               "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
                "$name.html" => make_html(),
                "$name.php" => make_php());
        if($GLOBALS['opt_db'] == 'Yes') {
diff --git a/metaform/htaccess b/metaform/htaccess
new file mode 100644 (file)
index 0000000..eb2ad6d
--- /dev/null
@@ -0,0 +1,3 @@
+RewriteEngine  on
+RewriteRule    ^$  /~form~/run.php
+RewriteRule    ^[^/]*\.html$  /~form~/run.php
index d9d0b6f..fd8f193 100644 (file)
@@ -3,9 +3,7 @@
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <title>Meta Form</title>
-  <style type="text/css"><!--
-td.caption { text-align: right; font-weight: bold; }
---></style>
+  <link rel="stylesheet" href="code/wfpl/metaform/style.css" type="text/css" />
 </head>
 
 <body>
diff --git a/metaform/style.css b/metaform/style.css
new file mode 100644 (file)
index 0000000..006ccab
--- /dev/null
@@ -0,0 +1,18 @@
+td.field, td.right_caption {
+       text-align: left;
+       vertical-align: bottom;
+}
+td.caption, td.errorcaption {
+       text-align: right;
+       vertical-align: top;
+}
+td.caption, td.errorcaption, td.right_caption {
+       font-weight: bold;
+}
+td.errorcaption { color: red; }
+div.error {
+       border: 2px solid red;
+       padding: 13px;
+       margin: 20px;
+       background: #fdd;
+}
diff --git a/metaform/template.htaccess b/metaform/template.htaccess
deleted file mode 100644 (file)
index eb2ad6d..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-RewriteEngine  on
-RewriteRule    ^$  /~form~/run.php
-RewriteRule    ^[^/]*\.html$  /~form~/run.php
index 039eaea..22656fd 100644 (file)
@@ -3,12 +3,7 @@
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <title>~form_name~ entry</title>
-  <style type="text/css"><!--
-td.field { vertical-align: bottom; }
-td.caption { text-align: right; vertical-align: top; font-weight: bold; }
-td.errorcaption { text-align: right; vertical-align: top; font-weight: bold; color: red; }
-div.error { border: 2px solid red; padding: 13px; margin: 20px; background: #ffdddd; }
---></style><!--~html_editor_headers start~-->
+  <link rel="stylesheet" href="style.css" type="text/css" /><!--~html_editor_headers start~-->
 <script type="text/javascript" src="fckeditor/fckeditor.js"></script>
   <script type="text/javascript" src="fckeditor/fckconfig.js"></script>
   <script type="text/javascript">
@@ -41,7 +36,7 @@ div.error { border: 2px solid red; padding: 13px; margin: 20px; background: #ffd
   <form action="~form_name~.html~enctype_attr~" method="post"><!--~opt_db_2 start~--><!--~~editing start~~--><input type="hidden" name="~form_name~_edit_id" value="~~~form_name~_edit_id.attr~~" /><!--~~end~~--><!--~end~--><!--~uploads start~--><input type="hidden" name="MAX_FILE_SIZE" value="~~upload_max_filesize~~" /><!--~end~-->
     <table cellspacing="0" cellpadding="4" border="0" summary=""><!--~row start~-->
 
-      <tr><!--~image start~--><td class="caption">~caption.html~: </td><td><input type="file" name="~name~" /></td><!--~end~--><!--~textbox start~--><td class="caption">~caption.html~: </td><td><input type="text" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~password start~--><td class="caption">~caption.html~: </td><td><input type="password" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~textarea start~--><td class="caption">~caption.html~: </td><td><textarea rows="20" cols="50" name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~html start~--><td class="caption">~caption.html~: </td><td></td></tr><tr><td colspan="2"><textarea rows="20" cols="50" name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~radio start~--><td class="caption">~caption.html~: </td><td class="field"><input type="radio" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~checkbox start~--><td class="caption">~caption.html~: </td><td><input type="checkbox" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~submit start~--><td class="submit_row" colspan="2"><input type="submit" name="~name~" value="~caption.attr~" /></td><!--~end~--></tr><!--~end~-->
+      <tr><!--~image start~--><td class="caption">~caption.html~: </td><td class="field"><input type="file" name="~name~" /></td><!--~end~--><!--~textbox start~--><td class="caption">~caption.html~: </td><td class="field"><input type="text" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~password start~--><td class="caption">~caption.html~: </td><td class="field"><input type="password" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~textarea start~--><td class="caption">~caption.html~: </td><td class="field"><textarea rows="20" cols="50" name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~html start~--><td class="caption">~caption.html~: </td><td></td></tr><tr><td colspan="2"><textarea rows="20" cols="50" name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~pulldown start~--><td class="caption">~caption.html~: </td><td class="field"><select name="~name~"><option value="">Choose One:</option><!--~~~name~.options~~--></select></td><!--~end~--><!--~radio start~--><td class="caption">~caption.html~: </td><td class="field"><input type="radio" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~checkbox start~--><td class="caption">~caption.html~? </td><td class="field"><input type="checkbox" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~leftcheck start~--><td class="caption"><input type="checkbox" name="~name~~~~name~.checked~~" /></td><td class="right_caption">~caption.html~?</td><!--~end~--><!--~submit start~--><td class="submit_row" colspan="2"><input type="submit" name="~name~" value="~caption.attr~" /></td><!--~end~--></tr><!--~end~-->
     </table>
   </form>
 <!--~end~--><!--~~end~~-->
index 537c22f..890c49b 100644 (file)
@@ -18,6 +18,10 @@ $GLOBALS['~form_name~_form_recipient'] = "fixme@example.com";
 # (the file ~form_name~.sql should help with this), and create a file called
 # 'db_connect.php' or 'code/db_connect.php' which calls db_connect() see:
 # code/wfpl/examples/db_connect.php
+#
+# if you rename any of the database fields, you'll need to update this:
+
+define('~form_name.upper~_DB_FIELDS', '~db_fields~');
 <!--~end~--><!--~upload_settings start~-->
 # Set this to the path to your uploads directory. It can be relative to the
 # location of this script. IT MUST END WITH A SLASH
@@ -38,18 +42,22 @@ require_once('code/wfpl/db.php');<!--~end~--><!--~image_include start~-->
 require_once('code/wfpl/upload.php');<!--~end~-->
 
 function ~form_name~_get_fields() {<!--~formats start~-->
-       $~name~ = format_~format~($_REQUEST['~name~']);<!--~end~--><!--~image_upload start~-->
+       $~name~ = format_~format~($_REQUEST['~name~']<!--~pulldown_format_extra start~-->, '~name~'<!--~end~-->);<!--~end~--><!--~image_upload start~-->
        if($_FILE['~name~'] && $_FILE['~name~']['error'] == 0) {
                $~name~ = substr(save_uploaded_image('~name~', $GLOBALS['upload_directory']), strlen($GLOBALS['upload_directory']));
        } else {
                $~name~ = format_filename($_REQUEST['old_~name~']);
        }<!--~end~-->
-       <!--~tem_sets start~-->
-       tem_set('~name~', $~name~);<!--~end~-->
+
+       ~form_name~_tem_sets(~php_fields~);
 
        return array(~php_fields~);
 }
 
+function ~form_name~_tem_sets(~php_fields~) {<!--~tem_sets start~-->
+       tem_set('~name~', $~name~);<!--~end~-->
+}
+
 function ~form_name~() {<!--~opt_http_pass_2 start~-->
        # To remove password protection, just delete this block:
        if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] != AUTH_USER || $_SERVER['PHP_AUTH_PW'] != AUTH_PASS) {
@@ -58,6 +66,8 @@ function ~form_name~() {<!--~opt_http_pass_2 start~-->
                echo '401 Unauthorized';
                exit;
        }
+       <!--~end~--><!--~pulldowns start~-->
+       pulldown('~name~', array('option 1', 'option 2', 'option 3'));
        <!--~end~--><!--~opt_db_3 start~-->
        $edit_id = format_int($_REQUEST['~form_name~_edit_id']);
        unset($_REQUEST['~form_name~_edit_id']);
@@ -94,10 +104,10 @@ function ~form_name~() {<!--~opt_http_pass_2 start~-->
                                                $~name~ = db_get_value('~form_name~', '~name~', 'where id=%i', $edit_id);
                                        }
                                        <!--~end~-->
-                                       db_update('~form_name~', '~db_fields~', ~php_fields~, 'where id=%i', $edit_id);
+                                       db_update('~form_name~', ~form_name.upper~_DB_FIELDS, ~php_fields~, 'where id=%i', $edit_id);
                                        message('Entry updated.');
                                } else {
-                                       db_insert('~form_name~', '~db_fields~', ~php_fields~);
+                                       db_insert('~form_name~', ~form_name.upper~_DB_FIELDS, ~php_fields~);
                                        message('Entry saved.');
                                }
                        }<!--~end~--><!--~opt_email_2 start~-->
@@ -133,8 +143,8 @@ function ~form_name~() {<!--~opt_http_pass_2 start~-->
                # fix their entry in whatever way you require.<!--~opt_db_5 start~-->
        } elseif($edit_id) {
                # we've recieved an edit id, but no data. So we grab the values to be edited from the database
-               list(~php_fields~) = db_get_row('~form_name~', '~db_fields~', 'where id=%i', $edit_id);
-               ~tem_sets.tab~<!--~end~-->
+               list(~php_fields~) = db_get_row('~form_name~', ~form_name.upper~_DB_FIELDS, 'where id=%i', $edit_id);
+               ~form_name~_tem_sets(~php_fields~);<!--~end~-->
        } else {
                # form not submitted, you can set default values like so:
                #tem_set('~always_field~', 'Yes');
index 28b4814..2425275 100644 (file)
@@ -246,7 +246,7 @@ function template_filler($matches) {
                foreach($encs as $enc) {
                        $enc = "enc_$enc";
                        if(function_exists($enc)) {
-                               $value = $enc($value);
+                               $value = $enc($value, $tag);
                        } else {
                                print "ERROR: encoder function '$enc' not found.<br>\n";
                                exit(1);