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