JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform codegen: use tem_auto_unset()
[wfpl.git] / metaform.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file writes the code for you (sql, php, html, email) to handle a form.
20
21 require_once('code/wfpl/template.php');
22 require_once('code/wfpl/http.php');
23 require_once('code/wfpl/tar.php');
24 require_once('code/wfpl/format.php');
25
26 # see code/wfpl/metaform/template.html for the html templates for these elements
27 $GLOBALS['types'] = array(
28 #    type                  input          format        sql     
29         'varname' =>    array('textbox',     'varname',    'varchar(50)'),
30         'name' =>       array('textbox',     'oneline',    'varchar(200)'),
31         'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
32         'int' =>        array('textbox',     'int',        'int'),
33         'decimal' =>    array('textbox',     'decimal',    'decimal(12,12)'),
34         'bigint' =>     array('textbox',     'int',        'varchar(100)'), # up to 100 digits, stored as a string
35         'zip' =>        array('textbox',     'zip',        'varchar(20)'),
36         'email' =>      array('textbox',     'email',      'varchar(100)'),
37         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
38         'state' =>      array('states',      'oneline',    'varchar(2)'),
39         'money' =>      array('textbox',     'money',      'varchar(32)'),
40         'date' =>       array('date',        'mdy_to_ymd', 'char(10)'),
41         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
42         'url' =>        array('textbox',     'url',        'varchar(200)'),
43         'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
44         'password' =>   array('password',    'oneline',    'varchar(200)'),
45         'textarea' =>   array('textarea',    'unix',       'text'),
46         'html' =>       array('html',        'unix',       'text'),
47         'pulldown' =>   array('pulldown',    'options',    'varchar(100)'),
48         'radio' =>      array('radio',       'oneline',    'varchar(200)'),
49         'checkbox' =>   array('checkbox',    'bool',       'int(1)'),
50         'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
51         'delete' =>     array('checkbox',    'yesno',      'n/a'),
52         'image' =>      array('image',       'oneline',    'varchar(120)'),
53         'thumb' =>      array('image',       'oneline',    'varchar(240)'),
54         'file' =>       array('file',        'oneline',    'varchar(100)'),
55         'submit' =>     array('submit',      'oneline',    'n/a')
56 );
57
58 function list_available_types() {
59         ksort($GLOBALS['types']);
60         foreach($GLOBALS['types'] as $key => $value) {
61                 tem_set('type', $key);
62                 tem_show('types');
63                 tem_show('types_sep');
64         }
65 }
66
67
68 function tem_set_globals(&$tem) {
69         $vars = array(
70                 'file_name',
71                 'table_name',
72                 'plural',
73                 'singular');
74         foreach($vars as $var) {
75                 $tem->set($var, $GLOBALS[$var]);
76         }
77
78         $bools = array(
79                 'opt_email',
80                 'opt_db',
81                 'opt_listing',
82                 'opt_display',
83                 'opt_pass');
84         foreach($bools as $bool) {
85                 if(format_bool($GLOBALS[$bool])) {
86                         $tem->set($bool);
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         $uploads_output_already = false;
234         $has_html_editors = false;
235         $tem = new tem();
236         $tem->load('code/wfpl/metaform/template.html');
237         tem_set_globals($tem);
238         $fields = get_fields();
239         $tem->set('always_field', find_always_field($fields));
240         foreach($fields as $field) {
241                 list($name, $type, $input, $format, $sql) = $field;
242                 $tem->set('name', $name);
243                 $tem->set('caption', format_caption($name));
244                 $tem->show($input);
245                 if($input != 'hidden') {
246                         $tem->show('row');
247                 }
248
249                 if(($input == 'image' || $input == 'file') && !$uploads_output_already) {
250                         $tem->show('uploads');
251                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
252                         $uploads_output_already = true;
253                 } elseif($input == 'html') {
254                         $has_html_editors = true;
255                         $tem->set('html_field_name', $name);
256                         $tem->show('replace_textarea');
257                 }
258
259                 if($GLOBALS['opt_display'] == 'Yes') {
260                         switch($input) {
261                                 case 'image':
262                                         $tem->show('display_image');
263                                 break;
264                                 case 'checkbox':
265                                         $tem->show('display_yesno');
266                                 break;
267                                 case 'date':
268                                         $tem->show('display_date');
269                                 break;
270                                 case 'textarea':
271                                         $tem->show('display_multiline');
272                                 break;
273                                 case 'html':
274                                         $tem->show('display_html');
275                                 break;
276                                 default:
277                                         $tem->show('display_short');
278                         }
279                         $tem->show('display_row');
280                 }
281
282                 if($GLOBALS['opt_listing'] == 'Yes') {
283                         if(show_in_listing($type, $input, $format, $sql)) {
284                                 if($format == 'bool' || $format == 'yesno') {
285                                         $tem->set('listing_enc', 'yesno');
286                                         $tem->show('listing_value_enc');
287                                 } elseif($input == 'date') {
288                                         $tem->set('listing_enc', 'mmddyyyy');
289                                         $tem->show('listing_value_enc');
290                                 } elseif($type == 'thumb') {
291                                         $tem->show('listing_value_thumb');
292                                 } else {
293                                         $tem->set('listing_enc', 'html');
294                                         $tem->show('listing_value_enc');
295                                 }
296
297                                 $tem->show('listing_head_col');
298                                 $tem->show('listing_row_col');
299                         }
300                 }
301         }
302
303         if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
304                 $tem->set('name', 'send');
305                 $tem->set('caption', 'Send');
306         } else {
307                 $tem->set('name', 'save');
308                 $tem->set('caption', 'Save');
309         }
310         $tem->show('submit');
311         $tem->show('row');
312
313         $tem->show('form');
314
315         if($has_html_editors) {
316                 $tem->show('html_editor_headers');
317         }
318
319         if($whole_file) {
320                 return $tem->run();
321         } else {
322                 return $tem->get('form');
323         }
324 }
325
326 function view_html() {
327         view_headers();
328         echo make_html();
329 }
330
331 function show_in_listing($type, $input, $format, $sql) {
332         switch($input) {
333                 case 'submit':
334                 case 'hidden':
335                 case 'password':
336                 case 'textarea':
337                 case 'html':
338                         return false;
339         }
340         if($type == 'image') {
341                 return false;
342         }
343
344         return true;
345 }
346
347 function make_php() {
348         $has_html_editors = false;
349         $tem = new tem();
350         $tem->load('code/wfpl/metaform/template.php');
351         tem_set_globals($tem);
352         $fields = get_fields();
353         $db_fields = '';
354         $always_field = find_always_field($fields);
355         $image_included_yet = false;
356         foreach($fields as $field) {
357                 list($name, $type, $input, $format, $sql) = $field;
358                 if($input != 'submit') {
359                         $tem->set('format', $format);
360                         $tem->set('name', $name);
361                         $tem->set('db_field', ''); # we don't want to use the value from last time
362                         if($sql != 'n/a') {
363                                 if($db_fields != '') $db_fields .= ',';
364                                 $db_fields .= $name;
365                         }
366                         if($input == 'image') {
367                                 if($type == 'thumb') {
368                                         $tem->show('thumb_settings');
369                                         $tem->show('thumb_upload_params');
370                                         $tem->show('thumb_w_h');
371                                 }
372                                 $tem->show('image_settings');
373                                 $tem->show('image_upload');
374                                 $has_uploads = true;
375                         } else if($input == 'file') {
376                                 $tem->show('file_settings');
377                                 $tem->show('file_upload');
378                                 $has_uploads = true;
379                         } else {
380                                 if($input == 'html') {
381                                         $has_html_editors = true;
382                                 } elseif($input == 'pulldown') {
383                                         $tem->show('pulldowns');
384                                         $tem->show('pulldown_format_extra');
385                                 }
386                                 $tem->show('formats');
387                         }
388                 }
389
390                 if($GLOBALS['opt_listing'] == 'Yes') {
391                         if(show_in_listing($type, $input, $format, $sql)) {
392                                 $tem->show('listing_fields_1');
393                                 $tem->show('listing_fields_2');
394                         }
395                 }
396         }
397         if($has_uploads) {
398                 $tem->show('uploads_include');
399                 $tem->show('upload_max');
400                 $tem->show('upload_settings');
401                 $image_included_yet = true;
402         }
403
404         if($has_html_editors) {
405                 $tem->show('show_extra_headers');
406         }
407
408         $tem->set('always_field', $always_field);
409         $tem->set('db_fields', $db_fields);
410         $tem->set('metaform_url', edit_url());
411         if($GLOBALS['opt_email'] == 'Yes') {
412                 $this_domain = $_SERVER['HTTP_HOST'];
413                 if(substr($this_domain, -2) == '.l') {
414                         $this_domain = substr($this_domain, 0, -1) . 'com';
415                 }
416                 $tem->set('this_domain', $this_domain);
417         }
418         return $tem->run();
419 }
420
421 # make a URL for the edit page with all the fields filled in
422 function edit_url() {
423         $url = this_url();
424         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
425         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
426         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
427         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
428         return $url;
429 }
430
431 function view_php() {
432         view_headers();
433         echo make_php();
434 }
435
436 function make_email() {
437         $tem = new tem();
438         $tem->load('code/wfpl/metaform/template.email.txt');
439         tem_set_globals($tem);
440         $fields = get_fields();
441         foreach($fields as $field) {
442                 list($name, $type, $input, $format, $sql) = $field;
443                 $tem->set('name', $name);
444                 $tem->set('caption', format_caption($name));
445                 if($type == 'textarea') {
446                         $tem->show('multi_line');
447                 } elseif($type == 'checkbox') {
448                         $tem->show('checkbox');
449                 } else {
450                         $tem->show('normal');
451                 }
452                 $tem->show('fields');
453         }
454         return $tem->run();
455 }
456
457 function make_htaccess() {
458         $tem = new tem();
459         $tem->set('form', $GLOBALS['file_name']);
460         return $tem->run('code/wfpl/metaform/htaccess');
461 }
462
463 function view_email() {
464         view_headers();
465         echo make_email();
466 }
467
468 function preview() {
469         tem_load('code/wfpl/metaform/preview.html');
470         tem_set_globals($GLOBALS['wfpl_template']);
471         tem_set('fields', $_REQUEST['fields']);
472         $preview_tem = new tem();
473         $preview_tem->load_str(make_html(false));
474         if($GLOBALS['opt_db'] == 'Yes') {
475                 $preview_tem->show('new_msg');
476         }
477         $fields = get_fields();
478         foreach($fields as $field) {
479                 list($name, $type, $input, $format, $sql) = $field;
480                 if($type == 'pulldown') {
481                         pulldown($name, array('option 1', 'option 2', 'option 3'));
482                 }
483         }
484         $preview = $preview_tem->run();
485         unset($preview_tem);
486         $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
487         tem_set('preview', $preview);
488         tem_show('hiddens');
489         set_form_action();
490         tem_output();
491 }
492
493 function download_tar() {
494         $name = $GLOBALS['file_name'];
495         $data = array(
496                 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
497                 ".htaccess" => make_htaccess(),
498                 #"run.php ->" => 'code/wfpl/run.php',
499                 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
500                 "$name.html" => make_html(),
501                 "$name.php" => make_php());
502         if($GLOBALS['opt_db'] == 'Yes') {
503                 $data["$name.sql"] = make_sql();
504         }
505         if($GLOBALS['opt_email'] == 'Yes') {
506                 $data["$name.email.txt"] = make_email();
507         }
508         make_tar($name, $data);
509 }
510
511
512 metaform();
513 exit();