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