From: Jason Woofenden Date: Mon, 18 Apr 2011 08:30:30 +0000 (-0400) Subject: metaform: php refactor, opt_public_* X-Git-Url: https://jasonwoof.com/gitweb/?p=wfpl.git;a=commitdiff_plain;h=c06ba3b1d66efa1478a5aea39fb833b81d35f846 metaform: php refactor, opt_public_* --- diff --git a/metaform.php b/metaform.php index cee8033..6ad925c 100644 --- a/metaform.php +++ b/metaform.php @@ -70,6 +70,7 @@ function list_available_types() { function tem_set_globals(&$tem) { $vars = array( 'file_name', + 'public_file_name', 'table_name', 'plural', 'singular'); @@ -84,7 +85,8 @@ function tem_set_globals(&$tem) { 'opt_display', 'opt_pass', 'opt_public_form', - 'opt_public_display'); + 'opt_public_display', + 'opt_public_something'); foreach($bools as $bool) { if($GLOBALS[$bool]) { $tem->set($bool); @@ -111,6 +113,12 @@ function metaform() { $GLOBALS['opt_public_form'] = format_bool($_REQUEST['opt_public_form']); $GLOBALS['opt_public_display'] = format_bool($_REQUEST['opt_public_display']); + $GLOBALS['public_file_name'] = $GLOBALS['file_name']; + if($GLOBALS['opt_public_form'] || $GLOBALS['opt_public_display']) { + $GLOBALS['opt_public_something'] = 1; + $GLOBALS['file_name'] = $GLOBALS['file_name'] . _admin; + } + tem_init(); tem_set_globals($GLOBALS['wfpl_template']); } @@ -537,22 +545,27 @@ function preview() { } function download_tar() { - $name = $GLOBALS['file_name']; - $data = array( + $admin_name = $GLOBALS['file_name']; + $nice_name = $GLOBALS['public_file_name']; + $files = array( "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'), ".htaccess" => make_htaccess(), - #"run.php ->" => 'code/wfpl/run.php', + "run.php ->" => 'code/wfpl/run.php', "style.less" => read_whole_file('code/wfpl/metaform/style.less'), "template.html" => read_whole_file('code/wfpl/metaform/site-template.html'), - "$name.html" => make_html(), - "$name.php" => make_php()); + "$admin_name.html" => make_html(), + "$admin_name.php" => make_php()); + if($GLOBALS['opt_public_something']) { + $files["$nice_name.html ->"] = "$admin_name.html"; + $files["$nice_name.php ->"] = "$admin_name.php"; + } if($GLOBALS['opt_db']) { - $data["$name.sql"] = make_sql(); + $files["$nice_name.sql"] = make_sql(); } if($GLOBALS['opt_email']) { - $data["$name.email.txt"] = make_email(); + $files["$nice_name.email.txt"] = make_email(); } - make_tar($name, $data); + make_tar($nice_name, $files); } diff --git a/metaform/main.html b/metaform/main.html index f168fc5..ef68b63 100644 --- a/metaform/main.html +++ b/metaform/main.html @@ -11,7 +11,7 @@

The form name should not contain any capitals or symbols (besides underscore)

-

File name: .html

+

File name: .html

DB table:

@@ -31,8 +31,8 @@

Provide a "Display" page.

Password protect this form.
-          But with publically accessible entry form
-          But with publically accessible view page.

+          But with publicly accessible submission form
+          But with publicly accessible view page.

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: , ~type html~.

diff --git a/metaform/preview.html b/metaform/preview.html index 09e0c4e..5e1d237 100644 --- a/metaform/preview.html +++ b/metaform/preview.html @@ -11,10 +11,10 @@

PREVIEW

~preview~ -

~hiddens~

+

~hiddens~

-

+

diff --git a/metaform/template.html b/metaform/template.html index 33ea178..8827bfe 100644 --- a/metaform/template.html +++ b/metaform/template.html @@ -20,13 +20,15 @@ -

~singular cap~ details

+

~singular cap~ details

-

Add another ~singular attr~

