JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / metaform.php
index 9fe48c9..e31a9a4 100644 (file)
 <?php
 
-#  Copyright (C) 2006 Jason Woofenden
-#
-#  This file is part of wfpl.
-#
-#  wfpl is free software; you can redistribute it and/or modify it
-#  under the terms of the GNU General Public License as published by
-#  the Free Software Foundation; either version 2, or (at your option)
-#  any later version.
-#
-#  wfpl is distributed in the hope that it will be useful, but
-#  WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-#  General Public License for more details.
-#
-#  You should have received a copy of the GNU General Public License
-#  along with wfpl; see the file COPYING.  If not, write to the
-#  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-#  MA 02111-1307, USA.
+# This program is in the public domain within the United States. Additionally,
+# we waive copyright and related rights in the work worldwide through the CC0
+# 1.0 Universal public domain dedication, which can be found at
+# http://creativecommons.org/publicdomain/zero/1.0/
 
 
 # This file writes the code for you (sql, php, html, email) to handle a form.
 
-require_once('code/wfpl/template.php');
-require_once('code/wfpl/tar.php');
+require_once(__DIR__.'/'.'template.php');
+require_once(__DIR__.'/'.'http.php');
+require_once(__DIR__.'/'.'tar.php');
+require_once(__DIR__.'/'.'format.php');
 
-# see code/wfpl/metaform/template.html for the html templates for these elements
+# see wfpl/metaform/template.html for the html templates for these elements
 $GLOBALS['types'] = array(
 #    type                  input          format        sql     
-       'name' =>       array('textbox',     'oneline',    'varchar(200)'),
-       'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
-       'int' =>        array('textbox',     'int',        'varchar(100)'),
-       'email' =>      array('textbox',     'email',      'varchar(100)'),
-       'phone' =>      array('textbox',     'phone',      'varchar(32)'),
-       'money' =>      array('textbox',     'money',      'varchar(32)'),
-       'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
-       'url' =>        array('textbox',     'url',        'varchar(200)'),
-       'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
-       'password' =>   array('password',    'oneline',    'varchar(200)'),
-       'textarea' =>   array('textarea',    'unix',       'text'),
-       'pulldown' =>   array('pulldown',    'options',    'int'),
-       'checkbox' =>   array('checkbox',    'yesno',      'varchar(3)'),
-       'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
-       'submit' =>     array('submit',      'oneline',    'n/a')
+       'varname' =>    array('textbox',     'varname',    'varchar(50) binary'),
+       'name' =>       array('textbox',     'oneline',    'varchar(200) binary'),
+       'textbox' =>    array('textbox',     'oneline',    'varchar(200) binary'),
+       'int' =>        array('textbox',     'int',        'int'),
+       'decimal' =>    array('textbox',     'decimal',    'decimal(12,2)'),
+       'bigint' =>     array('textbox',     'int',        'varchar(100) binary'), # up to 100 digits, stored as a string
+       'zip' =>        array('textbox',     'zip',        'varchar(20) binary'),
+       'email' =>      array('email',       'email',      'varchar(100) binary'),
+       'search' =>     array('search',      'oneline',    'varchar(200) binary'),
+       'phone' =>      array('textbox',     'phone',      'varchar(32) binary'),
+       'state' =>      array('states',      'oneline',    'varchar(2) binary'),
+       'money' =>      array('textbox',     'money',      'varchar(32) binary'),
+       'date' =>       array('date',        'mdy_to_ymd', 'char(10) binary'),
+       'dollars' =>    array('textbox',     'dollars',    'varchar(32) binary'),
+       'url' =>        array('textbox',     'url',        'varchar(200) binary'),
+       'hidden' =>     array('hidden',      'unix',       'varchar(200) binary'),
+       'password' =>   array('password',    'oneline',    'varchar(200) binary'),
+       'textarea' =>   array('textarea',    'unix',       'text binary'),
+       'html' =>       array('html',        'unix',       'mediumtext binary'),
+       'pulldown' =>   array('pulldown',    'options',    'varchar(100) binary'),
+       'radio' =>      array('radio',       'options',    'varchar(100) binary'),
+       'checkbox' =>   array('checkbox',    'bool',       'int(1)'),
+       'yesno' =>      array('checkbox',    'yesno',      'varchar(3) binary'),
+       'delete' =>     array('checkbox',    'yesno',      'n/a'),
+       'image' =>      array('image',       'oneline',    'varchar(120) binary'),
+       'thumb' =>      array('image',       'oneline',    'varchar(240) binary'),
+       'file' =>       array('file',        'oneline',    'varchar(100) binary'),
+       'submit' =>     array('submit',      'n/a',        'n/a'),
+       '{' =>          array('fieldset',    'n/a',        'n/a'),
+       '}' =>          array('end_fieldset','n/a',        'n/a')
 );
 
