JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added tar.php. metaform working
authorJason Woofenden <jason183@herkamire.com>
Mon, 11 Dec 2006 21:12:35 +0000 (16:12 -0500)
committerJason Woofenden <jason183@herkamire.com>
Mon, 11 Dec 2006 21:12:35 +0000 (16:12 -0500)
metaform.php
metaform/main.html
metaform/preview.html [new file with mode: 0644]
metaform/template.html
metaform/template.php
tar.php [new file with mode: 0644]
test/tar_test.php [new file with mode: 0644]

index c20515e..74ec74f 100644 (file)
@@ -1,6 +1,29 @@
 <?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 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');
 
 # see code/wfpl/metaform/template.html for the html templates for these elements
 $GLOBALS['types'] = array(
@@ -14,38 +37,50 @@ $GLOBALS['types'] = array(
        'url' =>        array('textbox',     'url',        'varchar(200)'),
        'textarea' =>   array('textarea',    'unix',       'text'),
        'pulldown' =>   array('pulldown',    'options',    'int'),
-       'checkbox' =>   array('checkbox',    'yesno',   'int'),
-       'yesno' =>      array('checkbox',    'yesno',   'int'),
+       'checkbox' =>   array('checkbox',    'yesno',      'int'),
+       'yesno' =>      array('checkbox',    'yesno',      'int'),
        'submit' =>     array('submit',      'oneline',    'n/a')
 );
 
 if(isset($_REQUEST['form_name'])) {
-       $GLOBALS['form_name'] = $_REQUEST['form_name'];
+       $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
 } else {
        $GLOBALS['form_name'] = 'some_form';
 }
 
 if(isset($_REQUEST['fields'])) {
-       if(isset($_REQUEST['download_sql'])) {
-               download_sql();
+       if(isset($_REQUEST['view_sql'])) {
+               view_sql();
                exit();
-       } elseif(isset($_REQUEST['download_php'])) {
-               download_php();
+       } elseif(isset($_REQUEST['view_php'])) {
+               view_php();
                exit();
-       } elseif(isset($_REQUEST['download_template'])) {
-               download_template();
+       } elseif(isset($_REQUEST['view_template'])) {
+               view_template();
                exit();
-       } elseif(isset($_REQUEST['download_email'])) {
-               download_email();
+       } elseif(isset($_REQUEST['view_email'])) {
+               view_email();
                exit();
-       } else {
-               tem_set('message', "Sorry... couldn't tell which button you pressed");
+       } 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");
        }
-} else {
-       tem_output('code/wfpl/metaform/main.html');
 }
 
+set_form_action();
+tem_output('code/wfpl/metaform/main.html');
+exit();
+
+
 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
 function field_format($type) { return $GLOBALS['types'][$type][1]; }
 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
@@ -67,97 +102,151 @@ function get_fields() {
        return $ret;
 }
 
-function download_headers() {
-       header('Content-type: application/octet-stream');
-       header('Content-disposition: download'); # is this correct? does it do anything?
+function set_form_action() {
+       $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
+       if($action == '') $action = './';
+       tem_set('form_action', $action);
+}
+
+# perfect HTTP headers for viewing created files
+function view_headers() {
+       header('Content-type: text/plain');
 }
        
 
 
 
-function download_sql() {
-       tem_load('code/wfpl/metaform/template.sql');
-       tem_set('form_name', $GLOBALS['form_name']);
+function make_sql() {
+       $tem = new tem();
+       $tem->load('code/wfpl/metaform/template.sql');
+       $tem->set('form_name', $GLOBALS['form_name']);
        $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);
+                       $tem->set('name', $name);
+                       $tem->set('type', $sql);
                        if($sql == 'int') {
-                               tem_set('default', '0');
+                               $tem->set('default', '0');
                        } else {
-                               tem_set('default', '""');
+                               $tem->set('default', '""');
                        }
-                       tem_sub('column');
+                       $tem->sub('column');
                }
        }
-       download_headers();
-       tem_output();
+       view_headers();
+       return $tem->run();
+}
+
+function view_sql() {
+       view_headers();
+       echo make_sql();
 }
        
 
