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