From: Jason Woofenden Date: Tue, 26 Oct 2010 17:40:30 +0000 (-0400) Subject: metaform: revamp codegen, fix hidden fields X-Git-Url: https://jasonwoof.com/gitweb/?p=wfpl.git;a=commitdiff_plain;h=b9ff9266110da0a7c3ddfe08c5d1b8bee4660414 metaform: revamp codegen, fix hidden fields --- diff --git a/metaform.php b/metaform.php index fdd1ad3..a9d3956 100644 --- a/metaform.php +++ b/metaform.php @@ -230,96 +230,98 @@ function find_always_field($fields) { # pass false if you want to exclude the and tag etc. function make_html($whole_file = true) { - $uploads_output_already = false; $has_html_editors = false; $tem = new tem(); $tem->load('code/wfpl/metaform/template.html'); tem_set_globals($tem); $fields = get_fields(); $tem->set('always_field', find_always_field($fields)); + $hidden_fields = array(); + $visible_fields = array(); + $display_fields = array(); + $listing_headers = array(); + $listing_fields = array(); foreach($fields as $field) { list($name, $type, $input, $format, $sql) = $field; - $tem->set('name', $name); - $tem->set('caption', format_caption($name)); - $tem->show($input); - if($input != 'hidden') { - $tem->show('row'); + if($input == 'hidden') { + $hidden_fields[] = array('name' => $name); + } else { + $visible_fields[] = array($input => array( + 'name' => $name, + 'caption' => format_caption($name))); } - if(($input == 'image' || $input == 'file') && !$uploads_output_already) { - $tem->show('uploads'); + if($input == 'image' || $input == 'file') { + $tem->set('uploads'); $tem->set('enctype_attr', '" enctype="multipart/form-data'); - $uploads_output_already = true; } elseif($input == 'html') { $has_html_editors = true; $tem->set('html_field_name', $name); - $tem->show('replace_textarea'); + $tem->set('replace_textarea'); } - if($GLOBALS['opt_display'] == 'Yes') { - switch($input) { - case 'image': - $tem->show('display_image'); - break; - case 'checkbox': - $tem->show('display_yesno'); - break; - case 'date': - $tem->show('display_date'); - break; - case 'textarea': - $tem->show('display_multiline'); - break; - case 'html': - $tem->show('display_html'); - break; - default: - $tem->show('display_short'); - } - $tem->show('display_row'); + switch($input) { + case 'image': + case 'checkbox': + case 'date': + case 'textarea': + case 'html': + $display_type = $input; + break; + default: + $display_type = 'short'; } - - if($GLOBALS['opt_listing'] == 'Yes') { - if(show_in_listing($type, $input, $format, $sql)) { - if($format == 'bool' || $format == 'yesno') { - $tem->set('listing_enc', 'yesno'); - $tem->show('listing_value_enc'); - } elseif($input == 'date') { - $tem->set('listing_enc', 'mmddyyyy'); - $tem->show('listing_value_enc'); - } elseif($type == 'thumb') { - $tem->show('listing_value_thumb'); - } else { - $tem->set('listing_enc', 'html'); - $tem->show('listing_value_enc'); - } - - $tem->show('listing_head_col'); - $tem->show('listing_row_col'); + $display_fields[] = array($display_type => array( + 'name' => $name, 'caption' => format_caption($name))); + + if(show_in_listing($type, $input, $format, $sql)) { + $listing_headers[] = array('caption' => format_caption($name)); + $listing_field = array('name' => $name); + if($format == 'bool' || $format == 'yesno') { + $listing_field['enc'] = 'yesno'; + } elseif($input == 'date') { + $listing_field['enc'] = 'mmddyyyy'; + } elseif($type == 'thumb') { + $listing_field['thumb'] = true; + } else { + $listing_field['enc'] = 'html'; } + $listing_fields[] = $listing_field; } } + # Submit/Send button if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') { - $tem->set('name', 'send'); - $tem->set('caption', 'Send'); + $visible_fields[] = array('submit' => array( + 'name' => 'send', + 'caption' => 'Send')); } else { - $tem->set('name', 'save'); - $tem->set('caption', 'Save'); + $visible_fields[] = array('submit' => array( + 'name' => 'save', + 'caption' => 'Save')); } - $tem->show('submit'); - $tem->show('row'); - $tem->show('form'); + $tem->set('form', array( + 'visible_fields' => $visible_fields, + 'hidden_fields' => $hidden_fields)); + + # opt_display and opt_listing control whether these are actually displayed + $tem->set('display_fields', $display_fields); + $tem->set('listing_headers', $listing_headers); + $tem->set('listing_fields', $listing_fields); + if($has_html_editors) { - $tem->show('html_editor_headers'); + $tem->set('html_editor_headers'); } if($whole_file) { return $tem->run(); } else { - return $tem->get('form'); + $tem2 = new tem(); + $tem2->load_str(''); + $tem2->merge($tem); + return $tem2->run(); } } diff --git a/metaform/template.html b/metaform/template.html index 0e04ef9..cf0ffcb 100644 --- a/metaform/template.html +++ b/metaform/template.html @@ -54,18 +54,18 @@

Edit

- + - + - + - + - + - +
~caption html~:
~caption html~:~~~name~ yesno~~
~caption html~:~~~name~ mmddyyyy~~
~caption html~:~~~name~ html~~
~caption html~:
~~~name~~~
~caption html~:~~~name~ htmlbrtab~~
@@ -76,8 +76,9 @@

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

-
- +
+
+
~caption html~
~caption html~
@@ -113,9 +114,9 @@

[Add a new ~singular~]

- - - + + + diff --git a/template.php b/template.php index 5643502..cbb9466 100644 --- a/template.php +++ b/template.php @@ -300,34 +300,34 @@ function merge_templates(&$main, &$tem) { function tem_auto_sep(&$value, $key, $context) { $rows =& $context['parent']['parent']; if($rows['cur'] != count($rows['rows'])-1) # last row? - return $value = true; # show once + return true; # show once } # auto-show once, only when this is the first row of the parent function tem_auto_last(&$value, $key, $context) { $rows =& $context['parent']['parent']; if($rows['cur'] == count($rows['rows'])-1) # last row? - return $value = true; # show once + return true; # show once } # auto-show once, only when this is the last row of the parent function tem_auto_first(&$value, $key, $context) { $rows =& $context['parent']['parent']; if($rows['cur'] == 0) # first row? - return $value = true; # show once + return true; # show once } # 'show' sections will be shown unless the corresponding data # value === false function tem_auto_show(&$value) { - if($value === null) $value = array(array()); + if($value === null) return true; return $value; } # 'nonempty' sections will not be shown if the corresponding data # value is the empty string function tem_auto_nonempty(&$value) { - if($value === '') $value = null; + if($value === '') return null; return $value; } @@ -335,11 +335,10 @@ function tem_auto_nonempty(&$value) { # value is not set (opposite of default) function tem_auto_unset(&$value) { if($value === null) { - $value = ''; + return ''; } else { - $value = null; + return null; } - return $value; } # 'evenodd' sections are given an 'evenodd' attribute whose value
~caption~ 
~~~name~ ~listing_enc~~~~caption~ 
~~~name~ ~enc~~~ [delete this ~singular~]