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