From fb2d0e47e11293f8b2a98d13a76d19b6e6a4f77d Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Mon, 5 Apr 2010 23:30:21 -0400 Subject: [PATCH] revamp metaform so php holds db/form data as hash --- TODO | 1 - metaform.php | 7 --- metaform/template.html | 124 +++++++++++++++++++++++------------------------- metaform/template.php | 115 ++++++++++++++++++-------------------------- 4 files changed, 106 insertions(+), 141 deletions(-) diff --git a/TODO b/TODO index 54dc266..7ce467f 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,3 @@ db_reposition(): add parameter for "where clause" so it can work on any subset of the rows - metaform: change php so it stores/passes form/db data in hashes instead of locals finish and test "delete" checkbox for images test db stuff with images diff --git a/metaform.php b/metaform.php index ee5b942..a4a7872 100644 --- a/metaform.php +++ b/metaform.php @@ -301,7 +301,6 @@ function make_html($whole_file = true) { if($GLOBALS['opt_display'] == 'Yes') { $tem->show('opt_display_1'); - $tem->show('opt_display_2'); } if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') { @@ -358,7 +357,6 @@ function make_php() { $tem->set('plural', $GLOBALS['plural']); $fields = get_fields(); $db_fields = ''; - $php_fields = ''; $always_field = find_always_field($fields); $image_included_yet = false; foreach($fields as $field) { @@ -370,8 +368,6 @@ function make_php() { if($sql != 'n/a') { if($db_fields != '') $db_fields .= ','; $db_fields .= $name; - if($php_fields != '') $php_fields .= ', '; - $php_fields .= '$' . $name; } if($input == 'image') { if($type == 'thumb') { @@ -396,7 +392,6 @@ function make_php() { } $tem->show('formats'); } - $tem->show('tem_sets'); } if($GLOBALS['opt_listing'] == 'Yes') { @@ -413,7 +408,6 @@ function make_php() { $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->show('opt_listing_1'); @@ -424,7 +418,6 @@ function make_php() { $tem->show('opt_display_2'); } else { $tem->show('opt_display_1_else'); - $tem->show('opt_display_2_else'); } if($GLOBALS['opt_db'] == 'Yes') { $tem->show('opt_db_1'); diff --git a/metaform/template.html b/metaform/template.html index 07b55e6..93702c3 100644 --- a/metaform/template.html +++ b/metaform/template.html @@ -35,21 +35,21 @@ - - -
- -

~~data html~~

- -
- - -
- + + +
- - -

~singular cap~ details

+

~~data html~~

+ +
+ + +
+ + + + +

~singular cap~ details

Edit

@@ -71,65 +71,61 @@

Edit

- + - - - -

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

+ +

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

-
+
-
~caption html~
-
-
~caption html~
-
-
~caption html~
-
mm/dd/yyyy
-
~caption html~
-
-
~caption html~
-
-
~caption html~
-
-
~caption html~
-
-
~caption html~
-
-
~caption html~
-
-
~caption html~
-
-
+
~caption html~
+
+
~caption html~
+
+
~caption html~
+
mm/dd/yyyy
+
~caption html~
+
+
~caption html~
+
+
~caption html~
+
+
~caption html~
+
+
~caption html~
+
+
~caption html~
+
+
~caption html~
+
+
 
+
- -
- - -

~plural cap~ Listing

+ + - -

[Add a new ~singular~]

+ +

~plural cap~ Listing

- - - - - - + +

[Add a new ~singular~]

-
~caption~ 
~~~name~ ~listing_enc~~~[delete this ~singular~]
- - -

No ~plural~ in database.

- + + + + + + -

[Add a new ~singular~]

- - +
~caption~ 
~~~name~ ~listing_enc~~~[delete this ~singular~]
+ + +

No ~plural~ in database.

+ + +

[Add a new ~singular~]

+ - - diff --git a/metaform/template.php b/metaform/template.php index 520ca57..6ae0602 100644 --- a/metaform/template.php +++ b/metaform/template.php @@ -40,69 +40,57 @@ require_once('code/wfpl/email.php'); require_once('code/db_connect.php'); require_once('code/wfpl/upload.php'); -function ~file_name~_get_fields() { - $~name~ = format_~format~($_REQUEST['~name~'], '~name~'); +function ~file_name~_get_fields() { + $data = array(); + + $data['~name~'] = format_~format~($_REQUEST['~name~'], '~name~'); if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) { - $~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']); + $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') { - $~name~ = ''; + $data['~name~'] = ''; } else { - $~name~ = format_image_w_h_thumb_w_h($_REQUEST['old_~name~']); + $data['~name~'] = format_image_w_h_thumb_w_h($_REQUEST['old_~name~']); } } - ~file_name~_tem_sets(~php_fields~); - - return array(~php_fields~); + return $data; } -function ~file_name~_tem_sets(~php_fields~) { - tem_set('~name~', $~name~); -} # You may pass a "where clause" for the db query. function ~file_name~_display_listing($where = 'order by ~always_field~ limit 100') { - $rows = db_get_rows('~table_name~', 'id,~name~', $where); + $rows = db_get_assocs('~table_name~', 'id,~name~', $where); if($rows == false || count($rows) == 0) { - tem_show('empty_listing'); - tem_show('listings'); - return false; + tem_set('listings', array('empty_listing' => true)); + return; } - foreach($rows as $row) { - list($id, $~name~) = $row; - tem_set('id', $id); - if($~always_field~ == '') { - $~always_field~ = '--'; + # make sure there's something clickable + foreach($rows as &$row) { + if($row['~always_field~'] == '') { + $row['~always_field~'] = '--'; } - ~file_name~_tem_sets(~php_fields~); - tem_show('listing_row'); } - tem_show('populated_listing'); - tem_show('listings'); + tem_set('listings', array( + 'populated_listing' => true, + 'rows' => $rows)); return true; } -function ~file_name~_main() { +function ~file_name~_main() { + if(!logged_in_as_admin()) { + $GLOBALS['url'] = this_url(); + return 'admin_login'; + } + if(isset($_REQUEST['~file_name~_id'])) { - $ret = ~file_name~_display_main(); - if($ret) { - return $ret; - } - tem_show('display_body'); + return ~file_name~_display_main(); } else { - $ret = ~file_name~_edit_main(); - if($ret) { - return $ret; - } - tem_show('edit_body'); + return ~file_name~_edit_main(); } - $ret = _~file_name~_main(); - if($ret) { - return $ret; - } + return ~file_name~_edit_main(); } @@ -113,25 +101,15 @@ function ~file_name~_display_main() { message('Error: Broken link'); return './~file_name~'; } - $row = db_get_row('~table_name~', ~file_name upper~_DB_FIELDS, 'where id=%i', $id); + $row = db_get_assoc('~table_name~', 'id,'.~file_name upper~_DB_FIELDS, 'where id=%i', $id); if(!$row) { message('Error: ~singular cap~ not found'); return './~file_name~'; } - list(~php_fields~) = $row; - ~file_name~_tem_sets(~php_fields~); - tem_set('id', $id); -} + tem_set('display', $row); +} -function ~file_name~_edit_main() { - - -function _~file_name~_main() { - if(!logged_in_as_admin()) { - $GLOBALS['url'] = this_url(); - return 'admin_login'; - } - +function ~file_name~_edit_main() { pulldown('~name~', array('option 1', 'option 2', 'option 3')); $edit_id = format_int($_REQUEST['~file_name~_edit_id']); @@ -139,7 +117,8 @@ function _~file_name~_main() { if($edit_id) { # add hidden field for database id of row we're editing tem_set('~file_name~_edit_id', $edit_id); - tem_show('editing'); + tem_set('editing', 'show'); + tem_set('edit_msg', 'show'); } $delete_id = format_int($_REQUEST['~file_name~_delete_id']); @@ -157,18 +136,18 @@ function _~file_name~_main() { return; } - tem_show('new_msg'); + tem_set('new_msg', 'show'); } if(isset($_POST['~always_field~'])) { - list(~php_fields~) = ~file_name~_get_fields(); + $data = ~file_name~_get_fields(); if("you're happy with the POSTed values") { if($edit_id) { - db_update('~table_name~', ~file_name upper~_DB_FIELDS, ~php_fields~, 'where id=%i', $edit_id); + db_update_assoc('~table_name~', $data, 'where id=%i', $edit_id); message('~singular cap~ updated.'); } else { - db_insert('~table_name~', ~file_name upper~_DB_FIELDS, ~php_fields~); + db_insert_assoc('~table_name~', $data); message('~singular cap~ saved.'); } if($GLOBALS['~file_name~_form_recipient'] != 'fixme@example.com') { @@ -182,7 +161,10 @@ function _~file_name~_main() { } } $subject = '~plural cap~ form submitted'; - $message = tem_run('~file_name~.email.txt'); + $email_template = new tem(); + $email_template->load('~file_name~.email.txt'); + $email_template->sets($data); + $message = $email_template->run(); $cc = ''; $bcc = ''; if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) { @@ -202,22 +184,17 @@ function _~file_name~_main() { # fix their entry in whatever way you require. } 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('~table_name~', ~file_name upper~_DB_FIELDS, 'where id=%i', $edit_id); - ~file_name~_tem_sets(~php_fields~); + $data = db_get_assoc('~table_name~', ~file_name upper~_DB_FIELDS, 'where id=%i', $edit_id); } else { # form not submitted, you can set default values like so: - #tem_set('~always_field~', 'Yes'); + #$data = array('~always_field~' => 'Yes'); + $data = array(); } tem_set('upload_max_filesize', upload_max_filesize()); - # this has to be later in the file because it requres that ~always_field~ be set already - if($edit_id) { - tem_show('edit_msg'); - } - - tem_show('form'); - tem_show('extra_headers'); + tem_set('form', $data); + tem_set('extra_headers', 'show'); } ?> -- 1.7.10.4