-if(isset($_REQUEST['form_name'])) {
-       $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
-} else {
-       $GLOBALS['form_name'] = 'some_form';
+function list_available_types() {
+       ksort($GLOBALS['types']);
+       foreach($GLOBALS['types'] as $key => $value) {
+               tem_set('type', $key);
+               tem_show('types');
+               tem_show('types_sep');
+       }
 }
 
-if(isset($_REQUEST['fields'])) {
-       if(isset($_REQUEST['view_sql'])) {
-               view_sql();
-               exit();
-       } elseif(isset($_REQUEST['view_php'])) {
-               view_php();
-               exit();
-       } elseif(isset($_REQUEST['view_template'])) {
-               view_template();
-               exit();
-       } elseif(isset($_REQUEST['view_email'])) {
-               view_email();
-               exit();
-       } elseif(isset($_REQUEST['download_tar'])) {
-               download_tar();
-               exit();
-       } elseif(isset($_REQUEST['preview'])) {
-               preview();
-               exit();
-       } elseif(isset($_REQUEST['edit'])) {
-               tem_set('fields', $_REQUEST['fields']);
-               tem_set('form_name', $GLOBALS['form_name']);
-               # fall through
-       } else {
-               die("Sorry... couldn't tell which button you pressed");
+
+function tem_set_globals(&$tem) {
+       $vars = array(
+               'file_name',
+               'public_file_name',
+               'table_name',
+               'plural',
+               'singular');
+       foreach($vars as $var) {
+               $tem->set($var, $GLOBALS[$var]);
+       }
+
+       $bools = array(
+               'opt_email',
+               'opt_db',
+               'opt_listing',
+               'opt_display',
+               'opt_pass',
+               'opt_public_form',
+               'opt_public_display',
+               'opt_public_something');
+       foreach($bools as $bool) {
+               if(isset($GLOBALS[$bool]) && $GLOBALS[$bool]) {
+                       $tem->set($bool);
+               }
        }
 }
 
-set_form_action();
-tem_output('code/wfpl/metaform/main.html');
-exit();
+function metaform_main() {
+       if(isset($_REQUEST['singular'])) {
+               $GLOBALS['file_name'] = format_varname(_REQUEST_cut('file_name'));
+               $GLOBALS['table_name'] = format_varname(_REQUEST_cut('table_name'));
+               $GLOBALS['plural'] = format_oneline(_REQUEST_cut('plural'));
+               # backwards compatibility:
+               if(isset($_REQUEST['form_name'])) {
+                       $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname(_REQUEST_cut('form_name'));
+               }
+
+               $GLOBALS['singular'] = format_oneline(_REQUEST_cut('singular'));
+               $GLOBALS['opt_email'] = format_bool(_REQUEST_cut('opt_email'));
+               $GLOBALS['opt_db'] = format_bool(_REQUEST_cut('opt_db'));
+               $GLOBALS['opt_listing'] = format_bool(_REQUEST_cut('opt_listing'));
+               $GLOBALS['opt_display'] = format_bool(_REQUEST_cut('opt_display'));
+               $GLOBALS['opt_pass'] = format_bool(_REQUEST_cut('opt_pass'));
+               $GLOBALS['opt_public_form'] = format_bool(_REQUEST_cut('opt_public_form'));
+               $GLOBALS['opt_public_display'] = format_bool(_REQUEST_cut('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;
+               } else {
+                       $GLOBALS['opt_public_something'] = 0;
+               }
+
+               tem_init();
+               tem_set_globals($GLOBALS['wfpl_template']);
+       }
+
+       if(isset($_REQUEST['fields'])) {
+               if(isset($_REQUEST['view_sql'])) {
+                       view_sql();
+                       exit();
+               } elseif(isset($_REQUEST['view_php'])) {
+                       view_php();
+                       exit();
+               } elseif(isset($_REQUEST['view_html'])) {
+                       view_html();
+                       exit();
+               } elseif(isset($_REQUEST['view_email'])) {
+                       view_email();
+                       exit();
+               } elseif(isset($_REQUEST['download_tar'])) {
+                       download_tar();
+                       exit();
+               } elseif(isset($_REQUEST['preview'])) {
+                       preview();
+                       exit();
+               } elseif(isset($_REQUEST['edit'])) {
+                       tem_set('fields', $_REQUEST['fields']);
+                       # fall through
+               } else {
+                       die("Sorry... couldn't tell which button you pressed");
+               }
+       }
+
+
+       set_form_action();
+       tem_load(__DIR__.'/'.'metaform/main.html');
+       list_available_types();
+       tem_output();
+       exit(); # in case we're being called by wfpl_main or something
+}
 
 
 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
@@ -89,24 +157,93 @@ function field_format($type) { return $GLOBALS['types'][$type][1]; }
 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
 
 function get_fields() {
+       # no sense in doing all this so many times
+       if(isset($GLOBALS['gotten_fields'])) {
+               return $GLOBALS['gotten_fields'];
+       }
+
        $fields_str = unix_newlines($_REQUEST['fields']);
-       $ret = array();
+       $GLOBALS['gotten_fields'] = array();
        $fields_str = rtrim($fields_str);
-       $fields = split("\n", $fields_str);
+       $fields = explode("\n", $fields_str);
        foreach($fields as $field) {
-               list($name, $type, $options) = split('  *', $field);
-               if($options) $options = split(',', $options);
-               if(!$type) $type = $name;
+               $first_char = substr($field, 0, 1);
+               $field = trim($field);
+               if ($first_char === ' ' || $first_char === "\t") {
+                       $i = count($GLOBALS['gotten_fields']);
+                       if($i > 0) {
+                               $i -= 1;
+                               if (!$GLOBALS['gotten_fields'][$i]['options']) {
+                                       $GLOBALS['gotten_fields'][$i]['options'] = array();
+                               }
+                               $GLOBALS['gotten_fields'][$i]['options'][] = $field;
+                               continue;
+                       }
+               }
+               if(substr($field, -1) == '{') {
+                       $caption = trim(substr($field, 0, -1));
+                       $name = format_varname($caption);
+                       $type = '{';
+                       $options = null;
+               } else {
+                       $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);
-               $ret[] = 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 $ret;
+
+       return $GLOBALS['gotten_fields'];
 }
 
+# this one, that you're using to create forms
 function set_form_action() {
-       $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
+       $action = preg_replace('|.*/|', '', $_SERVER['REQUEST_URI']);
        if($action == '') $action = './';
        tem_set('form_action', $action);
 }
@@ -121,20 +258,21 @@ function view_headers() {
 
 function make_sql() {
        $tem = new tem();
-       $tem->load('code/wfpl/metaform/template.sql');
-       $tem->set('form_name', $GLOBALS['form_name']);
+       $tem->load(__DIR__.'/'.'metaform/template.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($sql == 'int') {
+               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($field['format'] == 'yesno') {
+                               $tem->set('default', '"No"');
                        } else {
                                $tem->set('default', '""');
                        }
-                       $tem->sub('column');
+                       $tem->show('column');
                }
        }
        view_headers();
@@ -145,122 +283,355 @@ function view_sql() {
        view_headers();
        echo make_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) {
+       foreach($fields as $field) {
+               if($field['input'] != 'submit' && $field['input'] != 'image' && $field['input'] != 'file' && $field['input'] != 'checkbox' && $field['input'] != 'radio' && $field['type'] != '{' && $field['type'] != '}') {
+                       return $field['name'];
+               }
+       }
+
+       return 'FIXME';
+}
+       
        
 
 # pass false if you want to exclude the <head> and <body> tag etc.
-function make_template($whole_file = true) {
+function make_html($whole_file = true) {
+       $has_html_editors = false;
        $tem = new tem();
-       $tem->load('code/wfpl/metaform/template.html');
-       $tem->set('form_name', $GLOBALS['form_name']);
+       $tem->load(__DIR__.'/'.'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', $name); # fixme
-               $tem->sub($input);
-               if($input != 'hidden') {
-                       $tem->sub('row');
+               if($field['input'] == 'hidden') {
+                       $hidden_fields[] = array('name' => $field['name']);
+               } else {
+                       $visible_fields[] = array(
+                               $field['input'] => array(
+                                       'name' => $field['name'],
+                                       'caption' => $field['caption']
+                               )
+                       );
+                       if($field['type'] == 'radio') {
+                               $i = 0;
+                               $opts = array();
+                               foreach ($field['options'] as $row) {
+                                       if (is_array($row)) {
+                                               $cap = $row[1];
+                                       } else {
+                                               $cap = $row;
+                                       }
+                                       $opts[] = array('i' => $i, 'option_caption' => $cap);
+                                       $i += 1;
+                               }
+                               $visible_fields[count($visible_fields) - 1]['options'] = $opts;
+                       }
+               }
+
+               if($field['input'] == 'image' || $field['input'] == 'file') {
+                       $tem->set('uploads');
+                       $tem->set('enctype_attr', '" enctype="multipart/form-data');
+               } elseif($field['input'] == 'html') {
+                       $has_html_editors = true;
+                       $tem->set('html_field_name', $field['name']);
+                       $tem->set('replace_textarea');
+               }
+
+               switch($field['input']) {
+                       case 'image':
+                       case 'checkbox':
+                       case 'date':
+                       case 'textarea':
+                       case 'html':
+                               $display_type = $field['input'];
+                       break;
+                       default:
+                               $display_type = 'short';
+               }
+               if($field['format'] != 'n/a') {
+                       $display_fields[] = array($display_type => array(
+                               'name' => $field['name'], 'caption' => $field['caption']));
+               }
+
+               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($field['input'] == 'date') {
+                               $listing_field['enc'] = 'mmddyyyy';
+                       } elseif($field['type'] == 'thumb') {
+                               $listing_field['thumb'] = true;
+                       } else {
+                               $listing_field['enc'] = 'html';
+                       }
+                       $listing_fields[] = $listing_field;
                }
        }
-       $tem->set('name', 'save');
-       $tem->set('caption', 'Save');
-       $tem->sub('submit');
-       $tem->sub('row');
-       $tem->sub('form');
+
+       # Submit/Send button
+       if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
+               $visible_fields[] = array('submit' => array(
+                       'name' => 'send',
+                       'caption' => 'Send'));
+       } else {
+               $visible_fields[] = array('submit' => array(
+                       'name' => 'save',
+                       'caption' => 'Save'));
+       }
+
+       $form_fields = array();
+       $form_fields['visible_fields'] = $visible_fields;
+       if($hidden_fields) {
+               $form_fields['hidden_fields'] = $hidden_fields;
+       }
+       $tem->set('form', $form_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->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();
        }
 }
 
-function view_template() {
+function view_html() {
        view_headers();
-       echo make_template();
+       echo make_html();
 }
 
+function show_in_listing($type, $input, $format, $sql) {
+       switch($input) {
+               case 'submit':
+               case 'hidden':
+               case 'password':
+               case 'textarea':
+               case 'html':
+               case 'fieldset':
+               case 'end_fieldset':
+                       return false;
+       }
+       if($type == 'image') {
+               return false;
+       }
+
+       return true;
+}
+
+function pulldown_options_array($options) {
+       if($options) {
+               $pulldown_options = array();
+               foreach($options as $option) {
+                       $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
+                       $pulldown_options[] = "'$option'";
+               }
+               $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
+       } else {
+               $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
+       }
+
+       return $pulldown_options;
+}
 
 function make_php() {
+       $has_html_editors = false;
        $tem = new tem();
-       $tem->load('code/wfpl/metaform/template.php');
-       $tem->set('form_name', $GLOBALS['form_name']);
+       $tem->load(__DIR__.'/'.'metaform/template.php');
+       tem_set_globals($tem);
        $fields = get_fields();
        $db_fields = '';
-       $php_fields = '';
-       $always_field = false;
+       $always_field = find_always_field($fields);
+       $image_included_yet = false;
+       $name_to_caption = array();
+       $has_uploads = false;
        foreach($fields as $field) {
-               list($name, $type, $input, $format, $sql) = $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;
-                               if($php_fields != '') $php_fields .= ', ';
-                               $php_fields .= '$' . $name;
+                               $db_fields .= $field['name'];
                        }
-                       $tem->sub('formats');
-                       $tem->sub('tem_sets');
-                       if(!$always_field and $input != 'checkbox' and $input != 'radio') {
-                               $always_field = $name;
+                       if($field['input'] == 'image') {
+                               if($field['type'] == 'thumb') {
+                                       $tem->show('thumb_settings');
+                                       $tem->show('thumb_upload_params');
+                                       $tem->show('thumb_w_h');
+                               }
+                               $tem->show('image_settings');
+                               $tem->show('image_upload');
+                               $has_uploads = true;
+                       } else if($field['input'] == 'file') {
+                               $tem->show('file_settings');
+                               $tem->show('file_upload');
+                               $has_uploads = true;
+                       } else {
+                               if($field['input'] == 'html') {
+                                       $has_html_editors = true;
+                               } 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($field['format'] != 'n/a') {
+                                       $tem->show('formats');
+                               }
+                       }
+               }
+
+               if($GLOBALS['opt_listing']) {
+                       if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
+                               $tem->show('listing_fields_1');
+                               $tem->show('listing_fields_2');
                        }
                }
        }
-       # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
+
+       $tem->set('name_to_caption', $name_to_caption);
+
+       if($has_uploads) {
+               $tem->show('uploads_include');
+               $tem->show('upload_max');
+               $tem->show('upload_settings');
+               $image_included_yet = true;
+       }
+
+       if($has_html_editors) {
+               $tem->show('show_extra_headers');
+       }
+
        $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_email']) {
+               $this_domain = $_SERVER['HTTP_HOST'];
+               if(substr($this_domain, -2) == '.l') {
+                       $this_domain = substr($this_domain, 0, -1) . 'com';
+               }
+               $tem->set('this_domain', $this_domain);
+       }
        return $tem->run();
 }
 
+# make a URL for the edit page with all the fields filled in
+function edit_url() {
+       $url = this_url();
+       $url = preg_replace('|view_php=[^&]*|', 'edit=yes', $url);
+       $url = preg_replace('|download_tar=[^&]*|', 'edit=yes', $url);
+       $url = preg_replace('|/[a-z0-9_.]*\?|', '/?', $url);
+       return $url;
+}
+
 function view_php() {
        view_headers();
        echo make_php();
 }
 
-
 function make_email() {
        $tem = new tem();
-       $tem->load('code/wfpl/metaform/template.email.txt');
-       $tem->set('form_name', $GLOBALS['form_name']);
+       $tem->load(__DIR__.'/'.'metaform/template.email.txt');
+       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', $name); # fixme
-               $tem->sub('fields');
+               $tem->set('name', $field['name']);
+               $tem->set('caption', $field['caption']);
+               if($field['type'] == 'textarea') {
+                       $tem->show('multi_line');
+               } elseif($field['type'] == 'checkbox') {
+                       $tem->show('checkbox');
+               } else {
+                       $tem->show('normal');
+               }
+               $tem->show('fields');
        }
        return $tem->run();
 }
 
+function make_htaccess() {
+       $tem = new tem();
+       $tem->set('form', $GLOBALS['file_name']);
+       return $tem->run(__DIR__.'/'.'metaform/htaccess');
+}
+
 function view_email() {
        view_headers();
        echo make_email();
 }
 
-
 function preview() {
-       $tem = new tem();
-       $tem->load('code/wfpl/metaform/preview.html');
-       $tem->set('form_name', $GLOBALS['form_name']);
-       $tem->set('fields', $_REQUEST['fields']);
+       tem_load(__DIR__.'/'.'metaform/preview.html');
+       tem_set_globals($GLOBALS['wfpl_template']);
+       tem_set('fields', $_REQUEST['fields']);
        $preview_tem = new tem();
-       $preview = $preview_tem->run(make_template(false));
+       $preview_tem->load_str(make_html(false));
+       if($GLOBALS['opt_db']) {
+               $preview_tem->show('new_msg');
+       }
+       $fields = get_fields();
+       foreach($fields as $field) {
+               if($field['type'] == 'pulldown' || $field['type'] == 'radio') {
+                       pulldown($field['name'], eval('return ' . pulldown_options_array($field['options']) . ';'));
+               }
+       }
+       $preview = $preview_tem->run();
        unset($preview_tem);
-       $tem->set('preview', $preview);
+       $preview = preg_replace('|type="submit"|', 'type="submit" disabled="disabled"', $preview);
+       tem_set('preview', $preview);
+       tem_show('hiddens');
        set_form_action();
-       $tem->output();
+       tem_output();
 }
 
 function download_tar() {
-       $name = $GLOBALS['form_name'];
-       $data = array(
-               "$name.html" => make_template(),
-               "$name.sql" => make_sql(),
-               "$name.email.txt" => make_email(),
-               "$name.php" => make_php());
-       make_tar($name, $data);
+       $admin_name = $GLOBALS['file_name'];
+       $nice_name = $GLOBALS['public_file_name'];
+       $files = array(
+               "README" => "These files are meant to work with wfpl.\n\nSee: http://sametwice.com/wfpl\n",
+               ".htaccess" => make_htaccess(),
+               "config.php" => "<?php\n\n# put your site-wide configuration here\n",
+               "wfpl_main.php" => read_whole_file(__DIR__.'/'.'metaform/wfpl_main.php'),
+               "styl.styl" => read_whole_file(__DIR__.'/'.'metaform/styl.styl'),
+               "template.html" => read_whole_file(__DIR__.'/'.'metaform/site-template.html'),
+               "$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']) {
+               $files["$nice_name.sql"] = make_sql();
+       }
+       if($GLOBALS['opt_email']) {
+               $files["$nice_name.email.txt"] = make_email();
+       }
+       make_tar($nice_name, $files);
 }
-
-?>