From: Jason Woofenden
Date: Wed, 11 Jul 2007 22:04:33 +0000 (-0400)
Subject: fixed pulldowns, metaform suppors zip and decimal, run.php supports straight html...
X-Git-Url: https://jasonwoof.com/gitweb/?a=commitdiff_plain;h=d98d81886649a863d0e902f7c4e63b1257217e1f;p=wfpl.git
fixed pulldowns, metaform suppors zip and decimal, run.php supports straight html files with a template
---
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 .= '
+
+
+ ~form_name~ Listing
+
+
+
+ [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 '404404 File Not Found
';
- }
+ echo '404404 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;
+}
+
?>