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