JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: added fieldset support
[wfpl.git] / metaform.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file writes the code for you (sql, php, html, email) to handle a form.
20
21 require_once('code/wfpl/template.php');
22 require_once('code/wfpl/http.php');
23 require_once('code/wfpl/tar.php');
24 require_once('code/wfpl/format.php');
25
26 # see code/wfpl/metaform/template.html for the html templates for these elements
27 $GLOBALS['types'] = array(
28 #    type                  input          format        sql     
29         'varname' =>    array('textbox',     'varname',    'varchar(50)'),
30         'name' =>       array('textbox',     'oneline',    'varchar(200)'),
31         'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
32         'int' =>        array('textbox',     'int',        'int'),
33         'decimal' =>    array('textbox',     'decimal',    'decimal(12,12)'),
34         'bigint' =>     array('textbox',     'int',        'varchar(100)'), # up to 100 digits, stored as a string
35         'zip' =>        array('textbox',     'zip',        'varchar(20)'),
36         'email' =>      array('textbox',     'email',      'varchar(100)'),
37         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
38         'state' =>      array('states',      'oneline',    'varchar(2)'),
39         'money' =>      array('textbox',     'money',      'varchar(32)'),
40         'date' =>       array('date',        'mdy_to_ymd', 'char(10)'),
41         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
42         'url' =>        array('textbox',     'url',        'varchar(200)'),
43         'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
44         'password' =>   array('password',    'oneline',    'varchar(200)'),
45         'textarea' =>   array('textarea',    'unix',       'text'),
46         'html' =>       array('html',        'unix',       'text'),
47         'pulldown' =>   array('pulldown',    'options',    'varchar(100)'),
48         'radio' =>      array('radio',       'options',    'varchar(100)'),
49         'checkbox' =>   array('checkbox',    'bool',       'int(1)'),
50         'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
51         'delete' =>     array('checkbox',    'yesno',      'n/a'),
52         'image' =>      array('image',       'oneline',    'varchar(120)'),
53         'thumb' =>      array('image',       'oneline',    'varchar(240)'),
54         'file' =>       array('file',        'oneline',    'varchar(100)'),
55         'submit' =>     array('submit',      'n/a',        'n/a'),
56         '{' =>          array('fieldset',    'n/a',        'n/a'),
57         '}' =>          array('end_fieldset','n/a',        'n/a')
58 );
59
60 function list_available_types() {
61         ksort($GLOBALS['types']);
62         foreach($GLOBALS['types'] as $key => $value) {
63                 tem_set('type', $key);
64                 tem_show('types');
65                 tem_show('types_sep');
66         }
67 }
68
69
70 function tem_set_globals(&$tem) {
71         $vars = array(
72                 'file_name',
73                 'table_name',
74                 'plural',
75                 'singular');
76         foreach($vars as $var) {
77                 $tem->set($var, $GLOBALS[$var]);
78         }
79
80         $bools = array(
81                 'opt_email',
82                 'opt_db',
83                 'opt_listing',
84                 'opt_display',
85                 'opt_pass');
86         foreach($bools as $bool) {
87                 if(format_bool($GLOBALS[$bool])) {
88                         $tem->set($bool);
89                 }
90         }
91 }
92
93 function metaform() {
94         if(isset($_REQUEST['singular'])) {
95                 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
96                 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
97                 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
98                 # backwards compatibility:
99                 if(isset($_REQUEST['form_name'])) {
100                         $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
101                 }
102
103                 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
104                 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
105                 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
106                 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
107                 $GLOBALS['opt_display'] = format_yesno($_REQUEST['opt_display']);
108                 $GLOBALS['opt_pass'] = format_yesno($_REQUEST['opt_pass']);
109
110                 tem_init();
111                 tem_set_globals($GLOBALS['wfpl_template']);
112         }
113
114         if(isset($_REQUEST['fields'])) {
115                 if(isset($_REQUEST['view_sql'])) {
116                         view_sql();
117                         exit();
118                 } elseif(isset($_REQUEST['view_php'])) {
119                         view_php();
120                         exit();
121                 } elseif(isset($_REQUEST['view_html'])) {
122                         view_html();
123                         exit();
124                 } elseif(isset($_REQUEST['view_email'])) {
125                         view_email();
126                         exit();
127                 } elseif(isset($_REQUEST['download_tar'])) {
128                         download_tar();
129                         exit();
130                 } elseif(isset($_REQUEST['preview'])) {
131                         preview();
132                         exit();
133                 } elseif(isset($_REQUEST['edit'])) {
134                         tem_set('fields', $_REQUEST['fields']);
135                         # fall through
136                 } else {
137                         die("Sorry... couldn't tell which button you pressed");
138                 }
139         }
140
141
142         set_form_action();
143         tem_load('code/wfpl/metaform/main.html');
144         list_available_types();
145         tem_output();
146 }
147
148
149 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
150 function field_format($type) { return $GLOBALS['types'][$type][1]; }
151 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
152
153 function get_fields() {
154         # no sense in doing all this so many times
155         if(isset($GLOBALS['gotten_fields'])) {
156                 return $GLOBALS['gotten_fields'];
157         }
158
159         $fields_str = unix_newlines($_REQUEST['fields']);
160         $GLOBALS['gotten_fields'] = array();
161         $fields_str = rtrim($fields_str);
162         $fields = split("\n", $fields_str);
163         foreach($fields as $field) {
164                 $field = trim($field);
165                 if(substr($field, -1) == '{') {
166                         $name = trim(substr($field, 0, -1)); # FIXME: stop this from getting enc_caption()ed
167                         $type = '{';
168                         $options = null;
169                 } elseif(substr($field, -1) == '{') {
170                         $name = 'ignored';
171                         $type = '}';
172                         $options = null;
173                 } else {
174                         list($name, $type, $options) = split('  *', $field);
175                         if($options) $options = split(',', $options);
176                         if(!$type) $type = $name;
177                 }
178                 $input = field_input($type);
179                 $format = field_format($type);
180                 $sql = field_sql($type);
181                 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
182         }
183
184         return $GLOBALS['gotten_fields'];
185 }
186
187 # this one, that you're using to create forms
188 function set_form_action() {
189         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
190         if($action == '') $action = './';
191         tem_set('form_action', $action);
192 }
193
194 # perfect HTTP headers for viewing created files
195 function view_headers() {
196         header('Content-type: text/plain');
197 }
198         
199
200
201
202 function make_sql() {
203         $tem = new tem();
204         $tem->load('code/wfpl/metaform/template.sql');
205         tem_set_globals($tem);
206         $fields = get_fields();
207         foreach($fields as $field) {
208                 list($name, $type, $input, $format, $sql) = $field;
209                 if($sql != 'n/a') {
210                         $tem->set('name', $name);
211                         $tem->set('type', $sql);
212                         if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
213                                 $tem->set('default', '0');
214                         } elseif($format == 'yesno') {
215                                 $tem->set('default', '"No"');
216                         } else {
217                                 $tem->set('default', '""');
218                         }
219                         $tem->show('column');
220                 }
221         }
222         view_headers();
223         return $tem->run();
224 }
225
226 function view_sql() {
227         view_headers();
228         echo make_sql();
229 }
230
231 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
232 function find_always_field($fields) {
233         foreach($fields as $field) {
234                 list($name, $type, $input, $format, $sql) = $field;
235                 if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio') {
236                         return $name;
237                 }
238         }
239
240         return false;
241 }
242         
243         
244
245 # pass false if you want to exclude the <head> and <body> tag etc.
246 function make_html($whole_file = true) {
247         $has_html_editors = false;
248         $tem = new tem();
249         $tem->load('code/wfpl/metaform/template.html');
250         tem_set_globals($tem);
251         $fields = get_fields();
252         $tem->set('always_field', find_always_field($fields));
253         $hidden_fields = array();
254         $visible_fields = array();
255         $display_fields = array();
256         $listing_headers = array();
257         $listing_fields = array();
258         foreach($fields as $field) {
259                 list($name, $type, $input, $format, $sql) = $field;
260                 if($input == 'hidden') {
261                         $hidden_fields[] = array('name' => $name);
262                 } else {
263                         $visible_fields[] = array($input => array(
264                                 'name' => $name,
265                                 'caption' => format_caption($name)));
266                 }
267
268                 if($input == 'image' || $input == 'file') {
269                         $tem->set('uploads');
270                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
271                 } elseif($input == 'html') {
272                         $has_html_editors = true;
273                         $tem->set('html_field_name', $name);
274                         $tem->set('replace_textarea');
275                 }
276
277                 switch($input) {
278                         case 'image':
279                         case 'checkbox':
280                         case 'date':
281                         case 'textarea':
282                         case 'html':
283                                 $display_type = $input;
284                         break;
285                         default:
286                                 $display_type = 'short';
287                 }
288                 if($format != 'n/a') {
289                         $display_fields[] = array($display_type => array(
290                                 'name' => $name, 'caption' => format_caption($name)));
291                 }
292
293                 if(show_in_listing($type, $input, $format, $sql)) {
294                         $listing_headers[] = array('caption' => format_caption($name));
295                         $listing_field = array('name' => $name);
296                         if($format == 'bool' || $format == 'yesno') {
297                                 $listing_field['enc'] = 'yesno';
298                         } elseif($input == 'date') {
299                                 $listing_field['enc'] = 'mmddyyyy';
300                         } elseif($type == 'thumb') {
301                                 $listing_field['thumb'] = true;
302                         } else {
303                                 $listing_field['enc'] = 'html';
304                         }
305                         $listing_fields[] = $listing_field;
306                 }
307         }
308
309         # Submit/Send button
310         if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
311                 $visible_fields[] = array('submit' => array(
312                         'name' => 'send',
313                         'caption' => 'Send'));
314         } else {
315                 $visible_fields[] = array('submit' => array(
316                         'name' => 'save',
317                         'caption' => 'Save'));
318         }
319
320         $tem->set('form', array(
321                 'visible_fields' => $visible_fields,
322                 'hidden_fields' => $hidden_fields));
323
324         # opt_display and opt_listing control whether these are actually displayed
325         $tem->set('display_fields', $display_fields);
326         $tem->set('listing_headers', $listing_headers);
327         $tem->set('listing_fields', $listing_fields);
328
329
330         if($has_html_editors) {
331                 $tem->set('html_editor_headers');
332         }
333
334         if($whole_file) {
335                 return $tem->run();
336         } else {
337                 $tem2 = new tem();
338                 $tem2->load_str('<!--~form~-->');
339                 $tem2->merge($tem);
340                 return $tem2->run();
341         }
342 }
343
344 function view_html() {
345         view_headers();
346         echo make_html();
347 }
348
349 function show_in_listing($type, $input, $format, $sql) {
350         switch($input) {
351                 case 'submit':
352                 case 'hidden':
353                 case 'password':
354                 case 'textarea':
355                 case 'html':
356                 case 'fieldset':
357                 case 'end_fieldset':
358                         return false;
359         }
360         if($type == 'image') {
361                 return false;
362         }
363
364         return true;
365 }
366
367 function make_php() {
368         $has_html_editors = false;
369         $tem = new tem();
370         $tem->load('code/wfpl/metaform/template.php');
371         tem_set_globals($tem);
372         $fields = get_fields();
373         $db_fields = '';
374         $always_field = find_always_field($fields);
375         $image_included_yet = false;
376         foreach($fields as $field) {
377                 list($name, $type, $input, $format, $sql) = $field;
378                 if($input != 'submit') {
379                         $tem->set('format', $format);
380                         $tem->set('name', $name);
381                         $tem->set('db_field', ''); # we don't want to use the value from last time
382                         if($sql != 'n/a') {
383                                 if($db_fields != '') $db_fields .= ',';
384                                 $db_fields .= $name;
385                         }
386                         if($input == 'image') {
387                                 if($type == 'thumb') {
388                                         $tem->show('thumb_settings');
389                                         $tem->show('thumb_upload_params');
390                                         $tem->show('thumb_w_h');
391                                 }
392                                 $tem->show('image_settings');
393                                 $tem->show('image_upload');
394                                 $has_uploads = true;
395                         } else if($input == 'file') {
396                                 $tem->show('file_settings');
397                                 $tem->show('file_upload');
398                                 $has_uploads = true;
399                         } else {
400                                 if($input == 'html') {
401                                         $has_html_editors = true;
402                                 } elseif($input == 'pulldown' || $input == 'radio') {
403                                         $tem->show('pulldowns');
404                                         $tem->show('pulldown_format_extra');
405                                 }
406                                 if($format != 'n/a') {
407                                         $tem->show('formats');
408                                 }
409                         }
410                 }
411
412                 if($GLOBALS['opt_listing'] == 'Yes') {
413                         if(show_in_listing($type, $input, $format, $sql)) {
414                                 $tem->show('listing_fields_1');
415                                 $tem->show('listing_fields_2');
416                         }
417                 }
418         }
419         if($has_uploads) {
420                 $tem->show('uploads_include');
421                 $tem->show('upload_max');
422                 $tem->show('upload_settings');
423                 $image_included_yet = true;
424         }
425
426         if($has_html_editors) {
427                 $tem->show('show_extra_headers');
428         }
429
430         $tem->set('always_field', $always_field);
431         $tem->set('db_fields', $db_fields);
432         $tem->set('metaform_url', edit_url());
433         if($GLOBALS['opt_email'] == 'Yes') {
434                 $this_domain = $_SERVER['HTTP_HOST'];
435                 if(substr($this_domain, -2) == '.l') {
436                         $this_domain = substr($this_domain, 0, -1) . 'com';
437                 }
438                 $tem->set('this_domain', $this_domain);
439         }
440         return $tem->run();
441 }
442
443 # make a URL for the edit page with all the fields filled in
444 function edit_url() {
445         $url = this_url();
446         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
447         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
448         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
449         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
450         return $url;
451 }
452
453 function view_php() {
454         view_headers();
455         echo make_php();
456 }
457
458 function make_email() {
459         $tem = new tem();
460         $tem->load('code/wfpl/metaform/template.email.txt');
461         tem_set_globals($tem);
462         $fields = get_fields();
463         foreach($fields as $field) {
464                 list($name, $type, $input, $format, $sql) = $field;
465                 $tem->set('name', $name);
466                 $tem->set('caption', format_caption($name));
467                 if($type == 'textarea') {
468                         $tem->show('multi_line');
469                 } elseif($type == 'checkbox') {
470                         $tem->show('checkbox');
471                 } else {
472                         $tem->show('normal');
473                 }
474                 $tem->show('fields');
475         }
476         return $tem->run();
477 }
478
479 function make_htaccess() {
480         $tem = new tem();
481         $tem->set('form', $GLOBALS['file_name']);
482         return $tem->run('code/wfpl/metaform/htaccess');
483 }
484
485 function view_email() {
486         view_headers();
487         echo make_email();
488 }
489
490 function preview() {
491         tem_load('code/wfpl/metaform/preview.html');
492         tem_set_globals($GLOBALS['wfpl_template']);
493         tem_set('fields', $_REQUEST['fields']);
494         $preview_tem = new tem();
495         $preview_tem->load_str(make_html(false));
496         if($GLOBALS['opt_db'] == 'Yes') {
497                 $preview_tem->show('new_msg');
498         }
499         $fields = get_fields();
500         foreach($fields as $field) {
501                 list($name, $type, $input, $format, $sql) = $field;
502                 if($type == 'pulldown' || $type == 'radio') {
503                         pulldown($name, array('option 1', 'option 2', 'option 3'));
504                 }
505         }
506         $preview = $preview_tem->run();
507         unset($preview_tem);
508         $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
509         tem_set('preview', $preview);
510         tem_show('hiddens');
511         set_form_action();
512         tem_output();
513 }
514
515 function download_tar() {
516         $name = $GLOBALS['file_name'];
517         $data = array(
518                 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
519                 ".htaccess" => make_htaccess(),
520                 #"run.php ->" => 'code/wfpl/run.php',
521                 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
522                 "$name.html" => make_html(),
523                 "$name.php" => make_php());
524         if($GLOBALS['opt_db'] == 'Yes') {
525                 $data["$name.sql"] = make_sql();
526         }
527         if($GLOBALS['opt_email'] == 'Yes') {
528                 $data["$name.email.txt"] = make_email();
529         }
530         make_tar($name, $data);
531 }
532
533
534 metaform();
535 exit();