JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: in generated php file: template sub-section html-editor so it propogates...
[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_varname($_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_varname($_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($GLOBALS['opt_display'] != 'Yes') {
272                                 $tem->show('opt_display_a_else');
273                         }
274                         if(show_in_listing($type, $input, $format, $sql)) {
275                                 if($format == 'bool' || $format == 'yesno') {
276                                         $tem->set('listing_enc', 'yesno');
277                                         $tem->show('listing_value_enc');
278                                 } elseif($input == 'date') {
279                                         $tem->set('listing_enc', 'mmddyyyy');
280                                         $tem->show('listing_value_enc');
281                                 } elseif($type == 'thumb') {
282                                         $tem->show('listing_value_thumb');
283                                 } else {
284                                         $tem->set('listing_enc', 'html');
285                                         $tem->show('listing_value_enc');
286                                 }
287                                 $tem->show('listing_head_col');
288                                 $tem->show('listing_row_col');
289                         }
290                 }
291         }
292
293         if($GLOBALS['opt_db'] == 'Yes') {
294                 $tem->show('opt_db_1');
295                 $tem->show('opt_db_2');
296         } else {
297                 $tem->show('opt_db_1_else');
298         }
299
300         if($GLOBALS['opt_listing'] == 'Yes') {
301                 $tem->show('opt_listing_1');
302         }
303
304         if($GLOBALS['opt_display'] == 'Yes') {
305                 $tem->show('opt_display_1');
306                 $tem->show('opt_display_2');
307         }
308
309         if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
310                 $tem->set('name', 'send');
311                 $tem->set('caption', 'Send');
312         } else {
313                 $tem->set('name', 'save');
314                 $tem->set('caption', 'Save');
315         }
316         $tem->show('submit');
317         $tem->show('row');
318
319         $tem->show('form');
320
321         if($has_html_editors) {
322                 $tem->show('html_editor_headers');
323         }
324
325         if($whole_file) {
326                 return $tem->run();
327         } else {
328                 return $tem->get('form');
329         }
330 }
331
332 function view_html() {
333         view_headers();
334         echo make_html();
335 }
336
337 function show_in_listing($type, $input, $format, $sql) {
338         switch($input) {
339                 case 'submit':
340                 case 'hidden':
341                 case 'password':
342                 case 'textarea':
343                 case 'html':
344                         return false;
345         }
346         if($type == 'image') {
347                 return false;
348         }
349
350         return true;
351 }
352
353 function make_php() {
354         $has_html_editors = false;
355         $tem = new tem();
356         $tem->load('code/wfpl/metaform/template.php');
357         $tem->set('file_name', $GLOBALS['file_name']);
358         $tem->set('table_name', $GLOBALS['table_name']);
359         $tem->set('singular', $GLOBALS['singular']);
360         $tem->set('plural', $GLOBALS['plural']);
361         $fields = get_fields();
362         $db_fields = '';
363         $php_fields = '';
364         $always_field = find_always_field($fields);
365         $image_included_yet = false;
366         foreach($fields as $field) {
367                 list($name, $type, $input, $format, $sql) = $field;
368                 if($input != 'submit') {
369                         $tem->set('format', $format);
370                         $tem->set('name', $name);
371                         $tem->set('db_field', ''); # we don't want to use the value from last time
372                         if($sql != 'n/a') {
373                                 if($db_fields != '') $db_fields .= ',';
374                                 $db_fields .= $name;
375                                 if($php_fields != '') $php_fields .= ', ';
376                                 $php_fields .= '$' . $name;
377                         }
378                         if($input == 'image') {
379                                 if($type == 'thumb') {
380                                         $tem->show('thumb_settings');
381                                         $tem->show('thumb_upload_params');
382                                         $tem->show('thumb_w_h');
383                                 }
384                                 $tem->show('image_settings');
385                                 $tem->show('image_upload');
386                                 if(!$image_included_yet) {
387                                         $tem->show('image_include');
388                                         $tem->show('upload_max');
389                                         $tem->show('upload_settings');
390                                         $image_included_yet = true;
391                                 }
392                         } else {
393                                 if($input == 'html') {
394                                         $has_html_editors = true;
395                                 } elseif($input == 'pulldown') {
396                                         $tem->show('pulldowns');
397                                         $tem->show('pulldown_format_extra');
398                                 }
399                                 $tem->show('formats');
400                         }
401                         $tem->show('tem_sets');
402                 }
403
404                 if($GLOBALS['opt_listing'] == 'Yes') {
405                         if(show_in_listing($type, $input, $format, $sql)) {
406                                 $tem->show('listing_fields_1');
407                                 $tem->show('listing_fields_2');
408                         }
409                 }
410         }
411
412         if($has_html_editors) {
413                 $tem->show('show_extra_headers');
414         }
415
416         $tem->set('always_field', $always_field);
417         $tem->set('db_fields', $db_fields);
418         $tem->set('php_fields', $php_fields);
419         $tem->set('metaform_url', edit_url());
420         if($GLOBALS['opt_listing'] == 'Yes') {
421                 $tem->show('opt_listing_1');
422                 $tem->show('opt_listing_2');
423         }
424         if($GLOBALS['opt_display'] == 'Yes') {
425                 $tem->show('opt_display_1');
426                 $tem->show('opt_display_2');
427         } else {
428                 $tem->show('opt_display_1_else');
429                 $tem->show('opt_display_2_else');
430         }
431         if($GLOBALS['opt_db'] == 'Yes') {
432                 $tem->show('opt_db_1');
433                 $tem->show('opt_db_2');
434                 $tem->show('opt_db_3');
435                 $tem->show('opt_db_4');
436                 $tem->show('opt_db_5');
437         }
438         if($GLOBALS['opt_email'] == 'Yes') {
439                 $tem->show('opt_email_1');
440                 $tem->show('opt_email_2');
441         }
442         if($GLOBALS['opt_http_pass'] == 'Yes') {
443                 $tem->show('opt_http_pass_1');
444                 $tem->show('opt_http_pass_2');
445         }
446         return $tem->run();
447 }
448
449 # make a URL for the edit page with all the fields filled in
450 function edit_url() {
451         $url = this_url();
452         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
453         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
454         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
455         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
456         return $url;
457 }
458
459 function view_php() {
460         view_headers();
461         echo make_php();
462 }
463
464 function make_email() {
465         $tem = new tem();
466         $tem->load('code/wfpl/metaform/template.email.txt');
467         $tem->set('file_name', $GLOBALS['file_name']);
468         $tem->set('table_name', $GLOBALS['table_name']);
469         $tem->set('singular', $GLOBALS['singular']);
470         $tem->set('plural', $GLOBALS['plural']);
471         $fields = get_fields();
472         foreach($fields as $field) {
473                 list($name, $type, $input, $format, $sql) = $field;
474                 $tem->set('name', $name);
475                 $tem->set('caption', $name); # fixme
476                 if($type == 'textarea') {
477                         $tem->show('multi_line');
478                 } elseif($type == 'checkbox') {
479                         $tem->show('checkbox');
480                 } else {
481                         $tem->show('normal');
482                 }
483                 $tem->show('fields');
484         }
485         return $tem->run();
486 }
487
488 function make_htaccess() {
489         $tem = new tem();
490         $tem->set('form', $GLOBALS['file_name']);
491         return $tem->run('code/wfpl/metaform/htaccess');
492 }
493
494 function view_email() {
495         view_headers();
496         echo make_email();
497 }
498
499 function preview() {
500         tem_load('code/wfpl/metaform/preview.html');
501         tem_set('file_name', $GLOBALS['file_name']);
502         tem_set('table_name', $GLOBALS['table_name']);
503         tem_set('singular', $GLOBALS['singular']);
504         tem_set('plural', $GLOBALS['plural']);
505         tem_set('fields', $_REQUEST['fields']);
506         $preview_tem = new tem();
507         $preview_tem->load_str(make_html(false));
508         if($GLOBALS['opt_db'] == 'Yes') {
509                 $preview_tem->show('new_msg');
510         }
511         $fields = get_fields();
512         foreach($fields as $field) {
513                 list($name, $type, $input, $format, $sql) = $field;
514                 if($type == 'pulldown') {
515                         pulldown($name, array('option 1', 'option 2', 'option 3'));
516                 }
517         }
518         $preview = $preview_tem->run();
519         unset($preview_tem);
520         $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
521         tem_set('preview', $preview);
522         tem_show('hiddens');
523         set_form_action();
524         tem_output();
525 }
526
527 function download_tar() {
528         $name = $GLOBALS['file_name'];
529         $data = array(
530                 ".htaccess" => make_htaccess(),
531                 "run.php ->" => 'code/wfpl/run.php',
532                 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
533                 "$name.html" => make_html(),
534                 "$name.php" => make_php());
535         if($GLOBALS['opt_db'] == 'Yes') {
536                 $data["$name.sql"] = make_sql();
537         }
538         if($GLOBALS['opt_email'] == 'Yes') {
539                 $data["$name.email.txt"] = make_email();
540         }
541         make_tar($name, $data);
542 }
543
544
545 metaform();
546 exit();