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