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