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 'email' => array('textbox', 'email', 'varchar(100)'),
34 'phone' => array('textbox', 'phone', 'varchar(32)'),
35 'money' => array('textbox', 'money', 'varchar(32)'),
36 'dollars' => array('textbox', 'dollars', 'varchar(32)'),
37 'url' => array('textbox', 'url', 'varchar(200)'),
38 'textarea' => array('textarea', 'unix', 'text'),
39 'pulldown' => array('pulldown', 'options', 'int'),
40 'checkbox' => array('checkbox', 'yesno', 'int'),
41 'yesno' => array('checkbox', 'yesno', 'int'),
42 'submit' => array('submit', 'oneline', 'n/a')
45 if(isset($_REQUEST['form_name'])) {
46 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
48 $GLOBALS['form_name'] = 'some_form';
51 if(isset($_REQUEST['fields'])) {
52 if(isset($_REQUEST['view_sql'])) {
55 } elseif(isset($_REQUEST['view_php'])) {
58 } elseif(isset($_REQUEST['view_template'])) {
61 } elseif(isset($_REQUEST['view_email'])) {
64 } elseif(isset($_REQUEST['download_tar'])) {
67 } elseif(isset($_REQUEST['preview'])) {
70 } elseif(isset($_REQUEST['edit'])) {
71 tem_set('fields', $_REQUEST['fields']);
72 tem_set('form_name', $GLOBALS['form_name']);
75 die("Sorry... couldn't tell which button you pressed");
80 tem_output('code/wfpl/metaform/main.html');
84 function field_input($type) { return $GLOBALS['types'][$type][0]; }
85 function field_format($type) { return $GLOBALS['types'][$type][1]; }
86 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
88 function get_fields() {
89 $fields_str = unix_newlines($_REQUEST['fields']);
91 $fields_str = rtrim($fields_str);
92 $fields = split("\n", $fields_str);
93 foreach($fields as $field) {
94 list($name, $type, $options) = split(' *', $field);
95 if($options) $options = split(',', $options);
96 if(!$type) $type = $name;
97 $input = field_input($type);
98 $format = field_format($type);
99 $sql = field_sql($type);
100 $ret[] = array($name, $type, $input, $format, $sql, $options);
105 function set_form_action() {
106 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
107 if($action == '') $action = './';
108 tem_set('form_action', $action);
111 # perfect HTTP headers for viewing created files
112 function view_headers() {
113 header('Content-type: text/plain');
119 function make_sql() {
121 $tem->load('code/wfpl/metaform/template.sql');
122 $tem->set('form_name', $GLOBALS['form_name']);
123 $fields = get_fields();
124 foreach($fields as $field) {
125 list($name, $type, $input, $format, $sql) = $field;
127 $tem->set('name', $name);
128 $tem->set('type', $sql);
130 $tem->set('default', '0');
132 $tem->set('default', '""');
141 function view_sql() {
147 # pass false if you want to exclude the <head> and <body> tag etc.
148 function make_template($whole_file = true) {
150 $tem->load('code/wfpl/metaform/template.html');
151 $tem->set('form_name', $GLOBALS['form_name']);
152 $fields = get_fields();
153 foreach($fields as $field) {
154 list($name, $type, $input, $format, $sql) = $field;
155 $tem->set('name', $name);
156 $tem->set('caption', $name); # fixme
159 $tem->set('name', 'save');
160 $tem->set('caption', 'Save');
166 return $tem->get('form');
170 function view_template() {
172 echo make_template();
176 function make_php() {
178 $tem->load('code/wfpl/metaform/template.php');
179 $tem->set('form_name', $GLOBALS['form_name']);
180 $fields = get_fields();
182 $always_field = false;
183 foreach($fields as $field) {
184 list($name, $type, $input, $format, $sql) = $field;
185 if($input != 'submit') {
186 $tem->set('format', $format);
187 $tem->set('name', $name);
188 $tem->set('db_field', ''); # we don't want to use the value from last time
190 $tem->sub('db_field');
191 if($db_fields != '') $db_fields .= ',';
194 $tem->sub('formats');
195 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
196 $always_field = $name;
200 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
201 $tem->set('always_field', $always_field);
202 $tem->set('db_fields', $db_fields);
206 function view_php() {
212 function make_email() {
214 $tem->load('code/wfpl/metaform/template.email.txt');
215 $tem->set('form_name', $GLOBALS['form_name']);
216 $fields = get_fields();
217 foreach($fields as $field) {
218 list($name, $type, $input, $format, $sql) = $field;
219 $tem->set('name', $name);
220 $tem->set('caption', $name); # fixme
226 function view_email() {
234 $tem->load('code/wfpl/metaform/preview.html');
235 $tem->set('form_name', $GLOBALS['form_name']);
236 $tem->set('fields', $_REQUEST['fields']);
237 $tem->set('preview', make_template(false));
242 function download_tar() {
243 $name = $GLOBALS['form_name'];
245 "$name.html" => make_template(),
246 "$name.sql" => make_sql(),
247 "$name.email.txt" => make_email(),
248 "$name.php" => make_php());
249 make_tar($name, $data);