From d98d81886649a863d0e902f7c4e63b1257217e1f Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Wed, 11 Jul 2007 18:04:33 -0400 Subject: [PATCH] fixed pulldowns, metaform suppors zip and decimal, run.php supports straight html files with a template --- encode.php | 2 +- format.php | 24 +++++++++++++-- metaform.php | 40 +++++++++++++++++++++---- metaform/htaccess | 2 +- metaform/main.html | 2 ++ metaform/preview.html | 4 +-- metaform/template.html | 18 ++++++++--- metaform/template.php | 78 +++++++++++++++++++++++++++++++++--------------- run.php | 59 +++++++++++++++++++++++------------- template.php | 29 +++++++++++++++--- 10 files changed, 195 insertions(+), 63 deletions(-) diff --git a/encode.php b/encode.php index 3f4119c..243b21f 100644 --- a/encode.php +++ b/encode.php @@ -190,7 +190,7 @@ function encode_options($selected, $options, $keys_from) { foreach($options as $value => $display) { $out .= ' 5) { + return substr($str, 0, 5) . '-' . substr($str, 5); + } + return $str; } function format_filename($str) { diff --git a/metaform.php b/metaform.php index a473bbc..e6e4123 100644 --- a/metaform.php +++ b/metaform.php @@ -32,7 +32,9 @@ $GLOBALS['types'] = array( 'name' => array('textbox', 'oneline', 'varchar(200)'), 'textbox' => array('textbox', 'oneline', 'varchar(200)'), 'int' => array('textbox', 'int', 'int'), + 'decimal' => array('textbox', 'decimal', 'decimal(12,12)'), 'bigint' => array('textbox', 'int', 'varchar(100)'), # up to 100 digits, stored as a string + 'zip' => array('textbox', 'zip', 'varchar(20)'), 'email' => array('textbox', 'email', 'varchar(100)'), 'phone' => array('textbox', 'phone', 'varchar(32)'), 'state' => array('states', 'oneline', 'varchar(2)'), @@ -72,6 +74,8 @@ function metaform() { tem_set('opt_email', $GLOBALS['opt_email']); $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']); tem_set('opt_db', $GLOBALS['opt_db']); + $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']); + tem_set('opt_listing', $GLOBALS['opt_listing']); $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']); tem_set('opt_http_pass', $GLOBALS['opt_http_pass']); } else { @@ -180,6 +184,19 @@ function view_sql() { view_headers(); echo make_sql(); } + +# always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not. +function find_always_field($fields) { + foreach($fields as $field) { + list($name, $type, $input, $format, $sql) = $field; + if($input != 'submit' && $input != 'checkbox' && $input != 'radio') { + return $name; + } + } + + return false; +} + # pass false if you want to exclude the and tag etc. @@ -190,6 +207,7 @@ function make_html($whole_file = true) { $tem->load('code/wfpl/metaform/template.html'); $tem->set('form_name', $GLOBALS['form_name']); $fields = get_fields(); + $tem->set('always_field', find_always_field($fields)); foreach($fields as $field) { list($name, $type, $input, $format, $sql) = $field; $tem->set('name', $name); @@ -216,6 +234,12 @@ function make_html($whole_file = true) { $tem->sub('opt_db_1_else'); } + if($GLOBALS['opt_listing'] == 'Yes') { + $tem->sub('opt_listing_1'); + } else { + $tem->sub('opt_listing_1_else'); + } + if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') { $tem->set('name', 'send'); $tem->set('caption', 'Send'); @@ -252,7 +276,7 @@ function make_php() { $fields = get_fields(); $db_fields = ''; $php_fields = ''; - $always_field = false; + $always_field = find_always_field($fields); $image_included_yet = false; foreach($fields as $field) { list($name, $type, $input, $format, $sql) = $field; @@ -283,16 +307,22 @@ function make_php() { $tem->sub('formats'); } $tem->sub('tem_sets'); - 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); $tem->set('php_fields', $php_fields); $tem->set('metaform_url', edit_url()); + if($GLOBALS['opt_listing'] == 'Yes') { + $tem->sub('opt_listing_1'); + $tem->sub('opt_listing_2'); + $tem->sub('opt_listing_3'); + $tem->sub('opt_listing_4'); + } else { + $tem->sub('opt_listing_3_else'); + $tem->sub('opt_listing_4_else'); + } if($GLOBALS['opt_db'] == 'Yes') { $tem->sub('opt_db_1'); $tem->sub('opt_db_2'); diff --git a/metaform/htaccess b/metaform/htaccess index eb2ad6d..362282a 100644 --- a/metaform/htaccess +++ b/metaform/htaccess @@ -1,3 +1,3 @@ RewriteEngine on -RewriteRule ^$ /~form~/run.php +RewriteRule ^$ /run.php RewriteRule ^[^/]*\.html$ /~form~/run.php diff --git a/metaform/main.html b/metaform/main.html index 5d0bc2e..fd70d42 100644 --- a/metaform/main.html +++ b/metaform/main.html @@ -19,6 +19,8 @@

Save form results to a database.

+

Provide a "Listing" page.

+

Password protect this form (with HTTP authentication).

Below, specify the fields you'd like in your form, one field per line. After each field name, put at least one space, then the field type. The following field types are available: ~available_types.html~.

diff --git a/metaform/preview.html b/metaform/preview.html index fd8f193..2731e23 100644 --- a/metaform/preview.html +++ b/metaform/preview.html @@ -11,10 +11,10 @@

This grey thing is a preview. It will NOT work. Don't click button(s) in it

~preview~ -

+

-

+

diff --git a/metaform/template.html b/metaform/template.html index 22656fd..a7d84d5 100644 --- a/metaform/template.html +++ b/metaform/template.html @@ -19,9 +19,6 @@ - -

Add a new entryEdit entry "~~name.html~~"~form_name~ entry form

-
@@ -33,6 +30,9 @@
+ +

Add a new entryEdit entry "~~~always_field~.html~~"~form_name~ entry form

+ @@ -40,8 +40,18 @@
+ +

~form_name~ Listing

+ + + + +
~~~always_field~.html~~[delete this record]
+ +

[Add a new record]

+

Thank you for taking the time to fill out this form.

- + diff --git a/metaform/template.php b/metaform/template.php index 890c49b..48fb37f 100644 --- a/metaform/template.php +++ b/metaform/template.php @@ -38,7 +38,7 @@ require_once('code/wfpl/template.php'); require_once('code/wfpl/format.php'); require_once('code/wfpl/messages.php'); require_once('code/wfpl/email.php'); -require_once('code/wfpl/db.php'); +require_once('db_connect.php'); require_once('code/wfpl/upload.php'); function ~form_name~_get_fields() { @@ -57,7 +57,27 @@ function ~form_name~_get_fields() { function ~form_name~_tem_sets(~php_fields~) { tem_set('~name~', $~name~); } + +# You may pass a "where clause" for the db query. +function ~form_name~_display_listing($where = 'order by ~always_field~ limit 100') { + $rows = db_get_rows('~form_name~', 'id,~always_field~', $where); + if($rows == false || count($rows) == 0) { + return false; + } + foreach($rows as $row) { + list($id, $~always_field~) = $row; + tem_set('id', $id); + if($~always_field~ == '') { + $~always_field~ = '--'; + } + tem_set('~always_field~', $~always_field~); + tem_sub('listing_row'); + } + tem_sub('listings'); + return true; +} + function ~form_name~() { # 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) { @@ -75,20 +95,30 @@ function ~form_name~() { # add hidden field for database id of row we're editing tem_set('~form_name~_edit_id', $edit_id); tem_sub('editing'); - tem_sub('edit_msg'); } $delete_id = format_int($_REQUEST['~form_name~_delete_id']); unset($_REQUEST['~form_name~_delete_id']); if($delete_id) { db_delete('~form_name~', 'where id=%i', $delete_id); - message('Entry deleted.'); + message('Entry deleted.'); + + if(~form_name~_display_listing()) { + return; + } + unset($delete_id); # FIXME: what to do after delete? - return; + return; } - if(!$edit_id && !$delet_id) { + if(!$edit_id && !$delet_id) { + if(!isset($_REQUEST['~form_name~_new']) && !isset($_REQUEST['~always_field~'])) { + if(~form_name~_display_listing()) { + return; + } + } + tem_sub('new_msg'); } @@ -96,20 +126,17 @@ function ~form_name~() { list(~php_fields~) = ~form_name~_get_fields(); if("you're happy with the POSTed values") { - if(file_exists($db_connector = 'db_connect.php') || file_exists($db_connector = 'code/db_connect.php')) { - require_once($db_connector); - if($edit_id) { - # uploading nothing means leaving it as is. - if(!$~name~ && $delete_~name~ != 'Yes') { - $~name~ = db_get_value('~form_name~', '~name~', '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~', ~form_name.upper~_DB_FIELDS, ~php_fields~); - message('Entry saved.'); + if($edit_id) { + # uploading nothing means leaving it as is. + if(!$~name~ && $delete_~name~ != 'Yes') { + $~name~ = db_get_value('~form_name~', '~name~', '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~', ~form_name.upper~_DB_FIELDS, ~php_fields~); + message('Entry saved.'); } if($GLOBALS['~form_name~_form_recipient'] != "fixme@example.com") { $to = $GLOBALS['~form_name~_form_recipient']; @@ -130,11 +157,10 @@ function ~form_name~() { $error = true; } } - if($error !== true) { - tem_load('~form_name~.html'); - tem_sub('thankyou'); - tem_output(); - exit(); + if($error !== true) { + ~form_name~_display_listing(); + tem_sub('thankyou'); + return; } } # otherwise, we display the form again. ~form_name~_get_fields() has @@ -152,7 +178,11 @@ function ~form_name~() { tem_set('upload_max_filesize', upload_max_filesize()); - display_messages(); + # this has to be later in the file because it requres that ~always_field~ be set already + if($edit_id) { + tem_sub('edit_msg'); + } + tem_sub('form'); } diff --git a/run.php b/run.php index c5ae240..f77e7d0 100644 --- a/run.php +++ b/run.php @@ -59,6 +59,10 @@ require_once('code/wfpl/file_run.php'); require_once('code/wfpl/http.php'); require_once('code/wfpl/template.php'); +if(file_exists('code/config.php')) { + file_run('code/config.php'); +} + function run_php($basename = false) { if(!$basename) { $basename = $_SERVER['REDIRECT_URL']; @@ -72,36 +76,51 @@ function run_php($basename = false) { $html_file = "$basename.html"; $php_file = "$basename.php"; - if(!file_exists($php_file)) { - if(file_exists($html_file)) { - readfile($html_file); + $html_exists = file_exists($html_file); + $php_exists = file_exists($php_file); + + if(!$php_exists && !$html_exists) { + header('HTTP/1.0 404 File Not Found'); + if(file_exists('404.php') || file_exists('404.html')) { + run_php('404'); } else { - header('HTTP/1.0 404 File Not Found'); - if(file_exists('404.php') || file_exists('404.html')) { - run_php('404'); - } else { - echo '404

404 File Not Found

'; - } + echo '404

404 File Not Found

'; } + } + + # If there's no template.html we don't want to parse $html_file. + if($html_exists && !$php_exists && !file_exists('template.html')) { + readfile($html_file); exit(); } - if(file_exists($html_file)) { - $GLOBALS['wfpl_template'] = new tem(); - tem_load($html_file); + if($html_exists) { + tem_load_new($html_file); } - # files can return a basename or URL of a page to be run/displayed - $other = file_run($php_file); - if($other) { - if(strpos($other, ':')) { - redirect($other); - exit(); + if($php_exists) { + # files can return a basename or URL of a page to be run/displayed + $other = file_run($php_file); + if($other) { + if(strpos($other, ':')) { + redirect($other); + exit(); + } + if(substr($other, 0, 2) == './') { + redirect(ereg_replace('/[^/]*$', substr($other, 1), this_url())); + exit(); + } + run_php($other); + return; + } + } else { + $sub_names = tem_top_sub_names(); + foreach($sub_names as $sub_name) { + tem_sub($sub_name); } - run_php($other); - return; } + # Check for $GLOBALS['wfpl_template'] because it might have been set (or unset) by the php script. if($GLOBALS['wfpl_template']) { if(file_exists('template.html')) { $tem = new tem(); diff --git a/template.php b/template.php index 2425275..822738b 100644 --- a/template.php +++ b/template.php @@ -177,6 +177,15 @@ class tem { print($this->run($templ)); } + # return the names of the top level subs, or an empty array + function top_sub_names() { + if(isset($this->sub_subs['top_level_subs'])) { + return $this->sub_subs['top_level_subs']; + } else { + return array(); + } + } + # return the contents of the top-level sub-templates # # this does not run the sub-templates, so if you've not called tem_sub() on them, they will be blank. @@ -186,10 +195,9 @@ class tem { # values: contents of said sub-template. function top_subs() { $ret = array(); - if(isset($this->sub_subs['top_level_subs'])) { - foreach($this->sub_subs['top_level_subs'] as $name) { - $ret[$name] = $this->get($name); - } + $names = $this->top_sub_names(); + foreach($names as $name) { + $ret[$name] = $this->get($name); } return $ret; } @@ -264,9 +272,22 @@ function template_run($template, &$keyval) { return preg_replace_callback(array('||', '|~([^~]*)~|', '|([^<]*)|', '|

([^<]*)

|'), 'template_filler', $template); } +function tem_top_sub_names() { + tem_init(); + return $GLOBALS['wfpl_template']->top_sub_names(); +} + function tem_top_subs() { tem_init(); return $GLOBALS['wfpl_template']->top_subs(); } +# replaces currently set template, and returns the old. +function tem_load_new($file) { + $old = $GLOBALS['wfpl_template']; + $GLOBALS['wfpl_template'] = new tem(); + $GLOBALS['wfpl_template']->load($file); + return $old; +} + ?> -- 1.7.10.4