3 # Copyright (C) 2006 Jason Woofenden
5 # This file is part of wfpl.
7 # wfpl is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # wfpl is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with wfpl; see the file COPYING. If not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 # This file writes the code for you (sql, php, html, email) to handle a form.
25 require_once('code/wfpl/template.php');
26 require_once('code/wfpl/tar.php');
28 # see code/wfpl/metaform/template.html for the html templates for these elements
29 $GLOBALS['types'] = array(
30 # type input format sql
31 'name' => array('textbox', 'oneline', 'varchar(200)'),
32 'textbox' => array('textbox', 'oneline', 'varchar(200)'),
33 'int' => array('textbox', 'int', 'varchar(100)'),
34 'email' => array('textbox', 'email', 'varchar(100)'),
35 'phone' => array('textbox', 'phone', 'varchar(32)'),
36 'money' => array('textbox', 'money', 'varchar(32)'),
37 'dollars' => array('textbox', 'dollars', 'varchar(32)'),
38 'url' => array('textbox', 'url', 'varchar(200)'),
39 'hidden' => array('hidden', 'unix', 'varchar(200)'),
40 'password' => array('password', 'oneline', 'varchar(200)'),
41 'textarea' => array('textarea', 'unix', 'text'),
42 'pulldown' => array('pulldown', 'options', 'int'),
43 'checkbox' => array('checkbox', 'yesno', 'int'),
44 'yesno' => array('checkbox', 'yesno', 'int'),
45 'submit' => array('submit', 'oneline', 'n/a')
48 if(isset($_REQUEST['form_name'])) {
49 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
51 $GLOBALS['form_name'] = 'some_form';
54 if(isset($_REQUEST['fields'])) {
55 if(isset($_REQUEST['view_sql'])) {
58 } elseif(isset($_REQUEST['view_php'])) {
61 } elseif(isset($_REQUEST['view_template'])) {
64 } elseif(isset($_REQUEST['view_email'])) {
67 } elseif(isset($_REQUEST['download_tar'])) {
70 } elseif(isset($_REQUEST['preview'])) {
73 } elseif(isset($_REQUEST['edit'])) {
74 tem_set('fields', $_REQUEST['fields']);
75 tem_set('form_name', $GLOBALS['form_name']);
78 die("Sorry... couldn't tell which button you pressed");
83 tem_output('code/wfpl/metaform/main.html');
87 function field_input($type) { return $GLOBALS['types'][$type][0]; }
88 function field_format($type) { return $GLOBALS['types'][$type][1]; }
89 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
91 function get_fields() {
92 $fields_str = unix_newlines($_REQUEST['fields']);
94 $fields_str = rtrim($fields_str);
95 $fields = split("\n", $fields_str);
96 foreach($fields as $field) {
97 list($name, $type, $options) = split(' *', $field);
98 if($options) $options = split(',', $options);
99 if(!$type) $type = $name;
100 $input = field_input($type);
101 $format = field_format($type);
102 $sql = field_sql($type);
103 $ret[] = array($name, $type, $input, $format, $sql, $options);
108 function set_form_action() {
109 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
110 if($action == '') $action = './';
111 tem_set('form_action', $action);
114 # perfect HTTP headers for viewing created files
115 function view_headers() {
116 header('Content-type: text/plain');
122 function make_sql() {
124 $tem->load('code/wfpl/metaform/template.sql');
125 $tem->set('form_name', $GLOBALS['form_name']);
126 $fields = get_fields();
127 foreach($fields as $field) {
128 list($name, $type, $input, $format, $sql) = $field;
130 $tem->set('name', $name);
131 $tem->set('type', $sql);
133 $tem->set('default', '0');
135 $tem->set('default', '""');
144 function view_sql() {
150 # pass false if you want to exclude the <head> and <body> tag etc.
151 function make_template($whole_file = true) {
153 $tem->load('code/wfpl/metaform/template.html');
154 $tem->set('form_name', $GLOBALS['form_name']);
155 $fields = get_fields();
156 foreach($fields as $field) {
157 list($name, $type, $input, $format, $sql) = $field;
158 $tem->set('name', $name);
159 $tem->set('caption', $name); # fixme
161 if($input != 'hidden') {
165 $tem->set('name', 'save');
166 $tem->set('caption', 'Save');
173 return $tem->get('form');
177 function view_template() {
179 echo make_template();
183 function make_php() {
185 $tem->load('code/wfpl/metaform/template.php');
186 $tem->set('form_name', $GLOBALS['form_name']);
187 $fields = get_fields();
190 $always_field = false;
191 foreach($fields as $field) {
192 list($name, $type, $input, $format, $sql) = $field;
193 if($input != 'submit') {
194 $tem->set('format', $format);
195 $tem->set('name', $name);
196 $tem->set('db_field', ''); # we don't want to use the value from last time
198 if($db_fields != '') $db_fields .= ',';
200 if($php_fields != '') $php_fields .= ', ';
201 $php_fields .= '$' . $name;
203 $tem->sub('formats');
204 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
205 $always_field = $name;
209 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
210 $tem->set('always_field', $always_field);
211 $tem->set('db_fields', $db_fields);
212 $tem->set('php_fields', $php_fields);
216 function view_php() {
222 function make_email() {
224 $tem->load('code/wfpl/metaform/template.email.txt');
225 $tem->set('form_name', $GLOBALS['form_name']);
226 $fields = get_fields();
227 foreach($fields as $field) {
228 list($name, $type, $input, $format, $sql) = $field;
229 $tem->set('name', $name);
230 $tem->set('caption', $name); # fixme
236 function view_email() {
244 $tem->load('code/wfpl/metaform/preview.html');
245 $tem->set('form_name', $GLOBALS['form_name']);
246 $tem->set('fields', $_REQUEST['fields']);
247 $preview_tem = new tem();
248 $preview = $preview_tem->run(make_template(false));
250 $tem->set('preview', $preview);
255 function download_tar() {
256 $name = $GLOBALS['form_name'];
258 "$name.html" => make_template(),
259 "$name.sql" => make_sql(),
260 "$name.email.txt" => make_email(),
261 "$name.php" => make_php());
262 make_tar($name, $data);