JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
new options (don't do anything yet)
[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                 'opt_public_form',
87                 'opt_public_display');
88         foreach($bools as $bool) {
89                 if($GLOBALS[$bool]) {
90                         $tem->set($bool);
91                 }
92         }
93 }
94
95 function metaform() {
96         if(isset($_REQUEST['singular'])) {
97                 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
98                 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
99                 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
100                 # backwards compatibility:
101                 if(isset($_REQUEST['form_name'])) {
102                         $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
103                 }
104
105                 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
106                 $GLOBALS['opt_email'] = format_bool($_REQUEST['opt_email']);
107                 $GLOBALS['opt_db'] = format_bool($_REQUEST['opt_db']);
108                 $GLOBALS['opt_listing'] = format_bool($_REQUEST['opt_listing']);
109                 $GLOBALS['opt_display'] = format_bool($_REQUEST['opt_display']);
110                 $GLOBALS['opt_pass'] = format_bool($_REQUEST['opt_pass']);
111                 $GLOBALS['opt_public_form'] = format_bool($_REQUEST['opt_public_form']);
112                 $GLOBALS['opt_public_display'] = format_bool($_REQUEST['opt_public_display']);
113
114                 tem_init();
115                 tem_set_globals($GLOBALS['wfpl_template']);
116         }
117
118         if(isset($_REQUEST['fields'])) {
119                 if(isset($_REQUEST['view_sql'])) {
120                         view_sql();
121                         exit();
122                 } elseif(isset($_REQUEST['view_php'])) {
123                         view_php();
124                         exit();
125                 } elseif(isset($_REQUEST['view_html'])) {
126                         view_html();
127                         exit();
128                 } elseif(isset($_REQUEST['view_email'])) {
129                         view_email();
130                         exit();
131                 } elseif(isset($_REQUEST['download_tar'])) {
132                         download_tar();
133                         exit();
134                 } elseif(isset($_REQUEST['preview'])) {
135                         preview();
136                         exit();
137                 } elseif(isset($_REQUEST['edit'])) {
138                         tem_set('fields', $_REQUEST['fields']);
139                         # fall through
140                 } else {
141                         die("Sorry... couldn't tell which button you pressed");
142                 }
143         }
144
145
146         set_form_action();
147         tem_load('code/wfpl/metaform/main.html');
148         list_available_types();
149         tem_output();
150 }
151
152
153 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
154 function field_format($type) { return $GLOBALS['types'][$type][1]; }
155 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
156
157 function get_fields() {
158         # no sense in doing all this so many times
159         if(isset($GLOBALS['gotten_fields'])) {
160                 return $GLOBALS['gotten_fields'];
161         }
162
163         $fields_str = unix_newlines($_REQUEST['fields']);
164         $GLOBALS['gotten_fields'] = array();
165         $fields_str = rtrim($fields_str);
166         $fields = split("\n", $fields_str);
167         foreach($fields as $field) {
168                 $field = trim($field);
169                 if(substr($field, -1) == '{') {
170                         $name = trim(substr($field, 0, -1)); # FIXME: stop this from getting enc_caption()ed
171                         $type = '{';
172                         $options = null;
173                 } elseif(substr($field, -1) == '{') {
174                         $name = 'ignored';
175                         $type = '}';
176                         $options = null;
177                 } else {
178                         list($name, $type, $options) = split('  *', $field);
179                         if($options) $options = explode(',', $options);
180                         if(!$type) $type = $name;
181                 }
182                 $input = field_input($type);
183                 $format = field_format($type);
184                 $sql = field_sql($type);
185                 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
186         }
187
188         return $GLOBALS['gotten_fields'];
189 }
190
191 # this one, that you're using to create forms
192 function set_form_action() {
193         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
194         if($action == '') $action = './';
195         tem_set('form_action', $action);
196 }
197
198 # perfect HTTP headers for viewing created files
199 function view_headers() {
200         header('Content-type: text/plain');
201 }
202         
203
204
205
206 function make_sql() {
207         $tem = new tem();
208         $tem->load('code/wfpl/metaform/template.sql');
209         tem_set_globals($tem);
210         $fields = get_fields();
211         foreach($fields as $field) {
212                 list($name, $type, $input, $format, $sql) = $field;
213                 if($sql != 'n/a') {
214                         $tem->set('name', $name);
215                         $tem->set('type', $sql);
216                         if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
217                                 $tem->set('default', '0');
218                         } elseif($format == 'yesno') {
219                                 $tem->set('default', '"No"');
220                         } else {
221                                 $tem->set('default', '""');
222                         }
223                         $tem->show('column');
224                 }
225         }
226         view_headers();
227         return $tem->run();
228 }
229
230 function view_sql() {
231         view_headers();
232         echo make_sql();
233 }
234
235 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
236 function find_always_field($fields) {
237         foreach($fields as $field) {
238                 list($name, $type, $input, $format, $sql) = $field;
239                 if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio' && $type != '{') {
240                         return $name;
241                 }
242         }
243
244         return false;
245 }
246         
247         
248
249 # pass false if you want to exclude the <head> and <body> tag etc.
250 function make_html($whole_file = true) {
251         $has_html_editors = false;
252         $tem = new tem();
253         $tem->load('code/wfpl/metaform/template.html');
254         tem_set_globals($tem);
255         $fields = get_fields();
256         $tem->set('always_field', find_always_field($fields));
257         $hidden_fields = array();
258         $visible_fields = array();
259         $display_fields = array();
260         $listing_headers = array();
261         $listing_fields = array();
262         foreach($fields as $field) {
263                 list($name, $type, $input, $format, $sql) = $field;
264                 if($input == 'hidden') {
265                         $hidden_fields[] = array('name' => $name);
266                 } else {
267                         $visible_fields[] = array($input => array(
268                                 'name' => $name,
269                                 'caption' => format_caption($name)));
270                 }
271
272                 if($input == 'image' || $input == 'file') {
273                         $tem->set('uploads');
274                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
275                 } elseif($input == 'html') {
276                         $has_html_editors = true;
277                         $tem->set('html_field_name', $name);
278                         $tem->set('replace_textarea');
279                 }
280
281                 switch($input) {
282                         case 'image':
283                         case 'checkbox':
284                         case 'date':
285                         case 'textarea':
286                         case 'html':
287                                 $display_type = $input;
288                         break;
289                         default:
290                                 $display_type = 'short';
291                 }
292                 if($format != 'n/a') {
293                         $display_fields[] = array($display_type => array(
294                                 'name' => $name, 'caption' => format_caption($name)));
295                 }
296
297                 if(show_in_listing($type, $input, $format, $sql)) {
298                         $listing_headers[] = array('caption' => format_caption($name));
299                         $listing_field = array('name' => $name);
300                         if($format == 'bool' || $format == 'yesno') {
301                                 $listing_field['enc'] = 'yesno';
302                         } elseif($input == 'date') {
303                                 $listing_field['enc'] = 'mmddyyyy';
304                         } elseif($type == 'thumb') {
305                                 $listing_field['thumb'] = true;
306                         } else {
307                                 $listing_field['enc'] = 'html';
308                         }
309                         $listing_fields[] = $listing_field;
310                 }
311         }
312
313         # Submit/Send button
314         if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
315                 $visible_fields[] = array('submit' => array(
316                         'name' => 'send',
317                         'caption' => 'Send'));
318         } else {
319                 $visible_fields[] = array('submit' => array(
320                         'name' => 'save',
321                         'caption' => 'Save'));
322         }
323
324         $tem->set('form', array(
325                 'visible_fields' => $visible_fields,
326                 'hidden_fields' => $hidden_fields));
327
328         # opt_display and opt_listing control whether these are actually displayed
329         $tem->set('display_fields', $display_fields);
330         $tem->set('listing_headers', $listing_headers);
331         $tem->set('listing_fields', $listing_fields);
332
333
334         if($has_html_editors) {
335                 $tem->set('html_editor_headers');
336         }
337
338         if($whole_file) {
339                 return $tem->run();
340         } else {
341                 $tem2 = new tem();
342                 $tem2->load_str('<!--~form~-->');
343                 $tem2->merge($tem);
344                 return $tem2->run();
345         }
346 }
347
348 function view_html() {
349         view_headers();
350         echo make_html();
351 }
352
353 function show_in_listing($type, $input, $format, $sql) {
354         switch($input) {
355                 case 'submit':
356                 case 'hidden':
357                 case 'password':
358                 case 'textarea':
359                 case 'html':
360                 case 'fieldset':
361                 case 'end_fieldset':
362                         return false;
363         }
364         if($type == 'image') {
365                 return false;
366         }
367
368         return true;
369 }
370
371 function pulldown_options_array($options) {
372         if($options) {
373                 $pulldown_options = array();
374                 foreach($options as $option) {
375                         $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
376                         $pulldown_options[] = "'$option'";
377                 }
378                 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
379         } else {
380                 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
381         }
382
383         return $pulldown_options;
384 }
385
386 function make_php() {
387         $has_html_editors = false;
388         $tem = new tem();
389         $tem->load('code/wfpl/metaform/template.php');
390         tem_set_globals($tem);
391         $fields = get_fields();
392         $db_fields = '';
393         $always_field = find_always_field($fields);
394         $image_included_yet = false;
395         foreach($fields as $field) {
396                 list($name, $type, $input, $format, $sql, $options) = $field;
397                 if($input != 'submit') {
398                         $tem->set('format', $format);
399                         $tem->set('name', $name);
400                         $tem->set('db_field', ''); # we don't want to use the value from last time
401                         if($sql != 'n/a') {
402                                 if($db_fields != '') $db_fields .= ',';
403                                 $db_fields .= $name;
404                         }
405                         if($input == 'image') {
406                                 if($type == 'thumb') {
407                                         $tem->show('thumb_settings');
408                                         $tem->show('thumb_upload_params');
409                                         $tem->show('thumb_w_h');
410                                 }
411                                 $tem->show('image_settings');
412                                 $tem->show('image_upload');
413                                 $has_uploads = true;
414                         } else if($input == 'file') {
415                                 $tem->show('file_settings');
416                                 $tem->show('file_upload');
417                                 $has_uploads = true;
418                         } else {
419                                 if($input == 'html') {
420                                         $has_html_editors = true;
421                                 } elseif($input == 'pulldown' || $input == 'radio') {
422                                         $pulldown_options = pulldown_options_array($options);
423                                         $tem->set('pulldown_options', $pulldown_options);
424                                         $tem->show('pulldowns');
425                                         $tem->show('pulldown_format_extra');
426                                 }
427                                 if($format != 'n/a') {
428                                         $tem->show('formats');
429                                 }
430                         }
431                 }
432
433                 if($GLOBALS['opt_listing']) {
434                         if(show_in_listing($type, $input, $format, $sql)) {
435                                 $tem->show('listing_fields_1');
436                                 $tem->show('listing_fields_2');
437                         }
438                 }
439         }
440         if($has_uploads) {
441                 $tem->show('uploads_include');
442                 $tem->show('upload_max');
443                 $tem->show('upload_settings');
444                 $image_included_yet = true;
445         }
446
447         if($has_html_editors) {
448                 $tem->show('show_extra_headers');
449         }
450
451         $tem->set('always_field', $always_field);
452         $tem->set('db_fields', $db_fields);
453         $tem->set('metaform_url', edit_url());
454         if($GLOBALS['opt_email']) {
455                 $this_domain = $_SERVER['HTTP_HOST'];
456                 if(substr($this_domain, -2) == '.l') {
457                         $this_domain = substr($this_domain, 0, -1) . 'com';
458                 }
459                 $tem->set('this_domain', $this_domain);
460         }
461         return $tem->run();
462 }
463
464 # make a URL for the edit page with all the fields filled in
465 function edit_url() {
466         $url = this_url();
467         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
468         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
469         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
470         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
471         return $url;
472 }
473
474 function view_php() {
475         view_headers();
476         echo make_php();
477 }
478
479 function make_email() {
480         $tem = new tem();
481         $tem->load('code/wfpl/metaform/template.email.txt');
482         tem_set_globals($tem);
483         $fields = get_fields();
484         foreach($fields as $field) {
485                 list($name, $type, $input, $format, $sql) = $field;
486                 $tem->set('name', $name);
487                 $tem->set('caption', format_caption($name));
488                 if($type == 'textarea') {
489                         $tem->show('multi_line');
490                 } elseif($type == 'checkbox') {
491                         $tem->show('checkbox');
492                 } else {
493                         $tem->show('normal');
494                 }
495                 $tem->show('fields');
496         }
497         return $tem->run();
498 }
499
500 function make_htaccess() {
501         $tem = new tem();
502         $tem->set('form', $GLOBALS['file_name']);
503         return $tem->run('code/wfpl/metaform/htaccess');
504 }
505
506 function view_email() {
507         view_headers();
508         echo make_email();
509 }
510
511 function preview() {
512         tem_load('code/wfpl/metaform/preview.html');
513         tem_set_globals($GLOBALS['wfpl_template']);
514         tem_set('fields', $_REQUEST['fields']);
515         $preview_tem = new tem();
516         $preview_tem->load_str(make_html(false));
517         if($GLOBALS['opt_db']) {
518                 $preview_tem->show('new_msg');
519         }
520         $fields = get_fields();
521         foreach($fields as $field) {
522                 list($name, $type, $input, $format, $sql, $options) = $field;
523                 if($type == 'pulldown' || $type == 'radio') {
524                         pulldown($name, eval('return ' . pulldown_options_array($options) . ';'));
525                 }
526         }
527         $preview = $preview_tem->run();
528         unset($preview_tem);
529         $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
530         tem_set('preview', $preview);
531         tem_show('hiddens');
532         set_form_action();
533         tem_output();
534 }
535
536 function download_tar() {
537         $name = $GLOBALS['file_name'];
538         $data = array(
539                 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
540                 ".htaccess" => make_htaccess(),
541                 #"run.php ->" => 'code/wfpl/run.php',
542                 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
543                 "$name.html" => make_html(),
544                 "$name.php" => make_php());
545         if($GLOBALS['opt_db']) {
546                 $data["$name.sql"] = make_sql();
547         }
548         if($GLOBALS['opt_email']) {
549                 $data["$name.email.txt"] = make_email();
550         }
551         make_tar($name, $data);
552 }
553
554
555 metaform();
556 exit();