JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: revamp codegen, fix hidden fields
authorJason Woofenden <jason@jasonwoof.com>
Tue, 26 Oct 2010 17:40:30 +0000 (13:40 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Tue, 26 Oct 2010 17:40:30 +0000 (13:40 -0400)
metaform.php
metaform/template.html
template.php

index fdd1ad3..a9d3956 100644 (file)
@@ -230,96 +230,98 @@ function find_always_field($fields) {
 
 # pass false if you want to exclude the <head> and <body> 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('<!--~form~-->');
+               $tem2->merge($tem);
+               return $tem2->run();
        }
 }
 
index 0e04ef9..cf0ffcb 100644 (file)
                <p><a href="~file_name~?~file_name~_edit_id=~~id attr~~">Edit</a></p>
 
                <table border="0" cellpadding="3" cellspacing="0" summary="">
-<!--~display_row {~--><!--~display_image {~-->
+<!--~display_fields {~--><!--~image {~-->
                        <tr><td class="caption">~caption html~:</td><td><img src="~~~name~ image_src~~" width="~~~name~ image_width~~" height="~~~name~ image_height~~" alt=""></td></tr>
-<!--~}~--><!--~display_yesno {~-->
+<!--~}~--><!--~checkbox {~-->
                        <tr><td class="caption">~caption html~:</td><td>~~~name~ yesno~~</td></tr>
-<!--~}~--><!--~display_date {~-->
+<!--~}~--><!--~date {~-->
                        <tr><td class="caption">~caption html~:</td><td>~~~name~ mmddyyyy~~</td></tr>
-<!--~}~--><!--~display_short {~-->
+<!--~}~--><!--~short {~-->
                        <tr><td class="caption">~caption html~:</td><td>~~~name~ html~~</td></tr>
-<!--~}~--><!--~display_html {~-->
+<!--~}~--><!--~html {~-->
                        <tr><td class="caption">~caption html~:</span></td><td></td></tr>
                        <tr><td colspan="2"><div style="padding: 15px; border: 1px solid black">~~~name~~~</div></td></tr>
-<!--~}~--><!--~display_multiline {~-->
+<!--~}~--><!--~textarea {~-->
                        <tr><td class="caption">~caption html~:</td><td>~~~name~ htmlbrtab~~</td></tr>
 <!--~}~--><!--~}~-->
                </table>
@@ -76,8 +76,9 @@
        <!--~~form {~~--><!--~form {~-->
                <h2><!--~opt_db {~--><!--~~new_msg {~~-->Add a new ~singular~<!--~~}~~--><!--~~edit_msg {~~-->Edit ~singular~ "~~~always_field~ html~~"<!--~~}~~--><!--~}~--><!--~opt_db unset {~-->Submit a ~singular~<!--~}~--></h2>
 
-               <form action="~file_name~" method="post~enctype_attr~"><!--~opt_db {~--><!--~~editing {~~--><div style="display: none"><input type="hidden" name="~file_name~_edit_id" value="~~~file_name~_edit_id attr~~"></div><!--~~}~~--><!--~}~--><!--~uploads {~--><input type="hidden" name="MAX_FILE_SIZE" value="~~upload_max_filesize~~"><!--~}~-->
-<!--~row {~--><!--~image {~-->
+               <form action="~file_name~" method="post~enctype_attr~"><!--~opt_db {~--><!--~~editing {~~--><div style="display: none"><input type="hidden" name="~file_name~_edit_id" value="~~~file_name~_edit_id attr~~"></div><!--~~}~~--><!--~}~--><!--~uploads {~--><input type="hidden" name="MAX_FILE_SIZE" value="~~upload_max_filesize~~"><!--~}~--><!--~hidden_fields {~--><!--~ first {~-->
+                       <div style="display: none"><!--~}~--><input type="hidden" name="~name~" value="~~~name~ attr~~"><!--~ last {~--></div><!--~}~--><!--~}~-->
+<!--~visible_fields {~--><!--~image {~-->
                        <div class="caption">~caption html~</div>
                        <div class="field"><input type="file" name="~name~"><input type="hidden" name="old_~name~" value="~~~name~ attr~~"></div><!--~}~--><!--~file {~-->
                        <div class="caption">~caption html~</div>
                        <p><a href="~file_name~?~file_name~_new=1">[Add a new ~singular~]</a></p>
 
                        <table cellspacing="0" cellpadding="4" border="1" summary="">
-                               <!--~listing_head_col {~--><th>~caption~</th><!--~}~--><th>&nbsp;</th><!--~~rows {~~-->
-                               <tr><!--~listing_row_col {~-->
-                                       <td class="listing"><a href="~file_name~?~file_name~_<!--~opt_display unset {~-->edit_<!--~}~-->id=~~id~~"><!--~listing_value_enc {~-->~~~name~ ~listing_enc~~~<!--~}~--><!--~listing_value_thumb {~--><img src="~~~name~ thumb_src~~" width="~~~name~ thumb_width~~" height="~~~name~ thumb_height~~" alt=""><!--~}~--></a></td><!--~}~-->
+                               <!--~listing_headers {~--><th>~caption~</th><!--~}~--><th>&nbsp;</th><!--~~rows {~~-->
+                               <tr><!--~listing_fields {~-->
+                                       <td class="listing"><a href="~file_name~?~file_name~_<!--~opt_display unset {~-->edit_<!--~}~-->id=~~id~~"><!--~enc {~-->~~~name~ ~enc~~~<!--~}~--><!--~thumb {~--><img src="~~~name~ thumb_src~~" width="~~~name~ thumb_width~~" height="~~~name~ thumb_height~~" alt=""><!--~}~--></a></td><!--~}~-->
                                        <td><a href="~file_name~?~file_name~_delete_id=~~id~~" onclick="return confirm('Permanently delete?')">[delete this ~singular~]</a></td>
                                </tr><!--~~}~~-->
 
index 5643502..cbb9466 100644 (file)
@@ -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