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