JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added examples/session.sql, metaform: added varname
[wfpl.git] / metaform.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file writes the code for you (sql, php, html, email) to handle a form.
23
24 require_once('code/wfpl/template.php');
25 require_once('code/wfpl/http.php');
26 require_once('code/wfpl/tar.php');
27 require_once('code/wfpl/format.php');
28
29 # see code/wfpl/metaform/template.html for the html templates for these elements
30 $GLOBALS['types'] = array(
31 #    type                  input          format        sql     
32         'varname' =>    array('textbox',     'varname',    'varchar(50)'),
33         'name' =>       array('textbox',     'oneline',    'varchar(200)'),
34         'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
35         'int' =>        array('textbox',     'int',        'int'),
36         'decimal' =>    array('textbox',     'decimal',    'decimal(12,12)'),
37         'bigint' =>     array('textbox',     'int',        'varchar(100)'), # up to 100 digits, stored as a string
38         'zip' =>        array('textbox',     'zip',        'varchar(20)'),
39         'email' =>      array('textbox',     'email',      'varchar(100)'),
40         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
41         'state' =>      array('states',      'oneline',    'varchar(2)'),
42         'money' =>      array('textbox',     'money',      'varchar(32)'),
43         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
44         'url' =>        array('textbox',     'url',        'varchar(200)'),
45         'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
46         'password' =>   array('password',    'oneline',    'varchar(200)'),
47         'textarea' =>   array('textarea',    'unix',       'text'),
48         'html' =>       array('html',        'unix',       'text'),
49         'pulldown' =>   array('pulldown',    'options',    'varchar(100)'),
50         'radio' =>      array('radio',       'oneline',    'varchar(200)'),
51         'leftcheck' =>  array('leftcheck',   'yesno',      'varchar(3)'),
52         'checkbox' =>   array('checkbox',    'yesno',      'varchar(3)'),
53         'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
54         'delete' =>     array('checkbox',    'yesno',      'n/a'),
55         'image' =>      array('image',       'oneline',    'varchar(200)'),
56         'submit' =>     array('submit',      'oneline',    'n/a')
57 );
58
59 function list_available_types() {
60         $types = '';
61         foreach($GLOBALS['types'] as $key => $value) {
62                 if($types) {
63                         $types .= ', ';
64                 }
65                 $types .= $key;
66         }
67         tem_set('available_types', $types);
68 }
69
70
71 function metaform() {
72         if(isset($_REQUEST['form_name'])) {
73                 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
74                 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
75                 tem_set('opt_email', $GLOBALS['opt_email']);
76                 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
77                 tem_set('opt_db', $GLOBALS['opt_db']);
78                 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
79                 tem_set('opt_listing', $GLOBALS['opt_listing']);
80                 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
81                 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
82         } else {
83                 $GLOBALS['form_name'] = 'some_form';
84         }
85
86         if(isset($_REQUEST['fields'])) {
87                 if(isset($_REQUEST['view_sql'])) {
88                         view_sql();
89                         exit();
90                 } elseif(isset($_REQUEST['view_php'])) {
91                         view_php();
92                         exit();
93                 } elseif(isset($_REQUEST['view_html'])) {
94                         view_html();
95                         exit();
96                 } elseif(isset($_REQUEST['view_email'])) {
97                         view_email();
98                         exit();
99                 } elseif(isset($_REQUEST['download_tar'])) {
100                         download_tar();
101                         exit();
102                 } elseif(isset($_REQUEST['preview'])) {
103                         preview();
104                         exit();
105                 } elseif(isset($_REQUEST['edit'])) {
106                         tem_set('fields', $_REQUEST['fields']);
107                         tem_set('form_name', $GLOBALS['form_name']);
108                         # fall through
109                 } else {
110                         die("Sorry... couldn't tell which button you pressed");
111                 }
112         }
113
114
115         set_form_action();
116         list_available_types();
117         tem_output('code/wfpl/metaform/main.html');
118 }
119
120
121 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
122 function field_format($type) { return $GLOBALS['types'][$type][1]; }
123 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
124
125 function get_fields() {
126         # no sense in doing all this so many times
127         if(isset($GLOBALS['gotten_fields'])) {
128                 return $GLOBALS['gotten_fields'];
129         }
130
131         $fields_str = unix_newlines($_REQUEST['fields']);
132         $GLOBALS['gotten_fields'] = array();
133         $fields_str = rtrim($fields_str);
134         $fields = split("\n", $fields_str);
135         foreach($fields as $field) {
136                 list($name, $type, $options) = split('  *', $field);
137                 if($options) $options = split(',', $options);
138                 if(!$type) $type = $name;
139                 $input = field_input($type);
140                 $format = field_format($type);
141                 $sql = field_sql($type);
142                 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
143         }
144         return $GLOBALS['gotten_fields'];
145 }
146
147 # this one, that you're using to create forms
148 function set_form_action() {
149         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
150         if($action == '') $action = './';
151         tem_set('form_action', $action);
152 }
153
154 # perfect HTTP headers for viewing created files
155 function view_headers() {
156         header('Content-type: text/plain');
157 }
158         
159
160
161
162 function make_sql() {
163         $tem = new tem();
164         $tem->load('code/wfpl/metaform/template.sql');
165         $tem->set('form_name', $GLOBALS['form_name']);
166         $fields = get_fields();
167         foreach($fields as $field) {
168                 list($name, $type, $input, $format, $sql) = $field;
169                 if($sql != 'n/a') {
170                         $tem->set('name', $name);
171                         $tem->set('type', $sql);
172                         if($sql == 'int') {
173                                 $tem->set('default', '0');
174                         } else {
175                                 $tem->set('default', '""');
176                         }
177                         $tem->sub('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->sub($input);
217                 if($input != 'hidden') {
218                         $tem->sub('row');
219                 }
220                 if($input == 'image' && !$uploads_output_already) {
221                         $tem->sub('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->sub('replace_textarea');
228                 }
229         }
230
231         if($GLOBALS['opt_db'] == 'Yes') {
232                 $tem->sub('opt_db_1');
233                 $tem->sub('opt_db_2');
234         } else {
235                 $tem->sub('opt_db_1_else');
236         }
237
238         if($GLOBALS['opt_listing'] == 'Yes') {
239                 $tem->sub('opt_listing_1');
240         } else {
241                 $tem->sub('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->sub('submit');
252         $tem->sub('row');
253
254         $tem->sub('form');
255
256         if($has_html_editors) {
257                 $tem->sub('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->sub('image_upload');
296                                 $tem->sub('image_db');
297                                 if(!$image_included_yet) {
298                                         $tem->sub('image_include');
299                                         $tem->sub('upload_max');
300                                         $tem->sub('upload_settings');
301                                         $image_included_yet = true;
302                                 }
303                         } else {
304                                 if($input == 'pulldown') {
305                                         $tem->sub('pulldowns');
306                                         $tem->sub('pulldown_format_extra');
307                                 }
308                                 $tem->sub('formats');
309                         }
310                         $tem->sub('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->sub('opt_listing_1');
320                 $tem->sub('opt_listing_2');
321                 $tem->sub('opt_listing_3');
322                 $tem->sub('opt_listing_4');
323         } else {
324                 $tem->sub('opt_listing_3_else');
325                 $tem->sub('opt_listing_4_else');
326         }
327         if($GLOBALS['opt_db'] == 'Yes') {
328                 $tem->sub('opt_db_1');
329                 $tem->sub('opt_db_2');
330                 $tem->sub('opt_db_3');
331                 $tem->sub('opt_db_4');
332                 $tem->sub('opt_db_5');
333         }
334         if($GLOBALS['opt_email'] == 'Yes') {
335                 $tem->sub('opt_email_1');
336                 $tem->sub('opt_email_2');
337         }
338         if($GLOBALS['opt_http_pass'] == 'Yes') {
339                 $tem->sub('opt_http_pass_1');
340                 $tem->sub('opt_http_pass_2');
341         }
342         return $tem->run();
343 }
344
345 # make a URL for the edit page with all the fields filled in
346 function edit_url() {
347         $url = this_url();
348         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
349         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
350         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
351         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
352         return $url;
353 }
354
355 function view_php() {
356         view_headers();
357         echo make_php();
358 }
359
360
361 function make_email() {
362         $tem = new tem();
363         $tem->load('code/wfpl/metaform/template.email.txt');
364         $tem->set('form_name', $GLOBALS['form_name']);
365         $fields = get_fields();
366         foreach($fields as $field) {
367                 list($name, $type, $input, $format, $sql) = $field;
368                 $tem->set('name', $name);
369                 $tem->set('caption', $name); # fixme
370                 if($type == 'textarea') {
371                         $tem->sub('multi_line');
372                 } else {
373                         $tem->sub('fields');
374                 }
375         }
376         return $tem->run();
377 }
378
379 function make_htaccess() {
380         $tem = new tem();
381         $tem->set('form', $GLOBALS['form_name']);
382         return $tem->run('code/wfpl/metaform/htaccess');
383 }
384
385 function view_email() {
386         view_headers();
387         echo make_email();
388 }
389
390
391 function preview() {
392         tem_load('code/wfpl/metaform/preview.html');
393         tem_set('form_name', $GLOBALS['form_name']);
394         tem_set('fields', $_REQUEST['fields']);
395         $preview_tem = new tem();
396         $preview_tem->load_str(make_html(false));
397         if($GLOBALS['opt_db'] == 'Yes') {
398                 $preview_tem->sub('new_msg');
399         }
400         $fields = get_fields();
401         foreach($fields as $field) {
402                 list($name, $type, $input, $format, $sql) = $field;
403                 if($type == 'pulldown') {
404                         pulldown($name, array('option 1', 'option 2', 'option 3'));
405                 }
406         }
407         $preview = $preview_tem->run();
408         unset($preview_tem);
409         tem_set('preview', $preview);
410         set_form_action();
411         tem_output();
412 }
413
414 function download_tar() {
415         $name = $GLOBALS['form_name'];
416         $data = array(
417                 ".htaccess" => make_htaccess(),
418                 "run.php ->" => 'code/wfpl/run.php',
419                 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
420                 "$name.html" => make_html(),
421                 "$name.php" => make_php());
422         if($GLOBALS['opt_db'] == 'Yes') {
423                 $data["$name.sql"] = make_sql();
424         }
425         if($GLOBALS['opt_email'] == 'Yes') {
426                 $data["$name.email.txt"] = make_email();
427         }
428         make_wfpl_tar($name, $data);
429 }
430
431
432 metaform();
433 exit();
434
435 ?>