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