JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: fix form beginning with section
[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 = explode(',', $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' && $type != '{') {
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 pulldown_options_array($options) {
368         if($options) {
369                 $pulldown_options = array();
370                 foreach($options as $option) {
371                         $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
372                         $pulldown_options[] = "'$option'";
373                 }
374                 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
375         } else {
376                 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
377         }
378
379         return $pulldown_options;
380 }
381
382 function make_php() {
383         $has_html_editors = false;
384         $tem = new tem();
385         $tem->load('code/wfpl/metaform/template.php');
386         tem_set_globals($tem);
387         $fields = get_fields();
388         $db_fields = '';
389         $always_field = find_always_field($fields);
390         $image_included_yet = false;
391         foreach($fields as $field) {
392                 list($name, $type, $input, $format, $sql, $options) = $field;
393                 if($input != 'submit') {
394                         $tem->set('format', $format);
395                         $tem->set('name', $name);
396                         $tem->set('db_field', ''); # we don't want to use the value from last time
397                         if($sql != 'n/a') {
398                                 if($db_fields != '') $db_fields .= ',';
399                                 $db_fields .= $name;
400                         }
401                         if($input == 'image') {
402                                 if($type == 'thumb') {
403                                         $tem->show('thumb_settings');
404                                         $tem->show('thumb_upload_params');
405                                         $tem->show('thumb_w_h');
406                                 }
407                                 $tem->show('image_settings');
408                                 $tem->show('image_upload');
409                                 $has_uploads = true;
410                         } else if($input == 'file') {
411                                 $tem->show('file_settings');
412                                 $tem->show('file_upload');
413                                 $has_uploads = true;
414                         } else {
415                                 if($input == 'html') {
416                                         $has_html_editors = true;
417                                 } elseif($input == 'pulldown' || $input == 'radio') {
418                                         $pulldown_options = pulldown_options_array($options);
419                                         $tem->set('pulldown_options', $pulldown_options);
420                                         $tem->show('pulldowns');
421                                         $tem->show('pulldown_format_extra');
422                                 }
423                                 if($format != 'n/a') {
424                                         $tem->show('formats');
425                                 }
426                         }
427                 }
428
429                 if($GLOBALS['opt_listing'] == 'Yes') {
430                         if(show_in_listing($type, $input, $format, $sql)) {
431                                 $tem->show('listing_fields_1');
432                                 $tem->show('listing_fields_2');
433                         }
434                 }
435         }
436         if($has_uploads) {
437                 $tem->show('uploads_include');
438                 $tem->show('upload_max');
439                 $tem->show('upload_settings');
440                 $image_included_yet = true;
441         }
442
443         if($has_html_editors) {
444                 $tem->show('show_extra_headers');
445         }
446
447         $tem->set('always_field', $always_field);
448         $tem->set('db_fields', $db_fields);
449         $tem->set('metaform_url', edit_url());
450         if($GLOBALS['opt_email'] == 'Yes') {
451                 $this_domain = $_SERVER['HTTP_HOST'];
452                 if(substr($this_domain, -2) == '.l') {
453                         $this_domain = substr($this_domain, 0, -1) . 'com';
454                 }
455                 $tem->set('this_domain', $this_domain);
456         }
457         return $tem->run();
458 }
459
460 # make a URL for the edit page with all the fields filled in
461 function edit_url() {
462         $url = this_url();
463         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
464         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
465         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
466         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
467         return $url;
468 }
469
470 function view_php() {
471         view_headers();
472         echo make_php();
473 }
474
475 function make_email() {
476         $tem = new tem();
477         $tem->load('code/wfpl/metaform/template.email.txt');
478         tem_set_globals($tem);
479         $fields = get_fields();
480         foreach($fields as $field) {
481                 list($name, $type, $input, $format, $sql) = $field;
482                 $tem->set('name', $name);
483                 $tem->set('caption', format_caption($name));
484                 if($type == 'textarea') {
485                         $tem->show('multi_line');
486                 } elseif($type == 'checkbox') {
487                         $tem->show('checkbox');
488                 } else {
489                         $tem->show('normal');
490                 }
491                 $tem->show('fields');
492         }
493         return $tem->run();
494 }
495
496 function make_htaccess() {
497         $tem = new tem();
498         $tem->set('form', $GLOBALS['file_name']);
499         return $tem->run('code/wfpl/metaform/htaccess');
500 }
501
502 function view_email() {
503         view_headers();
504         echo make_email();
505 }
506
507 function preview() {
508         tem_load('code/wfpl/metaform/preview.html');
509         tem_set_globals($GLOBALS['wfpl_template']);
510         tem_set('fields', $_REQUEST['fields']);
511         $preview_tem = new tem();
512         $preview_tem->load_str(make_html(false));
513         if($GLOBALS['opt_db'] == 'Yes') {
514                 $preview_tem->show('new_msg');
515         }
516         $fields = get_fields();
517         foreach($fields as $field) {
518                 list($name, $type, $input, $format, $sql, $options) = $field;
519                 if($type == 'pulldown' || $type == 'radio') {
520                         pulldown($name, eval('return ' . pulldown_options_array($options) . ';'));
521                 }
522         }
523         $preview = $preview_tem->run();
524         unset($preview_tem);
525         $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
526         tem_set('preview', $preview);
527         tem_show('hiddens');
528         set_form_action();
529         tem_output();
530 }
531
532 function download_tar() {
533         $name = $GLOBALS['file_name'];
534         $data = array(
535                 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
536                 ".htaccess" => make_htaccess(),
537                 #"run.php ->" => 'code/wfpl/run.php',
538                 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
539                 "$name.html" => make_html(),
540                 "$name.php" => make_php());
541         if($GLOBALS['opt_db'] == 'Yes') {
542                 $data["$name.sql"] = make_sql();
543         }
544         if($GLOBALS['opt_email'] == 'Yes') {
545                 $data["$name.email.txt"] = make_email();
546         }
547         make_tar($name, $data);
548 }
549
550
551 metaform();
552 exit();