JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: enter captions and optionally field names
authorJason Woofenden <jason@jasonwoof.com>
Mon, 11 Aug 2014 17:20:54 +0000 (13:20 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Mon, 11 Aug 2014 17:20:54 +0000 (13:20 -0400)
encode.php
metaform.php
metaform/main.html
metaform/template.html
metaform/template.php

index 38de080..e79dae2 100644 (file)
@@ -28,10 +28,18 @@ function enc_cap($str) {
        return $str;
 }
 
+# quote for placing between single quotes in php
+function enc_phpsq($str) {
+       $str = str_replace("\\", "\\\\", $str);
+       $str = str_replace("'", "\\'", $str);
+       return $str;
+}
+
 function enc_jsdq($str) {
        $str = enc_sql($str);
        $str = str_replace("\n", "\\n", $str);
-       return str_replace("\r", "\\r", $str);
+       $str = str_replace("\r", "\\r", $str);
+       return $str;
 }
 
 # encode for putting within double-quotes in SQL
index 1cfb70c..411a9b2 100644 (file)
@@ -176,18 +176,62 @@ function get_fields() {
        foreach($fields as $field) {
                $field = trim($field);
                if(substr($field, -1) == '{') {
-                       $name = trim(substr($field, 0, -1)); # FIXME: stop this from getting enc_caption()ed
+                       $caption = trim(substr($field, 0, -1));
+                       $name = format_varname($caption);
                        $type = '{';
                        $options = null;
+               # FIXME restore parsing of option lists for pulldowns
                } else {
-                       list($name, $type, $options) = split('  *', $field);
-                       if($options) $options = explode(',', $options);
-                       if(!$type) $type = $name;
+                       $options = null;
+                       $type = null;
+                       $div = strpos($field, ' ');
+                       if ($div === false) {
+                               $div = strpos($field, "\t");
+                       }
+                       if ($div === false) {
+                               if (isset($GLOBALS['types'][$field])) {
+                                       # if just one word, and it's a type, use it as name/caption and type
+                                       $type = $field;
+                               }
+                       } else {
+                               $first_word = trim(substr($field, 0, $div));
+                               if (isset($GLOBALS['types'][$first_word])) {
+                                       # if the first word (of multiple) is a type, remove it from the name/caption
+                                       $type = $first_word;
+                                       $field = trim(substr($field, $div + 1));
+                               }
+                       }
+                       # see if there's an explicit name/caption splitter
+                       $div = strpos($field, '|');
+                       if ($div !== false) {
+                               $name = trim(substr($field, 0, $div));
+                               $caption = trim(substr($field, $div + 1));
+                               if (isset($GLOBALS['types'][$name])) {
+                                       $type = $name;
+                               } elseif (isset($GLOBALS['types'][strtolower($caption)])) {
+                                       $type = strtolower($caption);
+                               }
+                       } else {
+                               $name = format_varname($field);
+                               $caption = format_caption($field);
+                       }
+
+                       if ($type === null) {
+                               $type = 'textbox';
+                       }
                }
                $input = field_input($type);
                $format = field_format($type);
                $sql = field_sql($type);
-               $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
+               $GLOBALS['gotten_fields'][] = array(
+                       'caption' => $caption,
+                       'name' => $name,
+                       'type' => $type,
+                       'input' => $input,
+                       'format' => $format,
+                       'sql' => $sql,
+                       'options' => $options
+               );
        }
 
        return $GLOBALS['gotten_fields'];
@@ -214,13 +258,12 @@ function make_sql() {
        tem_set_globals($tem);
        $fields = get_fields();
        foreach($fields as $field) {
-               list($name, $type, $input, $format, $sql) = $field;
-               if($sql != 'n/a') {
-                       $tem->set('name', $name);
-                       $tem->set('type', $sql);
-                       if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
+               if($field['sql'] != 'n/a') {
+                       $tem->set('name', $field['name']);
+                       $tem->set('type', $field['sql']);
+                       if(substr($field['sql'], 0, 3) == 'int' || substr($field['sql'], 0, 7) == 'decimal') {
                                $tem->set('default', '0');
-                       } elseif($format == 'yesno') {
+                       } elseif($field['format'] == 'yesno') {
                                $tem->set('default', '"No"');
                        } else {
                                $tem->set('default', '""');
@@ -238,15 +281,14 @@ function view_sql() {
 }
 
 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
-function find_always_field($fields) {
+function find_always_field(&$fields) {
        foreach($fields as $field) {
-               list($name, $type, $input, $format, $sql) = $field;
-               if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio' && $type != '{') {
-                       return $name;
+               if($field['input'] != 'submit' && $field['input'] != 'image' && $field['input'] != 'file' && $field['input'] != 'checkbox' && $field['input'] != 'radio' && $field['type'] != '{' && $field['type'] != '}') {
+                       return $field['name'];
                }
        }
 
-       return false;
+       return 'FIXME';
 }
        
        
@@ -265,48 +307,55 @@ function make_html($whole_file = true) {
        $listing_headers = array();
        $listing_fields = array();
        foreach($fields as $field) {
-               list($name, $type, $input, $format, $sql) = $field;
-               if($input == 'hidden') {
-                       $hidden_fields[] = array('name' => $name);
+               if($field['input'] == 'hidden') {
+                       $hidden_fields[] = array('name' => $field['name']);
                } else {
-                       $visible_fields[] = array($input => array(
-                               'name' => $name,
-                               'caption' => format_caption($name)));
+                       $visible_fields[] = array(
+                               $field['input'] => array(
+                                       'name' => $field['name'],
+                                       'caption' => $field['caption']
+                               )
+                       );
                }
 
-               if($input == 'image' || $input == 'file') {
+               if($field['input'] == 'image' || $field['input'] == 'file') {
                        $tem->set('uploads');
                        $tem->set('enctype_attr', '" enctype="multipart/form-data');
-               } elseif($input == 'html') {
+               } elseif($field['input'] == 'html') {
                        $has_html_editors = true;
-                       $tem->set('html_field_name', $name);
+                       $tem->set('html_field_name', $field['name']);
                        $tem->set('replace_textarea');
                }
 
-               switch($input) {
+               switch($field['input']) {
                        case 'image':
                        case 'checkbox':
                        case 'date':
                        case 'textarea':
                        case 'html':
-                               $display_type = $input;
+                               $display_type = $field['input'];
                        break;
                        default:
                                $display_type = 'short';
                }
-               if($format != 'n/a') {
+               if($field['format'] != 'n/a') {
                        $display_fields[] = array($display_type => array(
-                               'name' => $name, 'caption' => format_caption($name)));
+                               'name' => $field['name'], 'caption' => $field['caption']));
                }
 
-               if(show_in_listing($type, $input, $format, $sql)) {
-                       $listing_headers[] = array('caption' => format_caption($name), 'name' => $name);
-                       $listing_field = array('name' => $name);
-                       if($format == 'bool' || $format == 'yesno') {
+               if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
+                       $listing_headers[] = array(
+                               'caption' => $field['caption'],
+                               'name' => $field['name']
+                       );
+                       $listing_field = array(
+                               'name' => $field['name']
+                       );
+                       if($field['format'] == 'bool' || $field['format'] == 'yesno') {
                                $listing_field['enc'] = 'yesno';
-                       } elseif($input == 'date') {
+                       } elseif($field['input'] == 'date') {
                                $listing_field['enc'] = 'mmddyyyy';
-                       } elseif($type == 'thumb') {
+                       } elseif($field['type'] == 'thumb') {
                                $listing_field['thumb'] = true;
                        } else {
                                $listing_field['enc'] = 'html';
@@ -400,18 +449,19 @@ function make_php() {
        $db_fields = '';
        $always_field = find_always_field($fields);
        $image_included_yet = false;
+       $name_to_caption = array();
        foreach($fields as $field) {
-               list($name, $type, $input, $format, $sql, $options) = $field;
-               if($input != 'submit') {
-                       $tem->set('format', $format);
-                       $tem->set('name', $name);
+               $name_to_caption[] = array('name' => $field['name'], 'caption' => $field['caption']);
+               if($field['input'] != 'submit') {
+                       $tem->set('format', $field['format']);
+                       $tem->set('name', $field['name']);
                        $tem->set('db_field', ''); # we don't want to use the value from last time
-                       if($sql != 'n/a') {
+                       if($field['sql'] != 'n/a') {
                                if($db_fields != '') $db_fields .= ',';
-                               $db_fields .= $name;
+                               $db_fields .= $field['name'];
                        }
-                       if($input == 'image') {
-                               if($type == 'thumb') {
+                       if($field['input'] == 'image') {
+                               if($field['type'] == 'thumb') {
                                        $tem->show('thumb_settings');
                                        $tem->show('thumb_upload_params');
                                        $tem->show('thumb_w_h');
@@ -419,33 +469,36 @@ function make_php() {
                                $tem->show('image_settings');
                                $tem->show('image_upload');
                                $has_uploads = true;
-                       } else if($input == 'file') {
+                       } else if($field['input'] == 'file') {
                                $tem->show('file_settings');
                                $tem->show('file_upload');
                                $has_uploads = true;
                        } else {
-                               if($input == 'html') {
+                               if($field['input'] == 'html') {
                                        $has_html_editors = true;
-                               } elseif($input == 'pulldown' || $input == 'radio') {
-                                       $pulldown_options = pulldown_options_array($options);
+                               } elseif($field['input'] == 'pulldown' || $field['input'] == 'radio') {
+                                       $pulldown_options = pulldown_options_array($field['options']);
                                        $tem->set('pulldown_options', $pulldown_options);
                                        $tem->set('has_pulldowns');
                                        $tem->show('pulldowns');
                                        $tem->show('pulldown_format_extra');
                                }
-                               if($format != 'n/a') {
+                               if($field['format'] != 'n/a') {
                                        $tem->show('formats');
                                }
                        }
                }
 
                if($GLOBALS['opt_listing']) {
-                       if(show_in_listing($type, $input, $format, $sql)) {
+                       if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
                                $tem->show('listing_fields_1');
                                $tem->show('listing_fields_2');
                        }
                }
        }
+
+       $tem->set('name_to_caption', $name_to_caption);
+
        if($has_uploads) {
                $tem->show('uploads_include');
                $tem->show('upload_max');
@@ -491,12 +544,11 @@ function make_email() {
        tem_set_globals($tem);
        $fields = get_fields();
        foreach($fields as $field) {
-               list($name, $type, $input, $format, $sql) = $field;
-               $tem->set('name', $name);
-               $tem->set('caption', format_caption($name));
-               if($type == 'textarea') {
+               $tem->set('name', $field['name']);
+               $tem->set('caption', $field['caption']);
+               if($field['type'] == 'textarea') {
                        $tem->show('multi_line');
-               } elseif($type == 'checkbox') {
+               } elseif($field['type'] == 'checkbox') {
                        $tem->show('checkbox');
                } else {
                        $tem->show('normal');
@@ -528,9 +580,8 @@ function preview() {
        }
        $fields = get_fields();
        foreach($fields as $field) {
-               list($name, $type, $input, $format, $sql, $options) = $field;
-               if($type == 'pulldown' || $type == 'radio') {
-                       pulldown($name, eval('return ' . pulldown_options_array($options) . ';'));
+               if($field['type'] == 'pulldown' || $field['type'] == 'radio') {
+                       pulldown($field['name'], eval('return ' . pulldown_options_array($field['options']) . ';'));
                }
        }
        $preview = $preview_tem->run();
index c0057c2..0103f65 100644 (file)
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="opt_public_form~opt_public_form checked~" value="Yes"> But with publicly accessible submission form<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="opt_public_display~opt_public_display checked~" value="Yes"> But with publicly accessible view page.</p>
 
-       <p>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: <!--~types {~--><!--~types_sep {~-->, <!--~}~-->~type html~<!--~}~-->.</p>
+       <p>Below, specify the fields you'd like in your form, one field per line. You can (optional) add a field type at the beginning of the line. The following field types are available: <!--~types {~--><!--~types_sep {~-->, <!--~}~-->~type html~<!--~}~-->.</p>
 
        <p>You can start a labeled fieldset by putting a <code>{</code> at the end of a line with the caption. Then end the fieldset with a <code>}</code> on a line by itself</p>
 
        <p>Example:<br><code><pre>
-email     textbox
+email
 contact info (all required) {
-name      textbox
-phone     phone
+  name
+  phone
 }
-comments  textarea
-color     pulldown Red,Green,Blue
-agree     yesno
+textarea comments
+yesno agree
 </pre></code></p>
 
     <p>Fields: 
index f0adb15..bdd5a01 100644 (file)
@@ -78,7 +78,7 @@
                <form action="~opt_public_form {~~~$basename~~~}~~opt_public_form unset {~~file_name attr~~}~" method="post~enctype_attr~"><!--~opt_db {~--><!--~~id {~~--><div style="display: none"><input type="hidden" name="edit_id" value="~~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 {~--><!--~fieldset {~-->
-                       <fieldset><legend>~caption html~</legend><!--~}~--><!--~end_fieldset {~-->
+                       <fieldset id="~name attr~"><legend>~caption html~</legend><!--~}~--><!--~end_fieldset {~-->
                        </fieldset><!--~}~--><!--~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 {~-->
index 23e2248..49e6b74 100644 (file)
@@ -36,6 +36,10 @@ require_once('code/wfpl/format.php');
 require_once('code/wfpl/email.php');~uploads_include {~
 require_once('code/wfpl/upload.php');~}~
 
+$GLOBALS['~file_name~_field_to_caption'] = array(~name_to_caption {~
+       '~name~' => '~caption phpsq~'~ sep {~,~}~~}~
+);
+
 function ~file_name~_get_fields() {
        $data = array();
 ~formats {~
@@ -146,7 +150,16 @@ function ~file_name~_main_delete($id) {
 function ~file_name~_csv_download() {
        require_once('code/wfpl/csv.php');
        $rows = db_get_rows('~table_name~', 'id,'.~file_name upper~_DB_FIELDS, 'order by id');
-       array_unshift($rows, explode(',', 'id,'.~file_name upper~_DB_FIELDS));
+       $fields = explode(',', 'id,'.~file_name upper~_DB_FIELDS);
+       $header = array();
+       foreach ($fields as $field) {
+               if (isset($GLOBALS['~file_name~_field_to_caption'][$field])) {
+                       $header[] = $GLOBALS['~file_name~_field_to_caption'][$field];
+               } else {
+                       $header[] = $field;
+               }
+       }
+       array_unshift($rows, $header);
        array2d_to_csv_download($rows, '~file_name~.csv');
 }