-function download_template() {
-       tem_load('code/wfpl/metaform/template.html');
-       tem_set('form_name', $GLOBALS['form_name']);
+# pass false if you want to exclude the <head> and <body> tag etc.
+function make_template($whole_file = true) {
+       $tem = new tem();
+       $tem->load('code/wfpl/metaform/template.html');
+       $tem->set('form_name', $GLOBALS['form_name']);
        $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($input);
+               $tem->set('name', $name);
+               $tem->set('caption', $name); # fixme
+               $tem->sub($input);
        }
-       tem_set('name', 'save');
-       tem_set('caption', 'Save');
-       tem_sub('submit');
-       download_headers();
-       tem_output();
+       $tem->set('name', 'save');
+       $tem->set('caption', 'Save');
+       $tem->sub('submit');
+       $tem->sub('body');
+       if($whole_file) {
+               return $tem->run();
+       } else {
+               return $tem->get('body');
+       }
+}
+
+function view_template() {
+       view_headers();
+       echo make_template();
 }
 
 
-function download_php() {
-       tem_load('code/wfpl/metaform/template.php');
-       tem_set('form_name', $GLOBALS['form_name']);
+function make_php() {
+       $tem = new tem();
+       $tem->load('code/wfpl/metaform/template.php');
+       $tem->set('form_name', $GLOBALS['form_name']);
        $fields = get_fields();
        $db_fields = '';
        $always_field = false;
        foreach($fields as $field) {
                list($name, $type, $input, $format, $sql) = $field;
                if($input != 'submit') {
-                       tem_set('format', $format);
-                       tem_set('name', $name);
-                       tem_set('db_field', ''); # we don't want to use the value from last time
+                       $tem->set('format', $format);
+                       $tem->set('name', $name);
+                       $tem->set('db_field', ''); # we don't want to use the value from last time
                        if($sql != 'n/a') {
-                               tem_sub('db_field');
+                               $tem->sub('db_field');
                                if($db_fields != '') $db_fields .= ',';
                                $db_fields .= $name;
                        }
-                       tem_sub('formats');
+                       $tem->sub('formats');
                        if(!$always_field and $input != 'checkbox' and $input != 'radio') {
                                $always_field = $name;
                        }
                }
        }
        # 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('always_field', $always_field);
-       tem_set('db_fields', $db_fields);
-       download_headers();
-       tem_output();
+       $tem->set('always_field', $always_field);
+       $tem->set('db_fields', $db_fields);
+       return $tem->run();
+}
+
+function view_php() {
+       view_headers();
+       echo make_php();
 }
 
 
-function download_email() {
-       tem_load('code/wfpl/metaform/template.email.txt');
-       tem_set('form_name', $GLOBALS['form_name']);
+function make_email() {
+       $tem = new tem();
+       $tem->load('code/wfpl/metaform/template.email.txt');
+       $tem->set('form_name', $GLOBALS['form_name']);
        $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', $name);
+               $tem->set('caption', $name); # fixme
+               $tem->sub('fields');
        }
-       download_headers();
-       tem_output();
+       return $tem->run();
+}
+
+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->set('preview', make_template(false));
+       set_form_action();
+       $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);
 }
 
 ?>
index 64c09d4..e8ab716 100644 (file)
@@ -6,19 +6,23 @@
 </head>
 
 <body>
-  <form action="metaform.php" method="get">
-    <p>Form name: <input type="text" name="form_name" value="~form_name~" /></p>
+  <h1>Meta Form</h1>
+  <p>This site will write code for you (sql code, php code, html template, email template) to handle a form.</p>
+  <form action="~metaform_name~" method="get">
+    <p>The form name should not contain any capitals or symbols (besides underscore)</p>
 
-    <p>Fields: 
-    <textarea name="fields">~fields.html~</textarea></p>
-
-    <p><input type="submit" name="download_sql" value="Download SQL" /></p>
+    <p>Form name: <input type="text" name="form_name" value="~form_name.attr~" /></p>
 
-    <p><input type="submit" name="download_php" value="Download PHP" /></p>
-
-    <p><input type="submit" name="download_template" value="Download template" /></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: name textbox email phone money dollars url textarea checkbox yesno submit.</p>
+       <p>Example:<br /><pre>
+name      textbox
+phone     phone
+comments  textarea
+agree     yesno</pre></p>
+    <p>Fields: 
+    <textarea rows="10" cols="40" name="fields">~fields.html~</textarea></p>
 
-    <p><input type="submit" name="download_email" value="Download e-mail" /></p>
+    <p><input type="submit" name="preview" value="Continue" /></p>
   </form>
 </body>
 </html>
