JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed pulldowns, metaform suppors zip and decimal, run.php supports straight html...
[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 under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file writes the code for you (sql, php, html, email) to handle a form.
23
24 require_once('code/wfpl/template.php');
25 require_once('code/wfpl/http.php');
26 require_once('code/wfpl/tar.php');
27 require_once('code/wfpl/format.php');
28
29 # see code/wfpl/metaform/template.html for the html templates for these elements
30 $GLOBALS['types'] = array(
31 #    type                  input          format        sql     
32         'name' =>       array('textbox',     'oneline',    'varchar(200)'),
33         'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
34         'int' =>        array('textbox',     'int',        'int'),
35         'decimal' =>    array('textbox',     'decimal',    'decimal(12,12)'),
36         'bigint' =>     array('textbox',     'int',        'varchar(100)'), # up to 100 digits, stored as a string
37         'zip' =>        array('textbox',     'zip',        'varchar(20)'),
38         'email' =>      array('textbox',     'email',      'varchar(100)'),
39         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
40         'state' =>      array('states',      'oneline',    'varchar(2)'),
41         'money' =>      array('textbox',     'money',      'varchar(32)'),
42         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
43         'url' =>        array('textbox',     'url',        'varchar(200)'),
44         'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
45         'password' =>   array('password',    'oneline',    'varchar(200)'),
46         'textarea' =>   array('textarea',    'unix',       'text'),
47         'html' =>       array('html',        'unix',       'text'),
48         'pulldown' =>   array('pulldown',    'options',    'varchar(100)'),
49         'radio' =>      array('radio',       'oneline',    'varchar(200)'),
50         'leftcheck' =>  array('leftcheck',   'yesno',      'varchar(3)'),
51         'checkbox' =>   array('checkbox',    'yesno',      'varchar(3)'),
52         'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
53         'delete' =>     array('checkbox',    'yesno',      'n/a'),
54         'image' =>      array('image',       'oneline',    'varchar(200)'),
55         'submit' =>     array('submit',      'oneline',    'n/a')
56 );
57
58 function list_available_types() {
59         $types = '';
60         foreach($GLOBALS['types'] as $key => $value) {
61                 if($types) {
62                         $types .= ', ';
63                 }
64                 $types .= $key;
65         }
66         tem_set('available_types', $types);
67 }
68
69
70 function metaform() {
71         if(isset($_REQUEST['form_name'])) {
72                 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
73                 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
74                 tem_set('opt_email', $GLOBALS['opt_email']);
75                 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
76                 tem_set('opt_db', $GLOBALS['opt_db']);
77                 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
78                 tem_set('opt_listing', $GLOBALS['opt_listing']);
79                 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
80                 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
81         } else {
82                 $GLOBALS['form_name'] = 'some_form';
83         }
84
85         if(isset($_REQUEST['fields'])) {
86                 if(isset($_REQUEST['view_sql'])) {
87                         view_sql();
88                         exit();
89                 } elseif(isset($_REQUEST['view_php'])) {
90                         view_php();
91                         exit();
92                 } elseif(isset($_REQUEST['view_html'])) {
93                         view_html();
94                         exit();
95                 } elseif(isset($_REQUEST['view_email'])) {
96                         view_email();
97                         exit();
98                 } elseif(isset($_REQUEST['download_tar'])) {
99                         download_tar();
100                         exit();
101                 } elseif(isset($_REQUEST['preview'])) {
102                         preview();
103                         exit();
104                 } elseif(isset($_REQUEST['edit'])) {
105                         tem_set('fields', $_REQUEST['fields']);
106                         tem_set('form_name', $GLOBALS['form_name']);
107                         # fall through
108                 } else {
109                         die("Sorry... couldn't tell which button you pressed");
110                 }
111         }
112
113
114         set_form_action();
115         list_available_types();
116         tem_output('code/wfpl/metaform/main.html');
117 }
118
119
120 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
121 function field_format($type) { return $GLOBALS['types'][$type][1]; }
122 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
123
124 function get_fields() {
125         # no sense in doing all this so many times
126         if(isset($GLOBALS['gotten_fields'])) {
127                 return $GLOBALS['gotten_fields'];
128         }
129
130         $fields_str = unix_newlines($_REQUEST['fields']);
131         $GLOBALS['gotten_fields'] = array();
132         $fields_str = rtrim($fields_str);
133         $fields = split("\n", $fields_str);
134         foreach($fields as $field) {
135                 list($name, $type, $options) = split('  *', $field);
136                 if($options) $options = split(',', $options);
137                 if(!$type) $type = $name;
138                 $input = field_input($type);
139                 $format = field_format($type);
140                 $sql = field_sql($type);
141                 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
142         }
143         return $GLOBALS['gotten_fields'];
144 }
145
146 # this one, that you're using to create forms
147 function set_form_action() {
148         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
149         if($action == '') $action = './';
150         tem_set('form_action', $action);
151 }
152
153 # perfect HTTP headers for viewing created files
154 function view_headers() {
155         header('Content-type: text/plain');
156 }
157         
158
159
160
161 function make_sql() {
162         $tem = new tem();
163         $tem->load('code/wfpl/metaform/template.sql');
164         $tem->set('form_name', $GLOBALS['form_name']);
165         $fields = get_fields();
166         foreach($fields as $field) {
167                 list($name, $type, $input, $format, $sql) = $field;
168                 if($sql != 'n/a') {
169                         $tem->set('name', $name);
170                         $tem->set('type', $sql);
171                         if($sql == 'int') {
172                                 $tem->set('default', '0');
173                         } else {
174                                 $tem->set('default', '""');
175                         }
176                         $tem->sub('column');
177                 }
178         }
179         view_headers();
180         return $tem->run();
181 }
182
183 function view_sql() {
184         view_headers();
185         echo make_sql();
186 }
187
188 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
189 function find_always_field($fields) {
190         foreach($fields as $field) {
191                 list($name, $type, $input, $format, $sql) = $field;
192                 if($input != 'submit' && $input != 'checkbox' && $input != 'radio') {
193                         return $name;
194                 }
195         }
196
197         return false;
198 }
199         
200         
201
202 # pass false if you want to exclude the <head> and <body> tag etc.
203 function make_html($whole_file = true) {
204         $uploads_output_already = false;
205         $has_html_editors = false;
206         $tem = new tem();
207         $tem->load('code/wfpl/metaform/template.html');
208         $tem->set('form_name', $GLOBALS['form_name']);
209         $fields = get_fields();
210         $tem->set('always_field', find_always_field($fields));
211         foreach($fields as $field) {
212                 list($name, $type, $input, $format, $sql) = $field;
213                 $tem->set('name', $name);
214                 $tem->set('caption', format_caption($name));
215                 $tem->sub($input);
216                 if($input != 'hidden') {
217                         $tem->sub('row');
218                 }
219                 if($input == 'image' && !$uploads_output_already) {
220                         $tem->sub('uploads');
221                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
222                         $uploads_output_already = true;
223                 } elseif($input == 'html') {
224                         $has_html_editors = true;
225                         $tem->set('html_field_name', $name);
226                         $tem->sub('replace_textarea');
227                 }
228         }
229
230         if($GLOBALS['opt_db'] == 'Yes') {
231                 $tem->sub('opt_db_1');
232                 $tem->sub('opt_db_2');
233         } else {
234                 $tem->sub('opt_db_1_else');
235         }
236
237         if($GLOBALS['opt_listing'] == 'Yes') {
238                 $tem->sub('opt_listing_1');
239         } else {
240                 $tem->sub('opt_listing_1_else');
241         }
242
243         if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
244                 $tem->set('name', 'send');
245                 $tem->set('caption', 'Send');
246         } else {
247                 $tem->set('name', 'save');
248                 $tem->set('caption', 'Save');
249         }
250         $tem->sub('submit');
251         $tem->sub('row');
252
253         $tem->sub('form');
254
255         if($has_html_editors) {
256                 $tem->sub('html_editor_headers');
257         }
258
259         if($whole_file) {
260                 return $tem->run();
261         } else {
262                 return $tem->get('form');
263         }
264 }
265
266 function view_html() {
267         view_headers();
268         echo make_html();
269 }
270
271
272 function make_php() {
273         $tem = new tem();
274         $tem->load('code/wfpl/metaform/template.php');
275         $tem->set('form_name', $GLOBALS['form_name']);
276         $fields = get_fields();
277         $db_fields = '';
278         $php_fields = '';
279         $always_field = find_always_field($fields);
280         $image_included_yet = false;
281         foreach($fields as $field) {
282                 list($name, $type, $input, $format, $sql) = $field;
283                 if($input != 'submit') {
284                         $tem->set('format', $format);
285                         $tem->set('name', $name);
286                         $tem->set('db_field', ''); # we don't want to use the value from last time
287                         if($sql != 'n/a') {
288                                 if($db_fields != '') $db_fields .= ',';
289                                 $db_fields .= $name;
290                                 if($php_fields != '') $php_fields .= ', ';
291                                 $php_fields .= '$' . $name;
292                         }
293                         if($input == 'image') {
294                                 $tem->sub('image_upload');
295                                 $tem->sub('image_db');
296                                 if(!$image_included_yet) {
297                                         $tem->sub('image_include');
298                                         $tem->sub('upload_max');
299                                         $tem->sub('upload_settings');
300                                         $image_included_yet = true;
301                                 }
302                         } else {
303                                 if($input == 'pulldown') {
304                                         $tem->sub('pulldowns');
305                                         $tem->sub('pulldown_format_extra');
306                                 }
307                                 $tem->sub('formats');
308                         }
309                         $tem->sub('tem_sets');
310                 }
311         }
312
313         $tem->set('always_field', $always_field);
314         $tem->set('db_fields', $db_fields);
315         $tem->set('php_fields', $php_fields);
316         $tem->set('metaform_url', edit_url());
317         if($GLOBALS['opt_listing'] == 'Yes') {
318                 $tem->sub('opt_listing_1');
319                 $tem->sub('opt_listing_2');
320                 $tem->sub('opt_listing_3');
321                 $tem->sub('opt_listing_4');
322         } else {
323                 $tem->sub('opt_listing_3_else');
324                 $tem->sub('opt_listing_4_else');
325         }
326         if($GLOBALS['opt_db'] == 'Yes') {
327                 $tem->sub('opt_db_1');
328                 $tem->sub('opt_db_2');
329                 $tem->sub('opt_db_3');
330                 $tem->sub('opt_db_4');
331                 $tem->sub('opt_db_5');
332         }
333         if($GLOBALS['opt_email'] == 'Yes') {
334                 $tem->sub('opt_email_1');
335                 $tem->sub('opt_email_2');
336         }
337         if($GLOBALS['opt_http_pass'] == 'Yes') {
338                 $tem->sub('opt_http_pass_1');
339                 $tem->sub('opt_http_pass_2');
340         }
341         return $tem->run();
342 }
343
344 # make a URL for the edit page with all the fields filled in
345 function edit_url() {
346         $url = this_url();
347         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
348         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
349         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
350         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
351         return $url;
352 }
353
354 function view_php() {
355         view_headers();
356         echo make_php();
357 }
358
359
360 function make_email() {
361         $tem = new tem();
362         $tem->load('code/wfpl/metaform/template.email.txt');
363         $tem->set('form_name', $GLOBALS['form_name']);
364         $fields = get_fields();
365         foreach($fields as $field) {
366                 list($name, $type, $input, $format, $sql) = $field;
367                 $tem->set('name', $name);
368                 $tem->set('caption', $name); # fixme
369                 if($type == 'textarea') {
370                         $tem->sub('multi_line');
371                 } else {
372                         $tem->sub('fields');
373                 }
374         }
375         return $tem->run();
376 }
377
378 function make_htaccess() {
379         $tem = new tem();
380         $tem->set('form', $GLOBALS['form_name']);
381         return $tem->run('code/wfpl/metaform/htaccess');
382 }
383
384 function view_email() {
385         view_headers();
386         echo make_email();
387 }
388
389
390 function preview() {
391         tem_load('code/wfpl/metaform/preview.html');
392         tem_set('form_name', $GLOBALS['form_name']);
393         tem_set('fields', $_REQUEST['fields']);
394         $preview_tem = new tem();
395         $preview_tem->load_str(make_html(false));
396         if($GLOBALS['opt_db'] == 'Yes') {
397                 $preview_tem->sub('new_msg');
398         }
399         $fields = get_fields();
400         foreach($fields as $field) {
401                 list($name, $type, $input, $format, $sql) = $field;
402                 if($type == 'pulldown') {
403                         pulldown($name, array('option 1', 'option 2', 'option 3'));
404                 }
405         }
406         $preview = $preview_tem->run();
407         unset($preview_tem);
408         tem_set('preview', $preview);
409         set_form_action();
410         tem_output();
411 }
412
413 function download_tar() {
414         $name = $GLOBALS['form_name'];
415         $data = array(
416                 ".htaccess" => make_htaccess(),
417                 "run.php ->" => 'code/wfpl/run.php',
418                 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
419                 "$name.html" => make_html(),
420                 "$name.php" => make_php());
421         if($GLOBALS['opt_db'] == 'Yes') {
422                 $data["$name.sql"] = make_sql();
423         }
424         if($GLOBALS['opt_email'] == 'Yes') {
425                 $data["$name.email.txt"] = make_email();
426         }
427         make_wfpl_tar($name, $data);
428 }
429
430
431 metaform();
432 exit();
433
434 ?>