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