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