diff --git a/metaform/preview.html b/metaform/preview.html
new file mode 100644 (file)
index 0000000..041cfd8
--- /dev/null
@@ -0,0 +1,28 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+  <title>Meta Form</title>
+</head>
+
+<body>
+  <h1>Preview</h1>
+  <div style="border: 2px solid black; padding: 13px">
+~preview~
+  </div>
+    <p><form action="~form_name~.tgz" method="get"><input type="hidden" name="form_name" value="~form_name.attr~" /><input type="hidden" name="fields" value="~fields.attr~" /><input type="submit" name="download_tar" value="Download tar ball" /></form></p>
+
+  <form action="~metaform_name~" method="get">
+       <p><input type="hidden" name="form_name" value="~form_name.attr~" /><input type="hidden" name="fields" value="~fields.attr~" /><input type="submit" name="edit" value="Back to editing" /></p>
+
+    <p><input type="submit" name="view_sql" value="View SQL" /></p>
+
+    <p><input type="submit" name="view_php" value="View PHP" /></p>
+
+    <p><input type="submit" name="view_template" value="View template" /></p>
+
+    <p><input type="submit" name="view_email" value="View e-mail" /></p>
+
+  </form>
+</body>
+</html>
index ca0d271..426be60 100644 (file)
@@ -6,6 +6,7 @@
 </head>
 
 <body>
+<!--~body start~-->
   <h2>~form_name~ entry form</h2>
 
   <form action="~form_name~.php" method="post">
@@ -16,5 +17,6 @@
 <!--~end~--><!--~submit start~-->      <tr><td class="submit_row" colspan="2"><input type="submit" name="~name~" value="~caption.attr~" /></td></tr>
 <!--~end~-->    </table>
   </form>
+<!--~end~-->
 </body>
 </html>
index a3266de..06a184b 100644 (file)
@@ -1,5 +1,8 @@
 <?php
 
+# This form requires wfpl. See: http://jasonwoof.org/wfpl
+
+if(!file_exists('code/wfpl/template.php')) { die('This form requires <a href="http://jasonwoof.org/wfpl">wfpl</a>.'); }
 require_once('code/wfpl/template.php');
 require_once('code/wfpl/format.php');
 #require_once('code/wfpl/db.php'); # fixme
diff --git a/tar.php b/tar.php
new file mode 100644 (file)
index 0000000..cf934d1
--- /dev/null
+++ b/tar.php
@@ -0,0 +1,65 @@
+<?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 file is for making a tar archive out of some strings you pass. See
+# make_tar()
+
+
+# create a file (including contents)
+function write_file($name, $data) {
+       $fd = fopen($name, 'w');
+       if($fd === false) {
+               die("Failed to open file: '$name' for writing");
+       }
+       $temp = fwrite($fd, $data);
+       fclose($fd);
+       return $temp;
+}
+
+
+# create (and output) a tar archive. Don't put in any symbols in filenames.
+#
+# parameters:
+#    $dirname: the name of the tar file (sans "tgz"). Also the name of the directory within.
+#    $files: a hash. The keys are the filenames, the values the file data
+function make_tar($dirname, $files) {
+       $tmpdir = '/tmp/make_tar';
+       $dirname = ereg_replace('[^a-z0-9_-]', '', $dirname);
+       if($dirname == '') $dirname = 'foo';
+       if(!file_exists($tmpdir)) {
+               mkdir($tmpdir);
+       }
+       mkdir("$tmpdir/$dirname");
+       foreach($files as $filename => $file_data) {
+               $filename_fixed = ereg_replace('[^a-zA-Z0-9_.-]', '', $filename);
+               if($filename != $filename_fixed) {
+                       die("Invalid filename for tar archive");
+               }
+               write_file("$tmpdir/$dirname/$filename", $file_data);
+       }
+       header("Content-type: application/x-gzip");
+       passthru("tar -C $tmpdir -czf - $dirname/");
+       foreach($files as $filename => $file_data) {
+               unlink("$tmpdir/$dirname/$filename");
+       }
+       rmdir("$tmpdir/$dirname");
+}
diff --git a/test/tar_test.php b/test/tar_test.php
new file mode 100644 (file)
index 0000000..59cdf99
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+require_once('code/wfpl/tar.php');
+
+# Unfortunately, I don't know how to set the suggested filename for downloads,
+# so you'll have to deal with that yourself.
+make_tar('foo', array(
+       'foo.txt' => 'foo two three four',
+       'README' => 'this is a test...',
+       'Makefile' => 'all: or_nothing'));
+
+?>