JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: tested and fixed. fixed sql for checkboxes
[wfpl.git] / metaform.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
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)
10 #  any later version.
11 #
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.
16 #
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,
20 #  MA 02111-1307, USA.
21
22
23 # This file writes the code for you (sql, php, html, email) to handle a form.
24
25 require_once('code/wfpl/template.php');
26 require_once('code/wfpl/tar.php');
27
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',      'varchar(3)'),
44         'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
45         'submit' =>     array('submit',      'oneline',    'n/a')
46 );
47
48 if(isset($_REQUEST['form_name'])) {
49         $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
50 } else {
51         $GLOBALS['form_name'] = 'some_form';
52 }
53
54 if(isset($_REQUEST['fields'])) {
55         if(isset($_REQUEST['view_sql'])) {
56                 view_sql();
57                 exit();
58         } elseif(isset($_REQUEST['view_php'])) {
59                 view_php();
60                 exit();
61         } elseif(isset($_REQUEST['view_template'])) {
62                 view_template();
63                 exit();
64         } elseif(isset($_REQUEST['view_email'])) {
65                 view_email();
66                 exit();
67         } elseif(isset($_REQUEST['download_tar'])) {
68                 download_tar();
69                 exit();
70         } elseif(isset($_REQUEST['preview'])) {
71                 preview();
72                 exit();
73         } elseif(isset($_REQUEST['edit'])) {
74                 tem_set('fields', $_REQUEST['fields']);
75                 tem_set('form_name', $GLOBALS['form_name']);
76                 # fall through
77         } else {
78                 die("Sorry... couldn't tell which button you pressed");
79         }
80 }
81
82 set_form_action();
83 tem_output('code/wfpl/metaform/main.html');
84 exit();
85
86
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]; }
90
91 function get_fields() {
92         $fields_str = unix_newlines($_REQUEST['fields']);
93         $ret = array();
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);
104         }
105         return $ret;
106 }
107
108 function set_form_action() {
109         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
110         if($action == '') $action = './';
111         tem_set('form_action', $action);
112 }
113
114 # perfect HTTP headers for viewing created files
115 function view_headers() {
116         header('Content-type: text/plain');
117 }
118         
119
120
121
122 function make_sql() {
123         $tem = new tem();
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;
129                 if($sql != 'n/a') {
130                         $tem->set('name', $name);
131                         $tem->set('type', $sql);
132                         if($sql == 'int') {
133                                 $tem->set('default', '0');
134                         } else {
135                                 $tem->set('default', '""');
136                         }
137                         $tem->sub('column');
138                 }
139         }
140         view_headers();
141         return $tem->run();
142 }
143
144 function view_sql() {
145         view_headers();
146         echo make_sql();
147 }
148         
149
150 # pass false if you want to exclude the <head> and <body> tag etc.
151 function make_template($whole_file = true) {
152         $tem = new tem();
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
160                 $tem->sub($input);
161                 if($input != 'hidden') {
162                         $tem->sub('row');
163                 }
164         }
165         $tem->set('name', 'save');
166         $tem->set('caption', 'Save');
167         $tem->sub('submit');
168         $tem->sub('row');
169         $tem->sub('form');
170         if($whole_file) {
171                 return $tem->run();
172         } else {
173                 return $tem->get('form');
174         }
175 }
176
177 function view_template() {
178         view_headers();
179         echo make_template();
180 }
181
182
183 function make_php() {
184         $tem = new tem();
185         $tem->load('code/wfpl/metaform/template.php');
186         $tem->set('form_name', $GLOBALS['form_name']);
187         $fields = get_fields();
188         $db_fields = '';
189         $php_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
197                         if($sql != 'n/a') {
198                                 if($db_fields != '') $db_fields .= ',';
199                                 $db_fields .= $name;
200                                 if($php_fields != '') $php_fields .= ', ';
201                                 $php_fields .= '$' . $name;
202                         }
203                         $tem->sub('formats');
204                         $tem->sub('tem_sets');
205                         if(!$always_field and $input != 'checkbox' and $input != 'radio') {
206                                 $always_field = $name;
207                         }
208                 }
209         }
210         # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
211         $tem->set('always_field', $always_field);
212         $tem->set('db_fields', $db_fields);
213         $tem->set('php_fields', $php_fields);
214         return $tem->run();
215 }
216
217 function view_php() {
218         view_headers();
219         echo make_php();
220 }
221
222
223 function make_email() {
224         $tem = new tem();
225         $tem->load('code/wfpl/metaform/template.email.txt');
226         $tem->set('form_name', $GLOBALS['form_name']);
227         $fields = get_fields();
228         foreach($fields as $field) {
229                 list($name, $type, $input, $format, $sql) = $field;
230                 $tem->set('name', $name);
231                 $tem->set('caption', $name); # fixme
232                 $tem->sub('fields');
233         }
234         return $tem->run();
235 }
236
237 function view_email() {
238         view_headers();
239         echo make_email();
240 }
241
242
243 function preview() {
244         $tem = new tem();
245         $tem->load('code/wfpl/metaform/preview.html');
246         $tem->set('form_name', $GLOBALS['form_name']);
247         $tem->set('fields', $_REQUEST['fields']);
248         $preview_tem = new tem();
249         $preview = $preview_tem->run(make_template(false));
250         unset($preview_tem);
251         $tem->set('preview', $preview);
252         set_form_action();
253         $tem->output();
254 }
255
256 function download_tar() {
257         $name = $GLOBALS['form_name'];
258         $data = array(
259                 "$name.html" => make_template(),
260                 "$name.sql" => make_sql(),
261                 "$name.email.txt" => make_email(),
262                 "$name.php" => make_php());
263         make_tar($name, $data);
264 }
265
266 ?>