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