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
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;
}
?>
# 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);
'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'),
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) {
$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
} 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) {
$image_included_yet = true;
}
} else {
+ if($input == 'pulldown') {
+ $tem->sub('pulldowns');
+ $tem->sub('pulldown_format_extra');
+ }
$tem->sub('formats');
}
$tem->sub('tem_sets');
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() {
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);
$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') {
--- /dev/null
+RewriteEngine on
+RewriteRule ^$ /~form~/run.php
+RewriteRule ^[^/]*\.html$ /~form~/run.php
<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>
--- /dev/null
+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;
+}
+++ /dev/null
-RewriteEngine on
-RewriteRule ^$ /~form~/run.php
-RewriteRule ^[^/]*\.html$ /~form~/run.php
<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">
<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~~-->
# (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
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) {
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']);
$~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~-->
# 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');
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);