+ +

Add another ~singular attr~

-

Back to ~plural attr~

- -

Edit

+

Back to ~plural attr~

+ +

Edit

+ @@ -43,17 +45,19 @@ -
~caption html~:~~~name~ htmlbrtab~~
+ -

Back to ~plural attr~

- -

Edit

+ +

Back to ~plural attr~

+ +

Edit

+

Add a new ~singular~Edit ~singular~ "~~~always_field~ html~~"Submit a ~singular~

-
+
~caption html~ diff --git a/metaform/template.php b/metaform/template.php index aea7bd6..c35534c 100644 --- a/metaform/template.php +++ b/metaform/template.php @@ -42,89 +42,120 @@ require_once('code/wfpl/upload.php'); function ~file_name~_get_fields() { $data = array(); - $data['~name~'] = format_~format~($_REQUEST['~name~'], '~name~'); + $data['~name~'] = format_~format~(_REQUEST_cut('~name~'), '~name~'); + if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) { $data['~name~'] = convert_uploaded_image('~name~', $GLOBALS['upload_directory'] . $GLOBALS['~name~_file_name'], $GLOBALS['~name~_max_width'], $GLOBALS['~name~_max_height'], $GLOBALS['~name~_thumb_max_width'], $GLOBALS['~name~_thumb_max_height']); } else { - if($_REQUEST['delete_~name~'] == 'Yes') { + if(_REQUEST_cut('delete_~name~') == 'Yes') { $data['~name~'] = ''; } else { - $data['~name~'] = format_image_w_h_thumb_w_h($_REQUEST['old_~name~']); + $data['~name~'] = format_image_w_h_thumb_w_h(_REQUEST_cut('old_~name~')); } - } + } + unset($_FILES['~name~']); + $~name~_filename_tmp = $GLOBALS['~name~_file_name']; if(!$~name~_filename_tmp) { - $~name~_filename_tmp = format_filename($_REQUEST['~name~']); + $~name~_filename_tmp = format_filename(_REQUEST_cut('~name~')); } if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) { $data['~name~'] = save_uploaded_file('~name~', $GLOBALS['upload_directory'] . $~name~_filename_tmp); } else { - if($_REQUEST['delete_~name~'] == 'Yes') { + if(_REQUEST_cut('delete_~name~') == 'Yes') { $data['~name~'] = ''; } else { - $data['~name~'] = format_path($_REQUEST['old_~name~']); + $data['~name~'] = format_path(_REQUEST_cut('old_~name~')); } - } + } + unset($_FILES['~name~']); return $data; -} +} -function ~file_name~_main() { - if(!logged_in_as_admin()) { +function ~public_file_name~_main() { + $id = _REQUEST_cut('~public_file_name~_id'); + if($id) { + return ~file_name~_main_display($id); + } else { + message("Error: Broken Link (~singular cap~ #$id not found)"); + return './'; # FIXME where should this go? + } + return ~file_name~_main_form(); +} + + +function ~file_name~_main($new_only = false) { + if(logged_in_as_admin()) { + tem_set('admin_privs'); + } else { $_REQUEST['url'] = this_url(); return 'admin_login'; } - - if(isset($_REQUEST['~file_name~_id'])) { - return ~file_name~_display_main(); - } else { - return ~file_name~_edit_main(); + + $id = _REQUEST_cut('~file_name~_edit_id'); + if($id) { + return ~file_name~_main_form($id); } -} -function ~file_name~_display_main() { - $id = format_int($_REQUEST['~file_name~_id']); - unset($_REQUEST['~file_name~_id']); - if(!$id) { - message('Error: Broken link'); - return './~file_name~'; + $id = _REQUEST_cut('~file_name~_delete_id'); + if($id) { + return ~file_name~_main_delete($id); + } + + if(_REQUEST_cut('~file_name~_new')) { + return ~file_name~_main_form(); + } + + if(_REQUEST_cut('~file_name~_list')) { + return ~file_name~_main_listing(); + } + + $id = _REQUEST_cut('~file_name~_id'); + if($id) { + return ~file_name~_main_display($id); } + + if(isset($_POST['~always_field~'])) { + return ~file_name~_main_form(); + } + + # default action: + return ~file_name~_main_listing(); + return ~file_name~_main_form(); +} + +function ~file_name~_main_display($id) { $data = db_get_assoc('~table_name~', 'id,'.~file_name upper~_DB_FIELDS, 'where id=%i', $id); if(!$data) { - message('Error: ~singular cap~ not found'); - return './~file_name~'; + message("Error: Broken Link (~singular cap~ #$id not found)"); + return './' . $GLOBALS['wfpl_basename']; + return './~file_name~'; } tem_set('display', $data); -} +} -function ~file_name~_edit_main() { - pulldown('~name~', ~pulldown_options~); - $edit_id = format_int($_REQUEST['~file_name~_edit_id']); - unset($_REQUEST['~file_name~_edit_id']); - if($edit_id) { +function ~file_name~_main_delete($id) { + db_delete('~table_name~', 'where id=%i', $id); + message('~singular cap~ deleted.'); + return './~file_name~'; +} + +function ~file_name~_main_listing() { + $listing_rows = db_get_assocs('~table_name~', 'id,~name~', 'order by ~always_field~ limit 100'); + tem_set('listings', $listing_rows); +} + +function ~file_name~_main_form($id = false) { + pulldown('~name~', ~pulldown_options~); + + if($id) { # add hidden field for database id of row we're editing - tem_set('~file_name~_edit_id', $edit_id); + tem_set('~file_name~_edit_id', $id); tem_set('editing'); tem_set('edit_msg'); - } - - $delete_id = format_int($_REQUEST['~file_name~_delete_id']); - unset($_REQUEST['~file_name~_delete_id']); - if($delete_id) { - db_delete('~table_name~', 'where id=%i', $delete_id); - message('~singular cap~ deleted.'); - - return './~file_name~'; - } - - if(!$edit_id) { - if(!isset($_REQUEST['~file_name~_new']) && !isset($_REQUEST['~always_field~'])) { - $listing_rows = db_get_assocs('~table_name~', 'id,~name~', 'order by ~always_field~ limit 100'); - tem_set('listings', $listing_rows); - return; - } - + } else { tem_set('new_msg'); } @@ -132,8 +163,8 @@ function ~file_name~_edit_main() { $data = ~file_name~_get_fields(); if("you're happy with the POSTed values") { - if($edit_id) { - db_update_assoc('~table_name~', $data, 'where id=%i', $edit_id); + if($id) { + db_update_assoc('~table_name~', $data, 'where id=%i', $id); message('~singular cap~ updated.'); } else { db_insert_assoc('~table_name~', $data); @@ -163,17 +194,24 @@ function ~file_name~_edit_main() { message('Message sent'); } } - if($error !== true) { - return './~file_name~'; # FIXME is this the page you want to go to after successful form submission? + if($error !== true) { + if($GLOBALS['wfpl_basename'] == '~file_name~') { + return './~file_name~'; + } else { + # FIXME create this page or change this to go elsewhere + return './~public_file_name~_thanks'; + } + # FIXME create this page or change this to go elsewhere + return './~file_name~_thanks'; } } # otherwise, we display the form again. ~file_name~_get_fields() has # already put the posted values back into the template engine, so they will # show up in the form fields. You should add some message asking people to # fix their entry in whatever way you require. - } elseif($edit_id) { + } elseif($id) { # we've recieved an edit id, but no data. So we grab the values to be edited from the database - $data = db_get_assoc('~table_name~', ~file_name upper~_DB_FIELDS, 'where id=%i', $edit_id); + $data = db_get_assoc('~table_name~', ~file_name upper~_DB_FIELDS, 'where id=%i', $id); } else { # form not submitted, you can set default values like so: #$data = array('~always_field~' => 'Yes'); @@ -185,5 +223,3 @@ function ~file_name~_edit_main() { tem_set('form', $data); tem_set('extra_headers'); } - -?>