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