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 'textarea' => array('textarea', 'unix', 'text'),
40 'pulldown' => array('pulldown', 'options', 'int'),
41 'checkbox' => array('checkbox', 'yesno', 'int'),
42 'yesno' => array('checkbox', 'yesno', 'int'),
43 'submit' => array('submit', 'oneline', 'n/a')
46 if(isset($_REQUEST['form_name'])) {
47 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
49 $GLOBALS['form_name'] = 'some_form';
52 if(isset($_REQUEST['fields'])) {
53 if(isset($_REQUEST['view_sql'])) {
56 } elseif(isset($_REQUEST['view_php'])) {
59 } elseif(isset($_REQUEST['view_template'])) {
62 } elseif(isset($_REQUEST['view_email'])) {
65 } elseif(isset($_REQUEST['download_tar'])) {
68 } elseif(isset($_REQUEST['preview'])) {
71 } elseif(isset($_REQUEST['edit'])) {
72 tem_set('fields', $_REQUEST['fields']);
73 tem_set('form_name', $GLOBALS['form_name']);
76 die("Sorry... couldn't tell which button you pressed");
81 tem_output('code/wfpl/metaform/main.html');
85 function field_input($type) { return $GLOBALS['types'][$type][0]; }
86 function field_format($type) { return $GLOBALS['types'][$type][1]; }
87 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
89 function get_fields() {
90 $fields_str = unix_newlines($_REQUEST['fields']);
92 $fields_str = rtrim($fields_str);
93 $fields = split("\n", $fields_str);
94 foreach($fields as $field) {
95 list($name, $type, $options) = split(' *', $field);
96 if($options) $options = split(',', $options);
97 if(!$type) $type = $name;
98 $input = field_input($type);
99 $format = field_format($type);
100 $sql = field_sql($type);
101 $ret[] = array($name, $type, $input, $format, $sql, $options);
106 function set_form_action() {
107 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
108 if($action == '') $action = './';
109 tem_set('form_action', $action);
112 # perfect HTTP headers for viewing created files
113 function view_headers() {
114 header('Content-type: text/plain');
120 function make_sql() {
122 $tem->load('code/wfpl/metaform/template.sql');
123 $tem->set('form_name', $GLOBALS['form_name']);
124 $fields = get_fields();
125 foreach($fields as $field) {
126 list($name, $type, $input, $format, $sql) = $field;
128 $tem->set('name', $name);
129 $tem->set('type', $sql);
131 $tem->set('default', '0');
133 $tem->set('default', '""');
142 function view_sql() {
148 # pass false if you want to exclude the <head> and <body> tag etc.
149 function make_template($whole_file = true) {
151 $tem->load('code/wfpl/metaform/template.html');
152 $tem->set('form_name', $GLOBALS['form_name']);
153 $fields = get_fields();
154 foreach($fields as $field) {
155 list($name, $type, $input, $format, $sql) = $field;
156 $tem->set('name', $name);
157 $tem->set('caption', $name); # fixme
161 $tem->set('name', 'save');
162 $tem->set('caption', 'Save');
169 return $tem->get('form');
173 function view_template() {
175 echo make_template();
179 function make_php() {
181 $tem->load('code/wfpl/metaform/template.php');
182 $tem->set('form_name', $GLOBALS['form_name']);
183 $fields = get_fields();
185 $always_field = false;
186 foreach($fields as $field) {
187 list($name, $type, $input, $format, $sql) = $field;
188 if($input != 'submit') {
189 $tem->set('format', $format);
190 $tem->set('name', $name);
191 $tem->set('db_field', ''); # we don't want to use the value from last time
193 $tem->sub('db_field');
194 if($db_fields != '') $db_fields .= ',';
197 $tem->sub('formats');
198 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
199 $always_field = $name;
203 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
204 $tem->set('always_field', $always_field);
205 $tem->set('db_fields', $db_fields);
209 function view_php() {
215 function make_email() {
217 $tem->load('code/wfpl/metaform/template.email.txt');
218 $tem->set('form_name', $GLOBALS['form_name']);
219 $fields = get_fields();
220 foreach($fields as $field) {
221 list($name, $type, $input, $format, $sql) = $field;
222 $tem->set('name', $name);
223 $tem->set('caption', $name); # fixme
229 function view_email() {
237 $tem->load('code/wfpl/metaform/preview.html');
238 $tem->set('form_name', $GLOBALS['form_name']);
239 $tem->set('fields', $_REQUEST['fields']);
240 $preview_tem = new tem();
241 $preview = $preview_tem->run(make_template(false));
243 $tem->set('preview', $preview);
248 function download_tar() {
249 $name = $GLOBALS['form_name'];
251 "$name.html" => make_template(),
252 "$name.sql" => make_sql(),
253 "$name.email.txt" => make_email(),
254 "$name.php" => make_php());
255 make_tar($name, $data);